From 27fbc658293db97775385f6cc54a36bf0d6c454d Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Tue, 6 Oct 2020 14:15:47 +0200 Subject: #941 Add og_json_parse_string_copy(...) function This function provides an easy way to copy the content of a json string into a regular C string. --- src/json.c | 15 +++++++++++++++ src/json.h | 1 + 2 files changed, 16 insertions(+) (limited to 'src') diff --git a/src/json.c b/src/json.c index c9c2078..239f601 100644 --- a/src/json.c +++ b/src/json.c @@ -18,6 +18,21 @@ int og_json_parse_string(json_t *element, const char **str) return 0; } +int og_json_parse_string_copy(json_t *element, char *str, size_t size) +{ + const char *reference_str; + int err = 0; + + err = og_json_parse_string(element, &reference_str); + if (err != 0) + return err; + + err = snprintf(str, size, "%s", reference_str); + if (err >= size) + return -1; + return 0; +} + int og_json_parse_uint64(json_t *element, uint64_t *integer) { if (json_typeof(element) != JSON_INTEGER) diff --git a/src/json.h b/src/json.h index cf78d46..df7964d 100644 --- a/src/json.h +++ b/src/json.h @@ -5,6 +5,7 @@ #include "schedule.h" int og_json_parse_string(json_t *element, const char **str); +int og_json_parse_string_copy(json_t *element, char *str, size_t size); int og_json_parse_uint64(json_t *element, uint64_t *integer); int og_json_parse_uint(json_t *element, uint32_t *integer); int og_json_parse_bool(json_t *element, bool *value); -- cgit v1.2.3-18-g5258