From 7764f3f3213d345a4091197563aeae15d59bf7df Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Tue, 9 Jun 2009 22:37:31 +0300 Subject: [PATCH] Implement json_dump and json_dumps --- src/dump.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/dump.c b/src/dump.c index 3bf01fd..a849b50 100644 --- a/src/dump.c +++ b/src/dump.c @@ -8,6 +8,37 @@ typedef int (*dump_func)(const char *buffer, int size, void *data); +struct string +{ + char *buffer; + int length; + int size; +}; + +static int dump_to_string(const char *buffer, int size, void *data) +{ + struct string *string = (struct string *)data; + if(string->length + size > string->size) + { + if(string->length == 0) + string->size = 16; + else + string->size *= 2; + + string->buffer = realloc(string->buffer, string->size); + if(!string->buffer) + return -1; + + memset(string->buffer + string->length, 0, + string->size - string->length); + } + + memcpy(string->buffer + string->length, buffer, size); + string->length += size; + + return 0; +} + static int dump_to_file(const char *buffer, int size, void *data) { FILE *dest = (FILE *)data; @@ -187,6 +218,34 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, } +int json_dump(const json_t *json, const char *path, uint32_t flags) +{ + FILE *output = fopen(path, "w"); + if(!output) + return -1; + + return json_dumpf(json, output, flags); +} + +char *json_dumps(const json_t *json, uint32_t flags) +{ + struct string string; + char *result; + memset(&string, 0, sizeof(struct string)); + + if(do_dump(json, flags, 0, dump_to_string, (void *)&string)) + return NULL; + + if(dump_to_string("\n", 1, (void *)&string)) + return NULL; + + /* consume just the right amount of memory */ + result = strdup(string.buffer); + free(string.buffer); + + return result; +} + int json_dumpf(const json_t *json, FILE *output, uint32_t flags) { if(do_dump(json, flags, 0, dump_to_file, (void *)output))