- Extern "C "{
- # Include "Lua. H"
- # Include "lualib. H"
- # Include "lauxlib. H"
- }
- # Include <iostream>
- # Include <string>
- Using namespace STD;
- Int main ()
- {
- // Lua sample code
- Char * szlua_code =
- "R = string. gsub (c_str, c_mode, c_tag) -- The variable assigned by the host"
- "U = string. Upper (r )";
- // Lua string Mode
- Char * szmode = "(% W +) % S * = % S * (% W + )";
- // String to be processed
- Char * szstr = "key1 = value1 key2 = value2 ";
- // Target string Mode
- Char * sztag = "<% 1> % 2 </% 1> ";
- Lua_state * l = lual_newstate ();
- Lual_openlibs (L );
- // Send a piece of data to Lua
- Lua_pushstring (L, szmode );
- Lua_setglobal (L, "c_mode ");
- Lua_pushstring (L, sztag );
- Lua_setglobal (L, "c_tag ");
- Lua_pushstring (L, szstr );
- Lua_setglobal (L, "c_str ");
- // Execute
- Bool err = lual_loadbuffer (L, szlua_code, strlen (szlua_code ),
- "Demo") | lua_pcall (L, 0, 0, 0 );
- If (ERR)
- {
- // If an error occurs, it is displayed
- Cerr <lua_tostring (L,-1 );
- // The error message at the top of the stack is displayed.
- Lua_pop (L, 1 );
- }
- Else
- {
- // Obtain the global variable value after Lua is executed
- Lua_getglobal (L, "R ");
- Cout <"r =" <lua_tostring (L,-1) <Endl;
- Lua_pop (L, 1 );
- Lua_getglobal (L, "U ");
- Cout <"u =" <lua_tostring (L,-1) <Endl;
- Lua_pop (L, 1 );
- }
- Lua_close (L );
- Return 0;
- }
This code converts all the key = value strings in the string to the XML format <key> value </key>
In this example, the C ++ program pushes the C string to the top of the stack by calling lua_pushstring. The function of lua_setglobal is to upload the data on the top of the stack to the Lua environment as a global variable.
After the code is executed, use lua_getglobal to obtain global variables from the Lua environment and press them to the top of the stack. Then, use lua_tostring to convert the data on the top of the stack to a string. Because lua_tostring itself does not have the stack function, in order to balance (that is, the data volume in the stack before and after the call remains unchanged), lua_pop is used to pop up the data pushed by lua_setglobal.
From the above example, we can see that C ++ and Lua are always turning around the stack, which is very important. It is necessary to list some Major Stack operations in Lua C APIs. Their functions can be seen directly from the function name.
Push elements into the stack
void lua_pushnil (lua_State *L); void lua_pushboolean (lua_State *L, int bool);void lua_pushnumber (lua_State *L, double n);void lua_pushlstring (lua_State *L, const char *s, size_t length);void lua_pushstring (lua_State *L, const char *s);void lua_pushcfunction (lua_State *L, lua_CFunction fn);
Query the elements in the stack
lua_isnil (lua_State *L, int index);lua_isboolean (lua_State *L, int index);int lua_isnumber (lua_State *L, int index);int lua_isstring (lua_State *L, int index);int lua_isfunction (lua_State *L, int index);int lua_istable (lua_State *L, int index);int lua_isuserdata (lua_State *L, int index);lua_islightuserdata (lua_State *L, int index);lua_isthread (lua_State *L, int index);
Elements in the conversion Stack
int lua_toboolean (lua_State *L, int index);double lua_tonumber (lua_State *L, int index);const char * lua_tostring (lua_State *L, int index);const char * lua_tolstring (lua_State *L, int idx, size_t *len);size_t lua_strlen (lua_State *L, int index);lua_CFunction lua_tocfunction (lua_State *L, int idx);void * lua_touserdata (lua_State *L, int idx);lua_State * lua_tothread (lua_State *L, int idx);
Lua stack Maintenance
Int lua_gettop (lua_state * l); gets the index of the top element of the stack, that is, the number of elements in the stack void lua_settop (lua_state * l, int index); sets the top index of the stack, that is, set the number of elements in the stack. If the index is <0, the number goes down from the top of the stack, the same as void lua_pushvalue (lua_state * l, int index ); copy the elements of the specified index in the stack to the top of the stack void lua_remove (lua_state * l, int index); Delete the element void lua_insert (lua_state * l, int index) of the specified index ); move the top element of the stack to the specified index location, and the number of stacks does not change void lua_replace (lua_state * l, int index ); the element value pops up from the top of the stack and is set to the specified index location. The number of elements in the stack is reduced by one int lua_checkstack. (Lua_state * l, int extra); Ensure that the stack has at least extra space. If the size of the stack cannot be extended, the function returns false. This function will never shrink the stack. Int lua_pop (L, n) pops up n elements from the top of the stack. It is a package of lua_settop: # define lua_pop (L, n) lua_settop (L,-(n) -1)
Table operations
The above list does not contain lua_pushtable and lua_totable. How can we obtain or set table data in Lua?
In Lua, table is a very important data type. In a table, not only can a group of data be put like data in C, you can also store data as key = value as map, such as in the Lua code:
TB = {"ABC", 12, true, x = 10, y = 20, Z = 30}
The first three data items can use TB [1] ~ TB [3] acquisition
The last three data items are obtained through TB. X, TB. Y, and TB. Z.
Although it looks like a cool, but it's amazing to peel off. In fact, in the Lua table, all the data is stored in the form of key = value. This Lua code can also be written as follows:
TB = {[1] = "ABC", [2] = 12, [3] = true, ["X"] = 10, ["Y"] = 20, ["Z"] = 30}
It is in the form of [Key] = value, the so-called TB. X is just the syntactic sugar of TB ["X"]. If you want to, you can also use TB ["X"] To get this data 10.
We changed the above example to use the table
- ...
- Int main ()
- {
- // Lua sample code, using table
- Char * szlua_code =
- "X = {} -- table used to store results"
- "X [1], X [2] = string. gsub (C. STR, C. mode, C. tag) -- The result in X [1] and the number of replicas in X [2"
- "X. U = string. Upper (X [1])";
- // Lua string Mode
- Char * szmode = "(% W +) % S * = % S * (% W + )";
- // String to be processed
- Char * szstr = "key1 = value1 key2 = value2 ";
- // Target string Mode
- Char * sztag = "<% 1> % 2 </% 1> ";
- Lua_state * l = lual_newstate ();
- Lual_openlibs (L );
- // Send a tabele to Lua
- Lua_newtable (l); // create a table and press it to the top of the stack.
- Lua_pushstring (L, "Mode"); // key
- Lua_pushstring (L, szmode); // Value
- // Set newtable [mode] = szmode
- // Because of the two previous stacks, the table element is now placed at the third position on the top of the stack.
- Lua_settable (L,-3 );
- // Lua_settable pops up the key and Value
- Lua_pushstring (L, "tag"); // key
- Lua_pushstring (L, sztag); // Value
- Lua_settable (L,-3); // set newtable [tag] = sztag
- Lua_pushstring (L, "str"); // key
- Lua_pushstring (L, szstr); // Value
- Lua_settable (L,-3); // set newtable [STR] = szstr
- Lua_setglobal (L, "C"); // set the top element of the stack (newtable) to the global variable C in Lua.
- // Execute
- Bool err = lual_loadbuffer (L, szlua_code, strlen (szlua_code ),
- "Demo") | lua_pcall (L, 0, 0, 0 );
- If (ERR)
- {
- // If an error occurs, it is displayed
- Cerr <lua_tostring (L,-1 );
- // The error message at the top of the stack is displayed.
- Lua_pop (L, 1 );
- }
- Else
- {
- // Obtain the global variable value after Lua is executed
- Lua_getglobal (L, "x ");
- // This X should be a table
- If (lua_istable (L,-1 ))
- {
- // Obtain X. U, that is, X ["U"]
- Lua_pushstring (L, "U"); // key
- // Due to this stack pressure, X is in the second position at the top of the stack
- Lua_gettable (L,-2 );
- // Lua_gettable will pop up the key pushed above, and then press the corresponding value
- // Obtain the data, and then the value pops up from the stack.
- Cout <"x. U =" <lua_tostring (L,-1) <Endl;
- Lua_pop (L, 1 );
- // Obtain X [1] and X [2]
- For (INT I = 1; I <= 2; I ++)
- {
- // Except that the key is a number, it is no different from the above.
- Lua_pushnumber (L, I );
- Lua_gettable (L,-2 );
- Cout <"X [" <I <"] =" <lua_tostring (L,-1) <Endl;
- Lua_pop (L, 1 );
- }
- }
- // The top X of the stack is displayed.
- Lua_pop (L, 1 );
- }
- Lua_close (L );
- Return 0;
- }
The new Lua c api used in this example is:
Void lua_newtable (lua_state * l); Create an empty table and press it to the top of the stack. Void lua_settable (lua_state * l, int idx); lua_settable uses the index of the table in the stack as the parameter, and outputs the key and value at the top of the stack to the stack and modifies the table with these two values. Void lua_gettable (lua_state * l, int idx); lua_gettable uses the index of the table in the stack as the parameter, and the element at the top of the stack is displayed as the key. The value corresponding to the key is returned and pushed to the top of the stack. Finally, Lua provides the access function void lua_rawgeti (lua_state * l, int idx, int N) for the table to get table [N] and put it on the top of the stack, in the preceding example, the lua_pushnumber (L, I); lua_gettable (L,-2) of lines 69-70 can be replaced by lua_rawgeti (L,-1. Lua_getfield (lua_state * l, int idx, const char * k) to get table. K and put it on the top of the stack. In the above example, the lua_pushstring (L, "U"); lua_gettable (L,-2); can be replaced with lua_getfield (L,-1, "U "). Void lua_setfield (lua_state * l, int idx, const char * k) puts the data at the top of the stack as value into table. in K, the shape in the above example is lua_pushstring (L, "key"); lua_pushstring (L, value); lua_settable (L,-3); you can change it to lua_pushstring (L, value ); the format of lua_setfield (L,-2, "key. Void lua_rawseti (lua_state * l, int idx, int N) puts the data at the top of the stack as value in table [N]