2
0

fspr_tables.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_TABLES_H
  17. #define APR_TABLES_H
  18. /**
  19. * @file fspr_tables.h
  20. * @brief APR Table library
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #if APR_HAVE_STDARG_H
  25. #include <stdarg.h> /* for va_list */
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. /**
  31. * @defgroup fspr_tables Table and Array Functions
  32. * @ingroup APR
  33. * Tables are used to store entirely opaque structures
  34. * for applications, while Arrays are usually used to
  35. * deal with string lists.
  36. * @{
  37. */
  38. /** the table abstract data type */
  39. typedef struct fspr_table_t fspr_table_t;
  40. /** @see fspr_array_header_t */
  41. typedef struct fspr_array_header_t fspr_array_header_t;
  42. /** An opaque array type */
  43. struct fspr_array_header_t {
  44. /** The pool the array is allocated out of */
  45. fspr_pool_t *pool;
  46. /** The amount of memory allocated for each element of the array */
  47. int elt_size;
  48. /** The number of active elements in the array */
  49. int nelts;
  50. /** The number of elements allocated in the array */
  51. int nalloc;
  52. /** The elements in the array */
  53. char *elts;
  54. };
  55. /**
  56. * The (opaque) structure for string-content tables.
  57. */
  58. typedef struct fspr_table_entry_t fspr_table_entry_t;
  59. /** The type for each entry in a string-content table */
  60. struct fspr_table_entry_t {
  61. /** The key for the current table entry */
  62. char *key; /* maybe NULL in future;
  63. * check when iterating thru table_elts
  64. */
  65. /** The value for the current table entry */
  66. char *val;
  67. /** A checksum for the key, for use by the fspr_table internals */
  68. fspr_uint32_t key_checksum;
  69. };
  70. /**
  71. * Get the elements from a table
  72. * @param t The table
  73. * @return An array containing the contents of the table
  74. */
  75. APR_DECLARE(const fspr_array_header_t *) fspr_table_elts(const fspr_table_t *t);
  76. /**
  77. * Determine if the table is empty
  78. * @param t The table to check
  79. * @return True if empty, False otherwise
  80. */
  81. APR_DECLARE(int) fspr_is_empty_table(const fspr_table_t *t);
  82. /**
  83. * Determine if the array is empty
  84. * @param a The array to check
  85. * @return True if empty, False otherwise
  86. */
  87. APR_DECLARE(int) fspr_is_empty_array(const fspr_array_header_t *a);
  88. /**
  89. * Create an array
  90. * @param p The pool to allocate the memory out of
  91. * @param nelts the number of elements in the initial array
  92. * @param elt_size The size of each element in the array.
  93. * @return The new array
  94. */
  95. APR_DECLARE(fspr_array_header_t *) fspr_array_make(fspr_pool_t *p,
  96. int nelts, int elt_size);
  97. /**
  98. * Add a new element to an array (as a first-in, last-out stack)
  99. * @param arr The array to add an element to.
  100. * @return Location for the new element in the array.
  101. * @remark If there are no free spots in the array, then this function will
  102. * allocate new space for the new element.
  103. */
  104. APR_DECLARE(void *) fspr_array_push(fspr_array_header_t *arr);
  105. /** A helper macro for accessing a member of an APR array.
  106. *
  107. * @param ary the array
  108. * @param i the index into the array to return
  109. * @param type the type of the objects stored in the array
  110. *
  111. * @return the item at index i
  112. */
  113. #define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
  114. /** A helper macro for pushing elements into an APR array.
  115. *
  116. * @param ary the array
  117. * @param type the type of the objects stored in the array
  118. *
  119. * @return the location where the new object should be placed
  120. */
  121. #define APR_ARRAY_PUSH(ary,type) (*((type *)fspr_array_push(ary)))
  122. /**
  123. * Remove an element from an array (as a first-in, last-out stack)
  124. * @param arr The array to remove an element from.
  125. * @return Location of the element in the array.
  126. * @remark If there are no elements in the array, NULL is returned.
  127. */
  128. APR_DECLARE(void *) fspr_array_pop(fspr_array_header_t *arr);
  129. /**
  130. * Remove all elements from an array.
  131. * @param arr The array to remove all elements from.
  132. * @remark As the underlying storage is allocated from a pool, no
  133. * memory is freed by this operation, but is available for reuse.
  134. */
  135. APR_DECLARE(void) fspr_array_clear(fspr_array_header_t *arr);
  136. /**
  137. * Concatenate two arrays together
  138. * @param dst The destination array, and the one to go first in the combined
  139. * array
  140. * @param src The source array to add to the destination array
  141. */
  142. APR_DECLARE(void) fspr_array_cat(fspr_array_header_t *dst,
  143. const fspr_array_header_t *src);
  144. /**
  145. * Copy the entire array
  146. * @param p The pool to allocate the copy of the array out of
  147. * @param arr The array to copy
  148. * @return An exact copy of the array passed in
  149. * @remark The alternate fspr_array_copy_hdr copies only the header, and arranges
  150. * for the elements to be copied if (and only if) the code subsequently
  151. * does a push or arraycat.
  152. */
  153. APR_DECLARE(fspr_array_header_t *) fspr_array_copy(fspr_pool_t *p,
  154. const fspr_array_header_t *arr);
  155. /**
  156. * Copy the headers of the array, and arrange for the elements to be copied if
  157. * and only if the code subsequently does a push or arraycat.
  158. * @param p The pool to allocate the copy of the array out of
  159. * @param arr The array to copy
  160. * @return An exact copy of the array passed in
  161. * @remark The alternate fspr_array_copy copies the *entire* array.
  162. */
  163. APR_DECLARE(fspr_array_header_t *) fspr_array_copy_hdr(fspr_pool_t *p,
  164. const fspr_array_header_t *arr);
  165. /**
  166. * Append one array to the end of another, creating a new array in the process.
  167. * @param p The pool to allocate the new array out of
  168. * @param first The array to put first in the new array.
  169. * @param second The array to put second in the new array.
  170. * @return A new array containing the data from the two arrays passed in.
  171. */
  172. APR_DECLARE(fspr_array_header_t *) fspr_array_append(fspr_pool_t *p,
  173. const fspr_array_header_t *first,
  174. const fspr_array_header_t *second);
  175. /**
  176. * Generates a new string from the fspr_pool_t containing the concatenated
  177. * sequence of substrings referenced as elements within the array. The string
  178. * will be empty if all substrings are empty or null, or if there are no
  179. * elements in the array. If sep is non-NUL, it will be inserted between
  180. * elements as a separator.
  181. * @param p The pool to allocate the string out of
  182. * @param arr The array to generate the string from
  183. * @param sep The separator to use
  184. * @return A string containing all of the data in the array.
  185. */
  186. APR_DECLARE(char *) fspr_array_pstrcat(fspr_pool_t *p,
  187. const fspr_array_header_t *arr,
  188. const char sep);
  189. /**
  190. * Make a new table
  191. * @param p The pool to allocate the pool out of
  192. * @param nelts The number of elements in the initial table.
  193. * @return The new table.
  194. * @warning This table can only store text data
  195. */
  196. APR_DECLARE(fspr_table_t *) fspr_table_make(fspr_pool_t *p, int nelts);
  197. /**
  198. * Create a new table and copy another table into it
  199. * @param p The pool to allocate the new table out of
  200. * @param t The table to copy
  201. * @return A copy of the table passed in
  202. */
  203. APR_DECLARE(fspr_table_t *) fspr_table_copy(fspr_pool_t *p,
  204. const fspr_table_t *t);
  205. /**
  206. * Delete all of the elements from a table
  207. * @param t The table to clear
  208. */
  209. APR_DECLARE(void) fspr_table_clear(fspr_table_t *t);
  210. /**
  211. * Get the value associated with a given key from the table. After this call,
  212. * The data is still in the table
  213. * @param t The table to search for the key
  214. * @param key The key to search for
  215. * @return The value associated with the key, or NULL if the key does not exist.
  216. */
  217. APR_DECLARE(const char *) fspr_table_get(const fspr_table_t *t, const char *key);
  218. /**
  219. * Add a key/value pair to a table, if another element already exists with the
  220. * same key, this will over-write the old data.
  221. * @param t The table to add the data to.
  222. * @param key The key fo use
  223. * @param val The value to add
  224. * @remark When adding data, this function makes a copy of both the key and the
  225. * value.
  226. */
  227. APR_DECLARE(void) fspr_table_set(fspr_table_t *t, const char *key,
  228. const char *val);
  229. /**
  230. * Add a key/value pair to a table, if another element already exists with the
  231. * same key, this will over-write the old data.
  232. * @param t The table to add the data to.
  233. * @param key The key to use
  234. * @param val The value to add
  235. * @warning When adding data, this function does not make a copy of the key or
  236. * the value, so care should be taken to ensure that the values will
  237. * not change after they have been added..
  238. */
  239. APR_DECLARE(void) fspr_table_setn(fspr_table_t *t, const char *key,
  240. const char *val);
  241. /**
  242. * Remove data from the table
  243. * @param t The table to remove data from
  244. * @param key The key of the data being removed
  245. */
  246. APR_DECLARE(void) fspr_table_unset(fspr_table_t *t, const char *key);
  247. /**
  248. * Add data to a table by merging the value with data that has already been
  249. * stored
  250. * @param t The table to search for the data
  251. * @param key The key to merge data for
  252. * @param val The data to add
  253. * @remark If the key is not found, then this function acts like fspr_table_add
  254. */
  255. APR_DECLARE(void) fspr_table_merge(fspr_table_t *t, const char *key,
  256. const char *val);
  257. /**
  258. * Add data to a table by merging the value with data that has already been
  259. * stored
  260. * @param t The table to search for the data
  261. * @param key The key to merge data for
  262. * @param val The data to add
  263. * @remark If the key is not found, then this function acts like fspr_table_addn
  264. */
  265. APR_DECLARE(void) fspr_table_mergen(fspr_table_t *t, const char *key,
  266. const char *val);
  267. /**
  268. * Add data to a table, regardless of whether there is another element with the
  269. * same key.
  270. * @param t The table to add to
  271. * @param key The key to use
  272. * @param val The value to add.
  273. * @remark When adding data, this function makes a copy of both the key and the
  274. * value.
  275. */
  276. APR_DECLARE(void) fspr_table_add(fspr_table_t *t, const char *key,
  277. const char *val);
  278. /**
  279. * Add data to a table, regardless of whether there is another element with the
  280. * same key.
  281. * @param t The table to add to
  282. * @param key The key to use
  283. * @param val The value to add.
  284. * @remark When adding data, this function does not make a copy of the key or the
  285. * value, so care should be taken to ensure that the values will not
  286. * change after they have been added..
  287. */
  288. APR_DECLARE(void) fspr_table_addn(fspr_table_t *t, const char *key,
  289. const char *val);
  290. /**
  291. * Merge two tables into one new table
  292. * @param p The pool to use for the new table
  293. * @param overlay The first table to put in the new table
  294. * @param base The table to add at the end of the new table
  295. * @return A new table containing all of the data from the two passed in
  296. */
  297. APR_DECLARE(fspr_table_t *) fspr_table_overlay(fspr_pool_t *p,
  298. const fspr_table_t *overlay,
  299. const fspr_table_t *base);
  300. /**
  301. * Declaration prototype for the iterator callback function of fspr_table_do()
  302. * and fspr_table_vdo().
  303. * @param rec The data passed as the first argument to fspr_table_[v]do()
  304. * @param key The key from this iteration of the table
  305. * @param value The value from this iteration of the table
  306. * @remark Iteration continues while this callback function returns non-zero.
  307. * To export the callback function for fspr_table_[v]do() it must be declared
  308. * in the _NONSTD convention.
  309. */
  310. typedef int (fspr_table_do_callback_fn_t)(void *rec, const char *key,
  311. const char *value);
  312. /**
  313. * Iterate over a table running the provided function once for every
  314. * element in the table. If there is data passed in as a vararg, then the
  315. * function is only run on those elements whose key matches something in
  316. * the vararg. If the vararg is NULL, then every element is run through the
  317. * function. Iteration continues while the function returns non-zero.
  318. * @param comp The function to run
  319. * @param rec The data to pass as the first argument to the function
  320. * @param t The table to iterate over
  321. * @param ... The vararg. If this is NULL, then all elements in the table are
  322. * run through the function, otherwise only those whose key matches
  323. * are run.
  324. * @return FALSE if one of the comp() iterations returned zero; TRUE if all
  325. * iterations returned non-zero
  326. * @see fspr_table_do_callback_fn_t
  327. */
  328. APR_DECLARE_NONSTD(int) fspr_table_do(fspr_table_do_callback_fn_t *comp,
  329. void *rec, const fspr_table_t *t, ...);
  330. /**
  331. * Iterate over a table running the provided function once for every
  332. * element in the table. If there is data passed in as a vararg, then the
  333. * function is only run on those element's whose key matches something in
  334. * the vararg. If the vararg is NULL, then every element is run through the
  335. * function. Iteration continues while the function returns non-zero.
  336. * @param comp The function to run
  337. * @param rec The data to pass as the first argument to the function
  338. * @param t The table to iterate over
  339. * @param vp The vararg table. If this is NULL, then all elements in the
  340. * table are run through the function, otherwise only those
  341. * whose key matches are run.
  342. * @return FALSE if one of the comp() iterations returned zero; TRUE if all
  343. * iterations returned non-zero
  344. * @see fspr_table_do_callback_fn_t
  345. */
  346. APR_DECLARE(int) fspr_table_vdo(fspr_table_do_callback_fn_t *comp,
  347. void *rec, const fspr_table_t *t, va_list vp);
  348. /** flag for overlap to use fspr_table_setn */
  349. #define APR_OVERLAP_TABLES_SET (0)
  350. /** flag for overlap to use fspr_table_mergen */
  351. #define APR_OVERLAP_TABLES_MERGE (1)
  352. /**
  353. * For each element in table b, either use setn or mergen to add the data
  354. * to table a. Which method is used is determined by the flags passed in.
  355. * @param a The table to add the data to.
  356. * @param b The table to iterate over, adding its data to table a
  357. * @param flags How to add the table to table a. One of:
  358. * APR_OVERLAP_TABLES_SET Use fspr_table_setn
  359. * APR_OVERLAP_TABLES_MERGE Use fspr_table_mergen
  360. * @remark This function is highly optimized, and uses less memory and CPU cycles
  361. * than a function that just loops through table b calling other functions.
  362. */
  363. /**
  364. *<PRE>
  365. * Conceptually, fspr_table_overlap does this:
  366. *
  367. * fspr_array_header_t *barr = fspr_table_elts(b);
  368. * fspr_table_entry_t *belt = (fspr_table_entry_t *)barr->elts;
  369. * int i;
  370. *
  371. * for (i = 0; i < barr->nelts; ++i) {
  372. * if (flags & APR_OVERLAP_TABLES_MERGE) {
  373. * fspr_table_mergen(a, belt[i].key, belt[i].val);
  374. * }
  375. * else {
  376. * fspr_table_setn(a, belt[i].key, belt[i].val);
  377. * }
  378. * }
  379. *
  380. * Except that it is more efficient (less space and cpu-time) especially
  381. * when b has many elements.
  382. *
  383. * Notice the assumptions on the keys and values in b -- they must be
  384. * in an ancestor of a's pool. In practice b and a are usually from
  385. * the same pool.
  386. * </PRE>
  387. */
  388. APR_DECLARE(void) fspr_table_overlap(fspr_table_t *a, const fspr_table_t *b,
  389. unsigned flags);
  390. /**
  391. * Eliminate redundant entries in a table by either overwriting
  392. * or merging duplicates
  393. *
  394. * @param t Table.
  395. * @param flags APR_OVERLAP_TABLES_MERGE to merge, or
  396. * APR_OVERLAP_TABLES_SET to overwrite
  397. */
  398. APR_DECLARE(void) fspr_table_compress(fspr_table_t *t, unsigned flags);
  399. /** @} */
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. #endif /* ! APR_TABLES_H */