LuaDataFormatter.cpp 3.5 KB

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