Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-17 09:55:36

0001 #ifndef JV_H
0002 #define JV_H
0003 
0004 #include <stdarg.h>
0005 #include <stdint.h>
0006 #include <stdio.h>
0007 
0008 #ifdef __cplusplus
0009 extern "C" {
0010 #endif
0011 
0012 #if (defined(__GNUC__) && __GNUC__ >= 7) || \
0013     (defined(__clang__) && __clang_major__ >= 10)
0014 # define JQ_FALLTHROUGH __attribute__((fallthrough))
0015 #else
0016 # define JQ_FALLTHROUGH do {} while (0) /* fallthrough */
0017 #endif
0018 
0019 typedef enum {
0020   JV_KIND_INVALID,
0021   JV_KIND_NULL,
0022   JV_KIND_FALSE,
0023   JV_KIND_TRUE,
0024   JV_KIND_NUMBER,
0025   JV_KIND_STRING,
0026   JV_KIND_ARRAY,
0027   JV_KIND_OBJECT
0028 } jv_kind;
0029 
0030 struct jv_refcnt;
0031 
0032 /* All of the fields of this struct are private.
0033    Really. Do not play with them. */
0034 typedef struct {
0035   unsigned char kind_flags;
0036   unsigned char pad_;
0037   unsigned short offset;  /* array offsets */
0038   int size;
0039   union {
0040     struct jv_refcnt* ptr;
0041     double number;
0042   } u;
0043 } jv;
0044 
0045 /*
0046  * All jv_* functions consume (decref) input and produce (incref) output
0047  * Except jv_copy
0048  */
0049 
0050 jv_kind jv_get_kind(jv);
0051 const char* jv_kind_name(jv_kind);
0052 static int jv_is_valid(jv x) { return jv_get_kind(x) != JV_KIND_INVALID; }
0053 
0054 jv jv_copy(jv);
0055 void jv_free(jv);
0056 
0057 int jv_get_refcnt(jv);
0058 
0059 int jv_equal(jv, jv);
0060 int jv_identical(jv, jv);
0061 int jv_contains(jv, jv);
0062 
0063 jv jv_invalid(void);
0064 jv jv_invalid_with_msg(jv);
0065 jv jv_invalid_get_msg(jv);
0066 int jv_invalid_has_msg(jv);
0067 
0068 jv jv_null(void);
0069 jv jv_true(void);
0070 jv jv_false(void);
0071 jv jv_bool(int);
0072 
0073 jv jv_number(double);
0074 jv jv_number_with_literal(const char*);
0075 double jv_number_value(jv);
0076 int jv_is_integer(jv);
0077 
0078 int jv_number_has_literal(jv n);
0079 const char* jv_number_get_literal(jv);
0080 
0081 jv jv_array(void);
0082 jv jv_array_sized(int);
0083 int jv_array_length(jv);
0084 jv jv_array_get(jv, int);
0085 jv jv_array_set(jv, int, jv);
0086 jv jv_array_append(jv, jv);
0087 jv jv_array_concat(jv, jv);
0088 jv jv_array_slice(jv, int, int);
0089 jv jv_array_indexes(jv, jv);
0090 #define jv_array_foreach(a, i, x) \
0091   for (int jv_len__ = jv_array_length(jv_copy(a)), i=0, jv_j__ = 1;     \
0092        jv_j__; jv_j__ = 0)                                              \
0093     for (jv x;                                                          \
0094          i < jv_len__ ?                                                 \
0095            (x = jv_array_get(jv_copy(a), i), 1) : 0;                    \
0096          i++)
0097 
0098 #define JV_ARRAY_1(e) (jv_array_append(jv_array(),e))
0099 #define JV_ARRAY_2(e1,e2) (jv_array_append(JV_ARRAY_1(e1),e2))
0100 #define JV_ARRAY_3(e1,e2,e3) (jv_array_append(JV_ARRAY_2(e1,e2),e3))
0101 #define JV_ARRAY_4(e1,e2,e3,e4) (jv_array_append(JV_ARRAY_3(e1,e2,e3),e4))
0102 #define JV_ARRAY_5(e1,e2,e3,e4,e5) (jv_array_append(JV_ARRAY_4(e1,e2,e3,e4),e5))
0103 #define JV_ARRAY_6(e1,e2,e3,e4,e5,e6) (jv_array_append(JV_ARRAY_5(e1,e2,e3,e4,e5),e6))
0104 #define JV_ARRAY_7(e1,e2,e3,e4,e5,e6,e7) (jv_array_append(JV_ARRAY_6(e1,e2,e3,e4,e5,e6),e7))
0105 #define JV_ARRAY_8(e1,e2,e3,e4,e5,e6,e7,e8) (jv_array_append(JV_ARRAY_7(e1,e2,e3,e4,e5,e6,e7),e8))
0106 #define JV_ARRAY_9(e1,e2,e3,e4,e5,e6,e7,e8,e9) (jv_array_append(JV_ARRAY_8(e1,e2,e3,e4,e5,e6,e7,e8),e9))
0107 #define JV_ARRAY_IDX(_1,_2,_3,_4,_5,_6,_7,_8,_9,NAME,...) NAME
0108 #define JV_ARRAY(...) \
0109   JV_ARRAY_IDX(__VA_ARGS__, JV_ARRAY_9, JV_ARRAY_8, JV_ARRAY_7, JV_ARRAY_6, JV_ARRAY_5, JV_ARRAY_4, JV_ARRAY_3, JV_ARRAY_2, JV_ARRAY_1, dummy)(__VA_ARGS__)
0110 
0111 #ifdef __GNUC__
0112 #define JV_PRINTF_LIKE(fmt_arg_num, args_num) \
0113   __attribute__ ((__format__( __printf__, fmt_arg_num, args_num)))
0114 #define JV_VPRINTF_LIKE(fmt_arg_num) \
0115   __attribute__ ((__format__( __printf__, fmt_arg_num, 0)))
0116 #endif
0117 
0118 
0119 jv jv_string(const char*);
0120 jv jv_string_sized(const char*, int);
0121 jv jv_string_empty(int len);
0122 int jv_string_length_bytes(jv);
0123 int jv_string_length_codepoints(jv);
0124 unsigned long jv_string_hash(jv);
0125 const char* jv_string_value(jv);
0126 jv jv_string_indexes(jv j, jv k);
0127 jv jv_string_slice(jv j, int start, int end);
0128 jv jv_string_concat(jv, jv);
0129 jv jv_string_vfmt(const char*, va_list) JV_VPRINTF_LIKE(1);
0130 jv jv_string_fmt(const char*, ...) JV_PRINTF_LIKE(1, 2);
0131 jv jv_string_append_codepoint(jv a, uint32_t c);
0132 jv jv_string_append_buf(jv a, const char* buf, int len);
0133 jv jv_string_append_str(jv a, const char* str);
0134 jv jv_string_split(jv j, jv sep);
0135 jv jv_string_explode(jv j);
0136 jv jv_string_implode(jv j);
0137 
0138 jv jv_object(void);
0139 jv jv_object_get(jv object, jv key);
0140 int jv_object_has(jv object, jv key);
0141 jv jv_object_set(jv object, jv key, jv value);
0142 jv jv_object_delete(jv object, jv key);
0143 int jv_object_length(jv object);
0144 jv jv_object_merge(jv, jv);
0145 jv jv_object_merge_recursive(jv, jv);
0146 
0147 int jv_object_iter(jv);
0148 int jv_object_iter_next(jv, int);
0149 int jv_object_iter_valid(jv, int);
0150 jv jv_object_iter_key(jv, int);
0151 jv jv_object_iter_value(jv, int);
0152 #define jv_object_foreach(t, k, v)                                      \
0153   for (int jv_i__ = jv_object_iter(t), jv_j__ = 1; jv_j__; jv_j__ = 0)  \
0154     for (jv k, v;                                                       \
0155          jv_object_iter_valid((t), jv_i__) ?                            \
0156            (k = jv_object_iter_key(t, jv_i__),                          \
0157             v = jv_object_iter_value(t, jv_i__),                        \
0158             1)                                                          \
0159            : 0;                                                         \
0160          jv_i__ = jv_object_iter_next(t, jv_i__))                       \
0161 
0162 #define jv_object_keys_foreach(t, k)                                 \
0163   for (int jv_i__ = jv_object_iter(t), jv_j__ = 1; jv_j__; jv_j__ = 0)  \
0164     for (jv k;                                                          \
0165          jv_object_iter_valid((t), jv_i__) ?                            \
0166            (k = jv_object_iter_key(t, jv_i__),                          \
0167             1)                                                          \
0168            : 0;                                                         \
0169          jv_i__ = jv_object_iter_next(t, jv_i__))
0170 
0171 #define JV_OBJECT_1(k1) (jv_object_set(jv_object(),(k1),jv_null()))
0172 #define JV_OBJECT_2(k1,v1) (jv_object_set(jv_object(),(k1),(v1)))
0173 #define JV_OBJECT_3(k1,v1,k2) (jv_object_set(JV_OBJECT_2((k1),(v1)),(k2),jv_null()))
0174 #define JV_OBJECT_4(k1,v1,k2,v2) (jv_object_set(JV_OBJECT_2((k1),(v1)),(k2),(v2)))
0175 #define JV_OBJECT_5(k1,v1,k2,v2,k3) (jv_object_set(JV_OBJECT_4((k1),(v1),(k2),(v2)),(k3),jv_null()))
0176 #define JV_OBJECT_6(k1,v1,k2,v2,k3,v3) (jv_object_set(JV_OBJECT_4((k1),(v1),(k2),(v2)),(k3),(v3)))
0177 #define JV_OBJECT_7(k1,v1,k2,v2,k3,v3,k4) (jv_object_set(JV_OBJECT_6((k1),(v1),(k2),(v2),(k3),(v3)),(k4),jv_null()))
0178 #define JV_OBJECT_8(k1,v1,k2,v2,k3,v3,k4,v4) (jv_object_set(JV_OBJECT_6((k1),(v1),(k2),(v2),(k3),(v3)),(k4),(v4)))
0179 #define JV_OBJECT_9(k1,v1,k2,v2,k3,v3,k4,v4,k5) \
0180     (jv_object_set(JV_OBJECT_8((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4)),(k5),jv_null()))
0181 #define JV_OBJECT_10(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5) \
0182     (jv_object_set(JV_OBJECT_8((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4)),(k5),(v5)))
0183 #define JV_OBJECT_11(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6) \
0184     (jv_object_set(JV_OBJECT_10((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5)),(k6),jv_null()))
0185 #define JV_OBJECT_12(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6) \
0186     (jv_object_set(JV_OBJECT_10((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5)),(k6),(v6)))
0187 #define JV_OBJECT_13(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7) \
0188     (jv_object_set(JV_OBJECT_12((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6)),(k7),jv_null()))
0189 #define JV_OBJECT_14(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7) \
0190     (jv_object_set(JV_OBJECT_12((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6)),(k7),(v7)))
0191 #define JV_OBJECT_15(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8) \
0192     (jv_object_set(JV_OBJECT_14((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6),(k7),(v7)),(k8),jv_null()))
0193 #define JV_OBJECT_16(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8) \
0194     (jv_object_set(JV_OBJECT_14((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6),(k7),(v7)),(k8),(v8)))
0195 #define JV_OBJECT_17(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9) \
0196     (jv_object_set(JV_OBJECT_16((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6),(k7),(v7),(k8),(v8)),(k9),jv_null()))
0197 #define JV_OBJECT_18(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9) \
0198     (jv_object_set(JV_OBJECT_16((k1),(v1),(k2),(v2),(k3),(v3),(k4),(v4),(k5),(v5),(k6),(v6),(k7),(v7),(k8),(v8)),(k9),(v9)))
0199 #define JV_OBJECT_IDX(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,NAME,...) NAME
0200 #define JV_OBJECT(...) \
0201   JV_OBJECT_IDX(__VA_ARGS__, \
0202                 JV_OBJECT_18, JV_OBJECT_17, JV_OBJECT_16, JV_OBJECT_15, \
0203                 JV_OBJECT_14, JV_OBJECT_13, JV_OBJECT_12, JV_OBJECT_11, \
0204                 JV_OBJECT_10, JV_OBJECT_9, JV_OBJECT_8, JV_OBJECT_7,    \
0205                 JV_OBJECT_6, JV_OBJECT_5, JV_OBJECT_4, JV_OBJECT_3,     \
0206                 JV_OBJECT_2, JV_OBJECT_1)(__VA_ARGS__)
0207 
0208 
0209 
0210 int jv_get_refcnt(jv);
0211 
0212 enum jv_print_flags {
0213   JV_PRINT_PRETTY   = 1,
0214   JV_PRINT_ASCII    = 2,
0215   JV_PRINT_COLOR    = 4, JV_PRINT_COLOUR = 4,
0216   JV_PRINT_SORTED   = 8,
0217   JV_PRINT_INVALID  = 16,
0218   JV_PRINT_REFCOUNT = 32,
0219   JV_PRINT_TAB      = 64,
0220   JV_PRINT_ISATTY   = 128,
0221   JV_PRINT_SPACE0   = 256,
0222   JV_PRINT_SPACE1   = 512,
0223   JV_PRINT_SPACE2   = 1024,
0224 };
0225 #define JV_PRINT_INDENT_FLAGS(n) \
0226     ((n) < 0 || (n) > 7 ? JV_PRINT_TAB | JV_PRINT_PRETTY : (n) == 0 ? 0 : (n) << 8 | JV_PRINT_PRETTY)
0227 void jv_dumpf(jv, FILE *f, int flags);
0228 void jv_dump(jv, int flags);
0229 void jv_show(jv, int flags);
0230 jv jv_dump_string(jv, int flags);
0231 char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize);
0232 
0233 enum {
0234   JV_PARSE_SEQ              = 1,
0235   JV_PARSE_STREAMING        = 2,
0236   JV_PARSE_STREAM_ERRORS    = 4,
0237 };
0238 
0239 jv jv_parse(const char* string);
0240 jv jv_parse_sized(const char* string, int length);
0241 jv jv_parse_custom_flags(const char* string, int flags);
0242 
0243 typedef void (*jv_nomem_handler_f)(void *);
0244 void jv_nomem_handler(jv_nomem_handler_f, void *);
0245 
0246 jv jv_load_file(const char *, int);
0247 
0248 typedef struct jv_parser jv_parser;
0249 jv_parser* jv_parser_new(int);
0250 void jv_parser_set_buf(jv_parser*, const char*, int, int);
0251 int jv_parser_remaining(jv_parser*);
0252 jv jv_parser_next(jv_parser*);
0253 void jv_parser_free(jv_parser*);
0254 
0255 jv jv_get(jv, jv);
0256 jv jv_set(jv, jv, jv);
0257 jv jv_has(jv, jv);
0258 jv jv_setpath(jv, jv, jv);
0259 jv jv_getpath(jv, jv);
0260 jv jv_delpaths(jv, jv);
0261 jv jv_keys(jv /*object or array*/);
0262 jv jv_keys_unsorted(jv /*object or array*/);
0263 int jv_cmp(jv, jv);
0264 jv jv_group(jv, jv);
0265 jv jv_sort(jv, jv);
0266 
0267 #ifdef __cplusplus
0268 }
0269 #endif
0270 
0271 #endif
0272 
0273 
0274 /*
0275 
0276 true/false/null:
0277 check kind
0278 
0279 number:
0280 introduce/eliminate jv
0281 to integer
0282 
0283 array:
0284 copy
0285 free
0286 slice
0287 index
0288 update
0289 
0290 updateslice?
0291 
0292 
0293  */