author | Myhailo Danylenko <isbear@ukrpost.net> |
Sun, 19 Aug 2012 22:59:43 +0300 | |
changeset 51 | dd1a2998d220 |
parent 50 | 2278dd806ad3 |
permissions | -rw-r--r-- |
23 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This code is free software: you can redistribute it and/or modify |
|
5 |
it under the terms of the GNU General Public License as published by |
|
6 |
the Free Software Foundation, either version 2 of the License, or |
|
7 |
(at your option) any later version. |
|
8 |
||
9 |
This program is distributed in the hope that it will be useful, |
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
GNU General Public License for more details. |
|
13 |
||
14 |
You should have received a copy of the GNU General Public License |
|
15 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 16 |
|
20 | 17 |
#include <strings.h> |
0 | 18 |
#include <lua.h> |
19 |
#include <lauxlib.h> |
|
20 |
||
21 |
#include "util.h" |
|
22 |
||
50 | 23 |
/// Utility Lua Routines |
24 |
/// To handle conversion of enums and flag fields to/from human-readable strings. |
|
25 |
||
0 | 26 |
enum_value_t string2enum (const char *string, const string2enum_t *set) |
27 |
{ |
|
28 |
while (set->string) { |
|
20 | 29 |
if (!strcasecmp (string, set->string)) |
0 | 30 |
return set->value; |
31 |
++set; |
|
32 |
} |
|
33 |
return set->value; |
|
34 |
} |
|
35 |
||
36 |
const char *enum2string (enum_value_t value, const string2enum_t *set) |
|
37 |
{ |
|
38 |
while (set->string) { |
|
39 |
if (value == set->value) |
|
40 |
return set->string; |
|
41 |
++set; |
|
42 |
} |
|
43 |
return NULL; |
|
44 |
} |
|
45 |
||
4 | 46 |
/// argument enum field |
47 |
/// String that will be converted to number or just plain number, that will be passed as is. |
|
48 |
/// Note, that if enum name is not recognized no error will be raised and default vale will be used. |
|
0 | 49 |
enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set) |
50 |
{ |
|
51 |
if (lua_type (L, index) == LUA_TNUMBER) |
|
52 |
return lua_tointeger (L, index); |
|
53 |
else |
|
54 |
return string2enum (luaL_checkstring (L, index), set); |
|
55 |
} |
|
56 |
||
4 | 57 |
/// return enum field |
58 |
/// String. If no string found, plain number will be returned. |
|
0 | 59 |
void luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set) |
60 |
{ |
|
61 |
const char *string = enum2string (value, set); |
|
62 |
if (string != NULL) |
|
63 |
lua_pushstring (L, string); |
|
64 |
else |
|
65 |
lua_pushinteger (L, value); |
|
66 |
} |
|
67 |
||
4 | 68 |
/// argument flags field |
69 |
/// Can be just plain number, then it is passed as is. |
|
70 |
/// Can be a string, then it is recognized as a single enabled flag. |
|
71 |
/// Or can be a table of the following format: |
|
72 |
/// * integer keys should have string values, that will be used as enabled flag names or numerical values, that will be just ORed; |
|
73 |
/// * string keys should be flag names, that will be enabled, if corresponding value contains true value. |
|
0 | 74 |
enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set) |
75 |
{ |
|
76 |
int type = lua_type (L, index); |
|
77 |
if (type == LUA_TNUMBER) |
|
78 |
return lua_tointeger (L, index); |
|
79 |
else if (type == LUA_TSTRING) |
|
80 |
return string2enum (lua_tostring (L, index), set); |
|
81 |
else if (type == LUA_TTABLE) { |
|
82 |
enum_value_t retval = 0; |
|
83 |
lua_pushnil (L); |
|
84 |
while (lua_next (L, index) != 0) { |
|
85 |
type = lua_type (L, -2); |
|
86 |
if (type == LUA_TNUMBER) { |
|
87 |
type = lua_type (L, -1); |
|
88 |
if (type == LUA_TNUMBER) |
|
89 |
retval |= lua_tointeger (L, -1); |
|
90 |
else if (type == LUA_TSTRING) |
|
91 |
retval |= string2enum (lua_tostring (L, -1), set); |
|
92 |
else |
|
93 |
luaL_argerror (L, index, "wrong value type of flag"); |
|
94 |
} else if (type == LUA_TSTRING) { |
|
95 |
if (lua_toboolean (L, -1)) |
|
96 |
retval |= string2enum (lua_tostring (L, -2), set); |
|
97 |
} else |
|
98 |
luaL_argerror (L, index, "wrong key type of flags table"); |
|
99 |
lua_pop (L, 1); |
|
100 |
} |
|
25
1ce483608721
Flag field parser fix (unused)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
101 |
return retval; |
0 | 102 |
} else |
103 |
luaL_argerror (L, index, "integer, string, or table of ones expected"); |
|
104 |
return 0; // never happens |
|
105 |
} |
|
106 |
||
4 | 107 |
/// returned flags field |
108 |
/// Is always a table with present flag names as keys with true values. |
|
109 |
/// Not present flags are not present in table either. |
|
110 |
/// Not recognized values, if present, will be stored as a number in a first sequential table member (table[1]). |
|
0 | 111 |
void luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set) |
112 |
{ |
|
113 |
enum_value_t matched = 0; |
|
114 |
lua_newtable (L); |
|
115 |
while (set->string) { |
|
116 |
if (value & set->value) { |
|
117 |
matched |= set->value & value; |
|
118 |
lua_pushstring (L, set->string); |
|
119 |
lua_pushboolean (L, 1); |
|
120 |
lua_settable (L, -3); |
|
121 |
} |
|
122 |
++set; |
|
123 |
} |
|
124 |
if (value ^ matched) { |
|
125 |
lua_pushinteger (L, 1); |
|
126 |
lua_pushinteger (L, value ^ matched); |
|
127 |
lua_settable (L, -3); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
void *luaL_malloc (lua_State *L, size_t size) |
|
132 |
{ |
|
133 |
void *ud; |
|
134 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
135 |
return (*allocf) (ud, NULL, 0, size); |
|
136 |
} |
|
137 |
||
138 |
void *luaL_realloc (lua_State *L, void *ptr, size_t osize, size_t nsize) |
|
139 |
{ |
|
140 |
void *ud; |
|
141 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
142 |
return (*allocf) (ud, ptr, osize, nsize); |
|
143 |
} |
|
144 |
||
145 |
void luaL_free (lua_State *L, void *ptr) |
|
146 |
{ |
|
147 |
void *ud; |
|
148 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
149 |
(*allocf) (ud, ptr, 1, 0); |
|
150 |
} |
|
151 |