Unify unsigned integer usage in the API
[jansson.git] / test / bin / json_process.c
1 /*
2  * Copyright (c) 2009, 2010 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 <jansson.h>
11
12 static int getenv_int(const char *name)
13 {
14     char *value, *end;
15     long result;
16
17     value = getenv(name);
18     if(!value)
19         return 0;
20
21     result = strtol(value, &end, 10);
22     if(*end != '\0')
23         return 0;
24
25     return (int)result;
26 }
27
28 int main(int argc, char *argv[])
29 {
30     int indent = 0;
31     size_t flags = 0;
32
33     json_t *json;
34     json_error_t error;
35
36     if(argc != 1) {
37         fprintf(stderr, "usage: %s\n", argv[0]);
38         return 2;
39     }
40
41     indent = getenv_int("JSON_INDENT");
42     if(indent < 0 || indent > 255) {
43         fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
44         return 2;
45     }
46
47     if(indent > 0)
48         flags |= JSON_INDENT(indent);
49
50     if(getenv_int("JSON_COMPACT") > 0)
51         flags |= JSON_COMPACT;
52
53     if(getenv_int("JSON_ENSURE_ASCII"))
54         flags |= JSON_ENSURE_ASCII;
55
56     if(getenv_int("JSON_PRESERVE_ORDER"))
57         flags |= JSON_PRESERVE_ORDER;
58
59     if(getenv_int("JSON_SORT_KEYS"))
60         flags |= JSON_SORT_KEYS;
61
62     json = json_loadf(stdin, &error);
63     if(!json) {
64         fprintf(stderr, "%d\n%s\n", error.line, error.text);
65         return 1;
66     }
67
68     json_dumpf(json, stdout, flags);
69     json_decref(json);
70
71     return 0;
72 }