LuaDataSource.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "LuaDataSource.h"
  29. #include <RmlUi/Lua/Interpreter.h>
  30. #include <RmlUi/Core/Log.h>
  31. namespace Rml {
  32. namespace Lua {
  33. LuaDataSource::LuaDataSource(const String& name) : DataSource(name), getRowRef(LUA_NOREF), getNumRowsRef(LUA_NOREF)
  34. {}
  35. /// Fetches the contents of one row of a table within the data source.
  36. /// @param[out] row The list of values in the table.
  37. /// @param[in] table The name of the table to query.
  38. /// @param[in] row_index The index of the desired row.
  39. /// @param[in] columns The list of desired columns within the row.
  40. void LuaDataSource::GetRow(StringList& row, const String& table, int row_index, const StringList& columns)
  41. {
  42. if(getRowRef == LUA_NOREF || getRowRef == LUA_REFNIL) return;
  43. //setup the call
  44. Interpreter::BeginCall(getRowRef);
  45. lua_State* L = Interpreter::GetLuaState();
  46. lua_pushstring(L,table.c_str());
  47. lua_pushinteger(L,row_index);
  48. lua_newtable(L);
  49. int index = 0;
  50. for(StringList::const_iterator itr = columns.begin(); itr != columns.end(); ++itr)
  51. {
  52. lua_pushstring(L,itr->c_str());
  53. lua_rawseti(L,-2,index++);
  54. }
  55. Interpreter::ExecuteCall(3,1); //3 parameters, 1 return. After here, the top of the stack contains the return value
  56. if(lua_type(L,-1) == LUA_TTABLE)
  57. {
  58. lua_pushnil(L);
  59. while (lua_next(L, -2))
  60. {
  61. //key at -2, value at -1
  62. row.push_back(luaL_checkstring(L,-1));
  63. lua_pop(L,1); //pops value, leaves key for next iteration
  64. }
  65. }
  66. else
  67. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetRow must return a table, the function it called returned a %s", lua_typename(L,-1));
  68. lua_pop(L,1);
  69. }
  70. /// Fetches the number of rows within one of this data source's tables.
  71. /// @param[in] table The name of the table to query.
  72. /// @return The number of rows within the specified table. Returns -1 in case of an incorrect Lua function.
  73. int LuaDataSource::GetNumRows(const String& table)
  74. {
  75. if(getNumRowsRef == LUA_NOREF || getNumRowsRef == LUA_REFNIL) return -1;
  76. lua_State* L = Interpreter::GetLuaState();
  77. Interpreter::BeginCall(getNumRowsRef);
  78. lua_pushstring(L,table.c_str());
  79. Interpreter::ExecuteCall(1,1); //1 parameter, 1 return. After this, the top of the stack contains the return value
  80. int res = -1;
  81. if(lua_type(L, -1) == LUA_TNUMBER)
  82. {
  83. res = (int)luaL_checkinteger(L,res);
  84. }
  85. else
  86. {
  87. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetNumRows must return an integer, the function it called returned a %s", lua_typename(L,-1));
  88. }
  89. lua_pop(L, 1);
  90. return res;
  91. }
  92. } // namespace Lua
  93. } // namespace Rml