author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 12 Jul 2011 07:28:06 +0300 | |
changeset 40 | 33ea13cef185 |
parent 25 | 1ce483608721 |
child 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 |
||
23 |
enum_value_t string2enum (const char *string, const string2enum_t *set) |
|
24 |
{ |
|
25 |
while (set->string) { |
|
20 | 26 |
if (!strcasecmp (string, set->string)) |
0 | 27 |
return set->value; |
28 |
++set; |
|
29 |
} |
|
30 |
return set->value; |
|
31 |
} |
|
32 |
||
33 |
const char *enum2string (enum_value_t value, const string2enum_t *set) |
|
34 |
{ |
|
35 |
while (set->string) { |
|
36 |
if (value == set->value) |
|
37 |
return set->string; |
|
38 |
++set; |
|
39 |
} |
|
40 |
return NULL; |
|
41 |
} |
|
42 |
||
4 | 43 |
/// argument enum field |
44 |
/// String that will be converted to number or just plain number, that will be passed as is. |
|
45 |
/// Note, that if enum name is not recognized no error will be raised and default vale will be used. |
|
0 | 46 |
enum_value_t luaL_checkenum (lua_State *L, int index, const string2enum_t *set) |
47 |
{ |
|
48 |
if (lua_type (L, index) == LUA_TNUMBER) |
|
49 |
return lua_tointeger (L, index); |
|
50 |
else |
|
51 |
return string2enum (luaL_checkstring (L, index), set); |
|
52 |
} |
|
53 |
||
4 | 54 |
/// return enum field |
55 |
/// String. If no string found, plain number will be returned. |
|
0 | 56 |
void luaL_pushenum (lua_State *L, enum_value_t value, const string2enum_t *set) |
57 |
{ |
|
58 |
const char *string = enum2string (value, set); |
|
59 |
if (string != NULL) |
|
60 |
lua_pushstring (L, string); |
|
61 |
else |
|
62 |
lua_pushinteger (L, value); |
|
63 |
} |
|
64 |
||
4 | 65 |
/// argument flags field |
66 |
/// Can be just plain number, then it is passed as is. |
|
67 |
/// Can be a string, then it is recognized as a single enabled flag. |
|
68 |
/// Or can be a table of the following format: |
|
69 |
/// * integer keys should have string values, that will be used as enabled flag names or numerical values, that will be just ORed; |
|
70 |
/// * string keys should be flag names, that will be enabled, if corresponding value contains true value. |
|
0 | 71 |
enum_value_t luaL_checkenum_multi (lua_State *L, int index, const string2enum_t *set) |
72 |
{ |
|
73 |
int type = lua_type (L, index); |
|
74 |
if (type == LUA_TNUMBER) |
|
75 |
return lua_tointeger (L, index); |
|
76 |
else if (type == LUA_TSTRING) |
|
77 |
return string2enum (lua_tostring (L, index), set); |
|
78 |
else if (type == LUA_TTABLE) { |
|
79 |
enum_value_t retval = 0; |
|
80 |
lua_pushnil (L); |
|
81 |
while (lua_next (L, index) != 0) { |
|
82 |
type = lua_type (L, -2); |
|
83 |
if (type == LUA_TNUMBER) { |
|
84 |
type = lua_type (L, -1); |
|
85 |
if (type == LUA_TNUMBER) |
|
86 |
retval |= lua_tointeger (L, -1); |
|
87 |
else if (type == LUA_TSTRING) |
|
88 |
retval |= string2enum (lua_tostring (L, -1), set); |
|
89 |
else |
|
90 |
luaL_argerror (L, index, "wrong value type of flag"); |
|
91 |
} else if (type == LUA_TSTRING) { |
|
92 |
if (lua_toboolean (L, -1)) |
|
93 |
retval |= string2enum (lua_tostring (L, -2), set); |
|
94 |
} else |
|
95 |
luaL_argerror (L, index, "wrong key type of flags table"); |
|
96 |
lua_pop (L, 1); |
|
97 |
} |
|
25
1ce483608721
Flag field parser fix (unused)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
98 |
return retval; |
0 | 99 |
} else |
100 |
luaL_argerror (L, index, "integer, string, or table of ones expected"); |
|
101 |
return 0; // never happens |
|
102 |
} |
|
103 |
||
4 | 104 |
/// returned flags field |
105 |
/// Is always a table with present flag names as keys with true values. |
|
106 |
/// Not present flags are not present in table either. |
|
107 |
/// Not recognized values, if present, will be stored as a number in a first sequential table member (table[1]). |
|
0 | 108 |
void luaL_pushenum_multi (lua_State *L, enum_value_t value, const string2enum_t *set) |
109 |
{ |
|
110 |
enum_value_t matched = 0; |
|
111 |
lua_newtable (L); |
|
112 |
while (set->string) { |
|
113 |
if (value & set->value) { |
|
114 |
matched |= set->value & value; |
|
115 |
lua_pushstring (L, set->string); |
|
116 |
lua_pushboolean (L, 1); |
|
117 |
lua_settable (L, -3); |
|
118 |
} |
|
119 |
++set; |
|
120 |
} |
|
121 |
if (value ^ matched) { |
|
122 |
lua_pushinteger (L, 1); |
|
123 |
lua_pushinteger (L, value ^ matched); |
|
124 |
lua_settable (L, -3); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
void *luaL_malloc (lua_State *L, size_t size) |
|
129 |
{ |
|
130 |
void *ud; |
|
131 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
132 |
return (*allocf) (ud, NULL, 0, size); |
|
133 |
} |
|
134 |
||
135 |
void *luaL_realloc (lua_State *L, void *ptr, size_t osize, size_t nsize) |
|
136 |
{ |
|
137 |
void *ud; |
|
138 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
139 |
return (*allocf) (ud, ptr, osize, nsize); |
|
140 |
} |
|
141 |
||
142 |
void luaL_free (lua_State *L, void *ptr) |
|
143 |
{ |
|
144 |
void *ud; |
|
145 |
lua_Alloc allocf = lua_getallocf (L, &ud); |
|
146 |
(*allocf) (ud, ptr, 1, 0); |
|
147 |
} |
|
148 |