From 906d24a554afc9c68624250cb03c61682e3a8c69 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 13 Jan 2015 12:45:12 +1100 Subject: [PATCH] Add an option to add an EOL marker to json_dumps --- src/jansson-2.6/src/dump.c | 9 ++++++++- src/jansson-2.6/src/jansson.h | 1 + src/jansson-2.6/src/jansson_private.h | 2 ++ src/jansson-2.6/src/memory.c | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/jansson-2.6/src/dump.c b/src/jansson-2.6/src/dump.c index a8e99c12..776f294d 100644 --- a/src/jansson-2.6/src/dump.c +++ b/src/jansson-2.6/src/dump.c @@ -427,6 +427,13 @@ static int do_dump(const json_t *json, size_t flags, int depth, } } +char *json_dump_dup(const char *str, size_t flags) +{ + if (flags & JSON_EOL) + return jsonp_eolstrdup(str); + return jsonp_strdup(str); +} + char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; @@ -438,7 +445,7 @@ char *json_dumps(const json_t *json, size_t flags) if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) result = NULL; else - result = jsonp_strdup(strbuffer_value(&strbuff)); + result = json_dump_dup(strbuffer_value(&strbuff), flags); strbuffer_close(&strbuff); return result; diff --git a/src/jansson-2.6/src/jansson.h b/src/jansson-2.6/src/jansson.h index 8cc2760e..2c86c1f3 100644 --- a/src/jansson-2.6/src/jansson.h +++ b/src/jansson-2.6/src/jansson.h @@ -261,6 +261,7 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla #define JSON_ENCODE_ANY 0x200 #define JSON_ESCAPE_SLASH 0x400 #define JSON_NO_UTF8 0x800 +#define JSON_EOL 0x1000 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); diff --git a/src/jansson-2.6/src/jansson_private.h b/src/jansson-2.6/src/jansson_private.h index 403b53a4..3ca4dffa 100644 --- a/src/jansson-2.6/src/jansson_private.h +++ b/src/jansson-2.6/src/jansson_private.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2009-2013 Petri Lehtinen + * Copyright (c) 2015 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -83,6 +84,7 @@ void* jsonp_malloc(size_t size); void jsonp_free(void *ptr); char *jsonp_strndup(const char *str, size_t length); char *jsonp_strdup(const char *str); +char *jsonp_eolstrdup(const char *str); /* Windows compatibility */ #ifdef _WIN32 diff --git a/src/jansson-2.6/src/memory.c b/src/jansson-2.6/src/memory.c index eb6cec54..0be24380 100644 --- a/src/jansson-2.6/src/memory.c +++ b/src/jansson-2.6/src/memory.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2009-2013 Petri Lehtinen * Copyright (c) 2011-2012 Basile Starynkevitch + * Copyright (c) 2015 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. @@ -49,6 +50,25 @@ char *jsonp_strdup(const char *str) return new_str; } +char *jsonp_eolstrdup(const char *str) +{ + char *new_str; + size_t len; + + len = strlen(str); + if(len == (size_t)-1) + return NULL; + + new_str = jsonp_malloc(len + 2); + if(!new_str) + return NULL; + + memcpy(new_str, str, len); + new_str[len] = '\n'; + new_str[len + 1] = '\0'; + return new_str; +} + void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) { do_malloc = malloc_fn;