#include "asterisk.h"
#include "asterisk/strings.h"
#include "asterisk/pbx.h"
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) |
Definition in file strings.c.
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 59 of file strings.c.
References AST_DYNSTR_BUILD_FAILED, ast_str_make_space(), ast_verbose, and len().
Referenced by ast_str_set_va().
00062 { 00063 int res, need; 00064 int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0; 00065 va_list aq; 00066 00067 do { 00068 if (max_len < 0) { 00069 max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */ 00070 } 00071 /* 00072 * Ask vsnprintf how much space we need. Remember that vsnprintf 00073 * does not count the final <code>'\0'</code> so we must add 1. 00074 */ 00075 va_copy(aq, ap); 00076 res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq); 00077 00078 need = res + offset + 1; 00079 /* 00080 * If there is not enough space and we are below the max length, 00081 * reallocate the buffer and return a message telling to retry. 00082 */ 00083 if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) { 00084 int len = (int)(*buf)->__AST_STR_LEN; 00085 if (max_len && max_len < need) { /* truncate as needed */ 00086 need = max_len; 00087 } else if (max_len == 0) { /* if unbounded, give more room for next time */ 00088 need += 16 + need / 4; 00089 } 00090 if (0) { /* debugging */ 00091 ast_verbose("extend from %d to %d\n", len, need); 00092 } 00093 if ( 00094 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) 00095 _ast_str_make_space(buf, need, file, lineno, function) 00096 #else 00097 ast_str_make_space(buf, need) 00098 #endif 00099 ) { 00100 ast_verbose("failed to extend from %d to %d\n", len, need); 00101 va_end(aq); 00102 return AST_DYNSTR_BUILD_FAILED; 00103 } 00104 (*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */ 00105 00106 /* Restart va_copy before calling vsnprintf() again. */ 00107 va_end(aq); 00108 continue; 00109 } 00110 va_end(aq); 00111 break; 00112 } while (1); 00113 /* update space used, keep in mind the truncation */ 00114 (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset; 00115 00116 return res; 00117 }
char* __ast_str_helper2 | ( | struct ast_str ** | buf, | |
ssize_t | maxlen, | |||
const char * | src, | |||
size_t | maxsrc, | |||
int | append, | |||
int | escapecommas | |||
) |
Definition at line 119 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().
00120 { 00121 int dynamic = 0; 00122 char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR; 00123 00124 if (maxlen < 1) { 00125 if (maxlen == 0) { 00126 dynamic = 1; 00127 } 00128 maxlen = (*buf)->__AST_STR_LEN; 00129 } 00130 00131 while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) { 00132 if (escapecommas && (*src == '\\' || *src == ',')) { 00133 *ptr++ = '\\'; 00134 maxlen--; 00135 (*buf)->__AST_STR_USED++; 00136 } 00137 *ptr++ = *src++; 00138 maxsrc--; 00139 maxlen--; 00140 (*buf)->__AST_STR_USED++; 00141 00142 if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) || 00143 (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) { 00144 char *oldbase = (*buf)->__AST_STR_STR; 00145 size_t old = (*buf)->__AST_STR_LEN; 00146 if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) { 00147 /* If the buffer can't be extended, end it. */ 00148 break; 00149 } 00150 /* What we extended the buffer by */ 00151 maxlen = old; 00152 00153 ptr += (*buf)->__AST_STR_STR - oldbase; 00154 } 00155 } 00156 if (__builtin_expect(!maxlen, 0)) { 00157 ptr--; 00158 } 00159 *ptr = '\0'; 00160 return (*buf)->__AST_STR_STR; 00161 }