Lua as a method of reading configuration files

Source: Internet
Author: User

struct PICTURECFG

{

String name;

float rotation;

};

1, global configuration

--Picture0-cfg.lua

Name = "Dragon.png"

rotation= 180

Global configuration Read

Voidloadpicturecfg (vector<picturecfg>& Rstpicturecfgvec)

{

Picturecfg stcfg;

lua_state* L = Lual_newstate (); Create a new independent state machine

Lual_dofile (L, "Picture0-cfg.lua");

Lua_getglobal (L, "name"); Pressing the value in the global variable name into the stack is equivalent to Lua_getfield (L,lua_globalsindex, "name")

printf ("%s\n", Lua_typename (L, Lua_type (L,-1));

ASSERT (Lua_isstring (L,-1) = = 1); Returns 1 when the value of the given index is a string or a number (the number is always converted to a string), or 0

Stcfg.name = lua_tostring (L,-1); Converts the Lua value at the given index to a C string, noting that the function of the series does not eject the value from the stack

Lua_getglobal (L, "rotation"); //

ASSERT (Lua_isnumber (L,-1) = = 1);

Stcfg.rotation = (float) lua_tonumber (L,-1); Converts the Lua value at a given index to a C type such as Lua_number (see Lua_number). This Lua value must be a number or a string that can be converted to a number (see §2.2.1); otherwise, Lua_tonumber returns 0. typedef double Lua_number;

Rstpicturecfgvec.push_back (STCFG);

Lua_close (L);

}

2, one-dimensional table configuration read

--Picture1-cfg.lua

CFG = {name= "Dragon.png", rotation=180}

Reading of a one-dimensional table

Voidloadpicturecfg (vector<picturecfg>& Rstpicturecfgvec)

{

lua_state* L = Lual_newstate ();

Lual_dofile (L, "Picture1-cfg.lua");

Get the table

Lua_getglobal (L, "cfg"); The global variable cfg pressure stack, cfg is table type, one-dimensional table

ASSERT (Lua_istable (L,-1) = = 1);

Picturecfg stcfg;

Push the key to stack for getting the value

Lua_pushstring (L, "name"); //

Now the ' table ' in The-2 and key (-1)

Lua_gettable (L,-2);

/*

void Lua_gettable (lua_state *l, int index);

The t[k] value is pressed into the stack, where T is the value that the valid index index points to, and K is the stack top value.

This function pops up the key on the stack (placing the result on the stack in the same position).

*/

ASSERT (Lua_isstring (L,-1));

Stcfg.name = lua_tostring (L,-1);

Lua_pop (L, 1);

Push the key to stack for getting the value

Lua_pushstring (L, "rotation");

Now the ' table ' in The-2 and key (-1)

Lua_gettable (L,-2);

ASSERT (Lua_isnumber (L,-1));

Stcfg.rotation = Lua_tonumber (L,-1);

Rstpicturecfgvec.push_back (STCFG);

/* Empty Stack * *

Lua_pop (L, 2);

Lua_close (L);

}

The typical one-dimensional table traversal method is this:

int t_idx = Lua_gettop (L); Fetch Table Index value

Lua_pushnil (L); First key

while (Lua_next (L, T_idx)!= 0)

{

Now the top of the stack (-1) is the value,-2 position is the corresponding key

Here you can determine what the key is and do a variety of processing of value

printf ("%s-%s\n", Lua_typename (L, Lua_type (L,-2)), Lua_typename (L, Lua_type (L,-1));

Lua_pop (L, 1); Remove ' value '; Keep ' key ' for next iteration

}

2, two-dimensional table configuration read

--Picture2-cfg.lua

CFG = {

{name= "Dragon.png", rotation=180},

{name= "Dragon.png", rotation=0}

}

Reading of two-dimensional tables

Voidloadpicturecfg (vector<picturecfg>& Rstpicturecfgvec)

{

lua_state* L = Lual_newstate ();

Lual_dofile (L, "Picture2-cfg.lua");

Get the table

Lua_getglobal (L, "cfg"); The global variable cfg pressure stack, cfg is table type, and is a two-dimensional table

ASSERT (Lua_istable (L,-1) = = 1);

/*

int Lua_type (lua_state *l, int index);

Returns the type of the value at the given index and returns Lua_tnone when the index is invalid (that is, an index pointing to an empty position on the stack). The type returned by Lua_type is some of the constants defined in Lua.h: Lua_tnil, Lua_tnumber, Lua_tboolean, lua_tstring, lua_ttable, Lua_tfunction, lua_t USERDATA, Lua_tthread, Lua_tlightuserdata.

const char *lua_typename (lua_state*l, int tp);

Returns the name of the type that the TP represents, which must be one of the values that Lua_type might return.

*/

Lua_pushnil (L); /* First key//////////////////////Nil

while (Lua_next (L,-2)!= 0)//index-2 is the table

{

/*

int Lua_next (lua_state *l, int index);

POPs a key from the stack and then pushes the next Key-value (health value) in the table specified by index onto the stack

If there are no more elements in the table, then Lua_next will return 0 (nothing is pressed onto the stack).

*/

/* ' key ' (at index-2) and ' value ' (at index-1) * *

Picturecfg rstcfg;

Push the key to stack for getting the value

Lua_pushstring (L, "name"); //

/*

void Lua_pushstring (lua_state *l, const char *s);

Put the pointer s to the end of the string of 0 to press the stack. Lua makes a memory copy of the string (or a duplicate copy), so the memory at S can be released or reused for other purposes after the function returns. The string cannot contain 0 characters; The first encounter of the 0-character specifier is considered to be the end of the string.

*/

Now the ' table ' in The-2 and key (-1)

Lua_gettable (L,-2);

/*

void Lua_gettable (lua_state *l, int index);

The t[k] value is pressed into the stack, where T is the value that the valid index index points to, and K is the stack top value.

This function pops up the key on the stack (placing the result on the stack in the same position).

*/

ASSERT (Lua_isstring (L,-1)); The value of name of the first child table is now on the top of the stack

Rstcfg.name = lua_tostring (L,-1);

Lua_pop (L, 1);

/*

void Lua_pop (lua_state *l, int n);

POPs an n element from the stack.

*/

Push the key to stack for getting the value

Lua_pushstring (L, "rotation");

Now the ' table ' in The-2 and key (-1)

Lua_gettable (L,-2);

ASSERT (Lua_isnumber (L,-1));

Rstcfg.rotation = Lua_tonumber (L,-1);

Rstpicturecfgvec.push_back (RSTCFG);

/* Removes the key we pushed and the ' value ' of the global table; Keeps ' key ' for next iteration * *

Lua_pop (L, 2);

}

Lua_close (L);

}

Another way to traverse a two-dimensional table is as follows:

T_idx = Lua_gettop (L);

Lua_pushnil (L);

while (Lua_next (L, T_idx))

{

It_idx = Lua_gettop (L); Inner table

Lua_pushnil (L);

while (Lua_next (L, It_idx))

{

printf ("%s\n", Lua_tostring (l,-1)); Here is value, but value is not necessarily a string type

Lua_pop (L, 1);

}

Lua_pop (L, 1);

}

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.