util.c
changeset 4 5770be2d5f3f
parent 0 84fdfb0344c9
child 20 0ce7093df25f
equal deleted inserted replaced
3:4fd19a188509 4:5770be2d5f3f
    23 		++set;
    23 		++set;
    24 	}
    24 	}
    25 	return NULL;
    25 	return NULL;
    26 }
    26 }
    27 
    27 
       
    28 /// argument enum field
       
    29 /// String that will be converted to number or just plain number, that will be passed as is.
       
    30 /// Note, that if enum name is not recognized no error will be raised and default vale will be used.
    28 enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set)
    31 enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set)
    29 {
    32 {
    30 	if (lua_type (L, index) == LUA_TNUMBER)
    33 	if (lua_type (L, index) == LUA_TNUMBER)
    31 		return lua_tointeger (L, index);
    34 		return lua_tointeger (L, index);
    32 	else
    35 	else
    33 		return string2enum (luaL_checkstring (L, index), set);
    36 		return string2enum (luaL_checkstring (L, index), set);
    34 }
    37 }
    35 
    38 
       
    39 /// return enum field
       
    40 /// String. If no string found, plain number will be returned.
    36 void luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set)
    41 void luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set)
    37 {
    42 {
    38 	const char *string = enum2string (value, set);
    43 	const char *string = enum2string (value, set);
    39 	if (string != NULL)
    44 	if (string != NULL)
    40 		lua_pushstring (L, string);
    45 		lua_pushstring (L, string);
    41 	else
    46 	else
    42 		lua_pushinteger (L, value);
    47 		lua_pushinteger (L, value);
    43 }
    48 }
    44 
    49 
       
    50 /// argument flags field
       
    51 /// Can be just plain number, then it is passed as is.
       
    52 /// Can be a string, then it is recognized as a single enabled flag.
       
    53 /// Or can be a table of the following format:
       
    54 /// * integer keys should have string values, that will be used as enabled flag names or numerical values, that will be just ORed;
       
    55 /// * string keys should be flag names, that will be enabled, if corresponding value contains true value.
    45 enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set)
    56 enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set)
    46 {
    57 {
    47 	int type = lua_type (L, index);
    58 	int type = lua_type (L, index);
    48 	if (type == LUA_TNUMBER)
    59 	if (type == LUA_TNUMBER)
    49 		return lua_tointeger (L, index);
    60 		return lua_tointeger (L, index);
    72 	} else
    83 	} else
    73 		luaL_argerror (L, index, "integer, string, or table of ones expected");
    84 		luaL_argerror (L, index, "integer, string, or table of ones expected");
    74 	return 0; // never happens
    85 	return 0; // never happens
    75 }
    86 }
    76 
    87 
       
    88 /// returned flags field
       
    89 /// Is always a table with present flag names as keys with true values.
       
    90 /// Not present flags are not present in table either.
       
    91 /// Not recognized values, if present, will be stored as a number in a first sequential table member (table[1]).
    77 void luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set)
    92 void luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set)
    78 {
    93 {
    79 	enum_value_t matched = 0;
    94 	enum_value_t matched = 0;
    80 	lua_newtable (L);
    95 	lua_newtable (L);
    81 	while (set->string) {
    96 	while (set->string) {