Fix test file comparisons on MinGW
[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 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <jansson.h>
17
18 #if HAVE_LOCALE_H
19 #include <locale.h>
20 #endif
21
22 #if _WIN32
23 #include <io.h>  /* for _setmode() */
24 #include <fcntl.h>  /* for _O_BINARY */
25 #endif
26
27 static int getenv_int(const char *name)
28 {
29     char *value, *end;
30     long result;
31
32     value = getenv(name);
33     if(!value)
34         return 0;
35
36     result = strtol(value, &end, 10);
37     if(*end != '\0')
38         return 0;
39
40     return (int)result;
41 }
42
43 /* Return a pointer to the first non-whitespace character of str.
44    Modifies str so that all trailing whitespace characters are
45    replaced by '\0'. */
46 static const char *strip(char *str)
47 {
48     size_t length;
49     char *result = str;
50     while(*result && isspace(*result))
51         result++;
52
53     length = strlen(result);
54     if(length == 0)
55         return result;
56
57     while(isspace(result[length - 1]))
58         result[--length] = '\0';
59
60     return result;
61 }
62
63 int main(int argc, char *argv[])
64 {
65     int indent = 0;
66     size_t flags = 0;
67
68     json_t *json;
69     json_error_t error;
70
71 #if HAVE_SETLOCALE
72     setlocale(LC_ALL, "");
73 #endif
74
75     if(argc != 1) {
76         fprintf(stderr, "usage: %s\n", argv[0]);
77         return 2;
78     }
79
80 #ifdef _WIN32
81     /* On Windows, set stdout and stderr to binary mode to avoid
82        outputting DOS line terminators */
83     _setmode(_fileno(stdout), _O_BINARY);
84     _setmode(_fileno(stderr), _O_BINARY);
85 #endif
86
87     indent = getenv_int("JSON_INDENT");
88     if(indent < 0 || indent > 255) {
89         fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
90         return 2;
91     }
92
93     if(indent > 0)
94         flags |= JSON_INDENT(indent);
95
96     if(getenv_int("JSON_COMPACT") > 0)
97         flags |= JSON_COMPACT;
98
99     if(getenv_int("JSON_ENSURE_ASCII"))
100         flags |= JSON_ENSURE_ASCII;
101
102     if(getenv_int("JSON_PRESERVE_ORDER"))
103         flags |= JSON_PRESERVE_ORDER;
104
105     if(getenv_int("JSON_SORT_KEYS"))
106         flags |= JSON_SORT_KEYS;
107
108     if(getenv_int("STRIP")) {
109         /* Load to memory, strip leading and trailing whitespace */
110         size_t size = 0, used = 0;
111         char *buffer = NULL;
112
113         while(1) {
114             int count;
115
116             size = (size == 0 ? 128 : size * 2);
117             buffer = realloc(buffer, size);
118             if(!buffer) {
119                 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
120                 return 1;
121             }
122
123             count = fread(buffer + used, 1, size - used, stdin);
124             if(count < size - used) {
125                 buffer[used + count] = '\0';
126                 break;
127             }
128             used += count;
129         }
130
131         json = json_loads(strip(buffer), 0, &error);
132         free(buffer);
133     }
134     else
135         json = json_loadf(stdin, 0, &error);
136
137     if(!json) {
138         fprintf(stderr, "%d %d %d\n%s\n",
139                 error.line, error.column, error.position,
140                 error.text);
141         return 1;
142     }
143
144     json_dumpf(json, stdout, flags);
145     json_decref(json);
146
147     return 0;
148 }