Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include "lua.hpp"
- using namespace std;
- int main(int argc, char *argv[])
- {
- lua_State *lstate1;
- lua_Integer size;
- int COMPONENTS_stack_index, second_stack_index;
- lstate1 = luaL_newstate();
- luaL_openlibs(lstate1);
- if (luaL_dofile(lstate1, "components.lua"))
- {
- cout<<"dofile() error"<<endl;
- return -1;
- }
- cout<<"LUAI_MAXSTACK = "<<LUAI_MAXSTACK<<endl;
- lua_getglobal(lstate1, "COMPONENTS");
- COMPONENTS_stack_index = lua_absindex(lstate1, -1);
- lua_len(lstate1, -1);
- size = lua_tointeger(lstate1, -1);
- lua_pop(lstate1, 1);
- cout<<"COMPONENTS size is "<<size<<endl;
- for (int i = 0; i < size; i++)
- {
- const char *str_ptr;
- cout<<"lua_gettop() = "<< lua_gettop(lstate1)<<endl;
- if (lua_istable(lstate1, COMPONENTS_stack_index))
- {
- lua_pushinteger(lstate1, i + 1);
- lua_gettable(lstate1, COMPONENTS_stack_index); //COMPONENTS[i]
- second_stack_index = lua_absindex(lstate1, -1);
- if (lua_istable(lstate1, -1))
- {
- lua_pushinteger(lstate1, 1);
- lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][1]
- str_ptr = lua_tolstring(lstate1, -1, NULL);
- if (!str_ptr)
- {
- cout<<"lua_tolstring() returned NULL!"<<endl;
- cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
- return -1;
- }
- cout<<"COMPONENTS["<<i<<"][1] = "<<str_ptr<<endl;
- //lua_pop(lstate1, 1);
- /////////////////////////////////
- lua_pushinteger(lstate1,2);
- lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][2]
- str_ptr = lua_tolstring(lstate1, -1, NULL);
- if (!str_ptr)
- {
- cout<<"lua_tolstring() returned NULL!"<<endl;
- cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
- return -1;
- }
- cout<<"COMPONENTS["<<i<<"][2] = "<<str_ptr<<endl;
- //lua_pop(lstate1, 1);
- /////////////////////////////////
- lua_pushinteger(lstate1,3);
- lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][3]
- str_ptr = lua_tolstring(lstate1, -1, NULL);
- if (!str_ptr)
- {
- cout<<"lua_tolstring() returned NULL!"<<endl;
- cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
- return -1;
- }
- cout<<"COMPONENTS["<<i<<"][3] = "<<str_ptr<<endl;
- //lua_pop(lstate1, 1);
- }
- else
- {
- cout<<"COMPONENTS["<<i<<"] value is not a table!"<<endl;
- return -1;
- }
- //lua_pop(lstate1, 1);
- }
- else
- {
- cout<<"error, not a table type"<<endl;
- return -1;
- }
- }
- lua_close(lstate1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement