lua c读取嵌套表

lua c读取嵌套表,第1张

概述下面是我需要从C读取的lua表: listen = { { port = 1234, address = "192.168.1.1", userdata = "liunx" }, { port = 1235, address = "192.168.1.2", userdata = "liunx1" }, { port = 1236, address = "192.168.1 下面是我需要从C读取的lua表:

Listen = {    { port = 1234,address = "192.168.1.1",userdata = "liunx" },{ port = 1235,address = "192.168.1.2",userdata = "liunx1" },{ port = 1236,address = "192.168.1.3",userdata = "liunx2" }}

下面是c代码:

#include <lua.h>                                /* Always include this when calling Lua */#include <lauxlib.h>                            /* Always include this when calling Lua */#include <lualib.h>                             /* Prototype for luaL_openlibs(),*//*   always include this when calling Lua */#include <stdlib.h>                             /* For function exit() */#include <stdio.h>                              /* For input/output */voID bail(lua_State *L,char *msg){    fprintf(stderr,"\nFatal error:\n  %s: %s\n\n",msg,lua_tostring(L,-1));    exit(1);}int main(voID){    lua_State *L;    L = luaL_newstate();                        /* Create Lua state variable */    luaL_openlibs(L);                           /* Load Lua librarIEs */    if (luaL_loadfile(L,"cfg.lua"))         bail(L,"luaL_loadfile() Failed");    if (lua_pcall(L,0))                   bail(L,"lua_pcall() Failed");    // how to read???    lua_getglobal(L,"Listen");    lua_close(L);    return 0;}

我想旅行这个表可能包含while循环中的一些数据,但真的不知道怎么做,所以任何提示?

非常感谢您的提示!以下是工作代码:

#include <lua.h>                                /* Always include this when calling Lua */#include <lauxlib.h>                            /* Always include this when calling Lua */#include <lualib.h>                             /* Prototype for luaL_openlibs(),char *msg){    fprintf(stderr,-1));    exit(1);}int main(voID){    lua_State *L;    static struct {        const char * name;        int type;    } fIElds[] = {        {"port",LUA_TNUMBER},{"address",LUA_TSTRING},{"userdata",{NulL,0}    };    L = luaL_newstate();                        /* Create Lua state variable */    luaL_openlibs(L);                           /* Load Lua librarIEs */    if (luaL_loadfile(L,"luaL_loadfile() Failed");    if (lua_pcall(L,"lua_pcall() Failed");    lua_getglobal(L,"Listen");    luaL_checktype(L,-1,LUA_Ttable);    int i;    for (i = 1; ; i++) {        lua_rawgeti(L,i);        if (lua_isnil(L,-1)) {            lua_pop(L,1);            break;        }        // an element of the 'Listen' table should Now be at the top of the stack        luaL_checktype(L,LUA_Ttable);        // read the content of that element        int fIEld_index;        for (fIEld_index = 0; (fIElds[fIEld_index].name != NulL                     && fIElds[fIEld_index].name != NulL); fIEld_index++) {            lua_getfIEld(L,fIElds[fIEld_index].name);            luaL_checktype(L,fIElds[fIEld_index].type);            // you should probably use a function pointer in the fIElds table.            // I am using a simple switch/case here            switch(fIEld_index) {                case 0:                    printf("port: %d\n",(int)lua_tonumber(L,-1));                    // do what you want with port                    break;                case 1:                    printf("address: %s\n",-1));                    break;                case 2:                    // handle userdata                    printf("userdata: %s\n",-1));                    break;            }            // remove the fIEld value from the top of the stack            lua_pop(L,1);         }        // remove the element of the 'Listen' table from the top of the stack.        lua_pop(L,1);    }    lua_close(L);    return 0;}
解决方法 你不是太远了.这里的关键是要了解Lua API如何使用堆栈来处理所有事情.

这是一个未经测试的代码示例,可以帮助您:

// this will be used to valIDate our tablestatic struct {    const char * name;    int type;} fIElds[] = {    {"port",NulL};lua_getglobal(L,"Listen");// the 'Listen' table should be at the top of the stackluaL_checktype(L,LUA_Ttable);// iterate the Listen tableint i;for (i = 1; ; i++) {    lua_rawgeti(L,i);    // when you get nil,you're done    if (lua_isnil(L,-1)) {        lua_pop(L,1);        break;    }    // an element of the 'Listen' table should Now be at the top of the stack    luaL_checktype(L,LUA_Ttable);    // read the content of that element    int fIEld_index;    for (fIEld_index = 0; fIElds[fIEld_index] != NulL; fIEld_index++) {        lua_getfIEld(L,fIElds[fIEld_index].name);        luaL_checktype(L,fIElds[fIEld_index].type);        // you should probably use a function pointer in the fIElds table.        // I am using a simple switch/case here        switch(fIEld_index) {        case 0:            port = lua_tonumber(L,-1);            // do what you want with port            break;        case 1:            address = lua_tostring(L,-1);            break;        case 2:            // handle userdata            break;        }        // remove the fIEld value from the top of the stack        lua_pop(L,1);     }    // remove the element of the 'Listen' table from the top of the stack.    lua_pop(L,1);}

我建议你使用这些文件:Lua API table Lua API ref

总结

以上是内存溢出为你收集整理的lua c读取嵌套表全部内容,希望文章能够帮你解决lua c读取嵌套表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1228062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存