LuaDataFormatter.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "LuaDataFormatter.h"
  29. #include <Rocket/Core/Lua/Interpreter.h>
  30. #include <Rocket/Core/Log.h>
  31. namespace Rocket {
  32. namespace Core {
  33. namespace Lua {
  34. LuaDataFormatter::LuaDataFormatter(const String& name) : Rocket::Controls::DataFormatter(name), ref_FormatData(LUA_NOREF)
  35. {
  36. }
  37. LuaDataFormatter::~LuaDataFormatter()
  38. {
  39. //empty
  40. }
  41. void LuaDataFormatter::FormatData(Rocket::Core::String& formatted_data, const Rocket::Core::StringList& raw_data)
  42. {
  43. if(ref_FormatData == LUA_NOREF || ref_FormatData == LUA_REFNIL)
  44. {
  45. Log::Message(Log::LT_ERROR, "In LuaDataFormatter: There is no value assigned to the \"FormatData\" variable.");
  46. return;
  47. }
  48. lua_State* L = Interpreter::GetLuaState();
  49. PushDataFormatterFunctionTable(L); // push the table where the function resides
  50. lua_rawgeti(L,-1,ref_FormatData); //push the function
  51. if(lua_type(L,-1) != LUA_TFUNCTION)
  52. {
  53. Log::Message(Log::LT_ERROR, "In LuaDataFormatter: The value for the FormatData variable must be a function. You passed in a %s.", lua_typename(L,lua_type(L,-1)));
  54. return;
  55. }
  56. lua_newtable(L); //to hold raw_data
  57. int tbl = lua_gettop(L);
  58. for(unsigned int i = 0; i < raw_data.size(); i++)
  59. {
  60. lua_pushstring(L,raw_data[i].CString());
  61. lua_rawseti(L,tbl,i);
  62. }
  63. Interpreter::ExecuteCall(1,1); //1 parameter (the table), 1 result (a string)
  64. //top of the stack should be the return value
  65. if(lua_type(L,-1) != LUA_TSTRING)
  66. {
  67. Log::Message(Log::LT_ERROR, "In LuaDataFormatter: the return value of FormatData must be a string. You returned a %s.", lua_typename(L,lua_type(L,-1)));
  68. return;
  69. }
  70. formatted_data = String(lua_tostring(L,-1));
  71. }
  72. void LuaDataFormatter::PushDataFormatterFunctionTable(lua_State* L)
  73. {
  74. lua_getglobal(L,"LUADATAFORMATTERFUNCTIONS");
  75. if(lua_isnoneornil(L,-1))
  76. {
  77. lua_newtable(L);
  78. lua_setglobal(L,"LUADATAFORMATTERFUNCTIONS");
  79. lua_pop(L,1); //pop the unsucessful getglobal
  80. lua_getglobal(L,"LUADATAFORMATTERFUNCTIONS");
  81. }
  82. }
  83. }
  84. }
  85. }