初始化
lua_State *L;
void initLua()
{
L = luaL_newstate();
luaL_openlibs(L);
}
传递table(数组)
C++-------------------------------------
/**
* 调用 lua 定义的函数
* lua 的返回值为 表(数组)
*/
QStringList cutStr(const QString scriptPath, QString input)
{
QStringList strlist;
QString str;
int len = lua_objlen(L, -1); // 获取数组长度
if(luaL_dofile(L, scriptPath.toLocal8Bit().data()) != 0){
QString error = lua_tostring(L, -1);
lua_pop(L, 1);
qDebug()<<QString("错误: %1").arg(error);
}
//获取全局函数
lua_getglobal(L, "process");
if(!lua_isfunction(L,-1)) {
lua_pop(L, 1);
qDebug()<<"错误: 没有这个函数";
}
lua_pushstring(L, input.toLocal8Bit().data());
//调用函数
if(lua_pcall(L, 1, 1, 0) != 0) {
QString error = lua_tostring(L, 1);
lua_pop(L, 1);
qDebug()<<"调用函数错误:" + error;
}
for(int i = 1;i <= len; i++) {
lua_pushnumber(L, i);
lua_gettable(L, 1);
str = lua_tostring(L, -1);
strlist.append(str);
lua_pop(L, 1);
}
lua_pop(L, 1);
return strlist;
}
lua
lua --------------------------------------------------------
//lua 返回table(数组)
function process(input)
local parts = {}
for part in input:gmatch("([^,]+)") do
table.insert(parts, part)
end
return parts
end
传递table(键值对)
递归转换 Lua 表为 QVariantMap
/**
* 递归转换 Lua 表为 QVariantMap
*/
QVariantMap luaTableToMap(lua_State* L, int index = 1) {
QVariantMap result;
if (!lua_istable(L, index)) {
qWarning() << "Not a Lua table at index" << index;
return result;
}
//遍历表中的所有键值对
lua_pushnil(L); // 第一个键
while (lua_next(L, index < 0 ? index - 1 : index) != 0) {
// 键在 -2,值在 -1
QString key;
// 处理不同类型的键
if (lua_isstring(L, -2)) {
key = lua_tostring(L, -2);
} else if (lua_isnumber(L, -2)) {
key = QString::number(lua_tonumber(L, -2));
} else {
lua_pop(L, 1); // 弹出值,保留键
continue; // 跳过非字符串/数字键
}
// 处理不同类型的值
QVariant value;
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
value = lua_tonumber(L, -1);
break;
case LUA_TSTRING:
value = QString(lua_tostring(L, -1));
break;
case LUA_TBOOLEAN:
value = static_cast<bool>(lua_toboolean(L, -1));
break;
case LUA_TTABLE:
// 递归处理嵌套表
value = luaTableToMap(L, -1);
break;
case LUA_TNIL:
value = QVariant();
break;
default:
value = QString("[%1]").arg(lua_typename(L, lua_type(L, -1)));
}
result[key] = value;
lua_pop(L, 1); // 弹出值,保留键
}
return result;
}
lua 的返回值(键值对)
/**
* 调用 lua 定义的函数
* 返回 lua 的返回值(键值对)
*/
QStringList cutStrMap(const QString scriptPath, QString input)
{
if(luaL_dofile(L, scriptPath.toLocal8Bit().data()) != 0){
QString error = lua_tostring(L, -1);
lua_pop(L, 1);
qDebug()<<QString("错误: %1").arg(error);
}
//获取全局函数
lua_getglobal(L, "process");
if(!lua_isfunction(L,-1)) {
lua_pop(L, 1);
qDebug()<<"错误: 没有这个函数";
}
lua_pushstring(L, input.toLocal8Bit().data());
//调用函数
if(lua_pcall(L, 1, 1, 0) != 0) {
QString error = lua_tostring(L, 1);
lua_pop(L, 1);
qDebug()<<"调用函数错误:" + error;
}
// 转换 Lua 表为 QVariantMap
QVariantMap userData = luaTableToMap(L);
lua_pop(L, 1); // 弹出表
qDebug()<<userData;
QStringList strlist;
return strlist;
}
lua
//lua 返回table(键值对)
function process(input)
return {
name = "Alice",
age = "30",
email = "alice@example.com",
phone = "123-4567",
active = "true",
created = "2023-05-15",
modified = "2023-05-20"
}
end
不使用函数方式
/**
* 递归转换 Lua 表为 QVariantMap
*/
QVariantMap luaTableToMap(lua_State* L, int index = 1) {
QVariantMap result;
if (!lua_istable(L, index)) {
qWarning() << "Not a Lua table at index" << index;
return result;
}
//遍历表中的所有键值对
lua_pushnil(L); // 第一个键
while (lua_next(L, index < 0 ? index - 1 : index) != 0) {
// 键在 -2,值在 -1
QString key;
// 处理不同类型的键
if (lua_isstring(L, -2)) {
key = lua_tostring(L, -2);
} else if (lua_isnumber(L, -2)) {
key = QString::number(lua_tonumber(L, -2));
} else {
lua_pop(L, 1); // 弹出值,保留键
continue; // 跳过非字符串/数字键
}
// 处理不同类型的值
QVariant value;
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
value = lua_tonumber(L, -1);
break;
case LUA_TSTRING:
value = QString(lua_tostring(L, -1));
break;
case LUA_TBOOLEAN:
value = static_cast<bool>(lua_toboolean(L, -1));
break;
case LUA_TTABLE:
// 递归处理嵌套表
value = luaTableToMap(L, -1);
break;
case LUA_TNIL:
value = QVariant();
break;
default:
value = QString("[%1]").arg(lua_typename(L, lua_type(L, -1)));
}
result[key] = value;
lua_pop(L, 1); // 弹出值,保留键
}
return result;
}
/**
* 合并两个QVariantMap
*/
QVariantMap mergeVariantMaps(const QVariantMap &v1, const QVariantMap &v2) {
QVariantMap result = v1; // 初始化为v1的副本(保留v1的所有键值)
// 遍历v2的所有键
for (auto it = v2.constBegin(); it != v2.constEnd(); ++it) {
const QString &key = it.key();
// 仅当v1中不存在该键时,才添加v2的键值对
if (!result.contains(key)) {
result[key] = it.value();
}
}
return result;
}
void cutStrMap(const QString scriptPath, QString input)
{
lua_pushstring(L, input.toLocal8Bit().data());
lua_setglobal(L, "input");
if(luaL_dofile(L, scriptPath.toLocal8Bit().data()) != 0) {
QString error = lua_tostring(L, -1);
lua_pop(L, 1);
qDebug()<<QString("错误: %1").arg(error);
return;
}
// 转换 Lua 表为 QVariantMap
QVariantMap userData = luaTableToMap(L);
static QVariantMap userData_Merge; //接受区域数据合并的内容的值
static QVariantMap userData_Merge_pre; //接受区域数据上一个接收来并且合并的内容的值
lua_pop(L, 1); // 弹出表
//当不同命令中有键名相同时,保留该键值对
userData_Merge = mergeVariantMaps(userData,userData_Merge);//合并新值和旧值
// qDebug()<<"userData_Merge:"<<userData_Merge;
// qDebug()<<"userData:"<<userData;
cmpFlashVariantMaps(userData_Merge_pre,userData_Merge);
userData_Merge_pre = userData_Merge;
}
local parts = {}
for part in input:gmatch("([^,]+)") do
table.insert(parts, part)
end
return {
_01 = parts[2],
_02 = parts[3],
_03 = parts[4]
}
1227

被折叠的 条评论
为什么被折叠?



