X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fdump.c;h=0ffdfc64be1a16efc86780c6e7063cef2fd1891b;hb=53383860e804d1e4f6e038a2c76461867b1ef497;hp=a862cba286ed5ba87f965f7f5d1503f397512b57;hpb=4e63fcd55d874485071073f181dbfc5b9e196600;p=jansson.git diff --git a/src/dump.c b/src/dump.c index a862cba..0ffdfc6 100644 --- a/src/dump.c +++ b/src/dump.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2011 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -41,10 +41,10 @@ static int dump_to_file(const char *buffer, int size, void *data) return 0; } -/* 256 spaces (the maximum indentation size) */ -static char whitespace[] = " "; +/* 32 spaces (the maximum indentation size) */ +static char whitespace[] = " "; -static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) +static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data) { if(JSON_INDENT(flags) > 0) { @@ -165,7 +165,7 @@ static int object_key_compare_serials(const void *key1, const void *key2) (*(const object_key_t **)key2)->serial; } -static int do_dump(const json_t *json, unsigned long flags, int depth, +static int do_dump(const json_t *json, size_t flags, int depth, dump_func dump, void *data) { int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0; @@ -185,7 +185,9 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, char buffer[MAX_INTEGER_STR_LENGTH]; int size; - size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json)); + size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, + "%" JSON_INTEGER_FORMAT, + json_integer_value(json)); if(size >= MAX_INTEGER_STR_LENGTH) return -1; @@ -231,38 +233,44 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, /* detect circular references */ array = json_to_array(json); if(array->visited) - return -1; + goto array_error; array->visited = 1; n = json_array_size(json); if(dump("[", 1, data)) - return -1; - if(n == 0) + goto array_error; + if(n == 0) { + array->visited = 0; return dump("]", 1, data); + } if(dump_indent(flags, depth + 1, 0, dump, data)) - return -1; + goto array_error; for(i = 0; i < n; ++i) { if(do_dump(json_array_get(json, i), flags, depth + 1, dump, data)) - return -1; + goto array_error; if(i < n - 1) { if(dump(",", 1, data) || dump_indent(flags, depth + 1, 1, dump, data)) - return -1; + goto array_error; } else { if(dump_indent(flags, depth, 0, dump, data)) - return -1; + goto array_error; } } array->visited = 0; return dump("]", 1, data); + + array_error: + array->visited = 0; + return -1; } case JSON_OBJECT: @@ -284,29 +292,30 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, /* detect circular references */ object = json_to_object(json); if(object->visited) - return -1; + goto object_error; object->visited = 1; iter = json_object_iter((json_t *)json); if(dump("{", 1, data)) - return -1; - if(!iter) + goto object_error; + if(!iter) { + object->visited = 0; return dump("}", 1, data); + } if(dump_indent(flags, depth + 1, 0, dump, data)) - return -1; + goto object_error; if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER) { const object_key_t **keys; - unsigned int size; - unsigned int i; + size_t size, i; int (*cmp_func)(const void *, const void *); size = json_object_size(json); keys = malloc(size * sizeof(object_key_t *)); if(!keys) - return -1; + goto object_error; i = 0; while(iter) @@ -338,7 +347,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, do_dump(value, flags, depth + 1, dump, data)) { free(keys); - return -1; + goto object_error; } if(i < size - 1) @@ -347,7 +356,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, dump_indent(flags, depth + 1, 1, dump, data)) { free(keys); - return -1; + goto object_error; } } else @@ -355,7 +364,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(dump_indent(flags, depth, 0, dump, data)) { free(keys); - return -1; + goto object_error; } } } @@ -374,18 +383,18 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(dump(separator, separator_length, data) || do_dump(json_object_iter_value(iter), flags, depth + 1, dump, data)) - return -1; + goto object_error; if(next) { if(dump(",", 1, data) || dump_indent(flags, depth + 1, 1, dump, data)) - return -1; + goto object_error; } else { if(dump_indent(flags, depth, 0, dump, data)) - return -1; + goto object_error; } iter = next; @@ -394,6 +403,10 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, object->visited = 0; return dump("}", 1, data); + + object_error: + object->visited = 0; + return -1; } default: @@ -403,7 +416,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, } -char *json_dumps(const json_t *json, unsigned long flags) +char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; char *result; @@ -425,7 +438,7 @@ char *json_dumps(const json_t *json, unsigned long flags) return result; } -int json_dumpf(const json_t *json, FILE *output, unsigned long flags) +int json_dumpf(const json_t *json, FILE *output, size_t flags) { if(!json_is_array(json) && !json_is_object(json)) return -1; @@ -433,7 +446,7 @@ int json_dumpf(const json_t *json, FILE *output, unsigned long flags) return do_dump(json, flags, 0, dump_to_file, (void *)output); } -int json_dump_file(const json_t *json, const char *path, unsigned long flags) +int json_dump_file(const json_t *json, const char *path, size_t flags) { int result;