lua与c交互 一

(一) lua 代码

    --Lua的字符串模式  
    szMode = "(%w+)%s*=%s*(%w+)";   
   --要处理的字符串   
   szStr = "key1 = value1 key2 = value2";   
    --目标字符串模式  
    szTag = "<%1>%2</%1>"; 

   r = string.gsub(szStr ,szMode , szTag );
   u = string.upper(r);


运行结果:
<key1>value1<key1> <key2>value2<key2>
<EKY1>VALUE1<KEY1> <KEY2>VALUE2<KEY2>


(二)C 与 lua 交互
#include "lua.h" 
#include "lualib.h"
#include "lauxlib.h" 
#include "stdio.h"

 int main() {  
    int ret ;  
 
    char *szLua_code =  "r = string.gsub(c_Str, c_Mode, c_Tag); u = string.upper(r);";

    //Lua的字符串模式  
    char *szMode = "(%w+)%s*=%s*(%w+)";   
    //要处理的字符串   
   char *szStr = "key1 = value1 key2 = value2";   
    //目标字符串模式  
    char *szTag = "<%1>%2</%1>";  

    lua_State *L = luaL_newstate();   
    luaL_openlibs(L);  

  
   //把一个数据送给Lua 
    lua_pushstring(L, szMode); 
    lua_setglobal(L, "c_Mode");
    lua_pushstring(L, szTag); 
    lua_setglobal(L, "c_Tag"); 
    lua_pushstring(L, szStr);
    lua_setglobal(L, "c_Str"); 

      //执行
    bool err = luaL_loadbuffer(L, szLua_code, strlen(szLua_code),  "demo") || lua_pcall(L, 0, 0, 0);  
    if(ret)  {    
         //如果错误,显示  
         printf("%s\n", lua_tostring(L, -1) );   
    
        //弹出栈顶的这个错误信息   
        lua_pop(L, 1);
    } else {  
    
          //Lua执行后取得全局变量的值
        lua_getglobal(L, "r");    
        printf("%s\n", lua_tostring(L,-1) ); 
        lua_pop(L, 1); 
          
        lua_getglobal(L, "u");  
        printf("%s\n" lua_tostring(L,-1) );     
        lua_pop(L, 1);  
 
   } 
      lua_close(L);  
      return 0; 
}


运行结果:
<key1>value1<key1> <key2>value2<key2>
<EKY1>VALUE1<KEY1> <KEY2>VALUE2<KEY2>

说明:
这段代码把字符串中的key=value字符串全部转换成XML格式<key>value</key>
    在这个例子中,C++程序通过调用lua_pushstring把C字符串压入栈顶,lua_setglobal的作用是把栈顶的数据传到Lua环境中作为全局变量。
    执行代码完成后,使用lua_getglobal从Lua环境中取得全局变量压入栈顶,然后使用lua_tostring把栈顶的数据转成字符串。

由于lua_tostring本身没有出栈功能,所以为了平衡(即调用前与调用后栈里的数据量不变),使用lua_pop弹出由lua_setglobal压入的数据。


(三)

table在Lua中唯一的数据结构,其它语言提供的各种数据结构Lua都是用table来实现的 。下面是一个C API操作table的例子。

#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main()
{
        lua_State *L;
        L = luaL_newstate();
        luaL_openlibs(L);
        // ta = {'AA', 'BB', {'CC', 'DD'}}
        lua_newtable(L);
        //依次将各个元素放入table
        lua_pushnumber(L,1);
        lua_pushstring(L, "AA");
        lua_rawset(L,1);
        lua_pushnumber(L,2);
        lua_pushstring(L, "BB");
        lua_rawset(L,1);
        lua_pushnumber(L,3);
        lua_newtable(L);
        lua_pushstring(L, "CC");
        lua_rawseti(L,-2,1);
        lua_pushstring(L, "DD");
        lua_rawseti(L,-2,2);
        lua_rawset(L,1);
        lua_setglobal(L,"ta");
        //此时栈中为空,此处省略其他操作
        //将ta压入栈顶
        lua_getglobal(L, "ta");
        //获得第一个元素
        lua_rawgeti(L, 1,1);
        if(lua_type(L,-1) == LUA_TSTRING)
                printf("%s", lua_tostring(L,-1));
        lua_pop(L,1);
        lua_rawgeti(L, 1,2);
        if(lua_type(L,-1) == LUA_TSTRING)
                printf("%s", lua_tostring(L,-1));
        lua_pop(L,1);
        lua_rawgeti(L, 1,3);
        if(lua_type(L,-1) == LUA_TTABLE)
        {
                //因为第三个元素是table,所以继续获得它的第一个元素
                lua_rawgeti(L, -1,1);
                if(lua_type(L,-1) == LUA_TSTRING)
                        printf("%s", lua_tostring(L,-1));
                lua_pop(L,1);
                lua_rawgeti(L, -1,2);
                if(lua_type(L,-1) == LUA_TSTRING)
                        printf("%s", lua_tostring(L,-1));
                lua_pop(L,1);                
        }
        lua_pop(L,1); //此时栈顶为ta
        lua_close(L);
}
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值