extern "C" {
#include "lua.hpp"
#include "lauxlib.h"
#include "lualib.h"
};
#pragma comment(lib, "lua.lib")
#include <iostream>
#include <Windows.h>
#include <vector>
#include <set>
using namespace std;
static const char* const ERROR_ARGUMENT_COUNT = "error param num!";
static const char* const ERROR_ARGUMENT_TYPE = "error param type!";
class Solution {
public:
vector<string> Permutation(string str, vector<string>&result) {
loc_Permutation(str, 0);
if (str.size() == 0)return result;
for (auto item : all_str){
result.push_back(item);
}
//result = all_str;
return result;
}
void loc_Permutation(string str, int loc){
all_str.insert(str);
//all_str.push_back(str);
//cout << str << endl;
int size = str.size();
if (loc == size - 1)return;
//loc_Permutation(str, loc + 1);
for (int idx_swap = loc; idx_swap < size; idx_swap++){
//cout << loc << " " << idx_swap << " ";
swap(str[loc], str[idx_swap]);
loc_Permutation(str, loc + 1);
swap(str[loc], str[idx_swap]);
}
}
public:
set<string> all_str;
};
// 发生错误,报告错误
void ErrorMsg(lua_State* L, const char* const pszErrorInfo)
{
lua_pushstring(L, pszErrorInfo);
lua_error(L);
}
// 检测函数调用参数个数是否正常
void CheckParamCount(lua_State* L, int paramCount)
{
// lua_gettop获取栈中元素个数.
if (lua_gettop(L) != paramCount)
{
ErrorMsg(L, ERROR_ARGUMENT_COUNT);
}
}
//全排列算法
extern "C" int AllSort(lua_State* L)
{
// 提取参数.
const char* str= luaL_checkstring(L, 1);
if (str)
{
Solution sln;
vector<string> strResult;
strResult = sln.Permutation(str, strResult);
//lua_settop(L, 0);
lua_newtable(L);
lua_newtable(L);
int count = 1;
for (int i = 0; i < strResult.size();i++)
{
lua_pushstring(L, strResult[i].c_str());
//lua_setfield(L, -2, i);
lua_rawseti(L, -2, count++);
}
//嵌套school表到参数表中
lua_pushstring(L, "array");
lua_insert(L, -2); /* school table */
lua_settable(L, -3); /* param.school = school */
//return strResult.size();
}
else
{
ErrorMsg(L, ERROR_ARGUMENT_TYPE);
}
// 返回值个数为0个.
return 1;
}
typedef struct student
{
int age; ///< 年龄
int grade; ///< 年级
char name[32]; ///< 姓名
}student;
typedef struct school
{
char phone[32]; ///< 电话
int totalPeople; ///< 总人数
}school;
int get_student_info(student* p_student)
{
p_student->age = 8;
p_student->grade = 3;
strncpy_s(p_student->name, "XiaoMing", 32);
return 0;
}
int get_school_info(school* p_school)
{
p_school->totalPeople = 500;
strncpy_s(p_school->phone, "1008611", 32);
return 0;
}
#define JL_GET_ARRAY_MAX 16 ///< 一次获取最多属性数目
#define JL_KEY_BUF_MAX 16 ///< 关键字最大缓存长度
static void format_student(lua_State *L)
{
student student_info;
memset(&student_info, 0, sizeof(student_info));
int ret = get_student_info(&student_info);
if (0 != ret)
{
}
else
{
lua_newtable(L);
lua_pushstring(L, student_info.name);
lua_setfield(L, -2, "name");
lua_pushinteger(L, student_info.age);
lua_setfield(L, -2, "age");
lua_pushinteger(L, student_info.grade);
lua_setfield(L, -2, "grade");
//嵌套student表到参数表中
lua_pushstring(L, "student");
lua_insert(L, -2); /* student table */
lua_settable(L, -3); /* param.student = student */
}
}
static void format_school(lua_State *L)
{
school school_info;
memset(&school_info, 0, sizeof(school_info));
int ret = get_school_info(&school_info);
if (0 != ret)
{
}
else
{
lua_newtable(L);
lua_pushstring(L, school_info.phone);
lua_setfield(L, -2, "phone");
lua_pushinteger(L, school_info.totalPeople);
lua_setfield(L, -2, "totalPeople");
//嵌套school表到参数表中
lua_pushstring(L, "school");
lua_insert(L, -2); /* school table */
lua_settable(L, -3); /* param.school = school */
}
}
//这里我们注册到Lua的函数 参数为数组{"school", "student"}
extern "C" int lua_get_info(lua_State *L)
{
luaL_checktype(L, 1, LUA_TTABLE); //检测传递过来的是否为table
int array_len = lua_rawlen(L, 1); //数组的长度
char keys[JL_GET_ARRAY_MAX][JL_KEY_BUF_MAX] = { 0 };
int len_max = (array_len <= JL_GET_ARRAY_MAX) ? array_len : JL_GET_ARRAY_MAX;
int idx = 1;
int keys_num = 0;
//取元素
for (int i = 0; i < len_max; i++)
{
lua_rawgeti(L, 1, idx++);
size_t tmp_len = 0;
const char* p_str = luaL_checklstring(L, -1, &tmp_len);
if ((0 < tmp_len) && (tmp_len < JL_KEY_BUF_MAX))
{
strcpy_s(keys[keys_num], p_str);
keys_num += 1;
}
lua_pop(L, 1);
}
//返回table
lua_newtable(L);
for (int k = 0; k < keys_num; ++k)
{
if (0 == strcmp(keys[k], "school"))
{
format_school(L); //拼school table
}
else if (0 == strcmp(keys[k], "student"))
{
format_student(L); //拼student table
}
}
return 1; //表明返回一个参数
}
// 显示Windows对话框.
// @param [in] pszMessage string 1
// @param [in] pszCaption string 2
extern "C" int ShowMsgBox(lua_State* L)
{
const char* pszMessage = "hdc";
const char* pszCaption = "hdc";
// 检测参数个数是否正确.
CheckParamCount(L, 2);
// 提取参数.
pszMessage = luaL_checkstring(L, 1);
pszCaption = luaL_checkstring(L, 2);
if (pszCaption && pszMessage)
{
cout << "Hello world from clibs!" << endl;
lua_pushstring(L, "Hello world from clibs!");
}
else
{
ErrorMsg(L, ERROR_ARGUMENT_TYPE);
}
// 返回值个数为0个.
return 1;
}
extern "C" int sub2(lua_State* L)
{
double op1 = luaL_checknumber(L, 1);
double op2 = luaL_checknumber(L, 2);
int temp = op1 - op2;
lua_pushnumber(L, temp);
return 1;
}
// 导出函数列表.
static luaL_Reg luaLibs[] =
{
{ "ShowMsgBox", ShowMsgBox },
{ "sub2", sub2 },
{ "AllSort", AllSort },
{ "lua_get_info", lua_get_info },
{ NULL, NULL }
};
// part two: DLL入口函数,Lua调用此DLL的入口函数.
extern "C" __declspec(dllexport)
int luaopen_WinFeature(lua_State* L) { //WinFeature是modole名, 将来require这个名字
const char* const LIBRARY_NAME = "WinFeature"; //这里也写WinFeature
//luaL_register(L, LIBRARY_NAME, luaLibs); //关键一行, 在luaState上注册好这个lib
lua_newtable(L);
luaL_setfuncs(L, luaLibs,0);
return 1;
}