vat: fix static analysis warning
[vpp.git] / src / vat / json_format.h
1 /*
2  *------------------------------------------------------------------
3  * json_format.h
4  *
5  * Copyright (c) 2015 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #ifndef __JSON_FORMAT_H__
21 #define __JSON_FORMAT_H__
22
23 #include <vppinfra/clib.h>
24 #include <vppinfra/format.h>
25 #include <netinet/ip.h>
26
27 /* JSON value type */
28 typedef enum
29 {
30   VAT_JSON_NONE,
31   VAT_JSON_OBJECT,
32   VAT_JSON_ARRAY,
33   VAT_JSON_STRING,
34   VAT_JSON_REAL,
35   VAT_JSON_UINT,
36   VAT_JSON_INT,
37   VAT_JSON_IPV4,
38   VAT_JSON_IPV6,
39   VAT_JSON_MAX
40 } vat_json_val_type_t;
41
42 typedef struct vat_json_node_s vat_json_node_t;
43 typedef struct vat_json_pair_s vat_json_pair_t;
44
45 /* JSON object structure */
46 struct vat_json_node_s
47 {
48   vat_json_val_type_t type;
49   union
50   {
51     vat_json_pair_t *pairs;
52     vat_json_node_t *array;
53     u8 *string;
54     struct in_addr ip4;
55     struct in6_addr ip6;
56     u64 uint;
57     i64 sint;
58     f64 real;
59   };
60 };
61
62 struct vat_json_pair_s
63 {
64   const char *name;
65   vat_json_node_t value;
66 };
67
68 void vat_json_print (FILE * ofp, vat_json_node_t * node);
69 void vat_json_free (vat_json_node_t * node);
70
71 static_always_inline void
72 vat_json_init_object (vat_json_node_t * json)
73 {
74   json->type = VAT_JSON_OBJECT;
75   json->pairs = NULL;
76 }
77
78 static_always_inline void
79 vat_json_init_array (vat_json_node_t * json)
80 {
81   json->type = VAT_JSON_ARRAY;
82   json->array = NULL;
83 }
84
85 static_always_inline void
86 vat_json_set_string (vat_json_node_t * json, u8 * str)
87 {
88   json->type = VAT_JSON_STRING;
89   json->string = str;
90 }
91
92 static_always_inline void
93 vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
94 {
95   u8 *ns = NULL;
96   int len = strlen ((const char *) str);
97   vec_validate (ns, len);
98   strncpy ((char *) ns, (const char *) str, len + 1);
99   vec_add1 (ns, '\0');
100   vat_json_set_string (json, ns);
101 }
102
103 static_always_inline void
104 vat_json_set_int (vat_json_node_t * json, i64 num)
105 {
106   json->type = VAT_JSON_INT;
107   json->sint = num;
108 }
109
110 static_always_inline void
111 vat_json_set_uint (vat_json_node_t * json, u64 num)
112 {
113   json->type = VAT_JSON_UINT;
114   json->uint = num;
115 }
116
117 static_always_inline void
118 vat_json_set_real (vat_json_node_t * json, f64 real)
119 {
120   json->type = VAT_JSON_REAL;
121   json->real = real;
122 }
123
124 static_always_inline void
125 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
126 {
127   json->type = VAT_JSON_IPV4;
128   json->ip4 = ip4;
129 }
130
131 static_always_inline void
132 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
133 {
134   json->type = VAT_JSON_IPV6;
135   json->ip6 = ip6;
136 }
137
138 static_always_inline vat_json_node_t *
139 vat_json_object_add (vat_json_node_t * json, const char *name)
140 {
141   ASSERT (VAT_JSON_OBJECT == json->type);
142   uword pos = vec_len (json->pairs);
143   vec_validate (json->pairs, pos);
144   json->pairs[pos].name = name;
145   return &json->pairs[pos].value;
146 }
147
148 static_always_inline vat_json_node_t *
149 vat_json_array_add (vat_json_node_t * json)
150 {
151   ASSERT (VAT_JSON_ARRAY == json->type);
152   uword pos = vec_len (json->array);
153   vec_validate (json->array, pos);
154   return &json->array[pos];
155 }
156
157 static_always_inline vat_json_node_t *
158 vat_json_object_add_list (vat_json_node_t * json, const char *name)
159 {
160   vat_json_node_t *array_node = vat_json_object_add (json, name);
161   vat_json_init_array (array_node);
162   return array_node;
163 }
164
165 static_always_inline void
166 vat_json_object_add_string_copy (vat_json_node_t * json,
167                                  const char *name, u8 * str)
168 {
169   vat_json_set_string_copy (vat_json_object_add (json, name), str);
170 }
171
172 static_always_inline void
173 vat_json_object_add_uint (vat_json_node_t * json,
174                           const char *name, u64 number)
175 {
176   vat_json_set_uint (vat_json_object_add (json, name), number);
177 }
178
179 static_always_inline void
180 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
181 {
182   vat_json_set_int (vat_json_object_add (json, name), number);
183 }
184
185 static_always_inline void
186 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
187 {
188   vat_json_set_real (vat_json_object_add (json, name), real);
189 }
190
191 static_always_inline void
192 vat_json_object_add_ip4 (vat_json_node_t * json,
193                          const char *name, struct in_addr ip4)
194 {
195   vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
196 }
197
198 static_always_inline void
199 vat_json_object_add_ip6 (vat_json_node_t * json,
200                          const char *name, struct in6_addr ip6)
201 {
202   vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
203 }
204
205 static_always_inline void
206 vat_json_array_add_int (vat_json_node_t * json, i64 number)
207 {
208   vat_json_set_int (vat_json_array_add (json), number);
209 }
210
211 static_always_inline void
212 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
213 {
214   vat_json_set_uint (vat_json_array_add (json), number);
215 }
216
217 static_always_inline void
218 vat_json_object_add_bytes (vat_json_node_t * json,
219                            const char *name, u8 * array, uword size)
220 {
221   ASSERT (VAT_JSON_OBJECT == json->type);
222   vat_json_node_t *json_array = vat_json_object_add (json, name);
223   vat_json_init_array (json_array);
224   int i;
225   for (i = 0; i < size; i++)
226     {
227       vat_json_array_add_uint (json_array, array[i]);
228     }
229 }
230
231 static_always_inline vat_json_node_t *
232 vat_json_object_get_element (vat_json_node_t * json, const char *name)
233 {
234   int i = 0;
235
236   ASSERT (VAT_JSON_OBJECT == json->type);
237   for (i = 0; i < vec_len (json->pairs); i++)
238     {
239       if (0 == strcmp (json->pairs[i].name, name))
240         {
241           return &json->pairs[i].value;
242         }
243     }
244   return NULL;
245 }
246
247 #endif /* __JSON_FORMAT_H__ */
248
249 /*
250  * fd.io coding-style-patch-verification: ON
251  *
252  * Local Variables:
253  * eval: (c-set-style "gnu")
254  * End:
255  */