James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_odict.h Interface to Ordered Dictionary |
| 3 | * |
| 4 | * Copyright (C) 2010 - 2015 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | enum odict_type { |
| 8 | ODICT_OBJECT, |
| 9 | ODICT_ARRAY, |
| 10 | ODICT_STRING, |
| 11 | ODICT_INT, |
| 12 | ODICT_DOUBLE, |
| 13 | ODICT_BOOL, |
| 14 | ODICT_NULL, |
| 15 | }; |
| 16 | |
| 17 | struct odict { |
| 18 | struct list lst; |
| 19 | struct hash *ht; |
| 20 | }; |
| 21 | |
| 22 | struct odict_entry { |
| 23 | struct le le, he; |
| 24 | char *key; |
| 25 | union { |
| 26 | struct odict *odict; /* ODICT_OBJECT / ODICT_ARRAY */ |
| 27 | char *str; /* ODICT_STRING */ |
| 28 | int64_t integer; /* ODICT_INT */ |
| 29 | double dbl; /* ODICT_DOUBLE */ |
| 30 | bool boolean; /* ODICT_BOOL */ |
| 31 | } u; |
| 32 | enum odict_type type; |
| 33 | }; |
| 34 | |
| 35 | int odict_alloc(struct odict **op, uint32_t hash_size); |
| 36 | const struct odict_entry *odict_lookup(const struct odict *o, const char *key); |
| 37 | size_t odict_count(const struct odict *o, bool nested); |
| 38 | int odict_debug(struct re_printf *pf, const struct odict *o); |
| 39 | |
| 40 | int odict_entry_add(struct odict *o, const char *key, |
| 41 | int type, ...); |
| 42 | void odict_entry_del(struct odict *o, const char *key); |
| 43 | int odict_entry_debug(struct re_printf *pf, const struct odict_entry *e); |
| 44 | |
| 45 | bool odict_type_iscontainer(enum odict_type type); |
| 46 | bool odict_type_isreal(enum odict_type type); |
| 47 | const char *odict_type_name(enum odict_type type); |
| 48 | |
| 49 | |
| 50 | /* Helpers */ |
| 51 | |
| 52 | const struct odict_entry *odict_get_type(const struct odict *o, |
| 53 | enum odict_type type, const char *key); |
| 54 | const char *odict_string(const struct odict *o, const char *key); |
| 55 | bool odict_get_number(const struct odict *o, uint64_t *num, const char *key); |
| 56 | bool odict_get_boolean(const struct odict *o, bool *value, const char *key); |