apr_tables.h revision 266735
1290001Sglebius/* Licensed to the Apache Software Foundation (ASF) under one or more
2290001Sglebius * contributor license agreements.  See the NOTICE file distributed with
3290001Sglebius * this work for additional information regarding copyright ownership.
4290001Sglebius * The ASF licenses this file to You under the Apache License, Version 2.0
5290001Sglebius * (the "License"); you may not use this file except in compliance with
6290001Sglebius * the License.  You may obtain a copy of the License at
7290001Sglebius *
8290001Sglebius *     http://www.apache.org/licenses/LICENSE-2.0
9290001Sglebius *
10290001Sglebius * Unless required by applicable law or agreed to in writing, software
11290001Sglebius * distributed under the License is distributed on an "AS IS" BASIS,
12290001Sglebius * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13290001Sglebius * See the License for the specific language governing permissions and
14290001Sglebius * limitations under the License.
15290001Sglebius */
16290001Sglebius
17290001Sglebius#ifndef APR_TABLES_H
18290001Sglebius#define APR_TABLES_H
19290001Sglebius
20290001Sglebius/**
21290001Sglebius * @file apr_tables.h
22290001Sglebius * @brief APR Table library
23290001Sglebius */
24290001Sglebius
25290001Sglebius#include "apr.h"
26290001Sglebius#include "apr_pools.h"
27290001Sglebius
28290001Sglebius#if APR_HAVE_STDARG_H
29290001Sglebius#include <stdarg.h>     /* for va_list */
30290001Sglebius#endif
31290001Sglebius
32290001Sglebius#ifdef __cplusplus
33290001Sglebiusextern "C" {
34290001Sglebius#endif /* __cplusplus */
35290001Sglebius
36290001Sglebius/**
37290001Sglebius * @defgroup apr_tables Table and Array Functions
38290001Sglebius * @ingroup APR
39290001Sglebius * Arrays are used to store data which is referenced sequentially or
40290001Sglebius * as a stack.  Functions are provided to push and pop individual
41290001Sglebius * elements as well as to operate on the entire array.
42290001Sglebius *
43290001Sglebius * Tables are used to store data which can be referenced by key.
44290001Sglebius * Limited capabilities are provided for tables with multiple elements
45290001Sglebius * which share a key; while key lookup will return only a single
46290001Sglebius * element, iteration is available.  Additionally, a table can be
47290001Sglebius * compressed to resolve duplicates.
48290001Sglebius *
49290001Sglebius * Both arrays and tables may store string or binary data; some features,
50290001Sglebius * such as concatenation or merging of elements, work only for string
51290001Sglebius * data.
52290001Sglebius * @{
53290001Sglebius */
54290001Sglebius
55290001Sglebius/** the table abstract data type */
56290001Sglebiustypedef struct apr_table_t apr_table_t;
57290001Sglebius
58290001Sglebius/** @see apr_array_header_t */
59290001Sglebiustypedef struct apr_array_header_t apr_array_header_t;
60290001Sglebius
61290001Sglebius/** An opaque array type */
62290001Sglebiusstruct apr_array_header_t {
63290001Sglebius    /** The pool the array is allocated out of */
64290001Sglebius    apr_pool_t *pool;
65290001Sglebius    /** The amount of memory allocated for each element of the array */
66290001Sglebius    int elt_size;
67290001Sglebius    /** The number of active elements in the array */
68290001Sglebius    int nelts;
69290001Sglebius    /** The number of elements allocated in the array */
70290001Sglebius    int nalloc;
71290001Sglebius    /** The elements in the array */
72290001Sglebius    char *elts;
73290001Sglebius};
74290001Sglebius
75290001Sglebius/**
76290001Sglebius * The (opaque) structure for string-content tables.
77290001Sglebius */
78290001Sglebiustypedef struct apr_table_entry_t apr_table_entry_t;
79290001Sglebius
80290001Sglebius/** The type for each entry in a string-content table */
81290001Sglebiusstruct apr_table_entry_t {
82290001Sglebius    /** The key for the current table entry */
83290001Sglebius    char *key;          /* maybe NULL in future;
84290001Sglebius                         * check when iterating thru table_elts
85290001Sglebius                         */
86290001Sglebius    /** The value for the current table entry */
87290001Sglebius    char *val;
88290001Sglebius
89290001Sglebius    /** A checksum for the key, for use by the apr_table internals */
90290001Sglebius    apr_uint32_t key_checksum;
91290001Sglebius};
92290001Sglebius
93290001Sglebius/**
94290001Sglebius * Get the elements from a table.
95290001Sglebius * @param t The table
96290001Sglebius * @return An array containing the contents of the table
97290001Sglebius */
98290001SglebiusAPR_DECLARE(const apr_array_header_t *) apr_table_elts(const apr_table_t *t);
99290001Sglebius
100290001Sglebius/**
101290001Sglebius * Determine if the table is empty (either NULL or having no elements).
102290001Sglebius * @param t The table to check
103290001Sglebius * @return True if empty, False otherwise
104290001Sglebius */
105290001SglebiusAPR_DECLARE(int) apr_is_empty_table(const apr_table_t *t);
106290001Sglebius
107290001Sglebius/**
108290001Sglebius * Determine if the array is empty (either NULL or having no elements).
109290001Sglebius * @param a The array to check
110290001Sglebius * @return True if empty, False otherwise
111290001Sglebius */
112290001SglebiusAPR_DECLARE(int) apr_is_empty_array(const apr_array_header_t *a);
113290001Sglebius
114290001Sglebius/**
115290001Sglebius * Create an array.
116290001Sglebius * @param p The pool to allocate the memory out of
117290001Sglebius * @param nelts the number of elements in the initial array
118290001Sglebius * @param elt_size The size of each element in the array.
119290001Sglebius * @return The new array
120290001Sglebius */
121290001SglebiusAPR_DECLARE(apr_array_header_t *) apr_array_make(apr_pool_t *p,
122290001Sglebius                                                 int nelts, int elt_size);
123290001Sglebius
124290001Sglebius/**
125290001Sglebius * Add a new element to an array (as a first-in, last-out stack).
126290001Sglebius * @param arr The array to add an element to.
127290001Sglebius * @return Location for the new element in the array.
128290001Sglebius * @remark If there are no free spots in the array, then this function will
129290001Sglebius *         allocate new space for the new element.
130290001Sglebius */
131290001SglebiusAPR_DECLARE(void *) apr_array_push(apr_array_header_t *arr);
132290001Sglebius
133290001Sglebius/** A helper macro for accessing a member of an APR array.
134290001Sglebius *
135290001Sglebius * @param ary the array
136290001Sglebius * @param i the index into the array to return
137290001Sglebius * @param type the type of the objects stored in the array
138290001Sglebius *
139290001Sglebius * @return the item at index i
140290001Sglebius */
141290001Sglebius#define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
142290001Sglebius
143290001Sglebius/** A helper macro for pushing elements into an APR array.
144290001Sglebius *
145290001Sglebius * @param ary the array
146290001Sglebius * @param type the type of the objects stored in the array
147290001Sglebius *
148290001Sglebius * @return the location where the new object should be placed
149290001Sglebius */
150290001Sglebius#define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary)))
151290001Sglebius
152290001Sglebius/**
153290001Sglebius * Remove an element from an array (as a first-in, last-out stack).
154290001Sglebius * @param arr The array to remove an element from.
155290001Sglebius * @return Location of the element in the array.
156290001Sglebius * @remark If there are no elements in the array, NULL is returned.
157290001Sglebius */
158290001SglebiusAPR_DECLARE(void *) apr_array_pop(apr_array_header_t *arr);
159290001Sglebius
160290001Sglebius/**
161290001Sglebius * Remove all elements from an array.
162290001Sglebius * @param arr The array to remove all elements from.
163290001Sglebius * @remark As the underlying storage is allocated from a pool, no
164290001Sglebius * memory is freed by this operation, but is available for reuse.
165290001Sglebius */
166290001SglebiusAPR_DECLARE(void) apr_array_clear(apr_array_header_t *arr);
167290001Sglebius
168290001Sglebius/**
169290001Sglebius * Concatenate two arrays together.
170290001Sglebius * @param dst The destination array, and the one to go first in the combined
171290001Sglebius *            array
172290001Sglebius * @param src The source array to add to the destination array
173290001Sglebius */
174290001SglebiusAPR_DECLARE(void) apr_array_cat(apr_array_header_t *dst,
175290001Sglebius			        const apr_array_header_t *src);
176290001Sglebius
177290001Sglebius/**
178290001Sglebius * Copy the entire array.
179290001Sglebius * @param p The pool to allocate the copy of the array out of
180290001Sglebius * @param arr The array to copy
181290001Sglebius * @return An exact copy of the array passed in
182290001Sglebius * @remark The alternate apr_array_copy_hdr copies only the header, and arranges
183290001Sglebius *         for the elements to be copied if (and only if) the code subsequently
184290001Sglebius *         does a push or arraycat.
185290001Sglebius */
186APR_DECLARE(apr_array_header_t *) apr_array_copy(apr_pool_t *p,
187                                      const apr_array_header_t *arr);
188/**
189 * Copy the headers of the array, and arrange for the elements to be copied if
190 * and only if the code subsequently does a push or arraycat.
191 * @param p The pool to allocate the copy of the array out of
192 * @param arr The array to copy
193 * @return An exact copy of the array passed in
194 * @remark The alternate apr_array_copy copies the *entire* array.
195 */
196APR_DECLARE(apr_array_header_t *) apr_array_copy_hdr(apr_pool_t *p,
197                                      const apr_array_header_t *arr);
198
199/**
200 * Append one array to the end of another, creating a new array in the process.
201 * @param p The pool to allocate the new array out of
202 * @param first The array to put first in the new array.
203 * @param second The array to put second in the new array.
204 * @return A new array containing the data from the two arrays passed in.
205*/
206APR_DECLARE(apr_array_header_t *) apr_array_append(apr_pool_t *p,
207                                      const apr_array_header_t *first,
208                                      const apr_array_header_t *second);
209
210/**
211 * Generate a new string from the apr_pool_t containing the concatenated
212 * sequence of substrings referenced as elements within the array.  The string
213 * will be empty if all substrings are empty or null, or if there are no
214 * elements in the array.  If sep is non-NUL, it will be inserted between
215 * elements as a separator.
216 * @param p The pool to allocate the string out of
217 * @param arr The array to generate the string from
218 * @param sep The separator to use
219 * @return A string containing all of the data in the array.
220 */
221APR_DECLARE(char *) apr_array_pstrcat(apr_pool_t *p,
222				      const apr_array_header_t *arr,
223				      const char sep);
224
225/**
226 * Make a new table.
227 * @param p The pool to allocate the pool out of
228 * @param nelts The number of elements in the initial table.
229 * @return The new table.
230 * @warning This table can only store text data
231 */
232APR_DECLARE(apr_table_t *) apr_table_make(apr_pool_t *p, int nelts);
233
234/**
235 * Create a new table and copy another table into it.
236 * @param p The pool to allocate the new table out of
237 * @param t The table to copy
238 * @return A copy of the table passed in
239 * @warning The table keys and respective values are not copied
240 */
241APR_DECLARE(apr_table_t *) apr_table_copy(apr_pool_t *p,
242                                          const apr_table_t *t);
243
244/**
245 * Create a new table whose contents are deep copied from the given
246 * table. A deep copy operation copies all fields, and makes copies
247 * of dynamically allocated memory pointed to by the fields.
248 * @param p The pool to allocate the new table out of
249 * @param t The table to clone
250 * @return A deep copy of the table passed in
251 */
252APR_DECLARE(apr_table_t *) apr_table_clone(apr_pool_t *p,
253                                           const apr_table_t *t);
254
255/**
256 * Delete all of the elements from a table.
257 * @param t The table to clear
258 */
259APR_DECLARE(void) apr_table_clear(apr_table_t *t);
260
261/**
262 * Get the value associated with a given key from the table.  After this call,
263 * the data is still in the table.
264 * @param t The table to search for the key
265 * @param key The key to search for (case does not matter)
266 * @return The value associated with the key, or NULL if the key does not exist.
267 */
268APR_DECLARE(const char *) apr_table_get(const apr_table_t *t, const char *key);
269
270/**
271 * Get values associated with a given key from the table.      If more than one
272 * value exists, return a comma separated list of values.  After this call, the
273 * data is still in the table.
274 * @param p The pool to allocate the combined value from, if necessary
275 * @param t The table to search for the key
276 * @param key The key to search for (case does not matter)
277 * @return The value associated with the key, or NULL if the key does not exist.
278 */
279APR_DECLARE(const char *) apr_table_getm(apr_pool_t *p, const apr_table_t *t,
280                                         const char *key);
281
282/**
283 * Add a key/value pair to a table.  If another element already exists with the
284 * same key, this will overwrite the old data.
285 * @param t The table to add the data to.
286 * @param key The key to use (case does not matter)
287 * @param val The value to add
288 * @remark When adding data, this function makes a copy of both the key and the
289 *         value.
290 */
291APR_DECLARE(void) apr_table_set(apr_table_t *t, const char *key,
292                                const char *val);
293
294/**
295 * Add a key/value pair to a table.  If another element already exists with the
296 * same key, this will overwrite the old data.
297 * @param t The table to add the data to.
298 * @param key The key to use (case does not matter)
299 * @param val The value to add
300 * @warning When adding data, this function does not make a copy of the key or
301 *          the value, so care should be taken to ensure that the values will
302 *          not change after they have been added..
303 */
304APR_DECLARE(void) apr_table_setn(apr_table_t *t, const char *key,
305                                 const char *val);
306
307/**
308 * Remove data from the table.
309 * @param t The table to remove data from
310 * @param key The key of the data being removed (case does not matter)
311 */
312APR_DECLARE(void) apr_table_unset(apr_table_t *t, const char *key);
313
314/**
315 * Add data to a table by merging the value with data that has already been
316 * stored. The merging is done by concatenating the two values, separated
317 * by the string ", ".
318 * @param t The table to search for the data
319 * @param key The key to merge data for (case does not matter)
320 * @param val The data to add
321 * @remark If the key is not found, then this function acts like apr_table_add
322 */
323APR_DECLARE(void) apr_table_merge(apr_table_t *t, const char *key,
324                                  const char *val);
325
326/**
327 * Add data to a table by merging the value with data that has already been
328 * stored. The merging is done by concatenating the two values, separated
329 * by the string ", ".
330 * @param t The table to search for the data
331 * @param key The key to merge data for (case does not matter)
332 * @param val The data to add
333 * @remark If the key is not found, then this function acts like apr_table_addn
334 */
335APR_DECLARE(void) apr_table_mergen(apr_table_t *t, const char *key,
336                                   const char *val);
337
338/**
339 * Add data to a table, regardless of whether there is another element with the
340 * same key.
341 * @param t The table to add to
342 * @param key The key to use
343 * @param val The value to add.
344 * @remark When adding data, this function makes a copy of both the key and the
345 *         value.
346 */
347APR_DECLARE(void) apr_table_add(apr_table_t *t, const char *key,
348                                const char *val);
349
350/**
351 * Add data to a table, regardless of whether there is another element with the
352 * same key.
353 * @param t The table to add to
354 * @param key The key to use
355 * @param val The value to add.
356 * @remark When adding data, this function does not make a copy of the key or the
357 *         value, so care should be taken to ensure that the values will not
358 *         change after they have been added.
359 */
360APR_DECLARE(void) apr_table_addn(apr_table_t *t, const char *key,
361                                 const char *val);
362
363/**
364 * Merge two tables into one new table.
365 * @param p The pool to use for the new table
366 * @param overlay The first table to put in the new table
367 * @param base The table to add at the end of the new table
368 * @return A new table containing all of the data from the two passed in
369 */
370APR_DECLARE(apr_table_t *) apr_table_overlay(apr_pool_t *p,
371                                             const apr_table_t *overlay,
372                                             const apr_table_t *base);
373
374/**
375 * Declaration prototype for the iterator callback function of apr_table_do()
376 * and apr_table_vdo().
377 * @param rec The data passed as the first argument to apr_table_[v]do()
378 * @param key The key from this iteration of the table
379 * @param value The value from this iteration of the table
380 * @remark Iteration continues while this callback function returns non-zero.
381 * To export the callback function for apr_table_[v]do() it must be declared
382 * in the _NONSTD convention.
383 */
384typedef int (apr_table_do_callback_fn_t)(void *rec, const char *key,
385                                                    const char *value);
386
387/**
388 * Iterate over a table running the provided function once for every
389 * element in the table.  The varargs array must be a list of zero or
390 * more (char *) keys followed by a NULL pointer.  If zero keys are
391 * given, the @param comp function will be invoked for every element
392 * in the table.  Otherwise, the function is invoked only for those
393 * elements matching the keys specified.
394 *
395 * If an invocation of the @param comp function returns zero,
396 * iteration will continue using the next specified key, if any.
397 *
398 * @param comp The function to run
399 * @param rec The data to pass as the first argument to the function
400 * @param t The table to iterate over
401 * @param ... A varargs array of zero or more (char *) keys followed by NULL
402 * @return FALSE if one of the comp() iterations returned zero; TRUE if all
403 *            iterations returned non-zero
404 * @see apr_table_do_callback_fn_t
405 */
406APR_DECLARE_NONSTD(int) apr_table_do(apr_table_do_callback_fn_t *comp,
407                                     void *rec, const apr_table_t *t, ...)
408#if defined(__GNUC__) && __GNUC__ >= 4
409    __attribute__((sentinel))
410#endif
411    ;
412
413/**
414 * Iterate over a table running the provided function once for every
415 * element in the table.  The @param vp varargs parameter must be a
416 * list of zero or more (char *) keys followed by a NULL pointer.  If
417 * zero keys are given, the @param comp function will be invoked for
418 * every element in the table.  Otherwise, the function is invoked
419 * only for those elements matching the keys specified.
420 *
421 * If an invocation of the @param comp function returns zero,
422 * iteration will continue using the next specified key, if any.
423 *
424 * @param comp The function to run
425 * @param rec The data to pass as the first argument to the function
426 * @param t The table to iterate over
427 * @param vp List of zero or more (char *) keys followed by NULL
428 * @return FALSE if one of the comp() iterations returned zero; TRUE if all
429 *            iterations returned non-zero
430 * @see apr_table_do_callback_fn_t
431 */
432APR_DECLARE(int) apr_table_vdo(apr_table_do_callback_fn_t *comp,
433                               void *rec, const apr_table_t *t, va_list vp);
434
435/** flag for overlap to use apr_table_setn */
436#define APR_OVERLAP_TABLES_SET   (0)
437/** flag for overlap to use apr_table_mergen */
438#define APR_OVERLAP_TABLES_MERGE (1)
439/**
440 * For each element in table b, either use setn or mergen to add the data
441 * to table a.  Which method is used is determined by the flags passed in.
442 * @param a The table to add the data to.
443 * @param b The table to iterate over, adding its data to table a
444 * @param flags How to add the table to table a.  One of:
445 *          APR_OVERLAP_TABLES_SET        Use apr_table_setn
446 *          APR_OVERLAP_TABLES_MERGE      Use apr_table_mergen
447 * @remark  When merging duplicates, the two values are concatenated,
448 *          separated by the string ", ".
449 * @remark  This function is highly optimized, and uses less memory and CPU cycles
450 *          than a function that just loops through table b calling other functions.
451 */
452/**
453 * Conceptually, apr_table_overlap does this:
454 *
455 * <pre>
456 *  apr_array_header_t *barr = apr_table_elts(b);
457 *  apr_table_entry_t *belt = (apr_table_entry_t *)barr->elts;
458 *  int i;
459 *
460 *  for (i = 0; i < barr->nelts; ++i) {
461 *      if (flags & APR_OVERLAP_TABLES_MERGE) {
462 *          apr_table_mergen(a, belt[i].key, belt[i].val);
463 *      }
464 *      else {
465 *          apr_table_setn(a, belt[i].key, belt[i].val);
466 *      }
467 *  }
468 * </pre>
469 *
470 *  Except that it is more efficient (less space and cpu-time) especially
471 *  when b has many elements.
472 *
473 *  Notice the assumptions on the keys and values in b -- they must be
474 *  in an ancestor of a's pool.  In practice b and a are usually from
475 *  the same pool.
476 */
477
478APR_DECLARE(void) apr_table_overlap(apr_table_t *a, const apr_table_t *b,
479                                     unsigned flags);
480
481/**
482 * Eliminate redundant entries in a table by either overwriting
483 * or merging duplicates.
484 *
485 * @param t Table.
486 * @param flags APR_OVERLAP_TABLES_MERGE to merge, or
487 *              APR_OVERLAP_TABLES_SET to overwrite
488 * @remark When merging duplicates, the two values are concatenated,
489 *         separated by the string ", ".
490 */
491APR_DECLARE(void) apr_table_compress(apr_table_t *t, unsigned flags);
492
493/** @} */
494
495#ifdef __cplusplus
496}
497#endif
498
499#endif	/* ! APR_TABLES_H */
500