I have been writing tools for a long time. I have encountered some problems and solved them by Google. Here I want to extract and share the problem-solving results, which is also convenient for me to use later. ^_^
Write tools should be as flexible as possible and highly configurable, and configuration files are indispensable.
I used a custom configuration file before (my other articleArticleMentioned in: http://www.cnblogs.com/MikeZhang/archive/2011/11/19/2255169.html), flexible is flexible, can write a little trouble, and if you want to write part of the logic outside, getting a function in the configuration file is not random-you need to write a script engine for parsing ......
Finally, we decided to use Lua as the parser of the configuration file. Here is a simple demo:
1 /*
2 File: Demo. cpp
3 Author: Mike
4 E-mail: Mike_Zhang@live.com
5 */
6 # Include <stdio. h>
7 Extern " C "
8 {
9 # Include " Lua. h "
10 # Include " Lualib. h "
11 # Include " Lauxlib. h "
12 }
13
14 Lua_state * l;
15 Const Char * Lua_script = " Function add (a, B) Return (a + B) End " ;
16
17 Int Lua_add (lua_state * l, Const Char * Fun_name, Int X, Int Y)
18 {
19 Int RET;
20 Lua_getglobal (L, fun_name );
21 Lua_pushnumber (L, X );
22 Lua_pushnumber (L, y );
23 Lua_call (L, 2 , 1 );
24 Ret = ( Int ) Lua_tointeger (L ,- 1 );
25 Lua_pop (L, 1 );
26 Return RET;
27 }
28
29 Int Test ()
30 {
31 Int Ret = 0 ;
32 Lua_state * l = lua_open (); /* Opens Lua */
33 Lual_openlibs (L );
34 If (Lual_dostring (L, lua_script )) // Run Lua script
35 {
36 Printf ( " Error! \ N " );
37 Lua_close (L );
38 Return - 1 ;
39 }
40 Ret = lua_add (L, " Add " , 4 , 5 );
41 Printf ( " % D \ n " , RET );
42 Lua_close (L );
43 Return 0 ;
44 }
45
46 Int Main ()
47 {
48 Test ();
49 Return 0 ;
50 }
In addition, I wrote a class for calling Lua in C ++. If you are interested, you can find it here:
Project address: http://sourceforge.net/projects/cppcalllua/
SVN access: SVN checkout http://svn.code.sf.net/p/cppcalllua/code-0/trunk cppcalllua-code-0
TIPS: This is a codeblocks project. Project File: cppcalllua. White