2 * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
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.
23 #include <io.h> /* for _setmode() */
24 #include <fcntl.h> /* for _O_BINARY */
27 #define l_isspace(c) ((c) == ' ' || (c) == '\n' || (c) == '\r' || (c) == '\t')
29 static int getenv_int(const char *name)
38 result = strtol(value, &end, 10);
45 /* Return a pointer to the first non-whitespace character of str.
46 Modifies str so that all trailing whitespace characters are
48 static const char *strip(char *str)
52 while(*result && l_isspace(*result))
55 length = strlen(result);
59 while(l_isspace(result[length - 1]))
60 result[--length] = '\0';
65 int main(int argc, char *argv[])
74 setlocale(LC_ALL, "");
78 fprintf(stderr, "usage: %s\n", argv[0]);
83 /* On Windows, set stdout and stderr to binary mode to avoid
84 outputting DOS line terminators */
85 _setmode(_fileno(stdout), _O_BINARY);
86 _setmode(_fileno(stderr), _O_BINARY);
89 indent = getenv_int("JSON_INDENT");
90 if(indent < 0 || indent > 255) {
91 fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
96 flags |= JSON_INDENT(indent);
98 if(getenv_int("JSON_COMPACT") > 0)
99 flags |= JSON_COMPACT;
101 if(getenv_int("JSON_ENSURE_ASCII"))
102 flags |= JSON_ENSURE_ASCII;
104 if(getenv_int("JSON_PRESERVE_ORDER"))
105 flags |= JSON_PRESERVE_ORDER;
107 if(getenv_int("JSON_SORT_KEYS"))
108 flags |= JSON_SORT_KEYS;
110 if(getenv_int("STRIP")) {
111 /* Load to memory, strip leading and trailing whitespace */
112 size_t size = 0, used = 0;
118 size = (size == 0 ? 128 : size * 2);
119 buffer = realloc(buffer, size);
121 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
125 count = fread(buffer + used, 1, size - used, stdin);
126 if(count < size - used) {
127 buffer[used + count] = '\0';
133 json = json_loads(strip(buffer), 0, &error);
137 json = json_loadf(stdin, 0, &error);
140 fprintf(stderr, "%d %d %d\n%s\n",
141 error.line, error.column, error.position,
146 json_dumpf(json, stdout, flags);