c++下 遍历 lua table

2 篇文章 0 订阅
2 篇文章 0 订阅

前不久在网上看到一段代码,功能是遍历lua的table。当然也可以衍生成 lua table向c++中的map的转化。直接上代码,仅供同学们参考。(注:这代段代码是网上的 非本人所写 仅供学习)

//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;
}

--$ cat test.lua lua文件 
user = {
        ["name"] = "zhangsan",
        ["age"] = "22",
        ["friend"] = {
                [1] = {
                    ["name"] = "小丽",
                    ["sex"] = "女",
                    ["age"] = "20",
                },
                [2] = {
                    ["name"] = "小罗",
                    ["sex"] = "男",
                    ["age"] = "20",
                },
            },
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值