这篇是说c调用lua的。
会通过代码来展示如何在配置文件里面配置记录,并让c调用去执行。
有一个配置文件名称是“config”用来记录配置的
另外有个.c程序来载入config,并调用config中函数来生成新的记录。
编译:gcc -g -Wall configbylua.c -o cblua -ldl -llua -lm
下面看代码:
config
max = 3
default={name="chlaws",addr="china",url="blog.csdn.net/chlaws"}
chlaws=default
file="userinfo.txt"
function logtofile(file,user)
out,err = io.open(file,"a+")
if not out then return -1 end
r={}
r[#r+1]='entry{\n'
for k ,v in pairs(user) do
r[#r+1] = k .. ':"' .. v ..'"\n'
end
r[#r+1]='}\n'
s=table.concat(r)
out:write(s)
return 0
end
/*
* =====================================================================================
*
* 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 256
char 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);
//清空stack
lua_pop(L,lua_gettop(L));
}
void set_luavar(lua_State *L)
{
//更改file变量的值
lua_pushstring(L,"the_user.txt");
lua_setglobal(L,"file");
//增加一个user table
lua_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 table
lua_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");
//清空stack
lua_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"
}