- 论坛徽章:
- 11
|
@starwing83
c中调用 lua, 代码如下:- lua_State* L = luaL_newstate();
- if (L == NULL) {
- printf("failed to create config file parser\n");
- return false;
- }
- luaopen_base(L);
- if (LUA_ERRFILE == luaL_loadfile(L, filename)) {
- printf("no config file %s\n", filename);
- lua_close(L);
- return false;
- }
- int n = lua_pcall(L, 0, 0, 0);
- // if (LUA_OK != n) {
- // printf("parse %s failed with err %d\n", filename, n);
- // lua_close(L);
- // return false;
- // }
复制代码 结果总是返回输出 parse config failed with err 2; 但如果我注释掉检查代码直接向下走, 却是能够正确执行的, 不知是怎么回事, 该怎么避免
版本 5.2.2
lua 代码:
- function config.new(t)
- config.cmn.__index = config.cmn;
- setmetatable(t, config.cmn);
- return t;
- end
- function get_config(type)
- local conf = nil;
- if (type == "xxxx") then
- conf = config.new(config.xxxx);
- elseif (type == "yyyy") then
- conf = config.new(config.yyyy);
- elseif (type == "zzzz") then
- conf = config.new(config.zzzz);
- elseif (type == "wwww") then
- conf = config.new(config.wwww);
- end
- return conf;
- end
- function oplog_get_address(addr, key)
- local conf = get_config(addr);
- if not conf then
- return nil;
- end
- conf = conf[key];
- if type(conf) == "string" then
- print(conf);
- elseif type(conf) == "table" then
- local str = "";
- for _, v in ipairs(conf) do
- str = str .. v .. ",";
- end
- str = string.sub(str, 1, -2);
- print(str);
- end
- return nil;
- end
- if (arg[1] == "oplog") then
- local addr = arg[2];
- local key = arg[3];
- oplog_get_address(addr, key);
- end
复制代码 |
|