Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / test-apps / test-lejp.c
1 /*
2  * Lightweight Embedded JSON Parser
3  *
4  * Copyright (C) 2013-2017 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include <libwebsockets.h>
23 #include <string.h>
24
25
26 static const char * const reason_names[] = {
27         "LEJPCB_CONSTRUCTED",
28         "LEJPCB_DESTRUCTED",
29         "LEJPCB_START",
30         "LEJPCB_COMPLETE",
31         "LEJPCB_FAILED",
32         "LEJPCB_PAIR_NAME",
33         "LEJPCB_VAL_TRUE",
34         "LEJPCB_VAL_FALSE",
35         "LEJPCB_VAL_NULL",
36         "LEJPCB_VAL_NUM_INT",
37         "LEJPCB_VAL_NUM_FLOAT",
38         "LEJPCB_VAL_STR_START",
39         "LEJPCB_VAL_STR_CHUNK",
40         "LEJPCB_VAL_STR_END",
41         "LEJPCB_ARRAY_START",
42         "LEJPCB_ARRAY_END",
43         "LEJPCB_OBJECT_START",
44         "LEJPCB_OBJECT_END",
45         "LEJPCB_OBJECT_END_PRE",
46 };
47
48 static const char * const tok[] = {
49         "dummy___"
50 };
51
52 static signed char
53 cb(struct lejp_ctx *ctx, char reason)
54 {
55         char buf[1024], *p = buf, *end = &buf[sizeof(buf)];
56         int n;
57
58         for (n = 0; n < ctx->sp; n++)
59                 *p++ = ' ';
60         *p = '\0';
61
62         if (reason & LEJP_FLAG_CB_IS_VALUE) {
63                 p += lws_snprintf(p, p - end, "   value '%s' ", ctx->buf);
64                 if (ctx->ipos) {
65                         int n;
66
67                         p += lws_snprintf(p, p - end, "(array indexes: ");
68                         for (n = 0; n < ctx->ipos; n++)
69                                 p += lws_snprintf(p, p - end, "%d ", ctx->i[n]);
70                         p += lws_snprintf(p, p - end, ") ");
71                 }
72                 lwsl_notice("%s (%s)\r\n", buf,
73                        reason_names[(unsigned int)
74                         (reason) & (LEJP_FLAG_CB_IS_VALUE - 1)]);
75
76                 (void)reason_names; /* NO_LOGS... */
77                 return 0;
78         }
79
80         switch (reason) {
81         case LEJPCB_COMPLETE:
82                 lwsl_notice("%sParsing Completed (LEJPCB_COMPLETE)\n", buf);
83                 break;
84         case LEJPCB_PAIR_NAME:
85                 lwsl_notice("%spath: '%s' (LEJPCB_PAIR_NAME)\n", buf, ctx->path);
86                 break;
87         }
88
89         lwsl_notice("%s%s: path %s match %d statckp %d\r\n", buf, reason_names[(unsigned int)
90                 (reason) & (LEJP_FLAG_CB_IS_VALUE - 1)], ctx->path,
91                 ctx->path_match, ctx->pst[ctx->pst_sp].ppos);
92
93         return 0;
94 }
95
96 int
97 main(int argc, char *argv[])
98 {
99         int fd, n = 1, ret = 1, m;
100         struct lejp_ctx ctx;
101         char buf[128];
102
103         lws_set_log_level(7, NULL);
104
105         lwsl_notice("libwebsockets-test-lejp  (C) 2017 - 2018 andy@warmcat.com\n");
106         lwsl_notice("  usage: cat my.json | libwebsockets-test-lejp\n\n");
107
108         lejp_construct(&ctx, cb, NULL, tok, LWS_ARRAY_SIZE(tok));
109
110         fd = 0;
111
112         while (n > 0) {
113                 n = read(fd, buf, sizeof(buf));
114                 if (n <= 0)
115                         continue;
116
117                 m = lejp_parse(&ctx, (uint8_t *)buf, n);
118                 if (m < 0 && m != LEJP_CONTINUE) {
119                         lwsl_err("parse failed %d\n", m);
120                         goto bail;
121                 }
122         }
123         lwsl_notice("okay\n");
124         ret = 0;
125 bail:
126         lejp_destruct(&ctx);
127
128         return ret;
129 }