[Lua]c解析lua 嵌套table

本文介绍了一个用C语言编写的Lua表格解析函数ParseLuaTable,该函数可以递归地遍历Lua中的table,并打印出table中所有键值对的类型及数值。通过一个测试函数展示了如何加载Lua脚本文件并调用此解析函数。
void ParseLuaTable(lua_State *L)
{
    if (!lua_istable(L, -1))
    {
        return;
    }

    lua_pushnil(L);
    while (lua_next(L, -2))
    {
        fprintf(stdout, "%s : %s    ", luaL_typename(L,-2), luaL_typename(L,-1));
        int nKeyType = lua_type(L, -2);
        int nValueType = lua_type(L, -1);

        if (nKeyType == LUA_TNUMBER)
        {
            fprintf(stdout, "%g,", lua_tonumber(L, -2));
        }
        else if (nKeyType == LUA_TSTRING)
        {
            fprintf(stdout, "\"%s\",", lua_tostring(L, -2));
        }
        fprintf(stdout, "   ");  
        if (nValueType == LUA_TNUMBER)
        {
            fprintf(stdout, "%g", lua_tonumber(L, -1));
        }
        else if (nValueType == LUA_TSTRING)
        {
            fprintf(stdout, "\"%s\"", lua_tostring(L, -1));
        }
        else if (nValueType == LUA_TTABLE)
        {
            fprintf(stdout, "\n");
            ParseLuaTable(L);
        }   

        lua_pop(L, 1);
        fprintf(stdout, "\n");   
    }
}

int test()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    int error = 0;

    // test.lua 
    //s = {
    //g = "g",
    //b = "b",
    //r = 2,
    //t = {
    //    width = 100,
    //    height = 200,
    //    path = "path",
    //    te = { a = 1, b = 2}
    //},
    //
    //3,
    //4,
    //5,
    //"test"
    //}
    //
    
    luaL_loadfile(L, "test.lua");
    error = lua_pcall(L, 0, 0, 0);
    if (error) {
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);/* pop error message from the stack */
    }
    
    lua_getglobal(L, "s");
    ParseLuaTable(L);

    if (error) {
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);/* pop error message from the stack */
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/dazhu/archive/2013/02/04/2891750.html

详细描述Lua和C之间相互传递Table类型数据 /* ====================================================== */ // 遍历Lua传入的Table类型参数, 获取它的Key/Value, 其关键操作是 lua_next() // lua_next() 返回1表示读取成功,返回0表示已经没有数据可读了 // lua_next() 会使用栈顶元素作为Key去定位本次需要取出Table里面的那个值对 // 如果Key=nil, 那就表示本次取出的是第一个元素 // 它会先将当前的这个Key弹出,然后将本次取出的Key/Value压入栈, Value在栈顶 // 一个比较隐晦的处理就是, 我们不应直接使用lua_tostring(L, -2)来读取Key // 因为lua_tostring()在Key类型不是字符串时, 它会修改栈上的Key数据 // 这样, 下次调用lua_next()时, 就会因为Key被修改了而导致错误 // 为此,先调用lua_pushvalue(L, -2),将它Copy一份到栈顶,对这个Copy进行lua_tostring() // 读取Key,Value到C变量里面后,将Value和Copy弹出,留着Key在栈顶,给下次lua_next()用 // // 指令及栈图变化如下: (假如Table的栈下标是Index) // 0. 刚进入函数时 ...Table, ... <--- 这里栈顶 // 1. lua_pushnil(L) ...Table, ..., nil <--- 这里栈顶 // 2. lua_next(L, Index) ...Table, ..., Key, Value <--- 这里栈顶 // 3. lua_pushvalue(L, -2) ...Table, ..., Key, Value, KeyCopy <--- 这里栈顶 // 4. lua_pop(L, 2), ...Table, ..., Key <--- 这里栈顶 // ... 如此重复2,3,4 // N. lua_next(L, Index)返回0 ...Table, ... <--- 这里栈顶 /* ====================================================== */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值