2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 static struct json_object
*
20 jp_match_next(struct jp_opcode
*ptr
,
21 struct json_object
*root
, struct json_object
*cur
,
22 jp_match_cb_t cb
, void *priv
);
25 jp_json_to_op(struct json_object
*obj
, struct jp_opcode
*op
)
27 switch (json_object_get_type(obj
))
29 case json_type_boolean
:
31 op
->num
= json_object_get_boolean(obj
);
36 op
->num
= json_object_get_int(obj
);
39 case json_type_string
:
41 op
->str
= (char *)json_object_get_string(obj
);
50 jp_resolve(struct json_object
*root
, struct json_object
*cur
,
51 struct jp_opcode
*op
, struct jp_opcode
*res
)
53 struct json_object
*val
;
58 val
= jp_match(op
, cur
, NULL
, NULL
);
61 return jp_json_to_op(val
, res
);
66 val
= jp_match(op
, root
, NULL
, NULL
);
69 return jp_json_to_op(val
, res
);
80 jp_cmp(struct jp_opcode
*op
, struct json_object
*root
, struct json_object
*cur
)
83 struct jp_opcode left
, right
;
85 if (!jp_resolve(root
, cur
, op
->down
, &left
) ||
86 !jp_resolve(root
, cur
, op
->down
->sibling
, &right
))
89 if (left
.type
!= right
.type
)
96 delta
= left
.num
- right
.num
;
100 delta
= strcmp(left
.str
, right
.str
);
133 jp_expr(struct jp_opcode
*op
, struct json_object
*root
, struct json_object
*cur
,
134 int idx
, const char *key
, jp_match_cb_t cb
, void *priv
)
136 struct jp_opcode
*sop
;
149 return jp_cmp(op
, root
, cur
);
152 return !!jp_match(op
, root
, NULL
, NULL
);
155 return !!jp_match(op
, cur
, NULL
, NULL
);
158 return !jp_expr(op
->down
, root
, cur
, idx
, key
, cb
, priv
);
161 for (sop
= op
->down
; sop
; sop
= sop
->sibling
)
162 if (!jp_expr(sop
, root
, cur
, idx
, key
, cb
, priv
))
168 for (sop
= op
->down
; sop
; sop
= sop
->sibling
)
169 if (jp_expr(sop
, root
, cur
, idx
, key
, cb
, priv
))
174 return (key
&& !strcmp(op
->str
, key
));
177 return (idx
== op
->num
);
184 static struct json_object
*
185 jp_match_expr(struct jp_opcode
*ptr
,
186 struct json_object
*root
, struct json_object
*cur
,
187 jp_match_cb_t cb
, void *priv
)
190 struct json_object
*tmp
, *res
= NULL
;
192 switch (json_object_get_type(cur
))
194 case json_type_object
:
195 ; /* a label can only be part of a statement and a declaration is not a statement */
196 json_object_object_foreach(cur
, key
, val
)
198 if (jp_expr(ptr
, root
, val
, -1, key
, cb
, priv
))
200 tmp
= jp_match_next(ptr
->sibling
, root
, val
, cb
, priv
);
209 case json_type_array
:
210 len
= json_object_array_length(cur
);
212 for (idx
= 0; idx
< len
; idx
++)
214 tmp
= json_object_array_get_idx(cur
, idx
);
216 if (jp_expr(ptr
, root
, tmp
, idx
, NULL
, cb
, priv
))
218 tmp
= jp_match_next(ptr
->sibling
, root
, tmp
, cb
, priv
);
234 static struct json_object
*
235 jp_match_next(struct jp_opcode
*ptr
,
236 struct json_object
*root
, struct json_object
*cur
,
237 jp_match_cb_t cb
, void *priv
)
239 struct json_object
*next
;
253 if (json_object_object_get_ex(cur
, ptr
->str
, &next
))
254 return jp_match_next(ptr
->sibling
, root
, next
, cb
, priv
);
259 next
= json_object_array_get_idx(cur
, ptr
->num
);
262 return jp_match_next(ptr
->sibling
, root
, next
, cb
, priv
);
267 return jp_match_expr(ptr
, root
, cur
, cb
, priv
);
274 jp_match(struct jp_opcode
*path
, json_object
*jsobj
,
275 jp_match_cb_t cb
, void *priv
)
277 if (path
->type
== T_LABEL
)
280 return jp_match_next(path
->down
, jsobj
, jsobj
, cb
, priv
);