X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fload.c;h=f05230ba41e0d1f2867b3d314a8d47ccd99212a9;hb=5422a862de2354b3419ef628bac5a18c6ef522da;hp=32d650022ec3697f2fe81b88bcabb5dd2532e12f;hpb=d67aeb9739bf3e963ceaa8b622d20cd87a0b65fe;p=jansson.git diff --git a/src/load.c b/src/load.c index 32d6500..f05230b 100644 --- a/src/load.c +++ b/src/load.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -14,7 +14,6 @@ #include #include #include -#include #include #include "jansson_private.h" @@ -53,7 +52,7 @@ typedef struct { int line, column; union { char *string; - int integer; + json_int_t integer; double real; } value; } lex_t; @@ -61,60 +60,53 @@ typedef struct { /*** error reporting ***/ -static void error_init(json_error_t *error) -{ - if(error) - { - error->text[0] = '\0'; - error->line = -1; - } -} - static void error_set(json_error_t *error, const lex_t *lex, const char *msg, ...) { va_list ap; - char text[JSON_ERROR_TEXT_LENGTH]; + char msg_text[JSON_ERROR_TEXT_LENGTH]; + + int line = -1, col = -1; + const char *result = msg_text; - if(!error || error->text[0] != '\0') { - /* error already set */ + if(!error) return; - } va_start(ap, msg); - vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap); + vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap); va_end(ap); if(lex) { const char *saved_text = strbuffer_value(&lex->saved_text); - error->line = lex->line; + char msg_with_context[JSON_ERROR_TEXT_LENGTH]; + + line = lex->line; + if(saved_text && saved_text[0]) { if(lex->saved_text.length <= 20) { - snprintf(error->text, JSON_ERROR_TEXT_LENGTH, - "%s near '%s'", text, saved_text); + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, + "%s near '%s'", msg_text, saved_text); + result = msg_with_context; } - else - snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text); } else { - snprintf(error->text, JSON_ERROR_TEXT_LENGTH, - "%s near end of file", text); + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, + "%s near end of file", msg_text); + result = msg_with_context; } } - else - { - error->line = -1; - snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text); - } + + jsonp_error_set(error, line, col, "%s", result); } /*** lexical analyzer ***/ -void stream_init(stream_t *stream, get_func get, eof_func eof, void *data) +static void +stream_init(stream_t *stream, get_func get, eof_func eof, void *data) { stream->get = get; stream->eof = eof; @@ -149,7 +141,7 @@ static char stream_get(stream_t *stream, json_error_t *error) for(i = 1; i < count; i++) stream->buffer[i] = stream->get(stream->data); - if(!utf8_check_full(stream->buffer, count)) + if(!utf8_check_full(stream->buffer, count, NULL)) goto out; stream->stream_pos += count; @@ -401,6 +393,12 @@ out: free(lex->value.string); } +#if JSON_INTEGER_IS_LONG_LONG +#define json_strtoint strtoll +#else +#define json_strtoint strtol +#endif + static int lex_scan_number(lex_t *lex, char c, json_error_t *error) { const char *saved_text; @@ -430,25 +428,26 @@ static int lex_scan_number(lex_t *lex, char c, json_error_t *error) } if(c != '.' && c != 'E' && c != 'e') { - long value; + json_int_t value; lex_unget_unsave(lex, c); saved_text = strbuffer_value(&lex->saved_text); - value = strtol(saved_text, &end, 10); - assert(end == saved_text + lex->saved_text.length); - if((value == LONG_MAX && errno == ERANGE) || value > INT_MAX) { - error_set(error, lex, "too big integer"); - goto out; - } - else if((value == LONG_MIN && errno == ERANGE) || value < INT_MIN) { - error_set(error, lex, "too big negative integer"); + errno = 0; + value = json_strtoint(saved_text, &end, 10); + if(errno == ERANGE) { + if(value < 0) + error_set(error, lex, "too big negative integer"); + else + error_set(error, lex, "too big integer"); goto out; } + assert(end == saved_text + lex->saved_text.length); + lex->token = TOKEN_INTEGER; - lex->value.integer = (int)value; + lex->value.integer = value; return 0; } @@ -484,14 +483,7 @@ static int lex_scan_number(lex_t *lex, char c, json_error_t *error) value = strtod(saved_text, &end); assert(end == saved_text + lex->saved_text.length); - if(value == 0 && errno == ERANGE) { - error_set(error, lex, "real number underflow"); - goto out; - } - - /* Cannot test for +/-HUGE_VAL because the HUGE_VAL constant is - only defined in C99 mode. So let's trust in sole errno. */ - else if(errno == ERANGE) { + if(errno == ERANGE && value != 0) { error_set(error, lex, "real number overflow"); goto out; } @@ -772,10 +764,8 @@ static json_t *parse_value(lex_t *lex, json_error_t *error) return json; } -json_t *parse_json(lex_t *lex, json_error_t *error) +static json_t *parse_json(lex_t *lex, json_error_t *error) { - error_init(error); - lex_scan(lex, error); if(lex->token != '[' && lex->token != '{') { error_set(error, lex, "'[' or '{' expected"); @@ -811,19 +801,19 @@ static int string_eof(void *data) return (stream->data[stream->pos] == '\0'); } -json_t *json_loads(const char *string, json_error_t *error) +json_t *json_loads(const char *string, size_t flags, json_error_t *error) { lex_t lex; json_t *result; + (void)flags; /* unused */ - string_data_t stream_data = { - .data = string, - .pos = 0 - }; + string_data_t stream_data = {string, 0}; if(lex_init(&lex, string_get, string_eof, (void *)&stream_data)) return NULL; + jsonp_error_init(error, ""); + result = parse_json(&lex, error); if(!result) goto out; @@ -840,14 +830,23 @@ out: return result; } -json_t *json_loadf(FILE *input, json_error_t *error) +json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) { lex_t lex; + const char *source; json_t *result; + (void)flags; /* unused */ if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input)) return NULL; + if(input == stdin) + source = ""; + else + source = ""; + + jsonp_error_init(error, source); + result = parse_json(&lex, error); if(!result) goto out; @@ -864,12 +863,12 @@ out: return result; } -json_t *json_load_file(const char *path, json_error_t *error) +json_t *json_load_file(const char *path, size_t flags, json_error_t *error) { json_t *result; FILE *fp; - error_init(error); + jsonp_error_init(error, path); fp = fopen(path, "r"); if(!fp) @@ -879,7 +878,7 @@ json_t *json_load_file(const char *path, json_error_t *error) return NULL; } - result = json_loadf(fp, error); + result = json_loadf(fp, flags, error); fclose(fp); return result;