Wed Aug 11 01:10:23 2010

Asterisk developer's documentation


strings.c File Reference

String manipulation API. More...

#include "asterisk.h"
#include "asterisk/strings.h"
#include "asterisk/pbx.h"

Include dependency graph for strings.c:

Go to the source code of this file.

Functions

int __ast_str_helper (struct ast_str **buf, ssize_t max_len, int append, const char *fmt, va_list ap)
 Core functionality of ast_str_(set|append)_va.
char * __ast_str_helper2 (struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
void ast_str_substitute_variables (struct ast_str **buf, size_t maxlen, struct ast_channel *chan, const char *template)


Detailed Description

String manipulation API.

Author:
Tilghman Lesher <tilghman@digium.com>

Definition in file strings.c.


Function Documentation

int __ast_str_helper ( struct ast_str **  buf,
ssize_t  max_len,
int  append,
const char *  fmt,
va_list  ap 
)

Core functionality of ast_str_(set|append)_va.

core handler for dynamic strings. This is not meant to be called directly, but rather through the various wrapper macros ast_str_set(...) ast_str_append(...) ast_str_set_va(...) ast_str_append_va(...)

Definition at line 55 of file strings.c.

References AST_DYNSTR_BUILD_FAILED, ast_str_make_space(), ast_verbose, and len().

Referenced by ast_str_set_va().

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          int len = (int)(*buf)->__AST_STR_LEN;
00081          if (max_len && max_len < need) { /* truncate as needed */
00082             need = max_len;
00083          } else if (max_len == 0) { /* if unbounded, give more room for next time */
00084             need += 16 + need / 4;
00085          }
00086          if (0) { /* debugging */
00087             ast_verbose("extend from %d to %d\n", len, need);
00088          }
00089          if (
00090 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00091                _ast_str_make_space(buf, need, file, lineno, function)
00092 #else
00093                ast_str_make_space(buf, need)
00094 #endif
00095             ) {
00096             ast_verbose("failed to extend from %d to %d\n", len, need);
00097             va_end(aq);
00098             return AST_DYNSTR_BUILD_FAILED;
00099          }
00100          (*buf)->__AST_STR_STR[offset] = '\0';  /* Truncate the partial write. */
00101 
00102          /* Restart va_copy before calling vsnprintf() again. */
00103          va_end(aq);
00104          continue;
00105       }
00106       va_end(aq);
00107       break;
00108    } while (1);
00109    /* update space used, keep in mind the truncation */
00110    (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1 : res + offset;
00111 
00112    return res;
00113 }

char* __ast_str_helper2 ( struct ast_str **  buf,
ssize_t  maxlen,
const char *  src,
size_t  maxsrc,
int  append,
int  escapecommas 
)

Definition at line 126 of file strings.c.

References ast_str::__AST_STR_LEN, and ast_str_make_space().

Referenced by ast_str_append_substr(), ast_str_append_va(), ast_str_set_escapecommas(), and ast_str_set_substr().

00127 {
00128    int dynamic = 0;
00129    char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
00130 
00131    if (maxlen < 1) {
00132       if (maxlen == 0) {
00133          dynamic = 1;
00134       }
00135       maxlen = (*buf)->__AST_STR_LEN;
00136    }
00137 
00138    while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
00139       if (escapecommas && (*src == '\\' || *src == ',')) {
00140          *ptr++ = '\\';
00141          maxlen--;
00142          (*buf)->__AST_STR_USED++;
00143       }
00144       *ptr++ = *src++;
00145       maxsrc--;
00146       maxlen--;
00147       (*buf)->__AST_STR_USED++;
00148 
00149       if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
00150          (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
00151          char *oldbase = (*buf)->__AST_STR_STR;
00152          size_t old = (*buf)->__AST_STR_LEN;
00153          if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
00154             /* If the buffer can't be extended, end it. */
00155             break;
00156          }
00157          /* What we extended the buffer by */
00158          maxlen = old;
00159 
00160          ptr += (*buf)->__AST_STR_STR - oldbase;
00161       }
00162    }
00163    if (__builtin_expect(!maxlen, 0)) {
00164       ptr--;
00165    }
00166    *ptr = '\0';
00167    return (*buf)->__AST_STR_STR;
00168 }

void ast_str_substitute_variables ( struct ast_str **  buf,
size_t  maxlen,
struct ast_channel *  chan,
const char *  template 
)

Definition at line 115 of file strings.c.

References ast_str_make_space(), first, and pbx_substitute_variables_helper_full().

Referenced by acf_odbc_read(), acf_odbc_write(), cli_odbc_read(), and cli_odbc_write().

00116 {
00117    int first = 1;
00118    do {
00119       ast_str_make_space(buf, maxlen ? maxlen :
00120          (first ? strlen(template) * 2 : (*buf)->__AST_STR_LEN * 2));
00121       pbx_substitute_variables_helper_full(chan, NULL, template, (*buf)->__AST_STR_STR, (*buf)->__AST_STR_LEN - 1, &((*buf)->__AST_STR_USED));
00122       first = 0;
00123    } while (maxlen == 0 && (*buf)->__AST_STR_LEN - 5 < (*buf)->__AST_STR_USED);
00124 }


Generated on Wed Aug 11 01:10:24 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9