Fix indentation
[jansson.git] / src / dump.c
index b8bd460..7dbc9f2 100644 (file)
@@ -1,12 +1,22 @@
+/*
+ * Copyright (c) 2009 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.
+ */
+
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 
 #include <jansson.h>
+#include "jansson_private.h"
 #include "strbuffer.h"
 
+#define MAX_INTEGER_STR_LENGTH  100
+#define MAX_REAL_STR_LENGTH     100
+
 typedef int (*dump_func)(const char *buffer, int size, void *data);
 
 struct string
@@ -29,33 +39,25 @@ static int dump_to_file(const char *buffer, int size, void *data)
     return 0;
 }
 
-static int dump_to_fd(const char *buffer, int size, void *data)
-{
-    int *fd = (int *)data;
-    if(write(*fd, buffer, size) != size)
-        return -1;
-    return 0;
-}
+/* 256 spaces (the maximum indentation size) */
+static char whitespace[] = "                                                                                                                                                                                                                                                                ";
 
-static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
+static int dump_indent(unsigned long flags, int depth, dump_func dump, void *data)
 {
     if(JSON_INDENT(flags) > 0)
     {
-        char *ws_buffer;
-        int ws_count = JSON_INDENT(flags) * depth;
+        int i, ws_count = JSON_INDENT(flags);
 
         if(dump("\n", 1, data))
             return -1;
 
-        if(ws_count == 0)
-            return 0;
-
-        ws_buffer = alloca(ws_count);
-        memset(ws_buffer, ' ', ws_count);
-        return dump(ws_buffer, ws_count, data);
+        for(i = 0; i < depth; i++)
+        {
+            if(dump(whitespace, ws_count, data))
+                return -1;
+        }
     }
-    else
-        return dump(" ", 1, data);
+    return 0;
 }
 
 static int dump_string(const char *str, dump_func dump, void *data)
@@ -72,7 +74,7 @@ static int dump_string(const char *str, dump_func dump, void *data)
         char seq[7];
         int length;
 
-        while(*end && *end != '\\' && *end != '"' && (*end < 0 || *end > 0x1F))
+        while(*end && *end != '\\' && *end != '"' && (unsigned char)*end > 0x1F)
             end++;
 
         if(end != str) {
@@ -113,7 +115,7 @@ static int dump_string(const char *str, dump_func dump, void *data)
     return dump("\"", 1, data);
 }
 
-static int do_dump(const json_t *json, uint32_t flags, int depth,
+static int do_dump(const json_t *json, unsigned long flags, int depth,
                    dump_func dump, void *data)
 {
     switch(json_typeof(json)) {
@@ -128,30 +130,26 @@ static int do_dump(const json_t *json, uint32_t flags, int depth,
 
         case JSON_INTEGER:
         {
-            char *buffer;
-            int size, ret;
+            char buffer[MAX_INTEGER_STR_LENGTH];
+            int size;
 
-            size = asprintf(&buffer, "%d", json_integer_value(json));
-            if(size == -1)
+            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
+            if(size >= MAX_INTEGER_STR_LENGTH)
                 return -1;
 
-            ret = dump(buffer, size, data);
-            free(buffer);
-            return ret;
+            return dump(buffer, size, data);
         }
 
         case JSON_REAL:
         {
-            char *buffer;
-            int size, ret;
+            char buffer[MAX_REAL_STR_LENGTH];
+            int size;
 
-            size = asprintf(&buffer, "%.17f", json_real_value(json));
-            if(size == -1)
+            size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%0.17f", json_real_value(json));
+            if(size >= MAX_REAL_STR_LENGTH)
                 return -1;
 
-            ret = dump(buffer, size, data);
-            free(buffer);
-            return ret;
+            return dump(buffer, size, data);
         }
 
         case JSON_STRING:
@@ -160,7 +158,16 @@ static int do_dump(const json_t *json, uint32_t flags, int depth,
         case JSON_ARRAY:
         {
             int i;
-            int n = json_array_size(json);
+            int n;
+            json_array_t *array;
+
+            /* detect circular references */
+            array = json_to_array(json);
+            if(array->visited)
+                return -1;
+            array->visited = 1;
+
+            n = json_array_size(json);
 
             if(dump("[", 1, data))
                 return -1;
@@ -186,12 +193,23 @@ static int do_dump(const json_t *json, uint32_t flags, int depth,
                         return -1;
                 }
             }
+
+            array->visited = 0;
             return dump("]", 1, data);
         }
 
         case JSON_OBJECT:
         {
-            void *iter = json_object_iter((json_t *)json);
+            json_object_t *object;
+            void *iter;
+
+            /* detect circular references */
+            object = json_to_object(json);
+            if(object->visited)
+                return -1;
+            object->visited = 1;
+
+            iter = json_object_iter((json_t *)json);
 
             if(dump("{", 1, data))
                 return -1;
@@ -224,6 +242,8 @@ static int do_dump(const json_t *json, uint32_t flags, int depth,
 
                 iter = next;
             }
+
+            object->visited = 0;
             return dump("}", 1, data);
         }
 
@@ -234,28 +254,26 @@ static int do_dump(const json_t *json, uint32_t flags, int depth,
 }
 
 
-int json_dump(const json_t *json, const char *path, uint32_t flags)
-{
-    FILE *output = fopen(path, "w");
-    if(!output)
-        return -1;
-
-    return json_dumpf(json, output, flags);
-}
-
-char *json_dumps(const json_t *json, uint32_t flags)
+char *json_dumps(const json_t *json, unsigned long flags)
 {
     strbuffer_t strbuff;
     char *result;
 
+    if(!json_is_array(json) && !json_is_object(json))
+        return NULL;
+
     if(strbuffer_init(&strbuff))
-      return NULL;
+        return NULL;
 
-    if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff))
+    if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
+        strbuffer_close(&strbuff);
         return NULL;
+    }
 
-    if(dump_to_strbuffer("\n", 1, (void *)&strbuff))
+    if(dump_to_strbuffer("\n", 1, (void *)&strbuff)) {
+        strbuffer_close(&strbuff);
         return NULL;
+    }
 
     result = strdup(strbuffer_value(&strbuff));
     strbuffer_close(&strbuff);
@@ -263,16 +281,26 @@ char *json_dumps(const json_t *json, uint32_t flags)
     return result;
 }
 
-int json_dumpf(const json_t *json, FILE *output, uint32_t flags)
+int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
 {
+    if(!json_is_array(json) && !json_is_object(json))
+        return -1;
+
     if(do_dump(json, flags, 0, dump_to_file, (void *)output))
         return -1;
     return dump_to_file("\n", 1, (void *)output);
 }
 
-int json_dumpfd(const json_t *json, int fd, uint32_t flags)
+int json_dump_file(const json_t *json, const char *path, unsigned long flags)
 {
-    if(do_dump(json, flags, 0, dump_to_fd, (void *)&fd))
+    int result;
+
+    FILE *output = fopen(path, "w");
+    if(!output)
         return -1;
-    return dump_to_fd("\n", 1, (void *)&fd);
+
+    result = json_dumpf(json, output, flags);
+
+    fclose(output);
+    return result;
 }