Programming in Lua 3 Reading Notes (22)

Source: Internet
Author: User
Date: 2014.8.6part IV the C API
26 extending the usage of Lua by your application is very important for the configuration language. Use the main language to configure some features.
  26.1 The BasicsSometimes the program needs to configure some function information. In many cases, there may be many other methods that are easier to configure than configuring with Lua: for example, using environment variables or reading files involves parsing files. If you use Lua for configuration, it is equivalent to replacing the CSV and TXT files to be read with the Lua file. When you use Lua for configuration, you need to use the Lua API to control Lua to parse the file, and then get the value to be configured from the Lua global variable. The load function is used to load files. The function calls lual_loadfile to read statements from files with a given name quickly, and then calls lua_pcall to run the statements quickly. If an error occurs (for example, a syntax error in the configuration file), the function pushes the error message to the stack and returns a non-zero error code; in this case, our program uses lua_tostring to obtain information from the top of the stack with the index-1 parameter. E. g. lua_tostring (L,-1) -- obtain the value of the variable from the global variable of Lua after loading the file from the top of the stack. Call lua_getglobal to obtain the required value. The parameter is the name of the global variable. Each call pushes the global variable value to the value stack, and the corresponding main program obtains the corresponding value from the stack. Then, use lua_is * to determine the value type, and then call lua_to * to obtain the value of the corresponding type. The advantage of using Lua for configuration is that Lua performs all the syntax detection and processing for you (can there be comments in the configuration file ?); You can use Lua for more complex configuration processing. For example, the configuration file prompts you some information or checks an environment variable to select the appropriate value:
e.g.--configuration fileif getenv("DISPLAY") == ":0.0" then     width = 300;height = 300else     width = 200;height = 200
Another reason is: now it is easy to use Lua to add new configuration information to the program. This feature makes the program more flexible.
  26.2 table manipulationAssume that the background color of the window needs to be configured. In this case, three values are required: RGB value. Generally in C, the range of each value is [0,255]. In Lua, because all numbers are real numbers, the range [] is used. It usually brings a lot of trouble to declare a variable for each value. For example, if the program requires multiple configurations, such as the foreground color and button color, many variables are required; in addition, this method is not suitable for predefined colors, or default values. In this case, the table in Lua is suitable for such Configuration:
e.g.background = {r = 0.30,g = 0.10,b = 0}
Using table will bring great convenience to the script. At this time, you can also conveniently use some predefined values, such:
e.g.BLUE = { r = 0, g = 0, b = 1.0}background = BLUE
To obtain these values in C code, you can implement them as follows:
E. g. lua_getglobal (L, "background") if (! Lua_istable (L,-1) error (L, "'background' is not a table"); Red = getcolorfield (L, "R"); Green = getcolorfield (L, "G"); Blue = getcolorfield (L, "B"); # define max_color 255int getcolorfield (lua_state * l, const char * Key) {int result; // key launched, push the corresponding value to the stack lua_pushstring (L, key); lua_gettable (L,-2); // The first two segments can use lua_getfield (L,-1, key ); to replace if (! Lua_isnumber (L,-1) error (L, "invalid component in background color"); Result = (INT) (lua_tonumber (L,-1) * max_color ); lua_pop (L, 1); return result ;}
 
26.3 calling Lua FunctionThe configuration file can define a function to call the program, which is a strong force of Lua. It is relatively simple to call a function through an API: first, push the function to be called to the stack; then, push the parameters to be called to the stack; then you can use the lua_pcall function to execute the actual function call, and finally get the call result from the stack. The second parameter of lua_pcall indicates the number of passed parameters and the number of returned results. The fourth parameter indicates the error processing function. The value 0 indicates no error processing function, the remaining values indicate the position index of the function in the stack. Therefore, these functions should be pushed to the stack first. Lua will adjust the parameters and the number of returned values according to the actual operation results of the function, if necessary, it will push Ni or discard the additional value. Before pushing the result, lua_pcall removes the parameters of functions and functions from the stack. When the function returns multiple results, the first result is pushed first. If you encounter any errors during lua_pcall running, lua_pcall returns an error code, the error message is pushed to the stack (and function parameters are still introduced ). Before pushing error messages, if there is an error processing function, lua_pcall will first execute the error processing function. In normal cases, lua_pcall returns lua_errun. two types of errors will return different error codes: The first case is a memory configuration error, which will return lua_errmem; the second case is that when Lua is running the error processing function itself, because the error function will not be called at this time, the error code lua_error will be returned immediately. the third case is also defined in lua5.2. When finalizer (interpreter ?) When an error is thrown, lua_errgcmm (error in a GC metamethod) is returned ). This Code indicates that the error is not related to calling the function itself.

  26.4 a generic call FunctionHere, a wrapper (wrapper?) will be created ?) Used to call the Lua function. vararg (Variable Parameter?) in C is used ?) Features. For example, we call the package call_va with the following parameters: 1. Name of the function to be called; 2. A string representing the parameters and returned results; 3. Parameter List; 4. List of pointers that store returned results. Use APIs to process all operations:
e.g.call_va(L,"f","dd>d",x,y,&z);
String "DD> D" indicates "Two Parameters of the double type, one of which is the return value of the double type". Here, "D" indicates "double ", "I" indicates "integer", "S" indicates "string ". Use ">" as the separator. If the function does not return a value, ">" is optional.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.