apr_tables.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 apr_tables.h
  20. * @brief APR Table library
  21. */
  22. #include "apr.h"
  23. #include "apr_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 apr_tables Table and Array Functions
  32. * @ingroup APR
  33. * Arrays are used to store data which is referenced sequentially or
  34. * as a stack. Functions are provided to push and pop individual
  35. * elements as well as to operate on the entire array.
  36. *
  37. * Tables are used to store data which can be referenced by key.
  38. * Limited capabilities are provided for tables with multiple elements
  39. * which share a key; while key lookup will return only a single
  40. * element, iteration is available. Additionally, a table can be
  41. * compressed to resolve duplicates.
  42. *
  43. * Both arrays and tables may store string or binary data; some features,
  44. * such as concatenation or merging of elements, work only for string
  45. * data.
  46. * @{
  47. */
  48. /** the table abstract data type */
  49. typedef struct apr_table_t apr_table_t;
  50. /** @see apr_array_header_t */
  51. typedef struct apr_array_header_t apr_array_header_t;
  52. /** An opaque array type */
  53. struct apr_array_header_t {
  54. /** The pool the array is allocated out of */
  55. apr_pool_t *pool;
  56. /** The amount of memory allocated for each element of the array */
  57. int elt_size;
  58. /** The number of active elements in the array */
  59. int nelts;
  60. /** The number of elements allocated in the array */
  61. int nalloc;
  62. /** The elements in the array */
  63. char *elts;
  64. };
  65. /**
  66. * The (opaque) structure for string-content tables.
  67. */
  68. typedef struct apr_table_entry_t apr_table_entry_t;
  69. /** The type for each entry in a string-content table */
  70. struct apr_table_entry_t {
  71. /** The key for the current table entry */
  72. char *key; /* maybe NULL in future;
  73. * check when iterating thru table_elts
  74. */
  75. /** The value for the current table entry */
  76. char *val;
  77. /** A checksum for the key, for use by the apr_table internals */
  78. apr_uint32_t key_checksum;
  79. };
  80. /**
  81. * Get the elements from a table.
  82. * @param t The table
  83. * @return An array containing the contents of the table
  84. */
  85. APR_DECLARE(const apr_array_header_t *) apr_table_elts(const apr_table_t *t);
  86. /**
  87. * Determine if the table is empty (either NULL or having no elements).
  88. * @param t The table to check
  89. * @return True if empty, False otherwise
  90. */
  91. APR_DECLARE(int) apr_is_empty_table(const apr_table_t *t);
  92. /**
  93. * Determine if the array is empty (either NULL or having no elements).
  94. * @param a The array to check
  95. * @return True if empty, False otherwise
  96. */
  97. APR_DECLARE(int) apr_is_empty_array(const apr_array_header_t *a);
  98. /**
  99. * Create an array.
  100. * @param p The pool to allocate the memory out of
  101. * @param nelts the number of elements in the initial array
  102. * @param elt_size The size of each element in the array.
  103. * @return The new array
  104. */
  105. APR_DECLARE(apr_array_header_t *) apr_array_make(apr_pool_t *p,
  106. int nelts, int elt_size);
  107. /**
  108. * Add a new element to an array (as a first-in, last-out stack).
  109. * @param arr The array to add an element to.
  110. * @return Location for the new element in the array.
  111. * @remark If there are no free spots in the array, then this function will
  112. * allocate new space for the new element.
  113. */
  114. APR_DECLARE(void *) apr_array_push(apr_array_header_t *arr);
  115. /** A helper macro for accessing a member of an APR array.
  116. *
  117. * @param ary the array
  118. * @param i the index into the array to return
  119. * @param type the type of the objects stored in the array
  120. *
  121. * @return the item at index i
  122. */
  123. #define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
  124. /** A helper macro for pushing elements into an APR array.
  125. *
  126. * @param ary the array
  127. * @param type the type of the objects stored in the array
  128. *
  129. * @return the location where the new object should be placed
  130. */
  131. #define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary)))
  132. /**
  133. * Remove an element from an array (as a first-in, last-out stack).
  134. * @param arr The array to remove an element from.
  135. * @return Location of the element in the array.
  136. * @remark If there are no elements in the array, NULL is returned.
  137. */
  138. APR_DECLARE(void *) apr_array_pop(apr_array_header_t *arr);
  139. /**
  140. * Remove all elements from an array.
  141. * @param arr The array to remove all elements from.
  142. * @remark As the underlying storage is allocated from a pool, no
  143. * memory is freed by this operation, but is available for reuse.
  144. */
  145. APR_DECLARE(void) apr_array_clear(apr_array_header_t *arr);
  146. /**
  147. * Concatenate two arrays together.
  148. * @param dst The destination array, and the one to go first in the combined
  149. * array
  150. * @param src The source array to add to the destination array
  151. */
  152. APR_DECLARE(void) apr_array_cat(apr_array_header_t *dst,
  153. const apr_array_header_t *src);
  154. /**
  155. * Copy the entire array.
  156. * @param p The pool to allocate the copy of the array out of
  157. * @param arr The array to copy
  158. * @return An exact copy of the array passed in
  159. * @remark The alternate apr_array_copy_hdr() copies only the header, and arranges
  160. * for the elements to be copied if (and only if) the code subsequently
  161. * does a push or arraycat.
  162. */
  163. APR_DECLARE(apr_array_header_t *) apr_array_copy(apr_pool_t *p,
  164. const apr_array_header_t *arr);
  165. /**
  166. * Copy the headers of the array, and arrange for the elements to be copied if
  167. * and only if the code subsequently does a push or arraycat.
  168. * @param p The pool to allocate the copy of the array out of
  169. * @param arr The array to copy
  170. * @return An exact copy of the array passed in
  171. * @remark The alternate apr_array_copy() copies the *entire* array.
  172. */
  173. APR_DECLARE(apr_array_header_t *) apr_array_copy_hdr(apr_pool_t *p,
  174. const apr_array_header_t *arr);
  175. /**
  176. * Append one array to the end of another, creating a new array in the process.
  177. * @param p The pool to allocate the new array out of
  178. * @param first The array to put first in the new array.
  179. * @param second The array to put second in the new array.
  180. * @return A new array containing the data from the two arrays passed in.
  181. */
  182. APR_DECLARE(apr_array_header_t *) apr_array_append(apr_pool_t *p,
  183. const apr_array_header_t *first,
  184. const apr_array_header_t *second);
  185. /**
  186. * Generate a new string from the apr_pool_t containing the concatenated
  187. * sequence of substrings referenced as elements within the array. The string
  188. * will be empty if all substrings are empty or null, or if there are no
  189. * elements in the array. If sep is non-NUL, it will be inserted between
  190. * elements as a separator.
  191. * @param p The pool to allocate the string out of
  192. * @param arr The array to generate the string from
  193. * @param sep The separator to use
  194. * @return A string containing all of the data in the array.
  195. */
  196. APR_DECLARE(char *) apr_array_pstrcat(apr_pool_t *p,
  197. const apr_array_header_t *arr,
  198. const char sep);
  199. /**
  200. * Make a new table.
  201. * @param p The pool to allocate the pool out of
  202. * @param nelts The number of elements in the initial table.
  203. * @return The new table.
  204. * @warning This table can only store text data
  205. */
  206. APR_DECLARE(apr_table_t *) apr_table_make(apr_pool_t *p, int nelts);
  207. /**
  208. * Create a new table and copy another table into it.
  209. * @param p The pool to allocate the new table out of
  210. * @param t The table to copy
  211. * @return A copy of the table passed in
  212. * @warning The table keys and respective values are not copied
  213. */
  214. APR_DECLARE(apr_table_t *) apr_table_copy(apr_pool_t *p,
  215. const apr_table_t *t);
  216. /**
  217. * Create a new table whose contents are deep copied from the given
  218. * table. A deep copy operation copies all fields, and makes copies
  219. * of dynamically allocated memory pointed to by the fields.
  220. * @param p The pool to allocate the new table out of
  221. * @param t The table to clone
  222. * @return A deep copy of the table passed in
  223. */
  224. APR_DECLARE(apr_table_t *) apr_table_clone(apr_pool_t *p,
  225. const apr_table_t *t);
  226. /**
  227. * Delete all of the elements from a table.
  228. * @param t The table to clear
  229. */
  230. APR_DECLARE(void) apr_table_clear(apr_table_t *t);
  231. /**
  232. * Get the value associated with a given key from the table. After this call,
  233. * the data is still in the table.
  234. * @param t The table to search for the key
  235. * @param key The key to search for (case does not matter)
  236. * @return The value associated with the key, or NULL if the key does not exist.
  237. */
  238. APR_DECLARE(const char *) apr_table_get(const apr_table_t *t, const char *key);
  239. /**
  240. * Get values associated with a given key from the table. If more than one
  241. * value exists, return a comma separated list of values. After this call, the
  242. * data is still in the table.
  243. * @param p The pool to allocate the combined value from, if necessary
  244. * @param t The table to search for the key
  245. * @param key The key to search for (case does not matter)
  246. * @return The value associated with the key, or NULL if the key does not exist.
  247. */
  248. APR_DECLARE(const char *) apr_table_getm(apr_pool_t *p, const apr_table_t *t,
  249. const char *key);
  250. /**
  251. * Add a key/value pair to a table. If another element already exists with the
  252. * same key, this will overwrite the old data.
  253. * @param t The table to add the data to.
  254. * @param key The key to use (case does not matter)
  255. * @param val The value to add
  256. * @remark When adding data, this function makes a copy of both the key and the
  257. * value.
  258. */
  259. APR_DECLARE(void) apr_table_set(apr_table_t *t, const char *key,
  260. const char *val);
  261. /**
  262. * Add a key/value pair to a table. If another element already exists with the
  263. * same key, this will overwrite the old data.
  264. * @param t The table to add the data to.
  265. * @param key The key to use (case does not matter)
  266. * @param val The value to add
  267. * @warning When adding data, this function does not make a copy of the key or
  268. * the value, so care should be taken to ensure that the values will
  269. * not change after they have been added..
  270. */
  271. APR_DECLARE(void) apr_table_setn(apr_table_t *t, const char *key,
  272. const char *val);
  273. /**
  274. * Remove data from the table.
  275. * @param t The table to remove data from
  276. * @param key The key of the data being removed (case does not matter)
  277. */
  278. APR_DECLARE(void) apr_table_unset(apr_table_t *t, const char *key);
  279. /**
  280. * Add data to a table by merging the value with data that has already been
  281. * stored. The merging is done by concatenating the two values, separated
  282. * by the string ", ".
  283. * @param t The table to search for the data
  284. * @param key The key to merge data for (case does not matter)
  285. * @param val The data to add
  286. * @remark If the key is not found, then this function acts like apr_table_add()
  287. */
  288. APR_DECLARE(void) apr_table_merge(apr_table_t *t, const char *key,
  289. const char *val);
  290. /**
  291. * Add data to a table by merging the value with data that has already been
  292. * stored. The merging is done by concatenating the two values, separated
  293. * by the string ", ".
  294. * @param t The table to search for the data
  295. * @param key The key to merge data for (case does not matter)
  296. * @param val The data to add
  297. * @remark If the key is not found, then this function acts like apr_table_addn()
  298. */
  299. APR_DECLARE(void) apr_table_mergen(apr_table_t *t, const char *key,
  300. const char *val);
  301. /**
  302. * Add data to a table, regardless of whether there is another element with the
  303. * same key.
  304. * @param t The table to add to
  305. * @param key The key to use
  306. * @param val The value to add.
  307. * @remark When adding data, this function makes a copy of both the key and the
  308. * value.
  309. */
  310. APR_DECLARE(void) apr_table_add(apr_table_t *t, const char *key,
  311. const char *val);
  312. /**
  313. * Add data to a table, regardless of whether there is another element with the
  314. * same key.
  315. * @param t The table to add to
  316. * @param key The key to use
  317. * @param val The value to add.
  318. * @remark When adding data, this function does not make a copy of the key or the
  319. * value, so care should be taken to ensure that the values will not
  320. * change after they have been added.
  321. */
  322. APR_DECLARE(void) apr_table_addn(apr_table_t *t, const char *key,
  323. const char *val);
  324. /**
  325. * Merge two tables into one new table.
  326. * @param p The pool to use for the new table
  327. * @param overlay The first table to put in the new table
  328. * @param base The table to add at the end of the new table
  329. * @return A new table containing all of the data from the two passed in
  330. */
  331. APR_DECLARE(apr_table_t *) apr_table_overlay(apr_pool_t *p,
  332. const apr_table_t *overlay,
  333. const apr_table_t *base);
  334. /**
  335. * Declaration prototype for the iterator callback function of apr_table_do()
  336. * and apr_table_vdo().
  337. * @param rec The data passed as the first argument to apr_table_[v]do()
  338. * @param key The key from this iteration of the table
  339. * @param value The value from this iteration of the table
  340. * @remark Iteration continues while this callback function returns non-zero.
  341. * To export the callback function for apr_table_[v]do() it must be declared
  342. * in the _NONSTD convention.
  343. * @see apr_table_do @see apr_table_vdo
  344. */
  345. typedef int (apr_table_do_callback_fn_t)(void *rec, const char *key,
  346. const char *value);
  347. /**
  348. * Iterate over a table running the provided function once for every
  349. * element in the table. The varargs array must be a list of zero or
  350. * more (char *) keys followed by a NULL pointer. If zero keys are
  351. * given, the @param comp function will be invoked for every element
  352. * in the table. Otherwise, the function is invoked only for those
  353. * elements matching the keys specified.
  354. *
  355. * If an invocation of the comp function returns zero,
  356. * iteration will continue using the next specified key, if any.
  357. *
  358. * @param comp The function to run
  359. * @param rec The data to pass as the first argument to the function
  360. * @param t The table to iterate over
  361. * @param ... A varargs array of zero or more (char *) keys followed by NULL
  362. * @return FALSE if one of the comp() iterations returned zero; TRUE if all
  363. * iterations returned non-zero
  364. * @see apr_table_do_callback_fn_t @see apr_table_vdo
  365. */
  366. APR_DECLARE_NONSTD(int) apr_table_do(apr_table_do_callback_fn_t *comp,
  367. void *rec, const apr_table_t *t, ...)
  368. #if defined(__GNUC__) && __GNUC__ >= 4
  369. __attribute__((sentinel))
  370. #endif
  371. ;
  372. /**
  373. * Iterate over a table running the provided function once for every
  374. * element in the table. The vp varargs parameter must be a
  375. * list of zero or more (char *) keys followed by a NULL pointer. If
  376. * zero keys are given, the @param comp function will be invoked for
  377. * every element in the table. Otherwise, the function is invoked
  378. * only for those elements matching the keys specified.
  379. *
  380. * If an invocation of the comp function returns zero,
  381. * iteration will continue using the next specified key, if any.
  382. *
  383. * @param comp The function to run
  384. * @param rec The data to pass as the first argument to the function
  385. * @param t The table to iterate over
  386. * @param vp List of zero or more (char *) keys followed by NULL
  387. * @return FALSE if one of the comp() iterations returned zero; TRUE if all
  388. * iterations returned non-zero
  389. * @see apr_table_do_callback_fn_t @see apr_table_do
  390. */
  391. APR_DECLARE(int) apr_table_vdo(apr_table_do_callback_fn_t *comp,
  392. void *rec, const apr_table_t *t, va_list vp);
  393. /** flag for overlap to use apr_table_setn */
  394. #define APR_OVERLAP_TABLES_SET (0)
  395. /** flag for overlap to use apr_table_mergen */
  396. #define APR_OVERLAP_TABLES_MERGE (1)
  397. /** flag for overlap to use apr_table_addn */
  398. #define APR_OVERLAP_TABLES_ADD (2)
  399. /**
  400. * For each element in table b, either use setn or mergen to add the data
  401. * to table a. Which method is used is determined by the flags passed in.
  402. * @param a The table to add the data to.
  403. * @param b The table to iterate over, adding its data to table a
  404. * @param flags How to add the table to table a. One of:
  405. * APR_OVERLAP_TABLES_SET Use apr_table_setn
  406. * APR_OVERLAP_TABLES_MERGE Use apr_table_mergen
  407. * APR_OVERLAP_TABLES_ADD Use apr_table_addn
  408. * @remark When merging duplicates, the two values are concatenated,
  409. * separated by the string ", ".
  410. * @remark This function is highly optimized, and uses less memory and CPU cycles
  411. * than a function that just loops through table b calling other functions.
  412. */
  413. /**
  414. * Conceptually, apr_table_overlap does this:
  415. *
  416. * <pre>
  417. * apr_array_header_t *barr = apr_table_elts(b);
  418. * apr_table_entry_t *belt = (apr_table_entry_t *)barr->elts;
  419. * int i;
  420. *
  421. * for (i = 0; i < barr->nelts; ++i) {
  422. * if (flags & APR_OVERLAP_TABLES_MERGE) {
  423. * apr_table_mergen(a, belt[i].key, belt[i].val);
  424. * }
  425. * else if (flags & APR_OVERLAP_TABLES_ADD) {
  426. * apr_table_addn(a, belt[i].key, belt[i].val);
  427. * }
  428. * else {
  429. * apr_table_setn(a, belt[i].key, belt[i].val);
  430. * }
  431. * }
  432. * </pre>
  433. *
  434. * Except that it is more efficient (less space and cpu-time) especially
  435. * when b has many elements.
  436. *
  437. * Notice the assumptions on the keys and values in b -- they must be
  438. * in an ancestor of a's pool. In practice b and a are usually from
  439. * the same pool.
  440. */
  441. APR_DECLARE(void) apr_table_overlap(apr_table_t *a, const apr_table_t *b,
  442. unsigned flags);
  443. /**
  444. * Eliminate redundant entries in a table by either overwriting
  445. * or merging duplicates.
  446. *
  447. * @param t Table.
  448. * @param flags APR_OVERLAP_TABLES_MERGE to merge, or
  449. * APR_OVERLAP_TABLES_SET to overwrite, or
  450. * APR_OVERLAP_TABLES_ADD to add
  451. * @remark When merging duplicates, the two values are concatenated,
  452. * separated by the string ", ".
  453. */
  454. APR_DECLARE(void) apr_table_compress(apr_table_t *t, unsigned flags);
  455. /** @} */
  456. #ifdef __cplusplus
  457. }
  458. #endif
  459. #endif /* ! APR_TABLES_H */