dll:导出示例完成。
This commit is contained in:
parent
4ac3d4c0b4
commit
22298f0583
@ -11,7 +11,7 @@ ReflowComments: true
|
|||||||
SpacesBeforeTrailingComments: 3
|
SpacesBeforeTrailingComments: 3
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
ColumnLimit: 80
|
ColumnLimit: 100
|
||||||
AllowShortBlocksOnASingleLine: Never
|
AllowShortBlocksOnASingleLine: Never
|
||||||
AllowShortFunctionsOnASingleLine: None
|
AllowShortFunctionsOnASingleLine: None
|
||||||
AllowShortEnumsOnASingleLine: false
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
@ -16,5 +16,5 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
|||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/)
|
||||||
|
|
||||||
add_subdirectory(lua)
|
add_subdirectory(lua)
|
||||||
add_subdirectory(demo)
|
add_subdirectory(fs)
|
||||||
add_executable(elua main.cpp)
|
add_executable(elua main.cpp)
|
@ -1,11 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
|
||||||
|
|
||||||
project(demo LANGUAGES CXX)
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
|
||||||
|
|
||||||
if (MSVC)
|
|
||||||
add_compile_options(/source-charset:utf-8)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_library(demo SHARED lib.h lib.cxx)
|
|
||||||
target_link_libraries(demo PRIVATE lua)
|
|
16
demo/lib.cxx
16
demo/lib.cxx
@ -1,16 +0,0 @@
|
|||||||
#include "lib.h"
|
|
||||||
|
|
||||||
int c_multiply(lua_State* L)
|
|
||||||
{
|
|
||||||
double a = luaL_checknumber(L, 1);
|
|
||||||
double b = luaL_checknumber(L, 2);
|
|
||||||
lua_pushnumber(L, a * b);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注册 C 函数
|
|
||||||
int luaopen_mylib(lua_State* L)
|
|
||||||
{
|
|
||||||
lua_register(L, "c_multiply", c_multiply); // 注册为全局函数
|
|
||||||
return 0;
|
|
||||||
}
|
|
18
demo/lib.h
18
demo/lib.h
@ -1,18 +0,0 @@
|
|||||||
#ifndef LIB_HEADER
|
|
||||||
#define LIB_HEADER
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <lauxlib.h>
|
|
||||||
#include <lua.h>
|
|
||||||
#include <lualib.h>
|
|
||||||
|
|
||||||
int luaopen_mylib(lua_State* L);
|
|
||||||
int c_multiply(lua_State* L);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
12
fs/CMakeLists.txt
Normal file
12
fs/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(lua_fs LANGUAGES CXX)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
|
if (MSVC)
|
||||||
|
add_compile_options(/source-charset:utf-8)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(lua_fs SHARED lib.h lib.cxx)
|
||||||
|
target_link_libraries(lua_fs PRIVATE lua)
|
||||||
|
target_compile_definitions(lua_fs PRIVATE FS_LIB_EXPORT)
|
2
fs/debug.bat
Normal file
2
fs/debug.bat
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
cmd /k "set PATH=%PATH%;%~dp0..\build\bin\Debug"
|
12
fs/fs.lua
Normal file
12
fs/fs.lua
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
-- 加载模块
|
||||||
|
local fs = require "lua_fs"
|
||||||
|
|
||||||
|
print(fs.file_exists("D:\\download"))
|
||||||
|
local files = fs.get_dir_files("D:\\download")
|
||||||
|
if files then
|
||||||
|
for i, filename in ipairs(files) do
|
||||||
|
print(i, filename) -- 输出索引和文件名
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("Error:")
|
||||||
|
end
|
75
fs/lib.cxx
Normal file
75
fs/lib.cxx
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include "lib.h"
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
static const luaL_Reg lua_fs[] = {
|
||||||
|
{"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file},
|
||||||
|
{"delete_file", delete_file}, {"get_dir_files", get_dir_files}, {NULL, NULL}};
|
||||||
|
|
||||||
|
int file_exists(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
bool result = fs::exists(path);
|
||||||
|
lua_pushboolean(L, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_regular_file(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
bool result = fs::is_regular_file(path);
|
||||||
|
lua_pushboolean(L, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int copy_file(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* source = luaL_checkstring(L, 1);
|
||||||
|
const char* destination = luaL_checkstring(L, 2);
|
||||||
|
bool result = fs::copy_file(source, destination);
|
||||||
|
lua_pushboolean(L, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int delete_file(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
bool result = fs::remove(path);
|
||||||
|
lua_pushboolean(L, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int luaopen_lua_fs(lua_State* L)
|
||||||
|
{
|
||||||
|
luaL_newlib(L, lua_fs);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_dir_files(lua_State* L)
|
||||||
|
{
|
||||||
|
// 获取第一个参数:目录路径
|
||||||
|
const char* dir_path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
// 检查目录是否存在
|
||||||
|
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "Invalid directory path");
|
||||||
|
return 2; // 返回 nil 和错误信息
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个新的 Lua 表用于存放文件列表
|
||||||
|
lua_newtable(L);
|
||||||
|
int table_index = lua_gettop(L); // 获取当前 Lua 表的位置
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
for (const auto& entry : fs::directory_iterator(dir_path)) {
|
||||||
|
if (fs::is_regular_file(entry.status())) {
|
||||||
|
lua_pushstring(L, entry.path().filename().string().c_str());
|
||||||
|
lua_rawseti(L, table_index, index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
33
fs/lib.h
Normal file
33
fs/lib.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef LIB_HEADER
|
||||||
|
#define LIB_HEADER
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifdef FS_LIB_EXPORT
|
||||||
|
#define LIB_API __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define LIB_API __declspec(dllimport)
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define LIB_API
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <lauxlib.h>
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lualib.h>
|
||||||
|
|
||||||
|
LIB_API int luaopen_lua_fs(lua_State* L);
|
||||||
|
LIB_API int file_exists(lua_State* L);
|
||||||
|
LIB_API int is_regular_file(lua_State* L);
|
||||||
|
LIB_API int copy_file(lua_State* L);
|
||||||
|
LIB_API int delete_file(lua_State* L);
|
||||||
|
LIB_API int get_dir_files(lua_State* L);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // LIB_HEADER
|
Loading…
x
Reference in New Issue
Block a user