lua 与 c/c++ 交互(6) lua调用C++(使用数组 和字符串函数)
2014-12-10 23:15
519 查看
lua调用 c++ 的 两个函数: 一个是 对lua 数组 调用函数替换 数组元素,一个 分割字符串
test.lua
cpp
// LuaCApi.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
using namespace std;
extern "C"{
#include "lua\lua.h"
#include "lua\lauxlib.h"
#include "lua\lualib.h"
}
void error(lua_State * l,const char * fmt,...){
va_list va;
va_start(va,fmt);
vfprintf(stderr,fmt,va);
va_end(va);
lua_close(l);
exit(EXIT_FAILURE);
}
int swapArray(lua_State *l){
luaL_checktype(l,1,LUA_TTABLE);
luaL_checktype(l,2,LUA_TFUNCTION);
int len = lua_objlen(l,1);
for (int i = 1; i <= len; i++)
{
lua_pushvalue(l,2);
lua_rawgeti(l,1,i);
if (lua_pcall(l,1,1,0))
{
printf("error :%s",lua_tostring(l,-1));
}
lua_rawseti(l,1,i);
}
return 0;
}
int split(lua_State *l){
const char * s = luaL_checkstring(l,1);
const char * sep = luaL_checkstring(l,2);
const char * index = NULL;
int count = 1;
lua_newtable(l);
while ((index = strchr(s,*sep)) != NULL)
{
lua_pushlstring(l,s,index-s);
lua_rawseti(l,-2,count++);
s = index + 1;
}
lua_pushstring(l,s);
lua_rawseti(l,-2,count);
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * l = luaL_newstate();
luaL_openlibs(l);
lua_register(l,"swapArray",swapArray);
lua_register(l,"split",split);
if (luaL_loadfile(l,"test.lua") || lua_pcall(l,0,0,0))
{
error(l,"error : %s",lua_tostring(l,-1));
}
return 0;
}
test.lua
--数组操作 a = {1,2,3,4,5,6} swapArray(a,function(t) return t + 1 end) local function printArray(array) for k,v in ipairs(array) do print(k,"\t",v,"\n") end end printArray(a) --分割字符串 a = split("a,b,c,d,e,fgh,ijk",",") printArray(a)
cpp
// LuaCApi.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
using namespace std;
extern "C"{
#include "lua\lua.h"
#include "lua\lauxlib.h"
#include "lua\lualib.h"
}
void error(lua_State * l,const char * fmt,...){
va_list va;
va_start(va,fmt);
vfprintf(stderr,fmt,va);
va_end(va);
lua_close(l);
exit(EXIT_FAILURE);
}
int swapArray(lua_State *l){
luaL_checktype(l,1,LUA_TTABLE);
luaL_checktype(l,2,LUA_TFUNCTION);
int len = lua_objlen(l,1);
for (int i = 1; i <= len; i++)
{
lua_pushvalue(l,2);
lua_rawgeti(l,1,i);
if (lua_pcall(l,1,1,0))
{
printf("error :%s",lua_tostring(l,-1));
}
lua_rawseti(l,1,i);
}
return 0;
}
int split(lua_State *l){
const char * s = luaL_checkstring(l,1);
const char * sep = luaL_checkstring(l,2);
const char * index = NULL;
int count = 1;
lua_newtable(l);
while ((index = strchr(s,*sep)) != NULL)
{
lua_pushlstring(l,s,index-s);
lua_rawseti(l,-2,count++);
s = index + 1;
}
lua_pushstring(l,s);
lua_rawseti(l,-2,count);
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * l = luaL_newstate();
luaL_openlibs(l);
lua_register(l,"swapArray",swapArray);
lua_register(l,"split",split);
if (luaL_loadfile(l,"test.lua") || lua_pcall(l,0,0,0))
{
error(l,"error : %s",lua_tostring(l,-1));
}
return 0;
}
相关文章推荐
- lua 与 c/c++ 交互(3)c/c++ 调用 lua 数组类型 table
- Lua与C/C++交互——C/C++调用Lua脚本
- tolua 使用 Lua调用c++多返回值函数
- (使用lua++)Lua脚本和C++交互(四)
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- C++中使用Lua脚本 和lua中调用c的方法
- vs项目中使用c++调用lua
- 使用cocos2d-x 3.x lua 开发时 lua与c++交互
- [lua] 使用lua string作为二进制buffer和c/c++交互
- c/c++_Lua交互----关于Lua中table类型的使用实例
- Lua与C/C++的交互6:Lua调用C/C++函数
- Lua使用C++中的类——调用类方法并访问类变量
- C++中使用Lua脚本 和lua中调用c的方法
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- Lua与C++交互调用(1)
- C++中使用Lua脚本 和lua中调用c的方法
- Lua的使用入门之C/C++与lua函数的混合调用
- Lua使用C++中的类——调用类方法并访问类变量
- 在C++使用LUA交互,LUA实现闭包,C++/LUA相互闭包
- lua 与 c/c++ 交互(2)c/c++ 调用 lua