[asterisk-commits] rizzo: trunk r48560 -
/trunk/include/asterisk/strings.h
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Mon Dec 18 09:24:44 MST 2006
Author: rizzo
Date: Mon Dec 18 10:24:44 2006
New Revision: 48560
URL: http://svn.digium.com/view/asterisk?view=rev&rev=48560
Log:
apply the proposed fix for bug 8602
http://bugs.digium.com/view.php?id=8602
(i am not sure if there is still missing cast in
front of the alloca() call - being a macro this is
probably triggered only when actually used).
Add function ast_str_reset() to reinitialize the
string to an empty string instead of playing with
the internal fields.
Modified:
trunk/include/asterisk/strings.h
Modified: trunk/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/strings.h?view=diff&rev=48560&r1=48559&r2=48560
==============================================================================
--- trunk/include/asterisk/strings.h (original)
+++ trunk/include/asterisk/strings.h Mon Dec 18 10:24:44 2006
@@ -176,6 +176,8 @@
/*!
\brief Build a string in a buffer, designed to be called repeatedly
+ \note This method is not recommended. New code should use ast_str_*() instead.
+
This is a wrapper for snprintf, that properly handles the buffer pointer
and buffer space available.
@@ -283,13 +285,19 @@
*
* Finally, the string can be manipulated with the following:
*
- * ast_str_set(&buf, max_len, ts, fmt, ...)
- * ast_str_append(&buf, max_len, ts, fmt, ...)
- *
- * and their varargs format.
+ * ast_str_set(&buf, max_len, fmt, ...)
+ * ast_str_append(&buf, max_len, fmt, ...)
+ *
+ * and their varargs variant
+ *
+ * ast_str_set_va(&buf, max_len, ap)
+ * ast_str_append_va(&buf, max_len, ap)
*
* \arg max_len The maximum allowed length, reallocating if needed.
* 0 means unlimited, -1 means "at most the available space"
+ *
+ * \return All the functions return <0 in case of error, or the
+ * length of the string added to the buffer otherwise.
*/
/*! \brief The descriptor of a dynamic string
@@ -325,7 +333,8 @@
{
struct ast_str *buf;
- if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
+ buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
+ if (buf == NULL)
return NULL;
buf->len = init_len;
@@ -336,6 +345,20 @@
}
)
+/*! \brief Reset the content of a dynamic string.
+ * Useful before a series of ast_str_append.
+ */
+AST_INLINE_API(
+void ast_str_reset(struct ast_str *buf),
+{
+ if (buf) {
+ buf->used = 0;
+ if (buf->len)
+ buf->str[0] = '\0';
+ }
+}
+)
+
/*!
* Make space in a new string (e.g. to read in data from a file)
*/
@@ -346,7 +369,7 @@
return 0; /* success */
if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
return -1; /* cannot extend */
- *buf = ast_realloc(*buf, new_len + sizeof(struct ast_str));
+ *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
if (*buf == NULL) /* XXX watch out, we leak memory here */
return -1;
if ((*buf)->ts != DS_MALLOC)
@@ -406,7 +429,8 @@
{
struct ast_str *buf;
- if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
+ buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
+ if (buf == NULL)
return NULL;
if (!buf->len) {
More information about the asterisk-commits
mailing list