diff options
| author | Mark Pulford <mark@kyne.com.au> | 2012-01-18 20:00:09 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2012-03-04 18:54:35 +1030 |
| commit | 1a5460be319ac29af31f201fbf9775340262ba9b (patch) | |
| tree | 590312b65704b47c674b08efe14b8464aa58ce5b /lua_cjson.c | |
| parent | 90e08aa69d23df8c2eb3e231d57a46281d330f03 (diff) | |
| download | lua-cjson-1a5460be319ac29af31f201fbf9775340262ba9b.tar.gz lua-cjson-1a5460be319ac29af31f201fbf9775340262ba9b.tar.bz2 lua-cjson-1a5460be319ac29af31f201fbf9775340262ba9b.zip | |
Return boolean values from configuration functions
Return boolean values from configuration functions to simplify usage in
the common case. Eg,:
if not cjson.encode_invalid_numbers() then .. end
Diffstat (limited to 'lua_cjson.c')
| -rw-r--r-- | lua_cjson.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index b64ed62..0662414 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
| @@ -235,23 +235,26 @@ static int json_integer_option(lua_State *l, int optindex, int *setting, | |||
| 235 | 235 | ||
| 236 | /* Process enumerated arguments for a configuration function */ | 236 | /* Process enumerated arguments for a configuration function */ |
| 237 | static int json_enum_option(lua_State *l, int optindex, int *setting, | 237 | static int json_enum_option(lua_State *l, int optindex, int *setting, |
| 238 | const char **options, int bool_value) | 238 | const char **options, int bool_true) |
| 239 | { | 239 | { |
| 240 | static const char *bool_options[] = { "off", "on", NULL }; | 240 | static const char *bool_options[] = { "off", "on", NULL }; |
| 241 | 241 | ||
| 242 | if (!options) { | 242 | if (!options) { |
| 243 | options = bool_options; | 243 | options = bool_options; |
| 244 | bool_value = 1; | 244 | bool_true = 1; |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | if (!lua_isnil(l, optindex)) { | 247 | if (!lua_isnil(l, optindex)) { |
| 248 | if (lua_isboolean(l, optindex)) | 248 | if (bool_true && lua_isboolean(l, optindex)) |
| 249 | *setting = lua_toboolean(l, optindex) * bool_value; | 249 | *setting = lua_toboolean(l, optindex) * bool_true; |
| 250 | else | 250 | else |
| 251 | *setting = luaL_checkoption(l, optindex, NULL, options); | 251 | *setting = luaL_checkoption(l, optindex, NULL, options); |
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | lua_pushstring(l, options[*setting]); | 254 | if (bool_true && (*setting == 0 || *setting == bool_true)) |
| 255 | lua_pushboolean(l, *setting); | ||
| 256 | else | ||
| 257 | lua_pushstring(l, options[*setting]); | ||
| 255 | 258 | ||
| 256 | return 1; | 259 | return 1; |
| 257 | } | 260 | } |
