c++ 遍历lua table方法
来源:互联网 发布:数据库原理视频教程 编辑:程序博客网 时间:2024/05/20 22:28
//C++代码:#include <lua.hpp>#include <iostream>#include <string>using namespace std; bool popTable(lua_State* L, int idx){ try{ lua_pushnil(L); while(lua_next(L, idx) != 0){ int keyType = lua_type(L, -2); if(keyType == LUA_TNUMBER){ double value = lua_tonumber(L, -2); cout << "Key:" << value << endl; }else if(keyType == LUA_TSTRING){ const char* value = lua_tostring(L, -2); cout << "Key:" << value << endl; }else{ cout << "Invalid key type: " << keyType << endl; return false; } int valueType = lua_type(L, -1); switch(valueType){ case LUA_TNIL: { cout << "Value: nil" << endl; break; } case LUA_TBOOLEAN: { int value = lua_toboolean(L, -1); cout << value << endl; break; } case LUA_TNUMBER: { cout << "Value:" << lua_tonumber(L, -1) << endl; break; } case LUA_TSTRING: { cout << "Value:" << lua_tostring(L, -1) << endl; break; } case LUA_TTABLE: { cout << "====sub table===" << endl; int index = lua_gettop(L); if (!popTable(L, index)) { cout << "popTable error in popTable,error occured" << endl; return false; } break; } default: { cout << "Invalid value type: " << valueType << endl; return false; } } lua_pop(L, 1); } }catch(const char* s){ string errMsg = s; lua_pop(L,1); cout << errMsg << endl; return false; }catch(std::exception& e){ const char* errMsg = e.what(); lua_pop(L,1); cout << errMsg << endl; return false; }catch(...){ const char* errMsg = lua_tostring(L,-1); lua_pop(L,1); cout << errMsg << endl; return false; } return true;} int main(int argc, char* argv){ lua_State* L = luaL_newstate(); luaL_openlibs(L); int r = luaL_dofile(L,"./test.lua"); lua_getglobal(L, "user"); int type = lua_type(L,1); if(type == LUA_TTABLE){ int index = lua_gettop(L); if(popTable(L,index)){ return 0; }else{ cout << "Error" << endl; return -1; } } return 0;}
int main(int argc, char* argv){ lua_State* L = luaL_newstate(); luaL_openlibs(L); int r = luaL_dofile(L,"./test.lua"); lua_getglobal(L, "user"); int type = lua_type(L,1); if(type == LUA_TTABLE){ int index = lua_gettop(L); if(popTable(L,index)){ return 0; }else{ cout << "Error" << endl; return -1; } } return 0;}
0 0
- c遍历lua中table的方法
- C遍历lua的table
- c++ 遍历lua table方法
- Lua 学习笔记:C API 遍历 Table
- Lua 学习笔记:C API 遍历 Table
- C/C++ 中遍历 Lua table 完整版
- C/C++ 中遍历 Lua table 完整版
- C/C++遍历Lua中的table
- 在C/C++中 用C API遍历lua table
- lua table 遍历
- Lua 中Table遍历
- lua 遍历 table
- Lua--table的遍历
- lua table 遍历
- lua遍历table
- lua遍历table方式
- lua中的table遍历
- Lua中table的遍历
- SGU - 132 Another Chocolate Maniac (状态压缩)
- Java进阶(极客)——单例模式(二)优化
- wordpress添加后台菜单及权限设置
- maven的pom.xml详解
- Inotify监视限值不足问题
- c++ 遍历lua table方法
- zoj 3636 Decode
- Hive动态分区
- 飞机最少换乘次数问题
- 初识WebP
- DIY:使用扩展程序在Chrome下干掉各种恶心的广告
- input输入框中的光标上下不居中的解决方法
- Java内存模型
- 水王争霸