summaryrefslogtreecommitdiff
path: root/com32/gplinclude/zzjson/zzjson.h
blob: d4b32e124b15467109c479ec1a801510687b69c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ZZJSON - Copyright (C) 2008 by Ivo van Poorten
 * License: GNU Lesser General Public License version 2.1
 */
#ifndef ZZJSON_H
#define ZZJSON_H

#include <stdlib.h>

/* Version: */

#define ZZJSON_VERSION_MAJOR    1
#define ZZJSON_VERSION_MINOR    1
#define ZZJSON_VERSION_MICRO    0
#define ZZJSON_VERSION_INT      ( 1<<16 | 1<<8 | 0 )
#define ZZJSON_IDENT            "zzjson 1.1.0"

/* Defines: */

#define ZZJSON_ALLOW_EXTRA_COMMA        1
#define ZZJSON_ALLOW_ILLEGAL_ESCAPE     2
#define ZZJSON_ALLOW_CONTROL_CHARS      4
#define ZZJSON_ALLOW_GARBAGE_AT_END     8
#define ZZJSON_ALLOW_COMMENTS           16

#define ZZJSON_VERY_LOOSE               (-1)
#define ZZJSON_VERY_STRICT              0

/* Types: */

/* needed by: pa = parser, pr = printer, f = free, q = query, c = create */
typedef struct ZZJSON_CONFIG {
    int strictness;                                        // pa
    void *ihandle;                                         // pa
    int (*getchar)(void *ihandle);                         // pa
    int (*ungetchar)(int c, void *ihandle);                // pa
    void *(*malloc)(size_t size);                          // pa      c
    void *(*calloc)(size_t nmemb, size_t size);            // pa      c
    void (*free)(void *ptr);                               // pa    f c
    void *(*realloc)(void *ptr, size_t size);              // pa
    void *ehandle;                                         // pa pr   c
    void (*error)(void *ehandle, const char *format, ...); // pa pr   c
    void *ohandle;                                         //    pr
    int (*print)(void *ohandle, const char *format, ...);  //    pr
    int (*putchar)(int c, void *handle);                   //    pr
} ZZJSON_CONFIG;

typedef enum ZZJSON_TYPE {
    ZZJSON_NONE = 0,
    ZZJSON_OBJECT,
    ZZJSON_ARRAY,
    ZZJSON_STRING,
    ZZJSON_NUMBER_NEGINT,
    ZZJSON_NUMBER_POSINT,
    ZZJSON_NUMBER_DOUBLE,
    ZZJSON_NULL,
    ZZJSON_TRUE,
    ZZJSON_FALSE
} ZZJSON_TYPE;

typedef struct ZZJSON {
    ZZJSON_TYPE type;
    union {
        struct {
            char *label;
            struct ZZJSON *val;
        } object;
        struct {
            struct ZZJSON *val;
        } array;
        struct {
            char *string;
        } string;
        struct {
            union {
                unsigned long long ival;
                double             dval;
            } val;
        } number;
    } value;
    struct ZZJSON *next;
} ZZJSON;

/* Functions: */

ZZJSON *zzjson_parse(ZZJSON_CONFIG *config);
void zzjson_free(ZZJSON_CONFIG *config, ZZJSON *zzjson);
int zzjson_print(ZZJSON_CONFIG *config, ZZJSON *zzjson);

ZZJSON *zzjson_object_find_label(ZZJSON *zzjson, char *label);
ZZJSON *zzjson_object_find_labels(ZZJSON *zzjson, ...); // end with , NULL
unsigned int zzjson_object_count(ZZJSON *zzjson);
unsigned int zzjson_array_count(ZZJSON *zzjson);

ZZJSON *zzjson_create_true(ZZJSON_CONFIG *config);
ZZJSON *zzjson_create_false(ZZJSON_CONFIG *config);
ZZJSON *zzjson_create_null(ZZJSON_CONFIG *config);
ZZJSON *zzjson_create_number_d(ZZJSON_CONFIG *config, double d);
ZZJSON *zzjson_create_number_i(ZZJSON_CONFIG *config, long long i);
ZZJSON *zzjson_create_string(ZZJSON_CONFIG *config, char *s);

/* list of ZZJSON *'s and end with , NULL */
ZZJSON *zzjson_create_array(ZZJSON_CONFIG *config, ...);

/* list of char*,ZZJSON* pairs, end with , NULL */
ZZJSON *zzjson_create_object(ZZJSON_CONFIG *config, ...);

ZZJSON *zzjson_array_prepend(ZZJSON_CONFIG *config, ZZJSON *array,
                                                    ZZJSON *val);
ZZJSON *zzjson_array_append (ZZJSON_CONFIG *config, ZZJSON *array,
                                                    ZZJSON *val);

ZZJSON *zzjson_object_prepend(ZZJSON_CONFIG *config, ZZJSON *object,
                                        char *label, ZZJSON *val);
ZZJSON *zzjson_object_append (ZZJSON_CONFIG *config, ZZJSON *object,
                                        char *label, ZZJSON *val);
#endif