LuaDataFormatter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. using Rocket::Core::Lua::Interpreter;
  32. using Rocket::Core::Log;
  33. namespace Rocket {
  34. namespace Controls {
  35. namespace Lua {
  36. LuaDataFormatter::LuaDataFormatter(const Rocket::Core::String& name) : Rocket::Controls::DataFormatter(name), ref_FormatData(LUA_NOREF)
  37. {
  38. }
  39. LuaDataFormatter::~LuaDataFormatter()
  40. {
  41. //empty
  42. }
  43. void LuaDataFormatter::FormatData(Rocket::Core::String& formatted_data, const Rocket::Core::StringList& raw_data)
  44. {
  45. if(ref_FormatData == LUA_NOREF || ref_FormatData == LUA_REFNIL)
  46. {
  47. Log::Message(Log::LT_ERROR, "In LuaDataFormatter: There is no value assigned to the \"FormatData\" variable.");
  48. return;
  49. }
  50. lua_State* L = Interpreter::GetLuaState();
  51. int top = lua_gettop(L);
  52. PushDataFormatterFunctionTable(L); // push the table where the function resides
  53. lua_rawgeti(L,-1,ref_FormatData); //push the function
  54. if(lua_type(L,-1) != LUA_TFUNCTION)
  55. {
  56. 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)));
  57. lua_settop(L,top);
  58. return;
  59. }
  60. lua_newtable(L); //to hold raw_data
  61. int tbl = lua_gettop(L);
  62. for(unsigned int i = 0; i < raw_data.size(); i++)
  63. {
  64. lua_pushstring(L,raw_data[i].CString());
  65. lua_rawseti(L,tbl,i);
  66. }
  67. Interpreter::ExecuteCall(1,1); //1 parameter (the table), 1 result (a string)
  68. //top of the stack should be the return value
  69. if(lua_type(L,-1) != LUA_TSTRING)
  70. {
  71. 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)));
  72. lua_settop(L,top);
  73. return;
  74. }
  75. formatted_data = Rocket::Core::String(lua_tostring(L,-1));
  76. lua_settop(L,top);
  77. }
  78. void LuaDataFormatter::PushDataFormatterFunctionTable(lua_State* L)
  79. {
  80. lua_getglobal(L,"LUADATAFORMATTERFUNCTIONS");
  81. if(lua_isnoneornil(L,-1))
  82. {
  83. lua_newtable(L);
  84. lua_setglobal(L,"LUADATAFORMATTERFUNCTIONS");
  85. lua_pop(L,1); //pop the unsucessful getglobal
  86. lua_getglobal(L,"LUADATAFORMATTERFUNCTIONS");
  87. }
  88. }
  89. }
  90. }
  91. }