xref: /unit/src/nxt_conf.h (revision 2139:99d792169ffb)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Valentin V. Bartenev
5  * Copyright (C) NGINX, Inc.
6  */
7 
8 #ifndef _NXT_CONF_INCLUDED_
9 #define _NXT_CONF_INCLUDED_
10 
11 
12 typedef enum {
13     NXT_CONF_NULL = 0,
14     NXT_CONF_BOOLEAN,
15     NXT_CONF_INTEGER,
16     NXT_CONF_NUMBER,
17     NXT_CONF_STRING,
18     NXT_CONF_ARRAY,
19     NXT_CONF_OBJECT,
20 } nxt_conf_type_t;
21 
22 
23 typedef enum {
24     NXT_CONF_OP_OK = 0,
25     NXT_CONF_OP_NOT_FOUND,
26     NXT_CONF_OP_NOT_ALLOWED,
27     NXT_CONF_OP_ERROR,
28 } nxt_conf_op_ret_t;
29 
30 
31 typedef struct nxt_conf_value_s  nxt_conf_value_t;
32 typedef struct nxt_conf_op_s     nxt_conf_op_t;
33 
34 
35 typedef struct {
36     u_char               *pos;
37     u_char               *detail;
38 } nxt_conf_json_error_t;
39 
40 
41 typedef enum {
42     NXT_CONF_MAP_INT8,
43     NXT_CONF_MAP_INT32,
44     NXT_CONF_MAP_INT64,
45     NXT_CONF_MAP_INT,
46     NXT_CONF_MAP_SIZE,
47     NXT_CONF_MAP_OFF,
48     NXT_CONF_MAP_MSEC,
49     NXT_CONF_MAP_DOUBLE,
50     NXT_CONF_MAP_STR,
51     NXT_CONF_MAP_STR_COPY,
52     NXT_CONF_MAP_CSTRZ,
53     NXT_CONF_MAP_PTR,
54 } nxt_conf_map_type_t;
55 
56 
57 typedef struct {
58     nxt_str_t            name;
59     nxt_conf_map_type_t  type;
60     size_t               offset;
61 } nxt_conf_map_t;
62 
63 
64 typedef struct {
65     uint32_t             level;
66     uint8_t              more_space;  /* 1 bit. */
67 } nxt_conf_json_pretty_t;
68 
69 
70 typedef struct {
71     nxt_conf_value_t     *conf;
72     nxt_mp_t             *pool;
73     nxt_str_t            error;
74     void                 *ctx;
75     nxt_mp_t             *conf_pool;
76     nxt_uint_t           ver;
77 } nxt_conf_validation_t;
78 
79 
80 NXT_EXPORT nxt_uint_t nxt_conf_type(nxt_conf_value_t *value);
81 
82 NXT_EXPORT nxt_conf_value_t *nxt_conf_get_path(nxt_conf_value_t *value,
83     nxt_str_t *path);
84 NXT_EXPORT nxt_conf_value_t *nxt_conf_get_object_member(nxt_conf_value_t *value,
85     nxt_str_t *name, uint32_t *index);
86 NXT_EXPORT nxt_conf_value_t *nxt_conf_next_object_member(
87     nxt_conf_value_t *value, nxt_str_t *name, uint32_t *next);
88 NXT_EXPORT nxt_conf_value_t *nxt_conf_get_array_element(nxt_conf_value_t *value,
89     uint32_t index);
90 NXT_EXPORT nxt_conf_value_t *nxt_conf_get_array_element_or_itself(
91     nxt_conf_value_t *value, uint32_t index);
92 
93 NXT_EXPORT nxt_int_t nxt_conf_map_object(nxt_mp_t *mp, nxt_conf_value_t *value,
94     nxt_conf_map_t *map, nxt_uint_t n, void *data);
95 
96 nxt_conf_op_ret_t nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops,
97     nxt_conf_value_t *root, nxt_str_t *path, nxt_conf_value_t *value,
98     nxt_bool_t add);
99 nxt_conf_value_t *nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op,
100     nxt_conf_value_t *value);
101 
102 nxt_conf_value_t *nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end,
103     nxt_conf_json_error_t *error);
104 
105 #define nxt_conf_json_parse_str(mp, str)                                      \
106     nxt_conf_json_parse(mp, (str)->start, (str)->start + (str)->length, NULL)
107 
108 size_t nxt_conf_json_length(nxt_conf_value_t *value,
109     nxt_conf_json_pretty_t *pretty);
110 u_char *nxt_conf_json_print(u_char *p, nxt_conf_value_t *value,
111     nxt_conf_json_pretty_t *pretty);
112 void nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line,
113     nxt_uint_t *column);
114 
115 nxt_int_t nxt_conf_validate(nxt_conf_validation_t *vldt);
116 
117 NXT_EXPORT void nxt_conf_get_string(nxt_conf_value_t *value, nxt_str_t *str);
118 NXT_EXPORT void nxt_conf_set_string(nxt_conf_value_t *value, nxt_str_t *str);
119 NXT_EXPORT nxt_int_t nxt_conf_set_string_dup(nxt_conf_value_t *value,
120     nxt_mp_t *mp, const nxt_str_t *str);
121 NXT_EXPORT double nxt_conf_get_number(nxt_conf_value_t *value);
122 NXT_EXPORT uint8_t nxt_conf_get_boolean(nxt_conf_value_t *value);
123 
124 // FIXME reimplement and reorder functions below
125 NXT_EXPORT nxt_uint_t nxt_conf_object_members_count(nxt_conf_value_t *value);
126 nxt_conf_value_t *nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count);
127 void nxt_conf_set_member(nxt_conf_value_t *object, nxt_str_t *name,
128     const nxt_conf_value_t *value, uint32_t index);
129 void nxt_conf_set_member_string(nxt_conf_value_t *object, nxt_str_t *name,
130     nxt_str_t *value, uint32_t index);
131 nxt_int_t nxt_conf_set_member_string_dup(nxt_conf_value_t *object, nxt_mp_t *mp,
132     nxt_str_t *name, nxt_str_t *value, uint32_t index);
133 void nxt_conf_set_member_integer(nxt_conf_value_t *object, nxt_str_t *name,
134     int64_t value, uint32_t index);
135 void nxt_conf_set_member_null(nxt_conf_value_t *object, nxt_str_t *name,
136     uint32_t index);
137 
138 nxt_conf_value_t *nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count);
139 void nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index,
140     const nxt_conf_value_t *value);
141 nxt_int_t nxt_conf_set_element_string_dup(nxt_conf_value_t *array, nxt_mp_t *mp,
142     nxt_uint_t index, nxt_str_t *value);
143 NXT_EXPORT nxt_uint_t nxt_conf_array_elements_count(nxt_conf_value_t *value);
144 NXT_EXPORT nxt_uint_t nxt_conf_array_elements_count_or_1(
145     nxt_conf_value_t *value);
146 void nxt_conf_array_qsort(nxt_conf_value_t *value,
147     int (*compare)(const void *, const void *));
148 
149 
150 #endif /* _NXT_CONF_INCLUDED_ */
151