VES EVEL Library VES 5.4.1 enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_HB / jsmn.h
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17
18 #ifndef __JSMN_H_
19 #define __JSMN_H_
20
21 #include <stddef.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /**
28  * JSON type identifier. Basic types are:
29  *      o Object
30  *      o Array
31  *      o String
32  *      o Other primitive: number, boolean (true/false) or null
33  */
34 typedef enum {
35         JSMN_UNDEFINED = 0,
36         JSMN_OBJECT = 1,
37         JSMN_ARRAY = 2,
38         JSMN_STRING = 3,
39         JSMN_PRIMITIVE = 4
40 } jsmntype_t;
41
42 enum jsmnerr {
43         /* Not enough tokens were provided */
44         JSMN_ERROR_NOMEM = -1,
45         /* Invalid character inside JSON string */
46         JSMN_ERROR_INVAL = -2,
47         /* The string is not a full JSON packet, more bytes expected */
48         JSMN_ERROR_PART = -3
49 };
50
51 typedef struct arrayValues {
52    char arrayString[32];
53 } ARRAYVAL;
54
55 typedef struct keyValResult {
56    char keyStr[80];
57    char valStr[250];
58    char resultStr[80];
59 } KEYVALRESULT;
60
61 /**
62  * JSON token description.
63  * @param               type    type (object, array, string etc.)
64  * @param               start   start position in JSON data string
65  * @param               end             end position in JSON data string
66  */
67 typedef struct {
68         jsmntype_t type;
69         int start;
70         int end;
71         int size;
72 //#ifdef JSMN_PARENT_LINKS
73         int parent;
74 //#endif
75 } jsmntok_t;
76
77 /**
78  * JSON parser. Contains an array of token blocks available. Also stores
79  * the string being parsed now and current position in that string
80  */
81 typedef struct {
82         unsigned int pos; /* offset in the JSON string */
83         unsigned int toknext; /* next token to allocate */
84         int toksuper; /* superior token node, e.g parent object or array */
85 } jsmn_parser;
86
87 #define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
88         ((t).start == tok_start \
89          && (t).end == tok_end  \
90          && (t).type == (tok_type))
91
92 #define TOKEN_STRING(js, t, s) \
93         (strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
94          && strlen(s) == (t).end - (t).start)
95
96 #define TOKEN_COPY(js, t, s) \
97         strncpy(s, js+(t).start, (t).end - (t).start)
98
99 #define TOKEN_PRINT(t) \
100         printf("start: %d, end: %d, type: %d, size: %d\n", \
101           (t).start, (t).end, (t).type, (t).size)
102
103
104 /**
105  * Create JSON parser over an array of tokens
106  */
107 void jsmn_init(jsmn_parser *parser);
108
109 /**
110  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
111  * a single JSON object.
112  */
113 int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
114                 jsmntok_t *tokens, unsigned int num_tokens);
115
116 int jsoneq(const char *json, jsmntok_t *tok, const char *s);
117
118 void printToken(char * js, jsmntok_t * tokens, int numToken);
119
120 int getStringToken(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, char * value, int maxValueSize);
121
122 int getIntToken(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, int * value);
123
124 int getArrayTokens(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, ARRAYVAL * arrayValue, int * numElements);
125
126 int isTokenPresent(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam);
127
128 int read_keyVal_params(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, KEYVALRESULT * keyValueResultList, int * numElements);
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif /* __JSMN_H_ */