Refactor decoder input stream
[jansson.git] / test / bin / json_process.c
1 /*
2  * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <jansson.h>
13
14 static int getenv_int(const char *name)
15 {
16     char *value, *end;
17     long result;
18
19     value = getenv(name);
20     if(!value)
21         return 0;
22
23     result = strtol(value, &end, 10);
24     if(*end != '\0')
25         return 0;
26
27     return (int)result;
28 }
29
30 /* Return a pointer to the first non-whitespace character of str.
31    Modifies str so that all trailing whitespace characters are
32    replaced by '\0'. */
33 static const char *strip(char *str)
34 {
35     size_t length;
36     char *result = str;
37     while(*result && isspace(*result))
38         result++;
39
40     length = strlen(result);
41     if(length == 0)
42         return result;
43
44     while(isspace(result[length - 1]))
45         result[--length] = '\0';
46
47     return result;
48 }
49
50 int main(int argc, char *argv[])
51 {
52     int indent = 0;
53     size_t flags = 0;
54
55     json_t *json;
56     json_error_t error;
57
58     if(argc != 1) {
59         fprintf(stderr, "usage: %s\n", argv[0]);
60         return 2;
61     }
62
63     indent = getenv_int("JSON_INDENT");
64     if(indent < 0 || indent > 255) {
65         fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
66         return 2;
67     }
68
69     if(indent > 0)
70         flags |= JSON_INDENT(indent);
71
72     if(getenv_int("JSON_COMPACT") > 0)
73         flags |= JSON_COMPACT;
74
75     if(getenv_int("JSON_ENSURE_ASCII"))
76         flags |= JSON_ENSURE_ASCII;
77
78     if(getenv_int("JSON_PRESERVE_ORDER"))
79         flags |= JSON_PRESERVE_ORDER;
80
81     if(getenv_int("JSON_SORT_KEYS"))
82         flags |= JSON_SORT_KEYS;
83
84     if(getenv_int("STRIP")) {
85         /* Load to memory, strip leading and trailing whitespace */
86         size_t size = 0, used = 0;
87         char *buffer = NULL;
88
89         while(1) {
90             int count;
91
92             size = (size == 0 ? 128 : size * 2);
93             buffer = realloc(buffer, size);
94             if(!buffer) {
95                 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
96                 return 1;
97             }
98
99             count = fread(buffer + used, 1, size - used, stdin);
100             if(count < size - used) {
101                 buffer[used + count] = '\0';
102                 break;
103             }
104             used += count;
105         }
106
107         json = json_loads(strip(buffer), 0, &error);
108         free(buffer);
109     }
110     else
111         json = json_loadf(stdin, 0, &error);
112
113     if(!json) {
114         fprintf(stderr, "%d %d %d\n%s\n",
115                 error.line, error.column, error.position,
116                 error.text);
117         return 1;
118     }
119
120     json_dumpf(json, stdout, flags);
121     json_decref(json);
122
123     return 0;
124 }