xref: /lighttpd1.4/src/array.h (revision 4f8f83ea)
1bcdc6a3bSJan Kneschke #ifndef ARRAY_H
2bcdc6a3bSJan Kneschke #define ARRAY_H
38abd06a7SGlenn Strauss #include "first.h"
422e8b456SStefan Bühler 
5bcdc6a3bSJan Kneschke #include "buffer.h"
6bcdc6a3bSJan Kneschke 
78c7f1dfbSGlenn Strauss struct data_unset; /* declaration */
88c7f1dfbSGlenn Strauss 
98c7f1dfbSGlenn Strauss struct data_methods {
10630bf7c8SJan Kneschke 	struct data_unset *(*copy)(const struct data_unset *src); \
11bcdc6a3bSJan Kneschke 	void (*free)(struct data_unset *p); \
12*4f8f83eaSGlenn Strauss 	void (*insert_dup)(struct data_unset *dst, struct data_unset *src);
138c7f1dfbSGlenn Strauss };
148c7f1dfbSGlenn Strauss 
15b87e8783SGlenn Strauss typedef enum { TYPE_STRING, TYPE_ARRAY, TYPE_INTEGER, TYPE_CONFIG, TYPE_OTHER } data_type_t;
168c7f1dfbSGlenn Strauss #define DATA_UNSET \
17ad9b7e00SGlenn Strauss 	buffer key; \
18ad9b7e00SGlenn Strauss 	const struct data_methods *fn; /* function table */ \
19ad9b7e00SGlenn Strauss 	data_type_t type
20bcdc6a3bSJan Kneschke 
21bcdc6a3bSJan Kneschke typedef struct data_unset {
22bcdc6a3bSJan Kneschke 	DATA_UNSET;
23bcdc6a3bSJan Kneschke } data_unset;
24bcdc6a3bSJan Kneschke 
25bcdc6a3bSJan Kneschke typedef struct {
26bcdc6a3bSJan Kneschke 	data_unset **data;
27e3dc34d1SGlenn Strauss 	data_unset **sorted;
28a762402dSGlenn Strauss 
29b2991c68SGlenn Strauss 	uint32_t used; /* <= INT32_MAX */
30b2991c68SGlenn Strauss 	uint32_t size;
31bcdc6a3bSJan Kneschke } array;
32bcdc6a3bSJan Kneschke 
33bcdc6a3bSJan Kneschke typedef struct {
34bcdc6a3bSJan Kneschke 	DATA_UNSET;
352e0676fdSGlenn Strauss 	int ext; /*(fits in space due to alignment in 64-bit; extends 32-bit)*/
36601c572cSGlenn Strauss 	buffer value;
37bcdc6a3bSJan Kneschke } data_string;
38bcdc6a3bSJan Kneschke 
39dde9df43SGlenn Strauss __attribute_returns_nonnull__
40*4f8f83eaSGlenn Strauss data_string *array_data_string_init(void);
41bcdc6a3bSJan Kneschke 
42bcdc6a3bSJan Kneschke typedef struct {
43bcdc6a3bSJan Kneschke 	DATA_UNSET;
44bcdc6a3bSJan Kneschke 
45c2238256SGlenn Strauss 	array value;
46bcdc6a3bSJan Kneschke } data_array;
47bcdc6a3bSJan Kneschke 
48dde9df43SGlenn Strauss __attribute_returns_nonnull__
49*4f8f83eaSGlenn Strauss data_array *array_data_array_init(void);
50bcdc6a3bSJan Kneschke 
51bcdc6a3bSJan Kneschke typedef struct {
52bcdc6a3bSJan Kneschke 	DATA_UNSET;
53bcdc6a3bSJan Kneschke 
54bcdc6a3bSJan Kneschke 	int value;
55bcdc6a3bSJan Kneschke } data_integer;
56bcdc6a3bSJan Kneschke 
57dde9df43SGlenn Strauss __attribute_returns_nonnull__
58*4f8f83eaSGlenn Strauss data_integer *array_data_integer_init(void);
59bcdc6a3bSJan Kneschke 
605c0c4936SGlenn Strauss __attribute_returns_nonnull__
6124680a91SGlenn Strauss array *array_init(uint32_t n);
6270b5d729SGlenn Strauss 
6370b5d729SGlenn Strauss __attribute_cold__
64c2238256SGlenn Strauss void array_copy_array(array *dst, const array *src);
65c2238256SGlenn Strauss 
66c2238256SGlenn Strauss __attribute_cold__
67c2238256SGlenn Strauss void array_free_data(array *a);
6870b5d729SGlenn Strauss 
69bcdc6a3bSJan Kneschke void array_free(array *a);
7070b5d729SGlenn Strauss 
71b2991c68SGlenn Strauss __attribute_hot__
72b2991c68SGlenn Strauss void array_reset_data_strings(array *a);
73b2991c68SGlenn Strauss 
74b2991c68SGlenn Strauss __attribute_cold__
75b2991c68SGlenn Strauss void array_insert_unique(array *a, data_unset *entry);
76b2991c68SGlenn Strauss 
77b2991c68SGlenn Strauss __attribute_cold__
78b2991c68SGlenn Strauss data_unset *array_pop(array *a); /* only works on "simple" lists with autogenerated keys */
79b2991c68SGlenn Strauss 
80b2991c68SGlenn Strauss __attribute_cold__
81b2991c68SGlenn Strauss __attribute_pure__
82b2991c68SGlenn Strauss int array_is_vlist(const array *a);
83b2991c68SGlenn Strauss 
84b2991c68SGlenn Strauss __attribute_cold__
85b2991c68SGlenn Strauss __attribute_pure__
86b2991c68SGlenn Strauss int array_is_kvany(const array *a);
87b2991c68SGlenn Strauss 
88b2991c68SGlenn Strauss __attribute_cold__
89b2991c68SGlenn Strauss __attribute_pure__
90b2991c68SGlenn Strauss int array_is_kvarray(const array *a);
91b2991c68SGlenn Strauss 
92b2991c68SGlenn Strauss __attribute_cold__
93b2991c68SGlenn Strauss __attribute_pure__
94b2991c68SGlenn Strauss int array_is_kvstring(const array *a);
95b2991c68SGlenn Strauss 
96b2991c68SGlenn Strauss __attribute_pure__
972e0676fdSGlenn Strauss data_unset *array_get_element_klen_ext(const array *a, int ext, const char *key, uint32_t klen);
982e0676fdSGlenn Strauss 
992e0676fdSGlenn Strauss __attribute_pure__
1000045b9aaSGlenn Strauss const data_unset *array_get_element_klen(const array *a, const char *key, uint32_t klen);
10183535bbeSGlenn Strauss 
10283535bbeSGlenn Strauss __attribute_cold__
10383535bbeSGlenn Strauss __attribute_pure__
10468ec5ad6SGlenn Strauss data_unset *array_get_data_unset(const array *a, const char *key, uint32_t klen);
105b2991c68SGlenn Strauss 
106b2991c68SGlenn Strauss __attribute_cold__
10768ec5ad6SGlenn Strauss data_unset *array_extract_element_klen(array *a, const char *key, uint32_t klen); /* removes found entry from array */
108b2991c68SGlenn Strauss 
1095c0c4936SGlenn Strauss __attribute_returns_nonnull__
11068ec5ad6SGlenn Strauss int * array_get_int_ptr(array *a, const char *k, uint32_t klen);
111b2991c68SGlenn Strauss 
1125c0c4936SGlenn Strauss __attribute_returns_nonnull__
1132e0676fdSGlenn Strauss buffer * array_get_buf_ptr_ext(array *a, int ext, const char *k, uint32_t klen);
1142e0676fdSGlenn Strauss 
1152e0676fdSGlenn Strauss __attribute_returns_nonnull__
11668ec5ad6SGlenn Strauss buffer * array_get_buf_ptr(array *a, const char *k, uint32_t klen);
117b2991c68SGlenn Strauss 
11868ec5ad6SGlenn Strauss void array_insert_value(array *a, const char *v, uint32_t vlen);
119b2991c68SGlenn Strauss 
12068ec5ad6SGlenn Strauss static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen);
121b2991c68SGlenn Strauss 
array_set_key_value(array * const a,const char * const k,const uint32_t klen,const char * const v,const uint32_t vlen)12268ec5ad6SGlenn Strauss static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen) {
123b2991c68SGlenn Strauss     buffer_copy_string_len(array_get_buf_ptr(a, k, klen), v, vlen);
124b2991c68SGlenn Strauss }
125b2991c68SGlenn Strauss 
126b2991c68SGlenn Strauss __attribute_cold__
127b2991c68SGlenn Strauss void array_replace(array *a, data_unset *entry);
128b2991c68SGlenn Strauss 
129b2991c68SGlenn Strauss __attribute_pure__
13068ec5ad6SGlenn Strauss data_unset * array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen);
131b2991c68SGlenn Strauss 
132b2991c68SGlenn Strauss __attribute_pure__
13368ec5ad6SGlenn Strauss data_unset * array_match_key_prefix_nc_klen (const array * const a, const char * const s, const uint32_t slen);
134b2991c68SGlenn Strauss 
135b2991c68SGlenn Strauss __attribute_pure__
136e6741acdSGlenn Strauss data_unset * array_match_key_prefix (const array * const a, const buffer * const b);
137b2991c68SGlenn Strauss 
138b2991c68SGlenn Strauss __attribute_pure__
139e6741acdSGlenn Strauss data_unset * array_match_key_prefix_nc (const array * const a, const buffer * const b);
140b2991c68SGlenn Strauss 
141b2991c68SGlenn Strauss __attribute_pure__
142e6741acdSGlenn Strauss const buffer * array_match_value_prefix (const array * const a, const buffer * const b);
143b2991c68SGlenn Strauss 
144b2991c68SGlenn Strauss __attribute_pure__
145e6741acdSGlenn Strauss const buffer * array_match_value_prefix_nc (const array * const a, const buffer * const b);
146b2991c68SGlenn Strauss 
147b2991c68SGlenn Strauss __attribute_pure__
148e6741acdSGlenn Strauss data_unset * array_match_key_suffix (const array * const a, const buffer * const b);
149b2991c68SGlenn Strauss 
150b2991c68SGlenn Strauss __attribute_pure__
151e6741acdSGlenn Strauss data_unset * array_match_key_suffix_nc (const array * const a, const buffer * const b);
152b2991c68SGlenn Strauss 
153b2991c68SGlenn Strauss __attribute_pure__
154e6741acdSGlenn Strauss const buffer * array_match_value_suffix (const array * const a, const buffer * const b);
155b2991c68SGlenn Strauss 
156b2991c68SGlenn Strauss __attribute_pure__
157e6741acdSGlenn Strauss const buffer * array_match_value_suffix_nc (const array * const a, const buffer * const b);
158b2991c68SGlenn Strauss 
159b2991c68SGlenn Strauss __attribute_pure__
160e6741acdSGlenn Strauss data_unset * array_match_path_or_ext (const array * const a, const buffer * const b);
161e6741acdSGlenn Strauss 
162bcdc6a3bSJan Kneschke #endif
163