1 #ifndef ARRAY_H 2 #define ARRAY_H 3 #include "first.h" 4 5 #include "buffer.h" 6 7 struct data_unset; /* declaration */ 8 9 struct data_methods { 10 struct data_unset *(*copy)(const struct data_unset *src); \ 11 void (*free)(struct data_unset *p); \ 12 int (*insert_dup)(struct data_unset *dst, struct data_unset *src); \ 13 void (*print)(const struct data_unset *p, int depth); 14 }; 15 16 typedef enum { TYPE_UNSET, TYPE_STRING, TYPE_OTHER, TYPE_ARRAY, TYPE_INTEGER, TYPE_DONOTUSE, TYPE_CONFIG } data_type_t; 17 #define DATA_UNSET \ 18 buffer *key; \ 19 data_type_t type; \ 20 const struct data_methods *fn /* function table */ 21 22 typedef struct data_unset { 23 DATA_UNSET; 24 } data_unset; 25 26 typedef struct { 27 data_unset **data; 28 29 uint32_t *sorted; 30 31 uint32_t used; /* <= INT32_MAX */ 32 uint32_t size; 33 } array; 34 35 typedef struct { 36 DATA_UNSET; 37 38 buffer *value; 39 } data_string; 40 41 data_string *data_string_init(void); 42 43 typedef struct { 44 DATA_UNSET; 45 46 array *value; 47 } data_array; 48 49 data_array *data_array_init(void); 50 51 typedef struct { 52 DATA_UNSET; 53 54 int value; 55 } data_integer; 56 57 data_integer *data_integer_init(void); 58 59 array *array_init(void); 60 61 __attribute_cold__ 62 array *array_init_array(const array *a); 63 64 void array_free(array *a); 65 66 __attribute_hot__ 67 void array_reset_data_strings(array *a); 68 69 __attribute_cold__ 70 void array_insert_unique(array *a, data_unset *entry); 71 72 __attribute_cold__ 73 data_unset *array_pop(array *a); /* only works on "simple" lists with autogenerated keys */ 74 75 __attribute_cold__ 76 __attribute_pure__ 77 int array_is_vlist(const array *a); 78 79 __attribute_cold__ 80 __attribute_pure__ 81 int array_is_kvany(const array *a); 82 83 __attribute_cold__ 84 __attribute_pure__ 85 int array_is_kvarray(const array *a); 86 87 __attribute_cold__ 88 __attribute_pure__ 89 int array_is_kvstring(const array *a); 90 91 #define array_get_element(a, key) array_get_element_klen((a), (key), sizeof(key)-1) 92 __attribute_pure__ 93 data_unset *array_get_element_klen(const array *a, const char *key, size_t klen); 94 95 __attribute_cold__ 96 data_unset *array_extract_element_klen(array *a, const char *key, size_t klen); /* removes found entry from array */ 97 98 int * array_get_int_ptr(array *a, const char *k, size_t klen); 99 100 __attribute_hot__ 101 buffer * array_get_buf_ptr(array *a, const char *k, size_t klen); 102 103 void array_insert_value(array *a, const char *v, size_t vlen); 104 105 static inline void array_set_key_value(array * const a, const char * const k, const size_t klen, const char * const v, const size_t vlen); 106 107 static inline void array_set_key_value(array * const a, const char * const k, const size_t klen, const char * const v, const size_t vlen) { 108 buffer_copy_string_len(array_get_buf_ptr(a, k, klen), v, vlen); 109 } 110 111 __attribute_cold__ 112 void array_replace(array *a, data_unset *entry); 113 114 __attribute_cold__ 115 int array_print(const array *a, int depth); 116 117 __attribute_cold__ 118 void array_print_indent(int depth); 119 120 __attribute_cold__ 121 __attribute_pure__ 122 size_t array_get_max_key_length(const array *a); 123 124 __attribute_pure__ 125 data_unset * array_match_key_prefix_klen (const array * const a, const char * const s, const size_t slen); 126 127 __attribute_pure__ 128 data_unset * array_match_key_prefix_nc_klen (const array * const a, const char * const s, const size_t slen); 129 130 __attribute_pure__ 131 data_unset * array_match_key_prefix (const array * const a, const buffer * const b); 132 133 __attribute_pure__ 134 data_unset * array_match_key_prefix_nc (const array * const a, const buffer * const b); 135 136 __attribute_pure__ 137 const buffer * array_match_value_prefix (const array * const a, const buffer * const b); 138 139 __attribute_pure__ 140 const buffer * array_match_value_prefix_nc (const array * const a, const buffer * const b); 141 142 __attribute_pure__ 143 data_unset * array_match_key_suffix (const array * const a, const buffer * const b); 144 145 __attribute_pure__ 146 data_unset * array_match_key_suffix_nc (const array * const a, const buffer * const b); 147 148 __attribute_pure__ 149 const buffer * array_match_value_suffix (const array * const a, const buffer * const b); 150 151 __attribute_pure__ 152 const buffer * array_match_value_suffix_nc (const array * const a, const buffer * const b); 153 154 __attribute_pure__ 155 data_unset * array_match_path_or_ext (const array * const a, const buffer * const b); 156 157 #endif 158