From 7b719cc6db003aa6fdb8e88ee12b2bed02d45026 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Sun, 29 Oct 2017 16:15:18 +0100 Subject: [PATCH] Added Floating point support to JSON --- CHANGELOG.md | 1 + lib/src/clixon_json.c | 79 +++++++++++++++++++++++++++++++++++++ lib/src/clixon_json_parse.l | 8 +++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a6acad..c5c641e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 3.3.3 Upcoming +* Added Floating point support to JSON * Restconf: http cookie sent as attribute in rpc restconf_post operations to backend. * Added option CLICON_CLISPEC_FILE as complement to CLICON_CLISPEC_DIR to specify single CLI specification file, not only directory containing files. diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c index c2c0934a..496151de 100644 --- a/lib/src/clixon_json.c +++ b/lib/src/clixon_json.c @@ -624,3 +624,82 @@ json_parse_str(char *str, return json_parse(str, "", *xt); } + +/* + * Turn this on to get a json parse and pretty print test program + * Usage: xpath + * read xml from input + * Example compile: + gcc -g -o json -I. -I../clixon ./clixon_json.c -lclixon -lcligen + * Example run: + echo "" | xml +*/ +#if 0 /* Test program */ + +static int +usage(char *argv0) +{ + fprintf(stderr, "usage:%s.\n\tInput on stdin\n", argv0); + exit(0); +} + +#define BUFLEN 1024 /* Size of xml read buffer */ +int +main(int argc, char **argv) +{ + cxobj *xt; + cxobj *xc; + cbuf *cb = cbuf_new(); + int maxbuf = BUFLEN; + char *xmlbuf = NULL; + char *buf = NULL; + int i; + int c; + int len; + FILE *f = stdin; + + if (argc != 1){ + usage(argv[0]); + return 0; + } + clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR); + len = 1024; /* any number is fine */ + if ((buf = malloc(len)) == NULL){ + perror("malloc"); + return -1; + } + memset(buf, 0, len); + + i = 0; /* position in buf */ + while (1){ /* read the whole file */ + if ((c = fgetc(f)) == EOF) + break; + if (len==i){ + if ((buf = realloc(buf, 2*len)) == NULL){ + fprintf(stderr, "%s: realloc: %s\n", __FUNCTION__, strerror(errno)); + goto done; + } + memset(buf+len, 0, len); + len *= 2; + } + buf[i++] = (char)(c&0xff); + } /* read a line */ + + if (json_parse_str(buf, &xt) < 0) + return -1; + xc = NULL; + while ((xc = xml_child_each(xt, xc, -1)) != NULL) { + xmltree2cbuf(cb, xc, 0); /* dump data structures */ + //clicon_xml2cbuf(cb, xc, 0, 1); /* print xml */ + } + fprintf(stdout, "%s", cbuf_get(cb)); + if (xt) + xml_free(xt); + if (cb) + cbuf_free(cb); + done: + return 0; +} + +#endif /* Test program */ + diff --git a/lib/src/clixon_json_parse.l b/lib/src/clixon_json_parse.l index a3545b68..d864c655 100644 --- a/lib/src/clixon_json_parse.l +++ b/lib/src/clixon_json_parse.l @@ -77,6 +77,11 @@ clixon_json_parsewrap(void) %} +digit [0-9] +integer {digit}+ +real ({digit}+[.]{digit}*)|({digit}*[.]{digit}+) +exp ({integer}|{real})[eE][+-]{integer} + %x START %s STRING %s ESCAPE @@ -95,8 +100,7 @@ clixon_json_parsewrap(void) null { return J_NULL; } false { return J_FALSE; } true { return J_TRUE; } -[-+]?[0-9]+ { clixon_json_parselval.string = strdup(yytext); - return J_NUMBER;} +({integer}|{real}|{exp}) { clixon_json_parselval.string = strdup(yytext); return J_NUMBER;} . { return -1; } \" { BEGIN(START); return J_DQ; } \\ { BEGIN(ESCAPE); }