From 9af59196da8bbf134c03a7c104f5e339a18b77b7 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sat, 23 Nov 2019 15:08:59 +0100 Subject: [PATCH 1/2] tests/pkg_jsmn: use kernel define ARRAY_SIZE macro --- tests/pkg_jsmn/simple.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/pkg_jsmn/simple.c b/tests/pkg_jsmn/simple.c index 2e3a2b4209..6360b12101 100644 --- a/tests/pkg_jsmn/simple.c +++ b/tests/pkg_jsmn/simple.c @@ -29,12 +29,9 @@ #include #include +#include "kernel_defines.h" /* for ARRAY_SIZE macro */ #include "jsmn.h" -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a)) -#endif - /* * A small example of jsmn parsing when JSON structure is known and number of * tokens is predictable. From d75650737b791621ef631bd3703f5c4140016351 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sat, 23 Nov 2019 15:09:46 +0100 Subject: [PATCH 2/2] tests/pkg_jsmn: don't use '%.*s' string formatter This is not compatible with avr-libc --- tests/pkg_jsmn/simple.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/pkg_jsmn/simple.c b/tests/pkg_jsmn/simple.c index 6360b12101..e7494f48b5 100644 --- a/tests/pkg_jsmn/simple.c +++ b/tests/pkg_jsmn/simple.c @@ -41,6 +41,8 @@ const char *JSON_STRING = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n " "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}"; +static char printbuf[32]; + static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && @@ -50,6 +52,19 @@ static int jsoneq(const char *json, jsmntok_t *tok, const char *s) return -1; } +static inline size_t min(size_t a, size_t b) +{ + return a <= b ? a : b; +} + +static void print_string(char *prefix, const char *buf, size_t len) +{ + len = min(len, sizeof(printbuf) - 1); + strncpy(printbuf, buf, len); + printbuf[len] = 0; + printf("%s%s\n", prefix, printbuf); +} + int main(void) { int i; @@ -74,20 +89,18 @@ int main(void) for (i = 1; i < r; i++) { if (jsoneq(JSON_STRING, &t[i], "user") == 0) { /* We may use strndup() to fetch string value */ - printf("- User: %.*s\n", t[i + 1].end - t[i + 1].start, - JSON_STRING + t[i + 1].start); + print_string("- User: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start); i++; } else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) { - /* We may additionally check if the value is either "true" or "false" */ - printf("- Admin: %.*s\n", t[i + 1].end - t[i + 1].start, - JSON_STRING + t[i + 1].start); + /* We may additionally check if the value is either "true" or + "false" */ + print_string("- Admin: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start); i++; } else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) { /* We may want to do strtol() here to get numeric value */ - printf("- UID: %.*s\n", t[i + 1].end - t[i + 1].start, - JSON_STRING + t[i + 1].start); + print_string("- UID: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start); i++; } else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) { @@ -98,13 +111,12 @@ int main(void) } for (j = 0; j < t[i + 1].size; j++) { jsmntok_t *g = &t[i + j + 2]; - printf(" * %.*s\n", g->end - g->start, JSON_STRING + g->start); + print_string(" * ", JSON_STRING + g->start, g->end - g->start); } i += t[i + 1].size + 1; } else { - printf("Unexpected key: %.*s\n", t[i].end - t[i].start, - JSON_STRING + t[i].start); + print_string("Unexpected key: ", JSON_STRING + t[i].start, t[i].end - t[i].start); } } return 0;