C 嵌入Lua讀取配置檔案

2022-02-10 07:18:11 字數 976 閱讀 7761

這段時間一直在寫工具,遇到一些問題,google下解決了,這裡想把解決問題的成果提取出來分享下,也方便我以後使用,^_^

寫工具就應該盡量的靈活,可配置性強,配置檔案是少不了的。

之前用過自定義配置檔案(我的另一篇文章中提到過:靈活是靈活,可寫起來有點麻煩,而且如果想把部分邏輯寫在外面,在配置檔案中弄個函式就很不隨意了——自己要寫個指令碼引擎進行解析……

最後決定用lua作為配置檔案的解析器,這裡有個簡單的demo:

1/*2

file : demo.cpp

3author : mike

4e-mail : [email protected]*/

6 #include

7extern"c

"8 1314 lua_state *l;

15const

char *lua_script = "

function add(a, b) return (a+b) end

";16

17int lua_add(lua_state *l, const

char *fun_name, int x, int y)

18 28

29int test()

30 40 ret = lua_add(l, "

add", 4, 5);

41 printf("

%d\n

",ret);

42 lua_close(l);

43return

0;44 }

4546

int main()

47

另外,我自己寫了個c++呼叫lua的類,感興趣的話,可以到這裡去找:

svn訪問:svn checkout cppcalllua-code-0

tips : 這是乙個codeblocks的工程,工程檔案 : cppcalllua.cbp

C 讀取配置檔案

1 首先引入標頭檔案 include 2 獲取應用程式的當前路徑 char buf 1000 getcurrentdirectory 1000,buf 得到當前工作路徑3 獲取配置檔案的路徑 char path 1024 definesysconfig config.ini sprintf path...

C 讀取配置檔案

1 利用系統介面,讀取ini配置檔案,詳情可查詢msdn。這裡做簡單宣告 宣告讀ini檔案的api函式 dllimport kernel32 private static extern int getprivateprofilestring string section,string key,str...

C讀取配置檔案

ifndef cfg op h define cfg op h ifdef cplusplus extern c endif endif define crt secure no warnings include include include define maxline 2048 獲取配置項 i...