2005-06-24 22:50:07 +00:00
|
|
|
/*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2006-02-15 00:24:24 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
2005-08-30 18:32:10 +00:00
|
|
|
*
|
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
|
* channels for your use.
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-08-30 18:32:10 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
|
* at the top of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
|
|
|
|
* \brief String manipulation functions
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _ASTERISK_STRINGS_H
|
|
|
|
|
#define _ASTERISK_STRINGS_H
|
|
|
|
|
|
2008-12-15 19:48:02 +00:00
|
|
|
/* #define DEBUG_OPAQUE */
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2008-11-14 16:53:38 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
#include "asterisk/utils.h"
|
|
|
|
|
#include "asterisk/threadstorage.h"
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
#if defined(DEBUG_OPAQUE)
|
|
|
|
|
#define __AST_STR_USED used2
|
|
|
|
|
#define __AST_STR_LEN len2
|
|
|
|
|
#define __AST_STR_STR str2
|
|
|
|
|
#define __AST_STR_TS ts2
|
|
|
|
|
#else
|
|
|
|
|
#define __AST_STR_USED used
|
|
|
|
|
#define __AST_STR_LEN len
|
|
|
|
|
#define __AST_STR_STR str
|
|
|
|
|
#define __AST_STR_TS ts
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-01-10 19:02:58 +00:00
|
|
|
/* You may see casts in this header that may seem useless but they ensure this file is C++ clean */
|
|
|
|
|
|
2008-12-15 19:48:02 +00:00
|
|
|
#define AS_OR(a,b) (a && ast_str_strlen(a)) ? ast_str_buffer(a) : (b)
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2008-05-15 22:05:47 +00:00
|
|
|
#ifdef AST_DEVMODE
|
|
|
|
|
#define ast_strlen_zero(foo) _ast_strlen_zero(foo, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
|
|
|
static force_inline int _ast_strlen_zero(const char *s, const char *file, const char *function, int line)
|
|
|
|
|
{
|
|
|
|
|
if (!s || (*s == '\0')) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp(s, "(null)")) {
|
|
|
|
|
ast_log(__LOG_WARNING, file, line, function, "Possible programming error: \"(null)\" is not NULL!\n");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
2009-01-19 21:42:46 +00:00
|
|
|
static force_inline int attribute_pure ast_strlen_zero(const char *s)
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
2005-10-26 18:54:24 +00:00
|
|
|
return (!s || (*s == '\0'));
|
2005-06-24 22:50:07 +00:00
|
|
|
}
|
2008-05-15 22:05:47 +00:00
|
|
|
#endif
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2009-12-15 23:51:05 +00:00
|
|
|
#ifdef SENSE_OF_HUMOR
|
|
|
|
|
#define ast_strlen_real(a) (a) ? strlen(a) : 0
|
|
|
|
|
#define ast_strlen_imaginary(a) ast_random()
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-03-24 15:06:42 +00:00
|
|
|
/*! \brief returns the equivalent of logic or for strings:
|
|
|
|
|
* first one if not empty, otherwise second one.
|
|
|
|
|
*/
|
2008-11-11 17:57:20 +00:00
|
|
|
#define S_OR(a, b) ({typeof(&((a)[0])) __x = (a); ast_strlen_zero(__x) ? (b) : __x;})
|
2007-11-27 19:12:33 +00:00
|
|
|
|
|
|
|
|
/*! \brief returns the equivalent of logic or for strings, with an additional boolean check:
|
|
|
|
|
* second one if not empty and first one is true, otherwise third one.
|
|
|
|
|
* example: S_COR(usewidget, widget, "<no widget>")
|
|
|
|
|
*/
|
2008-11-11 17:57:20 +00:00
|
|
|
#define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
|
2006-03-24 15:06:42 +00:00
|
|
|
|
2005-06-24 22:50:07 +00:00
|
|
|
/*!
|
|
|
|
|
\brief Gets a pointer to the first non-whitespace character in a string.
|
2008-07-02 14:50:45 +00:00
|
|
|
\param str the input string
|
2005-06-24 22:50:07 +00:00
|
|
|
\return a pointer to the first non-whitespace character
|
|
|
|
|
*/
|
2005-07-11 23:25:31 +00:00
|
|
|
AST_INLINE_API(
|
2009-01-19 21:42:46 +00:00
|
|
|
char * attribute_pure ast_skip_blanks(const char *str),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
2007-10-15 16:36:48 +00:00
|
|
|
while (*str && ((unsigned char) *str) < 33)
|
2005-06-24 22:50:07 +00:00
|
|
|
str++;
|
2009-01-19 21:42:46 +00:00
|
|
|
return (char *) str;
|
2005-06-24 22:50:07 +00:00
|
|
|
}
|
2005-07-11 23:25:31 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief Trims trailing whitespace characters from a string.
|
2008-07-02 14:50:45 +00:00
|
|
|
\param str the input string
|
2006-01-05 23:54:28 +00:00
|
|
|
\return a pointer to the modified string
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
2005-07-11 23:25:31 +00:00
|
|
|
AST_INLINE_API(
|
|
|
|
|
char *ast_trim_blanks(char *str),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
|
|
|
|
char *work = str;
|
|
|
|
|
|
|
|
|
|
if (work) {
|
|
|
|
|
work += strlen(work) - 1;
|
|
|
|
|
/* It's tempting to only want to erase after we exit this loop,
|
|
|
|
|
but since ast_trim_blanks *could* receive a constant string
|
|
|
|
|
(which we presumably wouldn't have to touch), we shouldn't
|
|
|
|
|
actually set anything unless we must, and it's easier just
|
|
|
|
|
to set each position to \0 than to keep track of a variable
|
|
|
|
|
for it */
|
2007-10-15 16:36:48 +00:00
|
|
|
while ((work >= str) && ((unsigned char) *work) < 33)
|
2005-06-24 22:50:07 +00:00
|
|
|
*(work--) = '\0';
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2005-07-11 23:25:31 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief Gets a pointer to first whitespace character in a string.
|
2008-07-02 14:50:45 +00:00
|
|
|
\param str the input string
|
2005-06-24 22:50:07 +00:00
|
|
|
\return a pointer to the first whitespace character
|
|
|
|
|
*/
|
2005-07-11 23:25:31 +00:00
|
|
|
AST_INLINE_API(
|
2009-01-19 21:42:46 +00:00
|
|
|
char * attribute_pure ast_skip_nonblanks(const char *str),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
2007-10-15 16:36:48 +00:00
|
|
|
while (*str && ((unsigned char) *str) > 32)
|
2005-06-24 22:50:07 +00:00
|
|
|
str++;
|
2009-01-19 21:42:46 +00:00
|
|
|
return (char *) str;
|
2005-06-24 22:50:07 +00:00
|
|
|
}
|
2005-07-11 23:25:31 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief Strip leading/trailing whitespace from a string.
|
2008-07-02 14:50:45 +00:00
|
|
|
\param s The string to be stripped (will be modified).
|
2005-06-24 22:50:07 +00:00
|
|
|
\return The stripped string.
|
|
|
|
|
|
|
|
|
|
This functions strips all leading and trailing whitespace
|
|
|
|
|
characters from the input string, and returns a pointer to
|
|
|
|
|
the resulting string. The string is modified in place.
|
|
|
|
|
*/
|
2005-07-11 23:25:31 +00:00
|
|
|
AST_INLINE_API(
|
|
|
|
|
char *ast_strip(char *s),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
2009-01-19 21:42:46 +00:00
|
|
|
if ((s = ast_skip_blanks(s))) {
|
2005-06-24 22:50:07 +00:00
|
|
|
ast_trim_blanks(s);
|
2009-01-19 21:42:46 +00:00
|
|
|
}
|
2005-06-24 22:50:07 +00:00
|
|
|
return s;
|
|
|
|
|
}
|
2005-07-11 23:25:31 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief Strip leading/trailing whitespace and quotes from a string.
|
|
|
|
|
\param s The string to be stripped (will be modified).
|
|
|
|
|
\param beg_quotes The list of possible beginning quote characters.
|
|
|
|
|
\param end_quotes The list of matching ending quote characters.
|
|
|
|
|
\return The stripped string.
|
|
|
|
|
|
|
|
|
|
This functions strips all leading and trailing whitespace
|
|
|
|
|
characters from the input string, and returns a pointer to
|
|
|
|
|
the resulting string. The string is modified in place.
|
|
|
|
|
|
|
|
|
|
It can also remove beginning and ending quote (or quote-like)
|
|
|
|
|
characters, in matching pairs. If the first character of the
|
|
|
|
|
string matches any character in beg_quotes, and the last
|
|
|
|
|
character of the string is the matching character in
|
|
|
|
|
end_quotes, then they are removed from the string.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
\code
|
|
|
|
|
ast_strip_quoted(buf, "\"", "\"");
|
|
|
|
|
ast_strip_quoted(buf, "'", "'");
|
|
|
|
|
ast_strip_quoted(buf, "[{(", "]})");
|
|
|
|
|
\endcode
|
|
|
|
|
*/
|
|
|
|
|
char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
|
|
|
|
|
|
2007-08-17 19:13:25 +00:00
|
|
|
/*!
|
2008-02-15 17:29:08 +00:00
|
|
|
\brief Strip backslash for "escaped" semicolons,
|
|
|
|
|
the string to be stripped (will be modified).
|
2007-08-17 19:13:25 +00:00
|
|
|
\return The stripped string.
|
|
|
|
|
*/
|
|
|
|
|
char *ast_unescape_semicolon(char *s);
|
|
|
|
|
|
2007-12-19 17:09:01 +00:00
|
|
|
/*!
|
2008-02-15 17:29:08 +00:00
|
|
|
\brief Convert some C escape sequences \verbatim (\b\f\n\r\t) \endverbatim into the
|
|
|
|
|
equivalent characters. The string to be converted (will be modified).
|
2007-12-19 17:09:01 +00:00
|
|
|
\return The converted string.
|
|
|
|
|
*/
|
|
|
|
|
char *ast_unescape_c(char *s);
|
|
|
|
|
|
2005-06-24 22:50:07 +00:00
|
|
|
/*!
|
|
|
|
|
\brief Size-limited null-terminating string copy.
|
2008-07-02 14:50:45 +00:00
|
|
|
\param dst The destination buffer.
|
|
|
|
|
\param src The source string
|
|
|
|
|
\param size The size of the destination buffer
|
2005-06-24 22:50:07 +00:00
|
|
|
\return Nothing.
|
|
|
|
|
|
|
|
|
|
This is similar to \a strncpy, with two important differences:
|
|
|
|
|
- the destination buffer will \b always be null-terminated
|
|
|
|
|
- the destination buffer is not filled with zeros past the copied string length
|
|
|
|
|
These differences make it slightly more efficient, and safer to use since it will
|
|
|
|
|
not leave the destination buffer unterminated. There is no need to pass an artificially
|
|
|
|
|
reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
|
|
|
|
|
to be initialized to zeroes prior to calling this function.
|
|
|
|
|
*/
|
2005-07-12 15:26:24 +00:00
|
|
|
AST_INLINE_API(
|
|
|
|
|
void ast_copy_string(char *dst, const char *src, size_t size),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
|
|
|
|
while (*src && size) {
|
|
|
|
|
*dst++ = *src++;
|
|
|
|
|
size--;
|
|
|
|
|
}
|
|
|
|
|
if (__builtin_expect(!size, 0))
|
|
|
|
|
dst--;
|
|
|
|
|
*dst = '\0';
|
|
|
|
|
}
|
2005-07-12 15:26:24 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\brief Build a string in a buffer, designed to be called repeatedly
|
|
|
|
|
|
2006-12-18 16:24:44 +00:00
|
|
|
\note This method is not recommended. New code should use ast_str_*() instead.
|
|
|
|
|
|
2005-06-24 22:50:07 +00:00
|
|
|
This is a wrapper for snprintf, that properly handles the buffer pointer
|
|
|
|
|
and buffer space available.
|
|
|
|
|
|
2008-07-02 14:50:45 +00:00
|
|
|
\param buffer current position in buffer to place string into (will be updated on return)
|
|
|
|
|
\param space remaining space in buffer (will be updated on return)
|
|
|
|
|
\param fmt printf-style format string
|
2007-07-16 02:51:56 +00:00
|
|
|
\retval 0 on success
|
|
|
|
|
\retval non-zero on failure.
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
2008-11-29 17:57:39 +00:00
|
|
|
int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2005-10-01 15:41:27 +00:00
|
|
|
/*!
|
|
|
|
|
\brief Build a string in a buffer, designed to be called repeatedly
|
|
|
|
|
|
|
|
|
|
This is a wrapper for snprintf, that properly handles the buffer pointer
|
|
|
|
|
and buffer space available.
|
|
|
|
|
|
|
|
|
|
\return 0 on success, non-zero on failure.
|
|
|
|
|
\param buffer current position in buffer to place string into (will be updated on return)
|
|
|
|
|
\param space remaining space in buffer (will be updated on return)
|
|
|
|
|
\param fmt printf-style format string
|
|
|
|
|
\param ap varargs list of arguments for format
|
|
|
|
|
*/
|
2008-11-29 17:57:39 +00:00
|
|
|
int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));
|
2005-10-01 15:41:27 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Make sure something is true.
|
2005-06-24 22:50:07 +00:00
|
|
|
* Determine if a string containing a boolean value is "true".
|
2007-07-16 02:51:56 +00:00
|
|
|
* This function checks to see whether a string passed to it is an indication of an "true" value.
|
|
|
|
|
* It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2007-07-16 02:51:56 +00:00
|
|
|
* \retval 0 if val is a NULL pointer.
|
|
|
|
|
* \retval -1 if "true".
|
|
|
|
|
* \retval 0 otherwise.
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
2009-01-19 21:42:46 +00:00
|
|
|
int attribute_pure ast_true(const char *val);
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Make sure something is false.
|
2005-06-24 22:50:07 +00:00
|
|
|
* Determine if a string containing a boolean value is "false".
|
2007-07-16 02:51:56 +00:00
|
|
|
* This function checks to see whether a string passed to it is an indication of an "false" value.
|
|
|
|
|
* It checks to see if the string is "no", "false", "n", "f", "off" or "0".
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2007-07-16 02:51:56 +00:00
|
|
|
* \retval 0 if val is a NULL pointer.
|
|
|
|
|
* \retval -1 if "true".
|
|
|
|
|
* \retval 0 otherwise.
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
2009-01-19 21:42:46 +00:00
|
|
|
int attribute_pure ast_false(const char *val);
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2005-12-20 20:20:04 +00:00
|
|
|
/*
|
2007-09-05 16:31:39 +00:00
|
|
|
* \brief Join an array of strings into a single string.
|
|
|
|
|
* \param s the resulting string buffer
|
|
|
|
|
* \param len the length of the result buffer, s
|
|
|
|
|
* \param w an array of strings to join.
|
|
|
|
|
*
|
|
|
|
|
* This function will join all of the strings in the array 'w' into a single
|
|
|
|
|
* string. It will also place a space in the result buffer in between each
|
|
|
|
|
* string from 'w'.
|
2005-12-20 20:20:04 +00:00
|
|
|
*/
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_join(char *s, size_t len, const char * const w[]);
|
2005-12-20 20:20:04 +00:00
|
|
|
|
2006-02-15 00:24:24 +00:00
|
|
|
/*
|
|
|
|
|
\brief Parse a time (integer) string.
|
|
|
|
|
\param src String to parse
|
|
|
|
|
\param dst Destination
|
|
|
|
|
\param _default Value to use if the string does not contain a valid time
|
2006-02-23 17:13:57 +00:00
|
|
|
\param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
|
2007-07-16 02:51:56 +00:00
|
|
|
\retval 0 on success
|
|
|
|
|
\retval non-zero on failure.
|
2006-02-15 00:24:24 +00:00
|
|
|
*/
|
2006-02-23 17:13:57 +00:00
|
|
|
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
|
2006-02-15 00:24:24 +00:00
|
|
|
|
2007-07-18 19:47:20 +00:00
|
|
|
/*
|
|
|
|
|
\brief Parse a time (float) string.
|
|
|
|
|
\param src String to parse
|
|
|
|
|
\param dst Destination
|
|
|
|
|
\param _default Value to use if the string does not contain a valid time
|
|
|
|
|
\param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
|
|
|
|
|
\return zero on success, non-zero on failure
|
|
|
|
|
*/
|
|
|
|
|
int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
|
|
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
/*!
|
|
|
|
|
* Support for dynamic strings.
|
|
|
|
|
*
|
|
|
|
|
* A dynamic string is just a C string prefixed by a few control fields
|
|
|
|
|
* that help setting/appending/extending it using a printf-like syntax.
|
|
|
|
|
*
|
|
|
|
|
* One should never declare a variable with this type, but only a pointer
|
|
|
|
|
* to it, e.g.
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* struct ast_str *ds;
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
|
|
|
|
* The pointer can be initialized with the following:
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* ds = ast_str_create(init_len);
|
2006-12-15 22:08:46 +00:00
|
|
|
* creates a malloc()'ed dynamic string;
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* ds = ast_str_alloca(init_len);
|
2006-12-15 22:08:46 +00:00
|
|
|
* creates a string on the stack (not very dynamic!).
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* ds = ast_str_thread_get(ts, init_len)
|
2006-12-15 22:08:46 +00:00
|
|
|
* creates a malloc()'ed dynamic string associated to
|
|
|
|
|
* the thread-local storage key ts
|
|
|
|
|
*
|
|
|
|
|
* Finally, the string can be manipulated with the following:
|
|
|
|
|
*
|
2006-12-18 16:24:44 +00:00
|
|
|
* ast_str_set(&buf, max_len, fmt, ...)
|
|
|
|
|
* ast_str_append(&buf, max_len, fmt, ...)
|
|
|
|
|
*
|
|
|
|
|
* and their varargs variant
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2006-12-18 16:24:44 +00:00
|
|
|
* ast_str_set_va(&buf, max_len, ap)
|
|
|
|
|
* ast_str_append_va(&buf, max_len, ap)
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2010-02-16 21:15:38 +00:00
|
|
|
* \param max_len The maximum allowed capacity of the ast_str. Note that
|
|
|
|
|
* if the value of max_len is less than the current capacity of the
|
|
|
|
|
* ast_str (as returned by ast_str_size), then the parameter is effectively
|
|
|
|
|
* ignored.
|
2006-12-15 22:08:46 +00:00
|
|
|
* 0 means unlimited, -1 means "at most the available space"
|
2006-12-18 16:24:44 +00:00
|
|
|
*
|
|
|
|
|
* \return All the functions return <0 in case of error, or the
|
2010-02-16 21:15:38 +00:00
|
|
|
* length of the string added to the buffer otherwise. Note that
|
|
|
|
|
* in most cases where an error is returned, characters ARE written
|
|
|
|
|
* to the ast_str.
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*! \brief The descriptor of a dynamic string
|
|
|
|
|
* XXX storage will be optimized later if needed
|
2006-12-15 23:10:42 +00:00
|
|
|
* We use the ts field to indicate the type of storage.
|
|
|
|
|
* Three special constants indicate malloc, alloca() or static
|
|
|
|
|
* variables, all other values indicate a
|
|
|
|
|
* struct ast_threadstorage pointer.
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
2006-12-15 23:10:42 +00:00
|
|
|
struct ast_str {
|
2008-12-18 15:25:15 +00:00
|
|
|
size_t __AST_STR_LEN; /*!< The current maximum length of the string */
|
|
|
|
|
size_t __AST_STR_USED; /*!< Amount of space used */
|
|
|
|
|
struct ast_threadstorage *__AST_STR_TS; /*!< What kind of storage is this ? */
|
2006-12-15 23:10:42 +00:00
|
|
|
#define DS_MALLOC ((struct ast_threadstorage *)1)
|
|
|
|
|
#define DS_ALLOCA ((struct ast_threadstorage *)2)
|
|
|
|
|
#define DS_STATIC ((struct ast_threadstorage *)3) /* not supported yet */
|
2008-12-18 15:25:15 +00:00
|
|
|
char __AST_STR_STR[0]; /*!< The string buffer */
|
2006-12-15 22:08:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Create a malloc'ed dynamic length string
|
|
|
|
|
*
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param init_len This is the initial length of the string buffer
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
|
|
|
|
* \return This function returns a pointer to the dynamic string length. The
|
|
|
|
|
* result will be NULL in the case of a memory allocation error.
|
|
|
|
|
*
|
|
|
|
|
* \note The result of this function is dynamically allocated memory, and must
|
|
|
|
|
* be free()'d after it is no longer needed.
|
|
|
|
|
*/
|
2009-03-11 00:29:59 +00:00
|
|
|
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
|
|
|
|
|
#define ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__)
|
|
|
|
|
AST_INLINE_API(
|
|
|
|
|
struct ast_str * attribute_malloc _ast_str_create(size_t init_len,
|
|
|
|
|
const char *file, int lineno, const char *func),
|
|
|
|
|
{
|
|
|
|
|
struct ast_str *buf;
|
|
|
|
|
|
|
|
|
|
buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func);
|
|
|
|
|
if (buf == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
buf->__AST_STR_LEN = init_len;
|
|
|
|
|
buf->__AST_STR_USED = 0;
|
|
|
|
|
buf->__AST_STR_TS = DS_MALLOC;
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
#else
|
2006-12-15 22:08:46 +00:00
|
|
|
AST_INLINE_API(
|
2006-12-15 23:10:42 +00:00
|
|
|
struct ast_str * attribute_malloc ast_str_create(size_t init_len),
|
2006-12-15 22:08:46 +00:00
|
|
|
{
|
2006-12-15 23:10:42 +00:00
|
|
|
struct ast_str *buf;
|
2006-12-15 22:08:46 +00:00
|
|
|
|
2007-01-10 19:02:58 +00:00
|
|
|
buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
|
2006-12-18 16:24:44 +00:00
|
|
|
if (buf == NULL)
|
2006-12-15 22:08:46 +00:00
|
|
|
return NULL;
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
buf->__AST_STR_LEN = init_len;
|
|
|
|
|
buf->__AST_STR_USED = 0;
|
|
|
|
|
buf->__AST_STR_TS = DS_MALLOC;
|
2006-12-15 22:08:46 +00:00
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
)
|
2009-03-11 00:29:59 +00:00
|
|
|
#endif
|
2006-12-15 22:08:46 +00:00
|
|
|
|
2006-12-18 16:24:44 +00:00
|
|
|
/*! \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) {
|
2008-12-18 15:25:15 +00:00
|
|
|
buf->__AST_STR_USED = 0;
|
2009-01-19 21:42:46 +00:00
|
|
|
if (buf->__AST_STR_LEN) {
|
2008-12-18 15:25:15 +00:00
|
|
|
buf->__AST_STR_STR[0] = '\0';
|
2009-01-19 21:42:46 +00:00
|
|
|
}
|
2006-12-18 16:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2009-01-09 18:30:55 +00:00
|
|
|
/*! \brief Update the length of the buffer, after using ast_str merely as a buffer.
|
|
|
|
|
* \param buf A pointer to the ast_str string.
|
|
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
|
|
|
|
void ast_str_update(struct ast_str *buf),
|
|
|
|
|
{
|
|
|
|
|
buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*! \brief Trims trailing whitespace characters from an ast_str string.
|
|
|
|
|
* \param buf A pointer to the ast_str string.
|
|
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
|
|
|
|
void ast_str_trim_blanks(struct ast_str *buf),
|
|
|
|
|
{
|
|
|
|
|
if (!buf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
while (buf->__AST_STR_USED && buf->__AST_STR_STR[buf->__AST_STR_USED - 1] < 33) {
|
|
|
|
|
buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2008-11-01 21:10:07 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
/*!\brief Returns the current length of the string stored within buf.
|
2009-04-01 20:13:28 +00:00
|
|
|
* \param buf A pointer to the ast_str structure.
|
2008-12-13 08:36:35 +00:00
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2009-05-21 21:13:09 +00:00
|
|
|
size_t attribute_pure ast_str_strlen(const struct ast_str *buf),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
2008-12-18 15:25:15 +00:00
|
|
|
return buf->__AST_STR_USED;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!\brief Returns the current maximum length (without reallocation) of the current buffer.
|
2009-04-01 20:13:28 +00:00
|
|
|
* \param buf A pointer to the ast_str structure.
|
|
|
|
|
* \retval Current maximum length of the buffer.
|
2008-12-13 08:36:35 +00:00
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2009-05-21 21:13:09 +00:00
|
|
|
size_t attribute_pure ast_str_size(const struct ast_str *buf),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
2008-12-18 15:25:15 +00:00
|
|
|
return buf->__AST_STR_LEN;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!\brief Returns the string buffer within the ast_str buf.
|
2009-04-01 20:13:28 +00:00
|
|
|
* \param buf A pointer to the ast_str structure.
|
|
|
|
|
* \retval A pointer to the enclosed string.
|
2008-12-13 08:36:35 +00:00
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2009-05-21 21:13:09 +00:00
|
|
|
char * attribute_pure ast_str_buffer(const struct ast_str *buf),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
2009-05-21 21:13:09 +00:00
|
|
|
/* for now, cast away the const qualifier on the pointer
|
|
|
|
|
* being returned; eventually, it should become truly const
|
|
|
|
|
* and only be modified via accessor functions
|
|
|
|
|
*/
|
|
|
|
|
return (char *) buf->__AST_STR_STR;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2009-04-01 20:13:28 +00:00
|
|
|
/*!\brief Truncates the enclosed string to the given length.
|
|
|
|
|
* \param buf A pointer to the ast_str structure.
|
2011-11-23 19:58:19 +00:00
|
|
|
* \param len Maximum length of the string. If len is larger than the
|
|
|
|
|
* current maximum length, things will explode. If it is negative
|
|
|
|
|
* at most -len characters will be trimmed off the end.
|
2009-04-01 20:13:28 +00:00
|
|
|
* \retval A pointer to the resulting string.
|
|
|
|
|
*/
|
2008-12-13 08:36:35 +00:00
|
|
|
AST_INLINE_API(
|
2008-12-14 18:16:28 +00:00
|
|
|
char *ast_str_truncate(struct ast_str *buf, ssize_t len),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
|
if (len < 0) {
|
2011-11-23 19:58:19 +00:00
|
|
|
if ((typeof(buf->__AST_STR_USED)) -len >= buf->__AST_STR_USED) {
|
|
|
|
|
buf->__AST_STR_USED = 0;
|
|
|
|
|
} else {
|
|
|
|
|
buf->__AST_STR_USED += len;
|
|
|
|
|
}
|
2008-12-13 08:36:35 +00:00
|
|
|
} else {
|
2008-12-18 15:25:15 +00:00
|
|
|
buf->__AST_STR_USED = len;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
|
|
|
|
|
return buf->__AST_STR_STR;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2007-01-19 18:00:00 +00:00
|
|
|
/*
|
|
|
|
|
* AST_INLINE_API() is a macro that takes a block of code as an argument.
|
|
|
|
|
* Using preprocessor #directives in the argument is not supported by all
|
|
|
|
|
* compilers, and it is a bit of an obfuscation anyways, so avoid it.
|
|
|
|
|
* As a workaround, define a macro that produces either its argument
|
|
|
|
|
* or nothing, and use that instead of #ifdef/#endif within the
|
|
|
|
|
* argument to AST_INLINE_API().
|
|
|
|
|
*/
|
|
|
|
|
#if defined(DEBUG_THREADLOCALS)
|
|
|
|
|
#define _DB1(x) x
|
|
|
|
|
#else
|
|
|
|
|
#define _DB1(x)
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-12-16 09:33:31 +00:00
|
|
|
/*!
|
|
|
|
|
* Make space in a new string (e.g. to read in data from a file)
|
|
|
|
|
*/
|
2008-10-14 22:38:06 +00:00
|
|
|
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
|
|
|
|
|
AST_INLINE_API(
|
|
|
|
|
int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function),
|
|
|
|
|
{
|
2008-11-19 01:02:45 +00:00
|
|
|
struct ast_str *old_buf = *buf;
|
2008-10-14 22:38:06 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
if (new_len <= (*buf)->__AST_STR_LEN)
|
2008-10-14 22:38:06 +00:00
|
|
|
return 0; /* success */
|
2008-12-18 15:25:15 +00:00
|
|
|
if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
|
2008-10-14 22:38:06 +00:00
|
|
|
return -1; /* cannot extend */
|
|
|
|
|
*buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
|
2008-11-19 01:02:45 +00:00
|
|
|
if (*buf == NULL) {
|
|
|
|
|
*buf = old_buf;
|
2008-10-14 22:38:06 +00:00
|
|
|
return -1;
|
2008-11-19 01:02:45 +00:00
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
if ((*buf)->__AST_STR_TS != DS_MALLOC) {
|
|
|
|
|
pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
|
2008-10-14 22:38:06 +00:00
|
|
|
_DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
(*buf)->__AST_STR_LEN = new_len;
|
2008-10-14 22:38:06 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
#define ast_str_make_space(a,b) _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
|
|
|
|
|
#else
|
2006-12-16 09:33:31 +00:00
|
|
|
AST_INLINE_API(
|
|
|
|
|
int ast_str_make_space(struct ast_str **buf, size_t new_len),
|
|
|
|
|
{
|
2008-11-19 01:02:45 +00:00
|
|
|
struct ast_str *old_buf = *buf;
|
2007-01-04 23:18:36 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
if (new_len <= (*buf)->__AST_STR_LEN)
|
2006-12-16 09:33:31 +00:00
|
|
|
return 0; /* success */
|
2008-12-18 15:25:15 +00:00
|
|
|
if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
|
2006-12-16 09:33:31 +00:00
|
|
|
return -1; /* cannot extend */
|
2007-01-10 19:02:58 +00:00
|
|
|
*buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
|
2008-11-19 01:02:45 +00:00
|
|
|
if (*buf == NULL) {
|
|
|
|
|
*buf = old_buf;
|
2006-12-16 09:33:31 +00:00
|
|
|
return -1;
|
2008-11-19 01:02:45 +00:00
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
if ((*buf)->__AST_STR_TS != DS_MALLOC) {
|
|
|
|
|
pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
|
2007-01-19 18:00:00 +00:00
|
|
|
_DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
|
2007-01-04 23:18:36 +00:00
|
|
|
}
|
2006-12-16 09:33:31 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
(*buf)->__AST_STR_LEN = new_len;
|
2008-10-14 00:08:52 +00:00
|
|
|
return 0;
|
2006-12-16 09:33:31 +00:00
|
|
|
}
|
|
|
|
|
)
|
2008-10-14 22:38:06 +00:00
|
|
|
#endif
|
2006-12-16 09:33:31 +00:00
|
|
|
|
2010-06-22 12:58:28 +00:00
|
|
|
AST_INLINE_API(
|
|
|
|
|
int ast_str_copy_string(struct ast_str **dst, struct ast_str *src),
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* make sure our destination is large enough */
|
|
|
|
|
if (src->__AST_STR_USED + 1 > (*dst)->__AST_STR_LEN) {
|
|
|
|
|
if (ast_str_make_space(dst, src->__AST_STR_USED + 1)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy((*dst)->__AST_STR_STR, src->__AST_STR_STR, src->__AST_STR_USED + 1);
|
|
|
|
|
(*dst)->__AST_STR_USED = src->__AST_STR_USED;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
#define ast_str_alloca(init_len) \
|
|
|
|
|
({ \
|
|
|
|
|
struct ast_str *__ast_str_buf; \
|
|
|
|
|
__ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len); \
|
2008-12-18 15:25:15 +00:00
|
|
|
__ast_str_buf->__AST_STR_LEN = init_len; \
|
|
|
|
|
__ast_str_buf->__AST_STR_USED = 0; \
|
|
|
|
|
__ast_str_buf->__AST_STR_TS = DS_ALLOCA; \
|
|
|
|
|
__ast_str_buf->__AST_STR_STR[0] = '\0'; \
|
2008-12-13 08:36:35 +00:00
|
|
|
(__ast_str_buf); \
|
|
|
|
|
})
|
2006-12-15 22:08:46 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Retrieve a thread locally stored dynamic string
|
|
|
|
|
*
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param ts This is a pointer to the thread storage structure declared by using
|
2006-12-15 22:08:46 +00:00
|
|
|
* the AST_THREADSTORAGE macro. If declared with
|
|
|
|
|
* AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be
|
|
|
|
|
* (&my_buf).
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param init_len This is the initial length of the thread's dynamic string. The
|
2006-12-15 22:08:46 +00:00
|
|
|
* current length may be bigger if previous operations in this thread have
|
|
|
|
|
* caused it to increase.
|
|
|
|
|
*
|
|
|
|
|
* \return This function will return the thread locally stored dynamic string
|
|
|
|
|
* associated with the thread storage management variable passed as the
|
|
|
|
|
* first argument.
|
|
|
|
|
* The result will be NULL in the case of a memory allocation error.
|
|
|
|
|
*
|
|
|
|
|
* Example usage:
|
|
|
|
|
* \code
|
|
|
|
|
* AST_THREADSTORAGE(my_str, my_str_init);
|
|
|
|
|
* #define MY_STR_INIT_SIZE 128
|
|
|
|
|
* ...
|
|
|
|
|
* void my_func(const char *fmt, ...)
|
|
|
|
|
* {
|
2006-12-15 23:10:42 +00:00
|
|
|
* struct ast_str *buf;
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
2006-12-15 22:08:46 +00:00
|
|
|
* return;
|
|
|
|
|
* ...
|
|
|
|
|
* }
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
2007-01-04 23:38:10 +00:00
|
|
|
#if !defined(DEBUG_THREADLOCALS)
|
2006-12-15 22:08:46 +00:00
|
|
|
AST_INLINE_API(
|
2006-12-15 23:10:42 +00:00
|
|
|
struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
|
2006-12-15 22:08:46 +00:00
|
|
|
size_t init_len),
|
|
|
|
|
{
|
2006-12-15 23:10:42 +00:00
|
|
|
struct ast_str *buf;
|
2006-12-15 22:08:46 +00:00
|
|
|
|
2007-01-10 19:02:58 +00:00
|
|
|
buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
|
2006-12-18 16:24:44 +00:00
|
|
|
if (buf == NULL)
|
2006-12-15 22:08:46 +00:00
|
|
|
return NULL;
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
if (!buf->__AST_STR_LEN) {
|
|
|
|
|
buf->__AST_STR_LEN = init_len;
|
|
|
|
|
buf->__AST_STR_USED = 0;
|
|
|
|
|
buf->__AST_STR_TS = ts;
|
2006-12-15 22:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
)
|
2007-01-04 23:38:10 +00:00
|
|
|
#else /* defined(DEBUG_THREADLOCALS) */
|
|
|
|
|
AST_INLINE_API(
|
|
|
|
|
struct ast_str *__ast_str_thread_get(struct ast_threadstorage *ts,
|
|
|
|
|
size_t init_len, const char *file, const char *function, unsigned int line),
|
|
|
|
|
{
|
|
|
|
|
struct ast_str *buf;
|
|
|
|
|
|
2007-01-10 19:02:58 +00:00
|
|
|
buf = (struct ast_str *)__ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line);
|
2007-01-04 23:38:10 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
|
return NULL;
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2008-12-18 15:25:15 +00:00
|
|
|
if (!buf->__AST_STR_LEN) {
|
|
|
|
|
buf->__AST_STR_LEN = init_len;
|
|
|
|
|
buf->__AST_STR_USED = 0;
|
|
|
|
|
buf->__AST_STR_TS = ts;
|
2007-01-04 23:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#define ast_str_thread_get(ts, init_len) __ast_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
|
|
|
#endif /* defined(DEBUG_THREADLOCALS) */
|
2006-12-15 22:08:46 +00:00
|
|
|
|
|
|
|
|
/*!
|
2006-12-15 23:10:42 +00:00
|
|
|
* \brief Error codes from __ast_str_helper()
|
2006-12-15 22:08:46 +00:00
|
|
|
* The undelying processing to manipulate dynamic string is done
|
2008-11-19 01:02:45 +00:00
|
|
|
* by __ast_str_helper(), which can return a success or a
|
|
|
|
|
* permanent failure (e.g. no memory).
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
|
|
|
|
enum {
|
2008-11-08 21:46:43 +00:00
|
|
|
/*! An error has occurred and the contents of the dynamic string
|
2006-12-15 22:08:46 +00:00
|
|
|
* are undefined */
|
|
|
|
|
AST_DYNSTR_BUILD_FAILED = -1,
|
|
|
|
|
/*! The buffer size for the dynamic string had to be increased, and
|
2006-12-15 23:10:42 +00:00
|
|
|
* __ast_str_helper() needs to be called again after
|
2008-11-19 01:02:45 +00:00
|
|
|
* a va_end() and va_start(). This return value is legacy and will
|
|
|
|
|
* no longer be used.
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
|
|
|
|
AST_DYNSTR_BUILD_RETRY = -2
|
|
|
|
|
};
|
|
|
|
|
|
2008-11-19 01:02:45 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Core functionality of ast_str_(set|append)_va
|
|
|
|
|
*
|
|
|
|
|
* The arguments to this function are the same as those described for
|
|
|
|
|
* ast_str_set_va except for an addition argument, append.
|
|
|
|
|
* If append is non-zero, this will append to the current string instead of
|
|
|
|
|
* writing over it.
|
|
|
|
|
*
|
|
|
|
|
* AST_DYNSTR_BUILD_RETRY is a legacy define. It should probably never
|
|
|
|
|
* again be used.
|
|
|
|
|
*
|
|
|
|
|
* A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
|
|
|
|
|
*
|
|
|
|
|
* A return value greater than or equal to zero indicates the number of
|
|
|
|
|
* characters that have been written, not including the terminating '\0'.
|
|
|
|
|
* In the append case, this only includes the number of characters appended.
|
|
|
|
|
*
|
|
|
|
|
* \note This function should never need to be called directly. It should
|
|
|
|
|
* through calling one of the other functions or macros defined in this
|
|
|
|
|
* file.
|
|
|
|
|
*/
|
2009-03-11 00:29:59 +00:00
|
|
|
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
|
2010-02-17 21:22:40 +00:00
|
|
|
int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
|
2009-03-11 00:29:59 +00:00
|
|
|
int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func);
|
|
|
|
|
#define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__)
|
|
|
|
|
#else
|
2010-02-17 21:22:40 +00:00
|
|
|
int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, ssize_t max_len,
|
2008-11-29 17:57:39 +00:00
|
|
|
int append, const char *fmt, va_list ap);
|
2009-03-11 00:29:59 +00:00
|
|
|
#endif
|
2010-02-17 21:22:40 +00:00
|
|
|
char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len,
|
2008-12-13 08:36:35 +00:00
|
|
|
const char *src, size_t maxsrc, int append, int escapecommas);
|
2008-11-19 01:02:45 +00:00
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
/*!
|
2006-12-15 23:10:42 +00:00
|
|
|
* \brief Set a dynamic string from a va_list
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param buf This is the address of a pointer to a struct ast_str.
|
2006-12-15 23:10:42 +00:00
|
|
|
* If it is retrieved using ast_str_thread_get, the
|
|
|
|
|
struct ast_threadstorage pointer will need to
|
2006-12-15 22:08:46 +00:00
|
|
|
* be updated in the case that the buffer has to be reallocated to
|
|
|
|
|
* accommodate a longer string than what it currently has space for.
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param max_len This is the maximum length to allow the string buffer to grow
|
2006-12-15 22:08:46 +00:00
|
|
|
* to. If this is set to 0, then there is no maximum length.
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param fmt This is the format string (printf style)
|
|
|
|
|
* \param ap This is the va_list
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
|
|
|
|
* \return The return value of this function is the same as that of the printf
|
|
|
|
|
* family of functions.
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* Example usage (the first part is only for thread-local storage)
|
2006-12-15 22:08:46 +00:00
|
|
|
* \code
|
|
|
|
|
* AST_THREADSTORAGE(my_str, my_str_init);
|
|
|
|
|
* #define MY_STR_INIT_SIZE 128
|
|
|
|
|
* ...
|
|
|
|
|
* void my_func(const char *fmt, ...)
|
|
|
|
|
* {
|
2006-12-15 23:10:42 +00:00
|
|
|
* struct ast_str *buf;
|
2006-12-15 22:08:46 +00:00
|
|
|
* va_list ap;
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
|
2006-12-15 22:08:46 +00:00
|
|
|
* return;
|
|
|
|
|
* ...
|
|
|
|
|
* va_start(fmt, ap);
|
2006-12-15 23:10:42 +00:00
|
|
|
* ast_str_set_va(&buf, 0, fmt, ap);
|
2006-12-15 22:08:46 +00:00
|
|
|
* va_end(ap);
|
|
|
|
|
*
|
|
|
|
|
* printf("This is the string we just built: %s\n", buf->str);
|
|
|
|
|
* ...
|
|
|
|
|
* }
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
2010-02-17 21:22:40 +00:00
|
|
|
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),
|
2008-11-19 01:02:45 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper(buf, max_len, 0, fmt, ap);
|
|
|
|
|
}
|
|
|
|
|
)
|
2006-12-15 22:08:46 +00:00
|
|
|
|
|
|
|
|
/*!
|
2006-12-15 23:10:42 +00:00
|
|
|
* \brief Append to a dynamic string using a va_list
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* Same as ast_str_set_va(), but append to the current content.
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
2010-02-17 21:22:40 +00:00
|
|
|
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),
|
2008-11-19 01:02:45 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper(buf, max_len, 1, fmt, ap);
|
|
|
|
|
}
|
|
|
|
|
)
|
2006-12-15 22:08:46 +00:00
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
/*!\brief Set a dynamic string to a non-NULL terminated substring. */
|
2010-02-17 21:22:40 +00:00
|
|
|
AST_INLINE_API(char *ast_str_set_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!\brief Append a non-NULL terminated substring to the end of a dynamic string. */
|
2010-02-17 21:22:40 +00:00
|
|
|
AST_INLINE_API(char *ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 0);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!\brief Set a dynamic string to a non-NULL terminated substring, with escaping of commas. */
|
2010-02-17 21:22:40 +00:00
|
|
|
AST_INLINE_API(char *ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!\brief Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas. */
|
2010-02-17 21:22:40 +00:00
|
|
|
AST_INLINE_API(char *ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
|
return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 1);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
/*!
|
2006-12-15 23:10:42 +00:00
|
|
|
* \brief Set a dynamic string using variable arguments
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param buf This is the address of a pointer to a struct ast_str which should
|
2006-12-15 23:10:42 +00:00
|
|
|
* have been retrieved using ast_str_thread_get. It will need to
|
2006-12-15 22:08:46 +00:00
|
|
|
* be updated in the case that the buffer has to be reallocated to
|
|
|
|
|
* accomodate a longer string than what it currently has space for.
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param max_len This is the maximum length to allow the string buffer to grow
|
2006-12-15 22:08:46 +00:00
|
|
|
* to. If this is set to 0, then there is no maximum length.
|
2006-12-15 23:10:42 +00:00
|
|
|
* If set to -1, we are bound to the current maximum length.
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param fmt This is the format string (printf style)
|
2006-12-15 22:08:46 +00:00
|
|
|
*
|
|
|
|
|
* \return The return value of this function is the same as that of the printf
|
|
|
|
|
* family of functions.
|
|
|
|
|
*
|
2006-12-15 23:10:42 +00:00
|
|
|
* All the rest is the same as ast_str_set_va()
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2008-11-29 17:57:39 +00:00
|
|
|
int __attribute__((format(printf, 3, 4))) ast_str_set(
|
2010-02-17 21:22:40 +00:00
|
|
|
struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
|
2006-12-15 22:08:46 +00:00
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2006-12-15 23:10:42 +00:00
|
|
|
res = ast_str_set_va(buf, max_len, fmt, ap);
|
2006-12-15 22:08:46 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Append to a thread local dynamic string
|
|
|
|
|
*
|
|
|
|
|
* The arguments, return values, and usage of this function are the same as
|
2006-12-15 23:10:42 +00:00
|
|
|
* ast_str_set(), but the new data is appended to the current value.
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2008-11-29 17:57:39 +00:00
|
|
|
int __attribute__((format(printf, 3, 4))) ast_str_append(
|
2010-02-17 21:22:40 +00:00
|
|
|
struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
|
2006-12-15 22:08:46 +00:00
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2006-12-15 23:10:42 +00:00
|
|
|
res = ast_str_append_va(buf, max_len, fmt, ap);
|
2006-12-15 22:08:46 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2011-10-19 07:45:06 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Check if a string is only digits
|
|
|
|
|
*
|
|
|
|
|
* \retval 1 The string contains only digits
|
|
|
|
|
* \retval 0 The string contains non-digit characters
|
|
|
|
|
*/
|
|
|
|
|
AST_INLINE_API(
|
2011-10-20 15:17:53 +00:00
|
|
|
int ast_check_digits(const char *arg),
|
2011-10-19 07:45:06 +00:00
|
|
|
{
|
2011-10-20 15:17:53 +00:00
|
|
|
while (*arg) {
|
|
|
|
|
if (*arg < '0' || *arg > '9') {
|
2011-10-19 07:45:06 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-10-20 15:17:53 +00:00
|
|
|
arg++;
|
2011-10-19 07:45:06 +00:00
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
Merged revisions 80362 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r80362 | russell | 2007-08-22 15:21:36 -0500 (Wed, 22 Aug 2007) | 34 lines
Merge changes from team/russell/iax_refcount.
This set of changes fixes problems with the handling of iax2_user and iax2_peer
objects. It was very possible for a thread to still hold a reference to one of
these objects while a reload operation tries to delete them. The fix here is to
ensure that all references to these objects are tracked so that they can't go away
while still in use.
To accomplish this, I used the astobj2 reference counted object model. This
code has been in one of Luigi Rizzo's branches for a long time and was primarily
developed by one of his students, Marta Carbone. I wanted to go ahead and bring
this in to 1.4 because there are other problems similar to the ones fixed by these
changes, so we might as well go ahead and use the new astobj if we're going to go
through all of the work necessary to fix the problems.
As a nice side benefit of these changes, peer and user handling got more efficient.
Using astobj2 lets us not hold the container lock for peers or users nearly as long
while iterating. Also, by changing a define at the top of chan_iax2.c, the objects
will be distributed in a hash table, drastically increasing lookup speed in these
containers, which will have a very big impact on systems that have a large number of
users or peers.
The use of the hash table will be made the default in trunk. It is not the default
in 1.4 because it changes the behavior slightly. Previously, since peers and users
were stored in memory in the same order they were specified in the configuration file,
you could influence peer and user matching order based on the order they are specified
in the configuration. The hash table does not guarantee any order in the container,
so this behavior will be going away. It just means that you have to be a little
more careful ensuring that peers and users are matched explicitly and not forcing
chan_iax2 to have to guess which user is the right one based on secret, host, and
access list settings, instead of simply using the username.
If you have any questions, feel free to ask on the asterisk-dev list.
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@80387 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-08-22 20:44:23 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Compute a hash value on a string
|
|
|
|
|
*
|
|
|
|
|
* This famous hash algorithm was written by Dan Bernstein and is
|
|
|
|
|
* commonly used.
|
|
|
|
|
*
|
|
|
|
|
* http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
|
*/
|
2009-01-19 21:42:46 +00:00
|
|
|
static force_inline int attribute_pure ast_str_hash(const char *str)
|
Merged revisions 80362 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r80362 | russell | 2007-08-22 15:21:36 -0500 (Wed, 22 Aug 2007) | 34 lines
Merge changes from team/russell/iax_refcount.
This set of changes fixes problems with the handling of iax2_user and iax2_peer
objects. It was very possible for a thread to still hold a reference to one of
these objects while a reload operation tries to delete them. The fix here is to
ensure that all references to these objects are tracked so that they can't go away
while still in use.
To accomplish this, I used the astobj2 reference counted object model. This
code has been in one of Luigi Rizzo's branches for a long time and was primarily
developed by one of his students, Marta Carbone. I wanted to go ahead and bring
this in to 1.4 because there are other problems similar to the ones fixed by these
changes, so we might as well go ahead and use the new astobj if we're going to go
through all of the work necessary to fix the problems.
As a nice side benefit of these changes, peer and user handling got more efficient.
Using astobj2 lets us not hold the container lock for peers or users nearly as long
while iterating. Also, by changing a define at the top of chan_iax2.c, the objects
will be distributed in a hash table, drastically increasing lookup speed in these
containers, which will have a very big impact on systems that have a large number of
users or peers.
The use of the hash table will be made the default in trunk. It is not the default
in 1.4 because it changes the behavior slightly. Previously, since peers and users
were stored in memory in the same order they were specified in the configuration file,
you could influence peer and user matching order based on the order they are specified
in the configuration. The hash table does not guarantee any order in the container,
so this behavior will be going away. It just means that you have to be a little
more careful ensuring that peers and users are matched explicitly and not forcing
chan_iax2 to have to guess which user is the right one based on secret, host, and
access list settings, instead of simply using the username.
If you have any questions, feel free to ask on the asterisk-dev list.
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@80387 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-08-22 20:44:23 +00:00
|
|
|
{
|
|
|
|
|
int hash = 5381;
|
|
|
|
|
|
|
|
|
|
while (*str)
|
|
|
|
|
hash = hash * 33 ^ *str++;
|
|
|
|
|
|
|
|
|
|
return abs(hash);
|
|
|
|
|
}
|
|
|
|
|
|
Improve performance of the ast_event cache functionality.
This code comes from svn/asterisk/team/russell/event_performance/.
Here is a summary of the changes that have been made, in order of both
invasiveness and performance impact, from smallest to largest.
1) Asterisk 1.6.1 introduces some additional logic to be able to handle
distributed device state. This functionality comes at a cost.
One relatively minor change in this patch is that the extra processing
required for distributed device state is now completely bypassed if
it's not needed.
2) One of the things that I noticed when profiling this code was that a
_lot_ of time was spent doing string comparisons. I changed the way
strings are represented in an event to include a hash value at the front.
So, before doing a string comparison, we do an integer comparison on the
hash.
3) Finally, the code that handles the event cache has been re-written.
I tried to do this in a such a way that it had minimal impact on the API.
I did have to change one API call, though - ast_event_queue_and_cache().
However, the way it works now is nicer, IMO. Each type of event that
can be cached (MWI, device state) has its own hash table and rules for
hashing and comparing objects. This by far made the biggest impact on
performance.
For additional details regarding this code and how it was tested, please see the
review request.
(closes issue #14738)
Reported by: russell
Review: http://reviewboard.digium.com/r/205/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@184339 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-03-25 21:57:19 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Compute a hash value on a string
|
|
|
|
|
*
|
|
|
|
|
* \param[in] str The string to add to the hash
|
|
|
|
|
* \param[in] hash The hash value to add to
|
|
|
|
|
*
|
|
|
|
|
* \details
|
|
|
|
|
* This version of the function is for when you need to compute a
|
|
|
|
|
* string hash of more than one string.
|
|
|
|
|
*
|
|
|
|
|
* This famous hash algorithm was written by Dan Bernstein and is
|
|
|
|
|
* commonly used.
|
|
|
|
|
*
|
|
|
|
|
* \sa http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
|
*/
|
|
|
|
|
static force_inline int ast_str_hash_add(const char *str, int hash)
|
|
|
|
|
{
|
|
|
|
|
while (*str)
|
|
|
|
|
hash = hash * 33 ^ *str++;
|
|
|
|
|
|
|
|
|
|
return abs(hash);
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-14 16:53:38 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Compute a hash value on a case-insensitive string
|
|
|
|
|
*
|
|
|
|
|
* Uses the same hash algorithm as ast_str_hash, but converts
|
|
|
|
|
* all characters to lowercase prior to computing a hash. This
|
|
|
|
|
* allows for easy case-insensitive lookups in a hash table.
|
|
|
|
|
*/
|
2009-01-19 21:42:46 +00:00
|
|
|
static force_inline int attribute_pure ast_str_case_hash(const char *str)
|
2008-11-14 16:53:38 +00:00
|
|
|
{
|
|
|
|
|
int hash = 5381;
|
|
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
|
hash = hash * 33 ^ tolower(*str++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return abs(hash);
|
|
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
|
2005-06-24 22:50:07 +00:00
|
|
|
#endif /* _ASTERISK_STRINGS_H */
|