LuaDataSource.cpp 4.2 KB

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