diff options
Diffstat (limited to 'lua_cjson.c')
| -rw-r--r-- | lua_cjson.c | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 0ee9f7d..4642b51 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
| @@ -38,6 +38,7 @@ | |||
| 38 | 38 | ||
| 39 | #include <assert.h> | 39 | #include <assert.h> |
| 40 | #include <stdint.h> | 40 | #include <stdint.h> |
| 41 | #include <stdlib.h> | ||
| 41 | #include <string.h> | 42 | #include <string.h> |
| 42 | #include <math.h> | 43 | #include <math.h> |
| 43 | #include <stdint.h> | 44 | #include <stdint.h> |
| @@ -314,18 +315,6 @@ static int json_enum_option(lua_State *l, int optindex, int *setting, | |||
| 314 | return 1; | 315 | return 1; |
| 315 | } | 316 | } |
| 316 | 317 | ||
| 317 | /* Process string option for a configuration function */ | ||
| 318 | static int json_string_option(lua_State *l, int optindex, const char **setting) | ||
| 319 | { | ||
| 320 | if (!lua_isnil(l, optindex)) { | ||
| 321 | const char *value = luaL_checkstring(l, optindex); | ||
| 322 | *setting = value; | ||
| 323 | } | ||
| 324 | |||
| 325 | lua_pushstring(l, *setting ? *setting : ""); | ||
| 326 | return 1; | ||
| 327 | } | ||
| 328 | |||
| 329 | /* Configures handling of extremely sparse arrays: | 318 | /* Configures handling of extremely sparse arrays: |
| 330 | * convert: Convert extremely sparse arrays into objects? Otherwise error. | 319 | * convert: Convert extremely sparse arrays into objects? Otherwise error. |
| 331 | * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio | 320 | * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio |
| @@ -431,9 +420,30 @@ static int json_cfg_encode_indent(lua_State *l) | |||
| 431 | { | 420 | { |
| 432 | json_config_t *cfg = json_arg_init(l, 1); | 421 | json_config_t *cfg = json_arg_init(l, 1); |
| 433 | 422 | ||
| 434 | json_string_option(l, 1, &cfg->encode_indent); | 423 | if (!lua_isnil(l, 1)) { |
| 435 | /* simplify further checking */ | 424 | size_t len; |
| 436 | if (cfg->encode_indent[0] == '\0') cfg->encode_indent = NULL; | 425 | const char *value = luaL_checklstring(l, 1, &len); |
| 426 | char *copy = NULL; | ||
| 427 | |||
| 428 | /* The supplied string belongs to Lua and may be garbage collected | ||
| 429 | * once this function returns. Keep a private copy owned by cfg so | ||
| 430 | * the pointer remains valid for later encode() calls. */ | ||
| 431 | if (len > 0) { | ||
| 432 | copy = (char *) malloc(len + 1); | ||
| 433 | if (!copy) | ||
| 434 | luaL_error(l, "Out of memory"); | ||
| 435 | memcpy(copy, value, len); | ||
| 436 | copy[len] = '\0'; | ||
| 437 | } | ||
| 438 | |||
| 439 | if (cfg->encode_indent) | ||
| 440 | free((void *) cfg->encode_indent); | ||
| 441 | |||
| 442 | /* simplify further checking: empty string => NULL */ | ||
| 443 | cfg->encode_indent = copy; | ||
| 444 | } | ||
| 445 | |||
| 446 | lua_pushstring(l, cfg->encode_indent ? cfg->encode_indent : ""); | ||
| 437 | 447 | ||
| 438 | return 1; | 448 | return 1; |
| 439 | } | 449 | } |
| @@ -492,8 +502,13 @@ static int json_destroy_config(lua_State *l) | |||
| 492 | json_config_t *cfg; | 502 | json_config_t *cfg; |
| 493 | 503 | ||
| 494 | cfg = (json_config_t *)lua_touserdata(l, 1); | 504 | cfg = (json_config_t *)lua_touserdata(l, 1); |
| 495 | if (cfg) | 505 | if (cfg) { |
| 496 | strbuf_free(&cfg->encode_buf); | 506 | strbuf_free(&cfg->encode_buf); |
| 507 | if (cfg->encode_indent) { | ||
| 508 | free((void *) cfg->encode_indent); | ||
| 509 | cfg->encode_indent = NULL; | ||
| 510 | } | ||
| 511 | } | ||
| 497 | cfg = NULL; | 512 | cfg = NULL; |
| 498 | 513 | ||
| 499 | return 0; | 514 | return 0; |
