Make json_error_t opaque
[jansson.git] / src / load.c
index 32d6500..a34fd0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
  *
  * 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 <string.h>
 #include <stdarg.h>
 #include <assert.h>
-#include <stdint.h>
 
 #include <jansson.h>
 #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,79 @@ typedef struct {
 
 /*** error reporting ***/
 
-static void error_init(json_error_t *error)
+#define JSON_ERROR_MSG_LENGTH 160
+
+struct json_error_t {
+  char msg[JSON_ERROR_MSG_LENGTH];
+  int line;
+};
+
+const char *json_error_msg(const json_error_t *error)
+{
+    return error ? error->msg : NULL;
+}
+
+int json_error_line(const json_error_t *error)
+{
+    return error ? error->line : -1;
+}
+
+static void error_init(json_error_t **error)
 {
     if(error)
-    {
-        error->text[0] = '\0';
-        error->line = -1;
-    }
+        *error = NULL;
 }
 
-static void error_set(json_error_t *error, const lex_t *lex,
+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 text[JSON_ERROR_MSG_LENGTH];
 
-    if(!error || error->text[0] != '\0') {
-        /* error already set */
+    if(!error || *error) {
+        /* error not given or already set */
         return;
     }
 
+    *error = malloc(sizeof(json_error_t));
+    if(!(*error))
+        return;
+
     va_start(ap, msg);
-    vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+    vsnprintf(text, JSON_ERROR_MSG_LENGTH, msg, ap);
     va_end(ap);
 
     if(lex)
     {
         const char *saved_text = strbuffer_value(&lex->saved_text);
-        error->line = lex->line;
+        (*error)->line = lex->line;
         if(saved_text && saved_text[0])
         {
             if(lex->saved_text.length <= 20) {
-                snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+                snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH,
                          "%s near '%s'", text, saved_text);
             }
             else
-                snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
+                snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
         }
         else
         {
-            snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
+            snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH,
                      "%s near end of file", text);
         }
     }
     else
     {
-        error->line = -1;
-        snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
+        (*error)->line = -1;
+        snprintf((*error)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
     }
 }
 
 
 /*** 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;
@@ -124,7 +142,7 @@ void stream_init(stream_t *stream, get_func get, eof_func eof, void *data)
     stream->buffer_pos = 0;
 }
 
-static char stream_get(stream_t *stream, json_error_t *error)
+static char stream_get(stream_t *stream, json_error_t **error)
 {
     char c;
 
@@ -149,7 +167,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;
@@ -182,7 +200,7 @@ static void stream_unget(stream_t *stream, char c)
 }
 
 
-static int lex_get(lex_t *lex, json_error_t *error)
+static int lex_get(lex_t *lex, json_error_t **error)
 {
     return stream_get(&lex->stream, error);
 }
@@ -197,7 +215,7 @@ static void lex_save(lex_t *lex, char c)
     strbuffer_append_byte(&lex->saved_text, c);
 }
 
-static int lex_get_save(lex_t *lex, json_error_t *error)
+static int lex_get_save(lex_t *lex, json_error_t **error)
 {
     char c = stream_get(&lex->stream, error);
     lex_save(lex, c);
@@ -245,7 +263,7 @@ static int32_t decode_unicode_escape(const char *str)
     return value;
 }
 
-static void lex_scan_string(lex_t *lex, json_error_t *error)
+static void lex_scan_string(lex_t *lex, json_error_t **error)
 {
     char c;
     const char *p;
@@ -401,7 +419,13 @@ out:
     free(lex->value.string);
 }
 
-static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
+#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;
     char *end;
@@ -430,25 +454,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 +509,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;
     }
@@ -504,7 +522,7 @@ out:
     return -1;
 }
 
-static int lex_scan(lex_t *lex, json_error_t *error)
+static int lex_scan(lex_t *lex, json_error_t **error)
 {
     char c;
 
@@ -610,9 +628,9 @@ static void lex_close(lex_t *lex)
 
 /*** parser ***/
 
-static json_t *parse_value(lex_t *lex, json_error_t *error);
+static json_t *parse_value(lex_t *lex, json_error_t **error);
 
-static json_t *parse_object(lex_t *lex, json_error_t *error)
+static json_t *parse_object(lex_t *lex, json_error_t **error)
 {
     json_t *object = json_object();
     if(!object)
@@ -677,7 +695,7 @@ error:
     return NULL;
 }
 
-static json_t *parse_array(lex_t *lex, json_error_t *error)
+static json_t *parse_array(lex_t *lex, json_error_t **error)
 {
     json_t *array = json_array();
     if(!array)
@@ -717,7 +735,7 @@ error:
     return NULL;
 }
 
-static json_t *parse_value(lex_t *lex, json_error_t *error)
+static json_t *parse_value(lex_t *lex, json_error_t **error)
 {
     json_t *json;
 
@@ -772,7 +790,7 @@ 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);
 
@@ -811,15 +829,13 @@ 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;
@@ -840,10 +856,11 @@ 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;
     json_t *result;
+    (void)flags; /* unused */
 
     if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
         return NULL;
@@ -864,7 +881,7 @@ 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;
@@ -879,7 +896,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;