Sun Dec 18 20:55:42 2011

Asterisk developer's documentation


strings.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief String manipulation functions
00021  */
00022 
00023 #ifndef _ASTERISK_STRINGS_H
00024 #define _ASTERISK_STRINGS_H
00025 
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <stdarg.h>
00029 
00030 #include "asterisk/inline_api.h"
00031 #include "asterisk/compiler.h"
00032 #include "asterisk/compat.h"
00033 
00034 static force_inline int ast_strlen_zero(const char *s)
00035 {
00036    return (!s || (*s == '\0'));
00037 }
00038 
00039 /*! \brief returns the equivalent of logic or for strings:
00040  * first one if not empty, otherwise second one.
00041  */
00042 #define S_OR(a, b)   (!ast_strlen_zero(a) ? (a) : (b))
00043 
00044 /*!
00045   \brief Gets a pointer to the first non-whitespace character in a string.
00046   \param ast_skip_blanks function being used
00047   \param str the input string
00048   \return a pointer to the first non-whitespace character
00049  */
00050 AST_INLINE_API(
00051 char *ast_skip_blanks(const char *str),
00052 {
00053    while (*str && ((unsigned char) *str) < 33)
00054       str++;
00055    return (char *)str;
00056 }
00057 )
00058 
00059 /*!
00060   \brief Trims trailing whitespace characters from a string.
00061   \param ast_trim_blanks function being used
00062   \param str the input string
00063   \return a pointer to the modified string
00064  */
00065 AST_INLINE_API(
00066 char *ast_trim_blanks(char *str),
00067 {
00068    char *work = str;
00069 
00070    if (work) {
00071       work += strlen(work) - 1;
00072       /* It's tempting to only want to erase after we exit this loop, 
00073          but since ast_trim_blanks *could* receive a constant string
00074          (which we presumably wouldn't have to touch), we shouldn't
00075          actually set anything unless we must, and it's easier just
00076          to set each position to \0 than to keep track of a variable
00077          for it */
00078       while ((work >= str) && ((unsigned char) *work) < 33)
00079          *(work--) = '\0';
00080    }
00081    return str;
00082 }
00083 )
00084 
00085 /*!
00086   \brief Gets a pointer to first whitespace character in a string.
00087   \param ast_skip_noblanks function being used
00088   \param str the input string
00089   \return a pointer to the first whitespace character
00090  */
00091 AST_INLINE_API(
00092 char *ast_skip_nonblanks(char *str),
00093 {
00094    while (*str && ((unsigned char) *str) > 32)
00095       str++;
00096    return str;
00097 }
00098 )
00099   
00100 /*!
00101   \brief Strip leading/trailing whitespace from a string.
00102   \param s The string to be stripped (will be modified).
00103   \return The stripped string.
00104 
00105   This functions strips all leading and trailing whitespace
00106   characters from the input string, and returns a pointer to
00107   the resulting string. The string is modified in place.
00108 */
00109 AST_INLINE_API(
00110 char *ast_strip(char *s),
00111 {
00112    s = ast_skip_blanks(s);
00113    if (s)
00114       ast_trim_blanks(s);
00115    return s;
00116 } 
00117 )
00118 
00119 /*!
00120   \brief Strip leading/trailing whitespace and quotes from a string.
00121   \param s The string to be stripped (will be modified).
00122   \param beg_quotes The list of possible beginning quote characters.
00123   \param end_quotes The list of matching ending quote characters.
00124   \return The stripped string.
00125 
00126   This functions strips all leading and trailing whitespace
00127   characters from the input string, and returns a pointer to
00128   the resulting string. The string is modified in place.
00129 
00130   It can also remove beginning and ending quote (or quote-like)
00131   characters, in matching pairs. If the first character of the
00132   string matches any character in beg_quotes, and the last
00133   character of the string is the matching character in
00134   end_quotes, then they are removed from the string.
00135 
00136   Examples:
00137   \code
00138   ast_strip_quoted(buf, "\"", "\"");
00139   ast_strip_quoted(buf, "'", "'");
00140   ast_strip_quoted(buf, "[{(", "]})");
00141   \endcode
00142  */
00143 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
00144 
00145 /*!
00146   \brief Strip backslash for "escaped" semicolons.
00147   \brief s The string to be stripped (will be modified).
00148   \return The stripped string.
00149  */
00150 char *ast_unescape_semicolon(char *s);
00151 
00152 /*!
00153   \brief Size-limited null-terminating string copy.
00154   \param ast_copy_string function being used
00155   \param dst The destination buffer.
00156   \param src The source string
00157   \param size The size of the destination buffer
00158   \return Nothing.
00159 
00160   This is similar to \a strncpy, with two important differences:
00161     - the destination buffer will \b always be null-terminated
00162     - the destination buffer is not filled with zeros past the copied string length
00163   These differences make it slightly more efficient, and safer to use since it will
00164   not leave the destination buffer unterminated. There is no need to pass an artificially
00165   reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
00166   to be initialized to zeroes prior to calling this function.
00167 */
00168 AST_INLINE_API(
00169 void ast_copy_string(char *dst, const char *src, size_t size),
00170 {
00171    while (*src && size) {
00172       *dst++ = *src++;
00173       size--;
00174    }
00175    if (__builtin_expect(!size, 0))
00176       dst--;
00177    *dst = '\0';
00178 }
00179 )
00180 
00181 
00182 /*!
00183   \brief Build a string in a buffer, designed to be called repeatedly
00184   
00185   This is a wrapper for snprintf, that properly handles the buffer pointer
00186   and buffer space available.
00187 
00188   \param buffer current position in buffer to place string into (will be updated on return)
00189   \param space remaining space in buffer (will be updated on return)
00190   \param fmt printf-style format string
00191   \return 0 on success, non-zero on failure.
00192 */
00193 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
00194 
00195 /*!
00196   \brief Build a string in a buffer, designed to be called repeatedly
00197   
00198   This is a wrapper for snprintf, that properly handles the buffer pointer
00199   and buffer space available.
00200 
00201   \return 0 on success, non-zero on failure.
00202   \param buffer current position in buffer to place string into (will be updated on return)
00203   \param space remaining space in buffer (will be updated on return)
00204   \param fmt printf-style format string
00205   \param ap varargs list of arguments for format
00206 */
00207 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap);
00208 
00209 /*! Make sure something is true */
00210 /*!
00211  * Determine if a string containing a boolean value is "true".
00212  * 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".  
00213  *
00214  * Returns 0 if val is a NULL pointer, -1 if "true", and 0 otherwise.
00215  */
00216 int ast_true(const char *val);
00217 
00218 /*! Make sure something is false */
00219 /*!
00220  * Determine if a string containing a boolean value is "false".
00221  * 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".  
00222  *
00223  * Returns 0 if val is a NULL pointer, -1 if "false", and 0 otherwise.
00224  */
00225 int ast_false(const char *val);
00226 
00227 /*
00228   \brief Join an array of strings into a single string.
00229   \param s the resulting string buffer
00230   \param len the length of the result buffer, s
00231   \param w an array of strings to join
00232 
00233   This function will join all of the strings in the array 'w' into a single
00234   string.  It will also place a space in the result buffer in between each
00235   string from 'w'.
00236 */
00237 void ast_join(char *s, size_t len, char * const w[]);
00238 
00239 /*
00240   \brief Parse a time (integer) string.
00241   \param src String to parse
00242   \param dst Destination
00243   \param _default Value to use if the string does not contain a valid time
00244   \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
00245   \return zero on success, non-zero on failure
00246 */
00247 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
00248 
00249 /* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
00250 
00251 struct ast_realloca {
00252    char *ptr;
00253    int alloclen;
00254 };
00255 
00256 #define ast_restrdupa(ra, s) \
00257    ({ \
00258       if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \
00259          strcpy((ra)->ptr, s); \
00260       } else { \
00261          (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \
00262          if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \
00263       } \
00264       (ra)->ptr; \
00265    })
00266 
00267 /*!
00268  * \brief Compute a hash value on a string
00269  *
00270  * This famous hash algorithm was written by Dan Bernstein and is
00271  * commonly used.
00272  *
00273  * http://www.cse.yorku.ca/~oz/hash.html
00274  */
00275 static force_inline int ast_str_hash(const char *str)
00276 {
00277    int hash = 5381;
00278 
00279    while (*str)
00280       hash = hash * 33 ^ *str++;
00281 
00282    return abs(hash);
00283 }
00284 
00285 #endif /* _ASTERISK_STRINGS_H */

Generated on Sun Dec 18 20:55:42 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.6