📄 strings.h
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * * 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. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! \file * \brief String manipulation functions */#ifndef _ASTERISK_STRINGS_H#define _ASTERISK_STRINGS_H#include <ctype.h>#include "asterisk/inline_api.h"#include "asterisk/utils.h"#include "asterisk/threadstorage.h"/* You may see casts in this header that may seem useless but they ensure this file is C++ clean */#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;}#elsestatic force_inline int ast_strlen_zero(const char *s){ return (!s || (*s == '\0'));}#endif/*! \brief returns the equivalent of logic or for strings: * first one if not empty, otherwise second one. */#define S_OR(a, b) (!ast_strlen_zero(a) ? (a) : (b))/*! \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>") */#define S_COR(a, b, c) ((a && !ast_strlen_zero(b)) ? (b) : (c))/*! \brief Gets a pointer to the first non-whitespace character in a string. \param ast_skip_blanks function being used \arg str the input string \return a pointer to the first non-whitespace character */AST_INLINE_API(char *ast_skip_blanks(const char *str),{ while (*str && ((unsigned char) *str) < 33) str++; return (char *)str;})/*! \brief Trims trailing whitespace characters from a string. \param ast_skip_blanks function being used \arg str the input string \return a pointer to the modified string */AST_INLINE_API(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) && ((unsigned char) *work) < 33) *(work--) = '\0'; } return str;})/*! \brief Gets a pointer to first whitespace character in a string. \param ast_skip_noblanks function being used \arg str the input string \return a pointer to the first whitespace character */AST_INLINE_API(char *ast_skip_nonblanks(char *str),{ while (*str && ((unsigned char) *str) > 32) str++; return str;}) /*! \brief Strip leading/trailing whitespace from a string. \param ast_strip function ast_strip being used. \arg 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.*/AST_INLINE_API(char *ast_strip(char *s),{ s = ast_skip_blanks(s); if (s) ast_trim_blanks(s); return s;} )/*! \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 Strip backslash for "escaped" semicolons, the string to be stripped (will be modified). \return The stripped string. */char *ast_unescape_semicolon(char *s);/*! \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). \return The converted string. */char *ast_unescape_c(char *s);/*! \brief Size-limited null-terminating string copy. \arg dst The destination buffer. \arg src The source string \arg 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.*/AST_INLINE_API(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';})/*! \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. \arg buffer current position in buffer to place string into (will be updated on return) \arg space remaining space in buffer (will be updated on return) \arg fmt printf-style format string \retval 0 on success \retval non-zero on failure.*/int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));/*! \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*/int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));/*! * \brief 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". * * \retval 0 if val is a NULL pointer. * \retval -1 if "true". * \retval 0 otherwise. */int ast_true(const char *val);/*! * \brief 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". * * \retval 0 if val is a NULL pointer. * \retval -1 if "true". * \retval 0 otherwise. */int ast_false(const char *val);/* * \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'.*/void ast_join(char *s, size_t len, char * const w[]);/* \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 \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details) \retval 0 on success \retval non-zero on failure.*/int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);/* \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);/*! * 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. * * struct ast_str *ds; * * The pointer can be initialized with the following: * * ds = ast_str_create(init_len); * creates a malloc()'ed dynamic string; * * ds = ast_str_alloca(init_len); * creates a string on the stack (not very dynamic!). * * ds = ast_str_thread_get(ts, init_len) * creates a malloc()'ed dynamic string associated to * the thread-local storage key ts * * Finally, the string can be manipulated with the following: * * 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 * XXX storage will be optimized later if needed * 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. */struct ast_str { size_t len; /*!< The current maximum length of the string */ size_t used; /*!< Amount of space used */ struct ast_threadstorage *ts; /*!< What kind of storage is this ? */#define DS_MALLOC ((struct ast_threadstorage *)1)#define DS_ALLOCA ((struct ast_threadstorage *)2)#define DS_STATIC ((struct ast_threadstorage *)3) /* not supported yet */ char str[0]; /*!< The string buffer */};/*! * \brief Create a malloc'ed dynamic length string * * \arg init_len This is the initial length of the string buffer * * \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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -