X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fjson.c;h=d162fd5143bfc9e8cd3994007655c46ccca989f8;hb=5d9cb63c9160df34d8f282db0e933e3f021f7fb6;hp=dfd6b84531dde279f9a23c3d0e7fe2326f689513;hpb=f38b84ea2b6b61d309c604faedd41ab3b0fcf16b;p=openvswitch diff --git a/lib/json.c b/lib/json.c index dfd6b845..d162fd51 100644 --- a/lib/json.c +++ b/lib/json.c @@ -99,6 +99,9 @@ struct json_parser { /* Lexical analysis. */ enum json_lex_state lex_state; struct ds buffer; /* Buffer for accumulating token text. */ + int line_number; + int column_number; + int byte_number; /* Parsing. */ enum json_parse_state parse_state; @@ -219,6 +222,14 @@ json_array_create(struct json **elements, size_t n) return json; } +struct json * +json_array_create_1(struct json *elem0) +{ + struct json **elems = xmalloc(sizeof *elems); + elems[0] = elem0; + return json_array_create(elems, 1); +} + struct json * json_array_create_2(struct json *elem0, struct json *elem1) { @@ -500,14 +511,15 @@ json_hash(const struct json *json, size_t basis) static bool json_equal_object(const struct shash *a, const struct shash *b) { - struct shash_node *node; + struct shash_node *a_node; if (shash_count(a) != shash_count(b)) { return false; } - SHASH_FOR_EACH (node, a) { - if (!shash_find(b, node->name)) { + SHASH_FOR_EACH (a_node, a) { + struct shash_node *b_node = shash_find(b, a_node->name); + if (!b_node || !json_equal(a_node->data, b_node->data)) { return false; } } @@ -868,10 +880,18 @@ exit: } static bool -json_lex_input(struct json_parser *p, int c) +json_lex_input(struct json_parser *p, unsigned char c) { struct json_token token; + p->byte_number++; + if (c == '\n') { + p->column_number = 0; + p->line_number++; + } else { + p->column_number++; + } + switch (p->lex_state) { case JSON_LEX_START: switch (c) { @@ -1339,12 +1359,18 @@ static void json_error(struct json_parser *p, const char *format, ...) { if (!p->error) { + struct ds msg; va_list args; + ds_init(&msg); + ds_put_format(&msg, "line %d, column %d, byte %d: ", + p->line_number, p->column_number, p->byte_number); va_start(args, format); - p->error = xvasprintf(format, args); + ds_put_format_valist(&msg, format, args); va_end(args); + p->error = ds_steal_cstr(&msg); + p->done = true; } }