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