diff options
author | Kevin P. Fleming <kpfleming@digium.com> | 2005-06-24 22:50:07 +0000 |
---|---|---|
committer | Kevin P. Fleming <kpfleming@digium.com> | 2005-06-24 22:50:07 +0000 |
commit | b83175862e4557b5b3ffae80e54fa9b852ad79e2 (patch) | |
tree | 50a9a4abdd0dbc7c6cc8e80129c958a9f3581290 /include/asterisk/strings.h | |
parent | 3e44b65090f3f2b5b3e0c1e8e11bf34a1b01afc8 (diff) |
add new header files
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6009 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'include/asterisk/strings.h')
-rwxr-xr-x | include/asterisk/strings.h | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h new file mode 100755 index 000000000..eee85a551 --- /dev/null +++ b/include/asterisk/strings.h @@ -0,0 +1,226 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * String manipulation functions + * + * Copyright (C) 2005, Digium, Inc. + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#ifndef _ASTERISK_STRINGS_H +#define _ASTERISK_STRINGS_H + +#include <string.h> + +#include "asterisk/compiler.h" + +static inline int ast_strlen_zero(const char *s) +{ + return (*s == '\0'); +} + +/*! + \brief Gets a pointer to the first non-whitespace character in a string. + \param str the input string + \return a pointer to the first non-whitespace character + */ +char *ast_skip_blanks(char *str); +#if !defined(LOW_MEMORY) && !defined(AST_API_MODULE) +extern inline +#endif +#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) +char *ast_skip_blanks(char *str) +{ + while (*str && *str < 33) + str++; + return str; +} +#endif + +/*! + \brief Trims trailing whitespace characters from a string. + \param str the input string + \return a pointer to the NULL following the string + */ +char *ast_trim_blanks(char *str); +#if !defined(LOW_MEMORY) && !defined(AST_API_MODULE) +extern inline +#endif +#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) +char *ast_trim_blanks(char *str) +{ + 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 */ + while ((work >= str) && *work < 33) + *(work--) = '\0'; + } + return str; +} +#endif + +/*! + \brief Gets a pointer to first whitespace character in a string. + \param str the input string + \return a pointer to the first whitespace character + */ +char *ast_skip_nonblanks(char *str); +#if !defined(LOW_MEMORY) && !defined(AST_API_MODULE) +extern inline +#endif +#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) +char *ast_skip_nonblanks(char *str) +{ + while (*str && *str > 32) + str++; + return str; +} +#endif + +/*! + \brief Strip leading/trailing whitespace from a string. + \param s The string to be stripped (will be modified). + \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. +*/ +char *ast_strip(char *s); +#if !defined(LOW_MEMORY) && !defined(AST_API_MODULE) +extern inline +#endif +#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) +char *ast_strip(char *s) +{ + s = ast_skip_blanks(s); + if (s) + ast_trim_blanks(s); + return s; +} +#endif + +/*! + \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); + +/*! + \brief Size-limited null-terminating string copy. + \param dst The destination buffer. + \param src The source string + \param size The size of the destination buffer + \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. +*/ +void ast_copy_string(char *dst, const char *src, size_t size); +#if !defined(LOW_MEMORY) && !defined(AST_API_MODULE) +extern inline +#endif +#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) +void ast_copy_string(char *dst, const char *src, size_t size) +{ + while (*src && size) { + *dst++ = *src++; + size--; + } + if (__builtin_expect(!size, 0)) + dst--; + *dst = '\0'; +} +#endif + +/*! + \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 +*/ +int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); + +/*! Make sure something is true */ +/*! + * Determine if a string containing a boolean value is "true". + * 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". + * + * Returns 0 if val is a NULL pointer, -1 if "true", and 0 otherwise. + */ +int ast_true(const char *val); + +/*! Make sure something is false */ +/*! + * Determine if a string containing a boolean value is "false". + * 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". + * + * Returns 0 if val is a NULL pointer, -1 if "false", and 0 otherwise. + */ +int ast_false(const char *val); + +/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */ + +struct ast_realloca { + char *ptr; + int alloclen; +}; + +#define ast_restrdupa(ra, s) \ + ({ \ + if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \ + strcpy((ra)->ptr, s); \ + } else { \ + (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \ + if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \ + } \ + (ra)->ptr; \ + }) + +#ifdef __linux__ +#define ast_strcasestr strcasestr +#else +extern char *ast_strcasestr(const char *, const char *); +#endif /* __linux__ */ + +#undef AST_API_MODULE +#endif /* _ASTERISK_STRINGS_H */ |