mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #12795 from aabadie/pr/tests/pkg_jsmn_cleanup
tests/pkg_jsmn: replace use of "%.*s" string formatter
This commit is contained in:
commit
ab94b7c8ad
@ -29,12 +29,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "kernel_defines.h" /* for ARRAY_SIZE macro */
|
||||||
#include "jsmn.h"
|
#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
|
* A small example of jsmn parsing when JSON structure is known and number of
|
||||||
* tokens is predictable.
|
* tokens is predictable.
|
||||||
@ -44,6 +41,8 @@ const char *JSON_STRING =
|
|||||||
"{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n "
|
"{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n "
|
||||||
"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
|
"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
|
||||||
|
|
||||||
|
static char printbuf[32];
|
||||||
|
|
||||||
static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
|
static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
|
||||||
{
|
{
|
||||||
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
|
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
|
||||||
@ -53,6 +52,19 @@ static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
|
|||||||
return -1;
|
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 main(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -77,20 +89,18 @@ int main(void)
|
|||||||
for (i = 1; i < r; i++) {
|
for (i = 1; i < r; i++) {
|
||||||
if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
|
if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
|
||||||
/* We may use strndup() to fetch string value */
|
/* We may use strndup() to fetch string value */
|
||||||
printf("- User: %.*s\n", t[i + 1].end - t[i + 1].start,
|
print_string("- User: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start);
|
||||||
JSON_STRING + t[i + 1].start);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
|
else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
|
||||||
/* We may additionally check if the value is either "true" or "false" */
|
/* We may additionally check if the value is either "true" or
|
||||||
printf("- Admin: %.*s\n", t[i + 1].end - t[i + 1].start,
|
"false" */
|
||||||
JSON_STRING + t[i + 1].start);
|
print_string("- Admin: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
|
else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
|
||||||
/* We may want to do strtol() here to get numeric value */
|
/* We may want to do strtol() here to get numeric value */
|
||||||
printf("- UID: %.*s\n", t[i + 1].end - t[i + 1].start,
|
print_string("- UID: ", JSON_STRING + t[i + 1].start, t[i + 1].end - t[i + 1].start);
|
||||||
JSON_STRING + t[i + 1].start);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
|
else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
|
||||||
@ -101,13 +111,12 @@ int main(void)
|
|||||||
}
|
}
|
||||||
for (j = 0; j < t[i + 1].size; j++) {
|
for (j = 0; j < t[i + 1].size; j++) {
|
||||||
jsmntok_t *g = &t[i + j + 2];
|
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;
|
i += t[i + 1].size + 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Unexpected key: %.*s\n", t[i].end - t[i].start,
|
print_string("Unexpected key: ", JSON_STRING + t[i].start, t[i].end - t[i].start);
|
||||||
JSON_STRING + t[i].start);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user