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