這篇是說c調用lua的。
會通過代碼來展示如何在設定檔裡面配置記錄,並讓c調用去執行。
有一個設定檔名稱是“config”用來記錄配置的
另外有個.c程式來載入config,並調用config中函數來產生新的記錄。
編譯:gcc -g -Wall configbylua.c -o cblua -ldl -llua -lm
下面看代碼:
config
max = 3default={name="chlaws",addr="china",url="blog.csdn.net/chlaws"}chlaws=defaultfile="userinfo.txt"function logtofile(file,user)out,err = io.open(file,"a+")if not out then return -1 endr={}r[#r+1]='entry{\n'for k ,v in pairs(user) dor[#r+1] = k .. ':"' .. v ..'"\n'endr[#r+1]='}\n's=table.concat(r)out:write(s)return 0end
.c
/* * ===================================================================================== * * Filename: configbylua.c * * Description: * * Version: 1.0 * Created: 12/28/2012 03:30:56 PM * Revision: none * Compiler: gcc * * Author: jiangwenlong (http://blog.csdn.net/chlaws), jiangwenlong@pipi.cn * Company: PIPI * * ===================================================================================== */#include <stdio.h>#include <string.h>#include "lua.h"#include "lauxlib.h"#include "lualib.h"#define MAX_PATH 256char config[MAX_PATH]="./config";void pstack(lua_State *L){int len = lua_gettop(L);int i = 1;printf("lua stack:\n");for(; i <= len ; ++i){int type = lua_type(L,i);switch(type){case LUA_TSTRING:printf("[%s]",lua_tostring(L,i));break;case LUA_TNUMBER:printf("[%d]",(int)lua_tonumber(L,i));break;case LUA_TNIL:printf("[nil]");break;case LUA_TBOOLEAN:printf("[%s]",lua_toboolean(L,i)? "true":"false");break;case LUA_TTABLE:printf("[table]");break;case LUA_TFUNCTION:printf("[func]");break;default:printf("[type %d]",type);break;}printf(" ");}printf("\n");}void get_luavar(lua_State *L){//取預設file值lua_getglobal(L,"file");if(!lua_isstring(L,-1)){luaL_error(L,"var file not type of string");}const char *lua_var_file = lua_tostring(L,-1);printf("lua var file:%s\n",lua_var_file);//取預設table值lua_getglobal(L,"default");if(!lua_istable(L,-1)){luaL_error(L,"var default not type of table");}lua_getfield(L,-1,"name");const char * lua_var_field = lua_tostring(L,-1);printf("default table field name:%s\n",lua_var_field);lua_pop(L,2);lua_getglobal(L,"default");lua_pushstring(L,"url");lua_gettable(L,-2);lua_var_field = lua_tostring(L,-1);printf("default table field url:%s\n",lua_var_field);//取函數壓到棧頂lua_getglobal(L,"logtofile");if(!lua_isfunction(L,-1)){luaL_error(L,"var logtofile not type of function");}else{printf("logtofile is lua function\n");}//刷一次棧中所有元素pstack(L);//清空stacklua_pop(L,lua_gettop(L));}void set_luavar(lua_State *L){//更改file變數的值lua_pushstring(L,"the_user.txt");lua_setglobal(L,"file");//增加一個user tablelua_newtable(L);lua_pushstring(L,"name");lua_pushstring(L,"jwl");lua_settable(L,-3);lua_pushstring(L,"addr");lua_pushstring(L,"hz");lua_settable(L,-3);lua_pushstring(L,"url");lua_pushstring(L,"pipi.cn");lua_settable(L,-3);lua_setglobal(L,"jwl");//增加一個user tablelua_newtable(L);lua_pushstring(L,"mr.jiang");lua_setfield(L,-2,"name");lua_pushstring(L,"usa");lua_setfield(L,-2,"addr");lua_pushstring(L,"about.com");lua_setfield(L,-2,"url");lua_setglobal(L,"jiang");//清空stacklua_pop(L,lua_gettop(L));}void exec_luafunc(lua_State *L,const char *func,const char *file,const char *record){lua_getglobal(L,func);lua_getglobal(L,file);lua_getglobal(L,record);if(lua_pcall(L,2,1,0)){lua_error(L);}int ret = (int)lua_tonumber(L,-1);if(ret != 0){printf("record %s write to log failed\n",record);}else{printf("record %s write to log successful\n",record);}lua_pop(L,1);}int main(int argc,char**argv){if(argc == 2){strcpy(config,argv[1]);}lua_State *L = luaL_newstate();luaL_openlibs(L);//0的時候表示都成功,否則出錯if(luaL_loadfile(L,config) || lua_pcall(L,0,0,0)){lua_error(L);}get_luavar(L);set_luavar(L);exec_luafunc(L,"logtofile","file","chlaws");exec_luafunc(L,"logtofile","file","default");exec_luafunc(L,"logtofile","file","jwl");exec_luafunc(L,"logtofile","file","jiang");lua_close(L);return 0;}
運行後會產生一個“the_user.txt",其內容如下:
entry{addr:"usa"name:"mr.jiang"url:"about.com"}entry{addr:"hz"name:"jwl"url:"pipi.cn"}entry{addr:"china"name:"chlaws"url:"blog.csdn.net/chlaws"}entry{addr:"china"name:"chlaws"url:"blog.csdn.net/chlaws"}