Wed Oct 28 13:31:09 2009

Asterisk developer's documentation


strings.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief String manipulation functions
00021  */
00022 
00023 #ifndef _ASTERISK_STRINGS_H
00024 #define _ASTERISK_STRINGS_H
00025 
00026 /* #define DEBUG_OPAQUE */
00027 
00028 #include <ctype.h>
00029 
00030 #include "asterisk/utils.h"
00031 #include "asterisk/threadstorage.h"
00032 
00033 #if defined(DEBUG_OPAQUE)
00034 #define __AST_STR_USED used2
00035 #define __AST_STR_LEN len2
00036 #define __AST_STR_STR str2
00037 #define __AST_STR_TS ts2
00038 #else
00039 #define __AST_STR_USED used
00040 #define __AST_STR_LEN len
00041 #define __AST_STR_STR str
00042 #define __AST_STR_TS ts
00043 #endif
00044 
00045 /* You may see casts in this header that may seem useless but they ensure this file is C++ clean */
00046 
00047 #define AS_OR(a,b)   (a && ast_str_strlen(a)) ? ast_str_buffer(a) : (b)
00048 
00049 #ifdef AST_DEVMODE
00050 #define ast_strlen_zero(foo)  _ast_strlen_zero(foo, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00051 static force_inline int _ast_strlen_zero(const char *s, const char *file, const char *function, int line)
00052 {
00053    if (!s || (*s == '\0')) {
00054       return 1;
00055    }
00056    if (!strcmp(s, "(null)")) {
00057       ast_log(__LOG_WARNING, file, line, function, "Possible programming error: \"(null)\" is not NULL!\n");
00058    }
00059    return 0;
00060 }
00061 
00062 #else
00063 static force_inline int attribute_pure ast_strlen_zero(const char *s)
00064 {
00065    return (!s || (*s == '\0'));
00066 }
00067 #endif
00068 
00069 /*! \brief returns the equivalent of logic or for strings:
00070  * first one if not empty, otherwise second one.
00071  */
00072 #define S_OR(a, b) ({typeof(&((a)[0])) __x = (a); ast_strlen_zero(__x) ? (b) : __x;})
00073 
00074 /*! \brief returns the equivalent of logic or for strings, with an additional boolean check:
00075  * second one if not empty and first one is true, otherwise third one.
00076  * example: S_COR(usewidget, widget, "<no widget>")
00077  */
00078 #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
00079 
00080 /*!
00081   \brief Gets a pointer to the first non-whitespace character in a string.
00082   \param str the input string
00083   \return a pointer to the first non-whitespace character
00084  */
00085 AST_INLINE_API(
00086 char * attribute_pure ast_skip_blanks(const char *str),
00087 {
00088    while (*str && ((unsigned char) *str) < 33)
00089       str++;
00090    return (char *) str;
00091 }
00092 )
00093 
00094 /*!
00095   \brief Trims trailing whitespace characters from a string.
00096   \param str the input string
00097   \return a pointer to the modified string
00098  */
00099 AST_INLINE_API(
00100 char *ast_trim_blanks(char *str),
00101 {
00102    char *work = str;
00103 
00104    if (work) {
00105       work += strlen(work) - 1;
00106       /* It's tempting to only want to erase after we exit this loop, 
00107          but since ast_trim_blanks *could* receive a constant string
00108          (which we presumably wouldn't have to touch), we shouldn't
00109          actually set anything unless we must, and it's easier just
00110          to set each position to \0 than to keep track of a variable
00111          for it */
00112       while ((work >= str) && ((unsigned char) *work) < 33)
00113          *(work--) = '\0';
00114    }
00115    return str;
00116 }
00117 )
00118 
00119 /*!
00120   \brief Gets a pointer to first whitespace character in a string.
00121   \param str the input string
00122   \return a pointer to the first whitespace character
00123  */
00124 AST_INLINE_API(
00125 char * attribute_pure ast_skip_nonblanks(const char *str),
00126 {
00127    while (*str && ((unsigned char) *str) > 32)
00128       str++;
00129    return (char *) str;
00130 }
00131 )
00132   
00133 /*!
00134   \brief Strip leading/trailing whitespace from a string.
00135   \param s The string to be stripped (will be modified).
00136   \return The stripped string.
00137 
00138   This functions strips all leading and trailing whitespace
00139   characters from the input string, and returns a pointer to
00140   the resulting string. The string is modified in place.
00141 */
00142 AST_INLINE_API(
00143 char *ast_strip(char *s),
00144 {
00145    if ((s = ast_skip_blanks(s))) {
00146       ast_trim_blanks(s);
00147    }
00148    return s;
00149 } 
00150 )
00151 
00152 /*!
00153   \brief Strip leading/trailing whitespace and quotes from a string.
00154   \param s The string to be stripped (will be modified).
00155   \param beg_quotes The list of possible beginning quote characters.
00156   \param end_quotes The list of matching ending quote characters.
00157   \return The stripped string.
00158 
00159   This functions strips all leading and trailing whitespace
00160   characters from the input string, and returns a pointer to
00161   the resulting string. The string is modified in place.
00162 
00163   It can also remove beginning and ending quote (or quote-like)
00164   characters, in matching pairs. If the first character of the
00165   string matches any character in beg_quotes, and the last
00166   character of the string is the matching character in
00167   end_quotes, then they are removed from the string.
00168 
00169   Examples:
00170   \code
00171   ast_strip_quoted(buf, "\"", "\"");
00172   ast_strip_quoted(buf, "'", "'");
00173   ast_strip_quoted(buf, "[{(", "]})");
00174   \endcode
00175  */
00176 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
00177 
00178 /*!
00179   \brief Strip backslash for "escaped" semicolons, 
00180    the string to be stripped (will be modified).
00181   \return The stripped string.
00182  */
00183 char *ast_unescape_semicolon(char *s);
00184 
00185 /*!
00186   \brief Convert some C escape sequences  \verbatim (\b\f\n\r\t) \endverbatim into the
00187    equivalent characters. The string to be converted (will be modified).
00188   \return The converted string.
00189  */
00190 char *ast_unescape_c(char *s);
00191 
00192 /*!
00193   \brief Size-limited null-terminating string copy.
00194   \param dst The destination buffer.
00195   \param src The source string
00196   \param size The size of the destination buffer
00197   \return Nothing.
00198 
00199   This is similar to \a strncpy, with two important differences:
00200     - the destination buffer will \b always be null-terminated
00201     - the destination buffer is not filled with zeros past the copied string length
00202   These differences make it slightly more efficient, and safer to use since it will
00203   not leave the destination buffer unterminated. There is no need to pass an artificially
00204   reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
00205   to be initialized to zeroes prior to calling this function.
00206 */
00207 AST_INLINE_API(
00208 void ast_copy_string(char *dst, const char *src, size_t size),
00209 {
00210    while (*src && size) {
00211       *dst++ = *src++;
00212       size--;
00213    }
00214    if (__builtin_expect(!size, 0))
00215       dst--;
00216    *dst = '\0';
00217 }
00218 )
00219 
00220 /*!
00221   \brief Build a string in a buffer, designed to be called repeatedly
00222   
00223   \note This method is not recommended. New code should use ast_str_*() instead.
00224 
00225   This is a wrapper for snprintf, that properly handles the buffer pointer
00226   and buffer space available.
00227 
00228   \param buffer current position in buffer to place string into (will be updated on return)
00229   \param space remaining space in buffer (will be updated on return)
00230   \param fmt printf-style format string
00231   \retval 0 on success
00232   \retval non-zero on failure.
00233 */
00234 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
00235 
00236 /*!
00237   \brief Build a string in a buffer, designed to be called repeatedly
00238   
00239   This is a wrapper for snprintf, that properly handles the buffer pointer
00240   and buffer space available.
00241 
00242   \return 0 on success, non-zero on failure.
00243   \param buffer current position in buffer to place string into (will be updated on return)
00244   \param space remaining space in buffer (will be updated on return)
00245   \param fmt printf-style format string
00246   \param ap varargs list of arguments for format
00247 */
00248 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));
00249 
00250 /*! 
00251  * \brief Make sure something is true.
00252  * Determine if a string containing a boolean value is "true".
00253  * This function checks to see whether a string passed to it is an indication of an "true" value.  
00254  * It checks to see if the string is "yes", "true", "y", "t", "on" or "1".  
00255  *
00256  * \retval 0 if val is a NULL pointer.
00257  * \retval -1 if "true".
00258  * \retval 0 otherwise.
00259  */
00260 int attribute_pure ast_true(const char *val);
00261 
00262 /*! 
00263  * \brief Make sure something is false.
00264  * Determine if a string containing a boolean value is "false".
00265  * This function checks to see whether a string passed to it is an indication of an "false" value.  
00266  * It checks to see if the string is "no", "false", "n", "f", "off" or "0".  
00267  *
00268  * \retval 0 if val is a NULL pointer.
00269  * \retval -1 if "true".
00270  * \retval 0 otherwise.
00271  */
00272 int attribute_pure ast_false(const char *val);
00273 
00274 /*
00275  *  \brief Join an array of strings into a single string.
00276  * \param s the resulting string buffer
00277  * \param len the length of the result buffer, s
00278  * \param w an array of strings to join.
00279  *
00280  * This function will join all of the strings in the array 'w' into a single
00281  * string.  It will also place a space in the result buffer in between each
00282  * string from 'w'.
00283 */
00284 void ast_join(char *s, size_t len, const char * const w[]);
00285 
00286 /*
00287   \brief Parse a time (integer) string.
00288   \param src String to parse
00289   \param dst Destination
00290   \param _default Value to use if the string does not contain a valid time
00291   \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
00292   \retval 0 on success
00293   \retval non-zero on failure.
00294 */
00295 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
00296 
00297 /*
00298   \brief Parse a time (float) string.
00299   \param src String to parse
00300   \param dst Destination
00301   \param _default Value to use if the string does not contain a valid time
00302   \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
00303   \return zero on success, non-zero on failure
00304 */
00305 int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
00306 
00307 /*!
00308  * Support for dynamic strings.
00309  *
00310  * A dynamic string is just a C string prefixed by a few control fields
00311  * that help setting/appending/extending it using a printf-like syntax.
00312  *
00313  * One should never declare a variable with this type, but only a pointer
00314  * to it, e.g.
00315  *
00316  * struct ast_str *ds;
00317  *
00318  * The pointer can be initialized with the following:
00319  *
00320  * ds = ast_str_create(init_len);
00321  *    creates a malloc()'ed dynamic string;
00322  *
00323  * ds = ast_str_alloca(init_len);
00324  *    creates a string on the stack (not very dynamic!).
00325  *
00326  * ds = ast_str_thread_get(ts, init_len)
00327  *    creates a malloc()'ed dynamic string associated to
00328  *    the thread-local storage key ts
00329  *
00330  * Finally, the string can be manipulated with the following:
00331  *
00332  * ast_str_set(&buf, max_len, fmt, ...)
00333  * ast_str_append(&buf, max_len, fmt, ...)
00334  *
00335  * and their varargs variant
00336  *
00337  * ast_str_set_va(&buf, max_len, ap)
00338  * ast_str_append_va(&buf, max_len, ap)
00339  *
00340  * \param max_len The maximum allowed length, reallocating if needed.
00341  *    0 means unlimited, -1 means "at most the available space"
00342  *
00343  * \return All the functions return <0 in case of error, or the
00344  * length of the string added to the buffer otherwise.
00345  */
00346 
00347 /*! \brief The descriptor of a dynamic string
00348  *  XXX storage will be optimized later if needed
00349  * We use the ts field to indicate the type of storage.
00350  * Three special constants indicate malloc, alloca() or static
00351  * variables, all other values indicate a
00352  * struct ast_threadstorage pointer.
00353  */
00354 struct ast_str {
00355    size_t __AST_STR_LEN;         /*!< The current maximum length of the string */
00356    size_t __AST_STR_USED;        /*!< Amount of space used */
00357    struct ast_threadstorage *__AST_STR_TS;   /*!< What kind of storage is this ? */
00358 #define DS_MALLOC ((struct ast_threadstorage *)1)
00359 #define DS_ALLOCA ((struct ast_threadstorage *)2)
00360 #define DS_STATIC ((struct ast_threadstorage *)3)  /* not supported yet */
00361    char __AST_STR_STR[0];        /*!< The string buffer */
00362 };
00363 
00364 /*!
00365  * \brief Create a malloc'ed dynamic length string
00366  *
00367  * \param init_len This is the initial length of the string buffer
00368  *
00369  * \return This function returns a pointer to the dynamic string length.  The
00370  *         result will be NULL in the case of a memory allocation error.
00371  *
00372  * \note The result of this function is dynamically allocated memory, and must
00373  *       be free()'d after it is no longer needed.
00374  */
00375 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00376 #define  ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00377 AST_INLINE_API(
00378 struct ast_str * attribute_malloc _ast_str_create(size_t init_len,
00379       const char *file, int lineno, const char *func),
00380 {
00381    struct ast_str *buf;
00382 
00383    buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func);
00384    if (buf == NULL)
00385       return NULL;
00386 
00387    buf->__AST_STR_LEN = init_len;
00388    buf->__AST_STR_USED = 0;
00389    buf->__AST_STR_TS = DS_MALLOC;
00390 
00391    return buf;
00392 }
00393 )
00394 #else
00395 AST_INLINE_API(
00396 struct ast_str * attribute_malloc ast_str_create(size_t init_len),
00397 {
00398    struct ast_str *buf;
00399 
00400    buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
00401    if (buf == NULL)
00402       return NULL;
00403 
00404    buf->__AST_STR_LEN = init_len;
00405    buf->__AST_STR_USED = 0;
00406    buf->__AST_STR_TS = DS_MALLOC;
00407 
00408    return buf;
00409 }
00410 )
00411 #endif
00412 
00413 /*! \brief Reset the content of a dynamic string.
00414  * Useful before a series of ast_str_append.
00415  */
00416 AST_INLINE_API(
00417 void ast_str_reset(struct ast_str *buf),
00418 {
00419    if (buf) {
00420       buf->__AST_STR_USED = 0;
00421       if (buf->__AST_STR_LEN) {
00422          buf->__AST_STR_STR[0] = '\0';
00423       }
00424    }
00425 }
00426 )
00427 
00428 /*! \brief Update the length of the buffer, after using ast_str merely as a buffer.
00429  *  \param buf A pointer to the ast_str string.
00430  */
00431 AST_INLINE_API(
00432 void ast_str_update(struct ast_str *buf),
00433 {
00434    buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
00435 }
00436 )
00437 
00438 /*! \brief Trims trailing whitespace characters from an ast_str string.
00439  *  \param buf A pointer to the ast_str string.
00440  */
00441 AST_INLINE_API(
00442 void ast_str_trim_blanks(struct ast_str *buf),
00443 {
00444    if (!buf) {
00445       return;
00446    }
00447    while (buf->__AST_STR_USED && buf->__AST_STR_STR[buf->__AST_STR_USED - 1] < 33) {
00448       buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
00449    }
00450 }
00451 )
00452 
00453 /*!\brief Returns the current length of the string stored within buf.
00454  * \param buf A pointer to the ast_str structure.
00455  */
00456 AST_INLINE_API(
00457 size_t attribute_pure ast_str_strlen(const struct ast_str *buf),
00458 {
00459    return buf->__AST_STR_USED;
00460 }
00461 )
00462 
00463 /*!\brief Returns the current maximum length (without reallocation) of the current buffer.
00464  * \param buf A pointer to the ast_str structure.
00465  * \retval Current maximum length of the buffer.
00466  */
00467 AST_INLINE_API(
00468 size_t attribute_pure ast_str_size(const struct ast_str *buf),
00469 {
00470    return buf->__AST_STR_LEN;
00471 }
00472 )
00473 
00474 /*!\brief Returns the string buffer within the ast_str buf.
00475  * \param buf A pointer to the ast_str structure.
00476  * \retval A pointer to the enclosed string.
00477  */
00478 AST_INLINE_API(
00479 char * attribute_pure ast_str_buffer(const struct ast_str *buf),
00480 {
00481    /* for now, cast away the const qualifier on the pointer
00482     * being returned; eventually, it should become truly const
00483     * and only be modified via accessor functions
00484     */
00485    return (char *) buf->__AST_STR_STR;
00486 }
00487 )
00488 
00489 /*!\brief Truncates the enclosed string to the given length.
00490  * \param buf A pointer to the ast_str structure.
00491  * \param len Maximum length of the string.
00492  * \retval A pointer to the resulting string.
00493  */
00494 AST_INLINE_API(
00495 char *ast_str_truncate(struct ast_str *buf, ssize_t len),
00496 {
00497    if (len < 0) {
00498       buf->__AST_STR_USED += ((ssize_t) abs(len)) > (ssize_t) buf->__AST_STR_USED ? -buf->__AST_STR_USED : len;
00499    } else {
00500       buf->__AST_STR_USED = len;
00501    }
00502    buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
00503    return buf->__AST_STR_STR;
00504 }
00505 )
00506    
00507 /*
00508  * AST_INLINE_API() is a macro that takes a block of code as an argument.
00509  * Using preprocessor #directives in the argument is not supported by all
00510  * compilers, and it is a bit of an obfuscation anyways, so avoid it.
00511  * As a workaround, define a macro that produces either its argument
00512  * or nothing, and use that instead of #ifdef/#endif within the
00513  * argument to AST_INLINE_API().
00514  */
00515 #if defined(DEBUG_THREADLOCALS)
00516 #define  _DB1(x)  x
00517 #else
00518 #define _DB1(x)
00519 #endif
00520 
00521 /*!
00522  * Make space in a new string (e.g. to read in data from a file)
00523  */
00524 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00525 AST_INLINE_API(
00526 int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function),
00527 {
00528    struct ast_str *old_buf = *buf;
00529 
00530    if (new_len <= (*buf)->__AST_STR_LEN) 
00531       return 0;   /* success */
00532    if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
00533       return -1;  /* cannot extend */
00534    *buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
00535    if (*buf == NULL) {
00536       *buf = old_buf;
00537       return -1;
00538    }
00539    if ((*buf)->__AST_STR_TS != DS_MALLOC) {
00540       pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
00541       _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
00542    }
00543 
00544    (*buf)->__AST_STR_LEN = new_len;
00545    return 0;
00546 }
00547 )
00548 #define ast_str_make_space(a,b)  _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00549 #else
00550 AST_INLINE_API(
00551 int ast_str_make_space(struct ast_str **buf, size_t new_len),
00552 {
00553    struct ast_str *old_buf = *buf;
00554 
00555    if (new_len <= (*buf)->__AST_STR_LEN) 
00556       return 0;   /* success */
00557    if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
00558       return -1;  /* cannot extend */
00559    *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
00560    if (*buf == NULL) {
00561       *buf = old_buf;
00562       return -1;
00563    }
00564    if ((*buf)->__AST_STR_TS != DS_MALLOC) {
00565       pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
00566       _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
00567    }
00568 
00569    (*buf)->__AST_STR_LEN = new_len;
00570    return 0;
00571 }
00572 )
00573 #endif
00574 
00575 #define ast_str_alloca(init_len)       \
00576    ({                \
00577       struct ast_str *__ast_str_buf;         \
00578       __ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len);  \
00579       __ast_str_buf->__AST_STR_LEN = init_len;        \
00580       __ast_str_buf->__AST_STR_USED = 0;           \
00581       __ast_str_buf->__AST_STR_TS = DS_ALLOCA;        \
00582       __ast_str_buf->__AST_STR_STR[0] = '\0';         \
00583       (__ast_str_buf);              \
00584    })
00585 
00586 /*!
00587  * \brief Retrieve a thread locally stored dynamic string
00588  *
00589  * \param ts This is a pointer to the thread storage structure declared by using
00590  *      the AST_THREADSTORAGE macro.  If declared with 
00591  *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
00592  *      (&my_buf).
00593  * \param init_len This is the initial length of the thread's dynamic string. The
00594  *      current length may be bigger if previous operations in this thread have
00595  *      caused it to increase.
00596  *
00597  * \return This function will return the thread locally stored dynamic string
00598  *         associated with the thread storage management variable passed as the
00599  *         first argument.
00600  *         The result will be NULL in the case of a memory allocation error.
00601  *
00602  * Example usage:
00603  * \code
00604  * AST_THREADSTORAGE(my_str, my_str_init);
00605  * #define MY_STR_INIT_SIZE   128
00606  * ...
00607  * void my_func(const char *fmt, ...)
00608  * {
00609  *      struct ast_str *buf;
00610  *
00611  *      if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
00612  *           return;
00613  *      ...
00614  * }
00615  * \endcode
00616  */
00617 #if !defined(DEBUG_THREADLOCALS)
00618 AST_INLINE_API(
00619 struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
00620    size_t init_len),
00621 {
00622    struct ast_str *buf;
00623 
00624    buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
00625    if (buf == NULL)
00626       return NULL;
00627 
00628    if (!buf->__AST_STR_LEN) {
00629       buf->__AST_STR_LEN = init_len;
00630       buf->__AST_STR_USED = 0;
00631       buf->__AST_STR_TS = ts;
00632    }
00633 
00634    return buf;
00635 }
00636 )
00637 #else /* defined(DEBUG_THREADLOCALS) */
00638 AST_INLINE_API(
00639 struct ast_str *__ast_str_thread_get(struct ast_threadstorage *ts,
00640    size_t init_len, const char *file, const char *function, unsigned int line),
00641 {
00642    struct ast_str *buf;
00643 
00644    buf = (struct ast_str *)__ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line);
00645    if (buf == NULL)
00646       return NULL;
00647 
00648    if (!buf->__AST_STR_LEN) {
00649       buf->__AST_STR_LEN = init_len;
00650       buf->__AST_STR_USED = 0;
00651       buf->__AST_STR_TS = ts;
00652    }
00653 
00654    return buf;
00655 }
00656 )
00657 
00658 #define ast_str_thread_get(ts, init_len) __ast_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00659 #endif /* defined(DEBUG_THREADLOCALS) */
00660 
00661 /*!
00662  * \brief Error codes from __ast_str_helper()
00663  * The undelying processing to manipulate dynamic string is done
00664  * by __ast_str_helper(), which can return a success or a
00665  * permanent failure (e.g. no memory).
00666  */
00667 enum {
00668    /*! An error has occurred and the contents of the dynamic string
00669     *  are undefined */
00670    AST_DYNSTR_BUILD_FAILED = -1,
00671    /*! The buffer size for the dynamic string had to be increased, and
00672     *  __ast_str_helper() needs to be called again after
00673     *  a va_end() and va_start().  This return value is legacy and will
00674     *  no longer be used.
00675     */
00676    AST_DYNSTR_BUILD_RETRY = -2
00677 };
00678 
00679 /*!
00680  * \brief Core functionality of ast_str_(set|append)_va
00681  *
00682  * The arguments to this function are the same as those described for
00683  * ast_str_set_va except for an addition argument, append.
00684  * If append is non-zero, this will append to the current string instead of
00685  * writing over it.
00686  *
00687  * AST_DYNSTR_BUILD_RETRY is a legacy define.  It should probably never
00688  * again be used.
00689  *
00690  * A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
00691  *
00692  * A return value greater than or equal to zero indicates the number of
00693  * characters that have been written, not including the terminating '\0'.
00694  * In the append case, this only includes the number of characters appended.
00695  *
00696  * \note This function should never need to be called directly.  It should
00697  *       through calling one of the other functions or macros defined in this
00698  *       file.
00699  */
00700 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00701 int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, size_t max_len,
00702                         int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func);
00703 #define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00704 #else
00705 int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, size_t max_len,
00706                         int append, const char *fmt, va_list ap);
00707 #endif
00708 char *__ast_str_helper2(struct ast_str **buf, size_t max_len,
00709    const char *src, size_t maxsrc, int append, int escapecommas);
00710 
00711 /*!
00712  * \brief Set a dynamic string from a va_list
00713  *
00714  * \param buf This is the address of a pointer to a struct ast_str.
00715  * If it is retrieved using ast_str_thread_get, the
00716    struct ast_threadstorage pointer will need to
00717  *      be updated in the case that the buffer has to be reallocated to
00718  *      accommodate a longer string than what it currently has space for.
00719  * \param max_len This is the maximum length to allow the string buffer to grow
00720  *      to.  If this is set to 0, then there is no maximum length.
00721  * \param fmt This is the format string (printf style)
00722  * \param ap This is the va_list
00723  *
00724  * \return The return value of this function is the same as that of the printf
00725  *         family of functions.
00726  *
00727  * Example usage (the first part is only for thread-local storage)
00728  * \code
00729  * AST_THREADSTORAGE(my_str, my_str_init);
00730  * #define MY_STR_INIT_SIZE   128
00731  * ...
00732  * void my_func(const char *fmt, ...)
00733  * {
00734  *      struct ast_str *buf;
00735  *      va_list ap;
00736  *
00737  *      if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
00738  *           return;
00739  *      ...
00740  *      va_start(fmt, ap);
00741  *      ast_str_set_va(&buf, 0, fmt, ap);
00742  *      va_end(ap);
00743  * 
00744  *      printf("This is the string we just built: %s\n", buf->str);
00745  *      ...
00746  * }
00747  * \endcode
00748  */
00749 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_set_va(struct ast_str **buf, size_t max_len, const char *fmt, va_list ap),
00750 {
00751    return __ast_str_helper(buf, max_len, 0, fmt, ap);
00752 }
00753 )
00754 
00755 /*!
00756  * \brief Append to a dynamic string using a va_list
00757  *
00758  * Same as ast_str_set_va(), but append to the current content.
00759  */
00760 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_append_va(struct ast_str **buf, size_t max_len, const char *fmt, va_list ap),
00761 {
00762    return __ast_str_helper(buf, max_len, 1, fmt, ap);
00763 }
00764 )
00765 
00766 /*!\brief Set a dynamic string to a non-NULL terminated substring. */
00767 AST_INLINE_API(char *ast_str_set_substr(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc),
00768 {
00769    return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 0);
00770 }
00771 )
00772 
00773 /*!\brief Append a non-NULL terminated substring to the end of a dynamic string. */
00774 AST_INLINE_API(char *ast_str_append_substr(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc),
00775 {
00776    return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 0);
00777 }
00778 )
00779 
00780 /*!\brief Set a dynamic string to a non-NULL terminated substring, with escaping of commas. */
00781 AST_INLINE_API(char *ast_str_set_escapecommas(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc),
00782 {
00783    return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 1);
00784 }
00785 )
00786 
00787 /*!\brief Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas. */
00788 AST_INLINE_API(char *ast_str_append_escapecommas(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc),
00789 {
00790    return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 1);
00791 }
00792 )
00793 
00794 /*!
00795  * \brief Set a dynamic string using variable arguments
00796  *
00797  * \param buf This is the address of a pointer to a struct ast_str which should
00798  *      have been retrieved using ast_str_thread_get.  It will need to
00799  *      be updated in the case that the buffer has to be reallocated to
00800  *      accomodate a longer string than what it currently has space for.
00801  * \param max_len This is the maximum length to allow the string buffer to grow
00802  *      to.  If this is set to 0, then there is no maximum length.
00803  * If set to -1, we are bound to the current maximum length.
00804  * \param fmt This is the format string (printf style)
00805  *
00806  * \return The return value of this function is the same as that of the printf
00807  *         family of functions.
00808  *
00809  * All the rest is the same as ast_str_set_va()
00810  */
00811 AST_INLINE_API(
00812 int __attribute__((format(printf, 3, 4))) ast_str_set(
00813    struct ast_str **buf, size_t max_len, const char *fmt, ...),
00814 {
00815    int res;
00816    va_list ap;
00817 
00818    va_start(ap, fmt);
00819    res = ast_str_set_va(buf, max_len, fmt, ap);
00820    va_end(ap);
00821 
00822    return res;
00823 }
00824 )
00825 
00826 /*!
00827  * \brief Append to a thread local dynamic string
00828  *
00829  * The arguments, return values, and usage of this function are the same as
00830  * ast_str_set(), but the new data is appended to the current value.
00831  */
00832 AST_INLINE_API(
00833 int __attribute__((format(printf, 3, 4))) ast_str_append(
00834    struct ast_str **buf, size_t max_len, const char *fmt, ...),
00835 {
00836    int res;
00837    va_list ap;
00838 
00839    va_start(ap, fmt);
00840    res = ast_str_append_va(buf, max_len, fmt, ap);
00841    va_end(ap);
00842 
00843    return res;
00844 }
00845 )
00846 
00847 /*!
00848  * \brief Compute a hash value on a string
00849  *
00850  * This famous hash algorithm was written by Dan Bernstein and is
00851  * commonly used.
00852  *
00853  * http://www.cse.yorku.ca/~oz/hash.html
00854  */
00855 static force_inline int attribute_pure ast_str_hash(const char *str)
00856 {
00857    int hash = 5381;
00858 
00859    while (*str)
00860       hash = hash * 33 ^ *str++;
00861 
00862    return abs(hash);
00863 }
00864 
00865 /*!
00866  * \brief Compute a hash value on a string
00867  *
00868  * \param[in] str The string to add to the hash
00869  * \param[in] hash The hash value to add to
00870  * 
00871  * \details
00872  * This version of the function is for when you need to compute a
00873  * string hash of more than one string.
00874  *
00875  * This famous hash algorithm was written by Dan Bernstein and is
00876  * commonly used.
00877  *
00878  * \sa http://www.cse.yorku.ca/~oz/hash.html
00879  */
00880 static force_inline int ast_str_hash_add(const char *str, int hash)
00881 {
00882    while (*str)
00883       hash = hash * 33 ^ *str++;
00884 
00885    return abs(hash);
00886 }
00887 
00888 /*!
00889  * \brief Compute a hash value on a case-insensitive string
00890  *
00891  * Uses the same hash algorithm as ast_str_hash, but converts
00892  * all characters to lowercase prior to computing a hash. This
00893  * allows for easy case-insensitive lookups in a hash table.
00894  */
00895 static force_inline int attribute_pure ast_str_case_hash(const char *str)
00896 {
00897    int hash = 5381;
00898 
00899    while (*str) {
00900       hash = hash * 33 ^ tolower(*str++);
00901    }
00902 
00903    return abs(hash);
00904 }
00905 
00906 #endif /* _ASTERISK_STRINGS_H */

Generated on Wed Oct 28 13:31:09 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6