Mon Sep 20 2010 00:23:53

Asterisk developer's documentation


strings.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Digium, Inc.
00005  *
00006  * Tilghman Lesher <tlesher@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  *
00021  * \brief String manipulation API
00022  *
00023  * \author Tilghman Lesher <tilghman@digium.com>
00024  */
00025 
00026 /*** MAKEOPTS
00027 <category name="MENUSELECT_CFLAGS" displayname="Compiler Flags" positive_output="yes" remove_on_change=".lastclean">
00028    <member name="DEBUG_OPAQUE" displayname="Change ast_str internals to detect improper usage">
00029       <defaultenabled>yes</defaultenabled>
00030    </member>
00031 </category>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 247337 $")
00037 
00038 #include "asterisk/strings.h"
00039 #include "asterisk/pbx.h"
00040 
00041 /*!
00042  * core handler for dynamic strings.
00043  * This is not meant to be called directly, but rather through the
00044  * various wrapper macros
00045  * ast_str_set(...)
00046  * ast_str_append(...)
00047  * ast_str_set_va(...)
00048  * ast_str_append_va(...)
00049  */
00050 
00051 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00052 int __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
00053    int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
00054 #else
00055 int __ast_str_helper(struct ast_str **buf, ssize_t max_len,
00056    int append, const char *fmt, va_list ap)
00057 #endif
00058 {
00059    int res, need;
00060    int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
00061    va_list aq;
00062 
00063    do {
00064       if (max_len < 0) {
00065          max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */
00066       }
00067       /*
00068        * Ask vsnprintf how much space we need. Remember that vsnprintf
00069        * does not count the final <code>'\0'</code> so we must add 1.
00070        */
00071       va_copy(aq, ap);
00072       res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
00073 
00074       need = res + offset + 1;
00075       /*
00076        * If there is not enough space and we are below the max length,
00077        * reallocate the buffer and return a message telling to retry.
00078        */
00079       if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
00080          if (max_len && max_len < need) { /* truncate as needed */
00081             need = max_len;
00082          } else if (max_len == 0) { /* if unbounded, give more room for next time */
00083             need += 16 + need / 4;
00084          }
00085          if (0) { /* debugging */
00086             ast_verbose("extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
00087          }
00088          if (
00089 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00090                _ast_str_make_space(buf, need, file, lineno, function)
00091 #else
00092                ast_str_make_space(buf, need)
00093 #endif
00094             ) {
00095             ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
00096             va_end(aq);
00097             return AST_DYNSTR_BUILD_FAILED;
00098          }
00099          (*buf)->__AST_STR_STR[offset] = '\0';  /* Truncate the partial write. */
00100 
00101          /* Restart va_copy before calling vsnprintf() again. */
00102          va_end(aq);
00103          continue;
00104       }
00105       va_end(aq);
00106       break;
00107    } while (1);
00108    /* update space used, keep in mind the truncation */
00109    (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1 : res + offset;
00110 
00111    return res;
00112 }
00113 
00114 void ast_str_substitute_variables(struct ast_str **buf, size_t maxlen, struct ast_channel *chan, const char *template)
00115 {
00116    int first = 1;
00117    do {
00118       ast_str_make_space(buf, maxlen ? maxlen :
00119          (first ? strlen(template) * 2 : (*buf)->__AST_STR_LEN * 2));
00120       pbx_substitute_variables_helper_full(chan, NULL, template, (*buf)->__AST_STR_STR, (*buf)->__AST_STR_LEN - 1, &((*buf)->__AST_STR_USED));
00121       first = 0;
00122    } while (maxlen == 0 && (*buf)->__AST_STR_LEN - 5 < (*buf)->__AST_STR_USED);
00123 }
00124 
00125 char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
00126 {
00127    int dynamic = 0;
00128    char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
00129 
00130    if (maxlen < 1) {
00131       if (maxlen == 0) {
00132          dynamic = 1;
00133       }
00134       maxlen = (*buf)->__AST_STR_LEN;
00135    }
00136 
00137    while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
00138       if (escapecommas && (*src == '\\' || *src == ',')) {
00139          *ptr++ = '\\';
00140          maxlen--;
00141          (*buf)->__AST_STR_USED++;
00142       }
00143       *ptr++ = *src++;
00144       maxsrc--;
00145       maxlen--;
00146       (*buf)->__AST_STR_USED++;
00147 
00148       if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
00149          (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
00150          char *oldbase = (*buf)->__AST_STR_STR;
00151          size_t old = (*buf)->__AST_STR_LEN;
00152          if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
00153             /* If the buffer can't be extended, end it. */
00154             break;
00155          }
00156          /* What we extended the buffer by */
00157          maxlen = old;
00158 
00159          ptr += (*buf)->__AST_STR_STR - oldbase;
00160       }
00161    }
00162    if (__builtin_expect(!maxlen, 0)) {
00163       ptr--;
00164    }
00165    *ptr = '\0';
00166    return (*buf)->__AST_STR_STR;
00167 }
00168