Add the 'I' format for both pack and unpack
[jansson.git] / src / load.c
index a34fd0c..5a48dc3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2011 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.
@@ -60,72 +60,46 @@ typedef struct {
 
 /*** error reporting ***/
 
-#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 = 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_MSG_LENGTH];
+    char msg_text[JSON_ERROR_TEXT_LENGTH];
 
-    if(!error || *error) {
-        /* error not given or already set */
-        return;
-    }
+    int line = -1, col = -1;
+    const char *result = msg_text;
 
-    *error = malloc(sizeof(json_error_t));
-    if(!(*error))
+    if(!error)
         return;
 
     va_start(ap, msg);
-    vsnprintf(text, JSON_ERROR_MSG_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)->msg, JSON_ERROR_MSG_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)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
         }
         else
         {
-            snprintf((*error)->msg, JSON_ERROR_MSG_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)->msg, JSON_ERROR_MSG_LENGTH, "%s", text);
-    }
+
+    jsonp_error_set(error, line, col, "%s", result);
 }
 
 
@@ -142,7 +116,7 @@ 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;
 
@@ -200,7 +174,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);
 }
@@ -215,7 +189,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);
@@ -263,7 +237,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;
@@ -425,7 +399,7 @@ out:
 #define json_strtoint     strtol
 #endif
 
-static int lex_scan_number(lex_t *lex, char c, json_error_t **error)
+static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
 {
     const char *saved_text;
     char *end;
@@ -522,7 +496,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;
 
@@ -628,9 +602,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)
@@ -695,7 +669,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)
@@ -735,7 +709,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;
 
@@ -790,10 +764,8 @@ static json_t *parse_value(lex_t *lex, json_error_t **error)
     return json;
 }
 
-static 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");
@@ -829,17 +801,19 @@ static int string_eof(void *data)
     return (stream->data[stream->pos] == '\0');
 }
 
-json_t *json_loads(const char *string, size_t flags, 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 = {string, 0};
 
+    (void)flags; /* unused */
+
     if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
         return NULL;
 
+    jsonp_error_init(error, "<string>");
+
     result = parse_json(&lex, error);
     if(!result)
         goto out;
@@ -856,15 +830,23 @@ out:
     return result;
 }
 
-json_t *json_loadf(FILE *input, size_t flags, 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 = "<stdin>";
+    else
+        source = "<stream>";
+
+    jsonp_error_init(error, source);
+
     result = parse_json(&lex, error);
     if(!result)
         goto out;
@@ -881,12 +863,12 @@ out:
     return result;
 }
 
-json_t *json_load_file(const char *path, size_t flags, 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)