cleanup specfile for packaging
[profile/ivi/gpsd.git] / json.h
1 /* Structures for JSON parsing using only fixed-extent memory
2  *
3  * This file is Copyright (c) 2010 by the GPSD project
4  * BSD terms apply: see the file COPYING in the distribution root for details.
5  */
6
7 #include <stdbool.h>
8 #include <ctype.h>
9
10 typedef enum {t_integer, t_uinteger, t_real,
11               t_string, t_boolean, t_character,
12               t_object, t_structobject, t_array,
13               t_check} json_type;
14
15 #define nullbool        -1      /* not true, not false */
16
17 struct json_enum_t {
18     char        *name;
19     int         value;
20 };
21
22 struct json_array_t { 
23     json_type element_type;
24     union {
25         struct {
26             const struct json_attr_t *subtype;
27             char *base;
28             size_t stride;
29         } objects;
30         struct {
31             char **ptrs;
32             char *store;
33             int storelen;
34         } strings;
35     } arr;
36     int *count, maxlen;
37 };
38
39 struct json_attr_t {
40     char *attribute;
41     json_type type;
42     union {
43         int *integer;
44         unsigned int *uinteger;
45         double *real;
46         char *string;
47         bool *boolean;
48         char *character;
49         struct json_array_t array;
50         size_t offset;
51     } addr;
52     union {
53         int integer;
54         unsigned int uinteger;
55         double real;
56         bool boolean;
57         char character;
58         char *check;
59     } dflt;
60     size_t len;
61     const struct json_enum_t *map;
62     bool nodefault; 
63 };
64
65 #define JSON_ATTR_MAX   31      /* max chars in JSON attribute name */
66 #define JSON_VAL_MAX    120     /* max chars in JSON value part */
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 int json_read_object(const char *, const struct json_attr_t *, 
72                      /*@null@*/const char **);
73 int json_read_array(const char *, const struct json_array_t *, 
74                     /*@null@*/const char **);
75 const /*@observer@*/char *json_error_string(int);
76
77 void json_enable_debug(int, FILE *);
78 #ifdef __cplusplus
79 }
80 #endif
81
82 #define JSON_ERR_OBSTART        1       /* non-WS when expecting object start */
83 #define JSON_ERR_ATTRSTART      2       /* non-WS when expecting attrib start */
84 #define JSON_ERR_BADATTR        3       /* unknown attribute name */
85 #define JSON_ERR_ATTRLEN        4       /* attribute name too long */
86 #define JSON_ERR_NOARRAY        5       /* saw [ when not expecting array */
87 #define JSON_ERR_NOBRAK         6       /* array element specified, but no [ */
88 #define JSON_ERR_STRLONG        7       /* string value too long */
89 #define JSON_ERR_TOKLONG        8       /* token value too long */
90 #define JSON_ERR_BADTRAIL       9       /* garbage while expecting , or } */
91 #define JSON_ERR_ARRAYSTART     10      /* didn't find expected array start */
92 #define JSON_ERR_OBJARR         11      /* error while parsing object array */
93 #define JSON_ERR_SUBTOOLONG     12      /* too many array elements */
94 #define JSON_ERR_BADSUBTRAIL    13      /* garbage while expecting array comma */
95 #define JSON_ERR_SUBTYPE        14      /* unsupported array element type */
96 #define JSON_ERR_BADSTRING      15      /* error while string parsing */
97 #define JSON_ERR_CHECKFAIL      16      /* check attribute not matched */
98 #define JSON_ERR_NOPARSTR       17      /* can't support strings in parallel arrays */
99 #define JSON_ERR_BADENUM        18      /* invalid enumerated value */
100 #define JSON_ERR_QNONSTRING     19      /* saw quoted value when expecting nonstring */
101 #define JSON_ERR_NONQSTRING     19      /* didn't see quoted value when expecting string */
102 #define JSON_ERR_MISC           20      /* other data conversion error */
103
104 /*
105  * Use the following macros to declare template initializers for structobject 
106  * arrays.  Writing the equivalents out by hand is error-prone.
107  *
108  * STRUCTOBJECT takes a structure name s, and a fieldname f in s. 
109  *
110  * STRUCTARRAY takes the name of a structure array, a pointer to a an 
111  * initializer defining the subobject type, and the address of an integer to
112  * store the length in. 
113  */
114 #define STRUCTOBJECT(s, f)      .addr.offset = offsetof(s, f)
115 #define STRUCTARRAY(a, e, n) \
116         .addr.array.element_type = t_structobject, \
117         .addr.array.arr.objects.subtype = e, \
118         .addr.array.arr.objects.base = (char*)a, \
119         .addr.array.arr.objects.stride = sizeof(a[0]), \
120         .addr.array.count = n, \
121         .addr.array.maxlen = NITEMS(a)
122
123 /* json.h ends here */