yading@11: /* yading@11: * Copyright (c) 2007 Mans Rullgard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #ifndef AVUTIL_AVSTRING_H yading@11: #define AVUTIL_AVSTRING_H yading@11: yading@11: #include yading@11: #include "attributes.h" yading@11: yading@11: /** yading@11: * @addtogroup lavu_string yading@11: * @{ yading@11: */ yading@11: yading@11: /** yading@11: * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to yading@11: * the address of the first character in str after the prefix. yading@11: * yading@11: * @param str input string yading@11: * @param pfx prefix to test yading@11: * @param ptr updated if the prefix is matched inside str yading@11: * @return non-zero if the prefix matches, zero otherwise yading@11: */ yading@11: int av_strstart(const char *str, const char *pfx, const char **ptr); yading@11: yading@11: /** yading@11: * Return non-zero if pfx is a prefix of str independent of case. If yading@11: * it is, *ptr is set to the address of the first character in str yading@11: * after the prefix. yading@11: * yading@11: * @param str input string yading@11: * @param pfx prefix to test yading@11: * @param ptr updated if the prefix is matched inside str yading@11: * @return non-zero if the prefix matches, zero otherwise yading@11: */ yading@11: int av_stristart(const char *str, const char *pfx, const char **ptr); yading@11: yading@11: /** yading@11: * Locate the first case-independent occurrence in the string haystack yading@11: * of the string needle. A zero-length string needle is considered to yading@11: * match at the start of haystack. yading@11: * yading@11: * This function is a case-insensitive version of the standard strstr(). yading@11: * yading@11: * @param haystack string to search in yading@11: * @param needle string to search for yading@11: * @return pointer to the located match within haystack yading@11: * or a null pointer if no match yading@11: */ yading@11: char *av_stristr(const char *haystack, const char *needle); yading@11: yading@11: /** yading@11: * Locate the first occurrence of the string needle in the string haystack yading@11: * where not more than hay_length characters are searched. A zero-length yading@11: * string needle is considered to match at the start of haystack. yading@11: * yading@11: * This function is a length-limited version of the standard strstr(). yading@11: * yading@11: * @param haystack string to search in yading@11: * @param needle string to search for yading@11: * @param hay_length length of string to search in yading@11: * @return pointer to the located match within haystack yading@11: * or a null pointer if no match yading@11: */ yading@11: char *av_strnstr(const char *haystack, const char *needle, size_t hay_length); yading@11: yading@11: /** yading@11: * Copy the string src to dst, but no more than size - 1 bytes, and yading@11: * null-terminate dst. yading@11: * yading@11: * This function is the same as BSD strlcpy(). yading@11: * yading@11: * @param dst destination buffer yading@11: * @param src source string yading@11: * @param size size of destination buffer yading@11: * @return the length of src yading@11: * yading@11: * @warning since the return value is the length of src, src absolutely yading@11: * _must_ be a properly 0-terminated string, otherwise this will read beyond yading@11: * the end of the buffer and possibly crash. yading@11: */ yading@11: size_t av_strlcpy(char *dst, const char *src, size_t size); yading@11: yading@11: /** yading@11: * Append the string src to the string dst, but to a total length of yading@11: * no more than size - 1 bytes, and null-terminate dst. yading@11: * yading@11: * This function is similar to BSD strlcat(), but differs when yading@11: * size <= strlen(dst). yading@11: * yading@11: * @param dst destination buffer yading@11: * @param src source string yading@11: * @param size size of destination buffer yading@11: * @return the total length of src and dst yading@11: * yading@11: * @warning since the return value use the length of src and dst, these yading@11: * absolutely _must_ be a properly 0-terminated strings, otherwise this yading@11: * will read beyond the end of the buffer and possibly crash. yading@11: */ yading@11: size_t av_strlcat(char *dst, const char *src, size_t size); yading@11: yading@11: /** yading@11: * Append output to a string, according to a format. Never write out of yading@11: * the destination buffer, and always put a terminating 0 within yading@11: * the buffer. yading@11: * @param dst destination buffer (string to which the output is yading@11: * appended) yading@11: * @param size total size of the destination buffer yading@11: * @param fmt printf-compatible format string, specifying how the yading@11: * following parameters are used yading@11: * @return the length of the string that would have been generated yading@11: * if enough space had been available yading@11: */ yading@11: size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4); yading@11: yading@11: /** yading@11: * Print arguments following specified format into a large enough auto yading@11: * allocated buffer. It is similar to GNU asprintf(). yading@11: * @param fmt printf-compatible format string, specifying how the yading@11: * following parameters are used. yading@11: * @return the allocated string yading@11: * @note You have to free the string yourself with av_free(). yading@11: */ yading@11: char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2); yading@11: yading@11: /** yading@11: * Convert a number to a av_malloced string. yading@11: */ yading@11: char *av_d2str(double d); yading@11: yading@11: /** yading@11: * Unescape the given string until a non escaped terminating char, yading@11: * and return the token corresponding to the unescaped string. yading@11: * yading@11: * The normal \ and ' escaping is supported. Leading and trailing yading@11: * whitespaces are removed, unless they are escaped with '\' or are yading@11: * enclosed between ''. yading@11: * yading@11: * @param buf the buffer to parse, buf will be updated to point to the yading@11: * terminating char yading@11: * @param term a 0-terminated list of terminating chars yading@11: * @return the malloced unescaped string, which must be av_freed by yading@11: * the user, NULL in case of allocation failure yading@11: */ yading@11: char *av_get_token(const char **buf, const char *term); yading@11: yading@11: /** yading@11: * Split the string into several tokens which can be accessed by yading@11: * successive calls to av_strtok(). yading@11: * yading@11: * A token is defined as a sequence of characters not belonging to the yading@11: * set specified in delim. yading@11: * yading@11: * On the first call to av_strtok(), s should point to the string to yading@11: * parse, and the value of saveptr is ignored. In subsequent calls, s yading@11: * should be NULL, and saveptr should be unchanged since the previous yading@11: * call. yading@11: * yading@11: * This function is similar to strtok_r() defined in POSIX.1. yading@11: * yading@11: * @param s the string to parse, may be NULL yading@11: * @param delim 0-terminated list of token delimiters, must be non-NULL yading@11: * @param saveptr user-provided pointer which points to stored yading@11: * information necessary for av_strtok() to continue scanning the same yading@11: * string. saveptr is updated to point to the next character after the yading@11: * first delimiter found, or to NULL if the string was terminated yading@11: * @return the found token, or NULL when no token is found yading@11: */ yading@11: char *av_strtok(char *s, const char *delim, char **saveptr); yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII isdigit. yading@11: */ yading@11: int av_isdigit(int c); yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII isgraph. yading@11: */ yading@11: int av_isgraph(int c); yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII isspace. yading@11: */ yading@11: int av_isspace(int c); yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII characters to uppercase. yading@11: */ yading@11: static inline int av_toupper(int c) yading@11: { yading@11: if (c >= 'a' && c <= 'z') yading@11: c ^= 0x20; yading@11: return c; yading@11: } yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII characters to lowercase. yading@11: */ yading@11: static inline int av_tolower(int c) yading@11: { yading@11: if (c >= 'A' && c <= 'Z') yading@11: c ^= 0x20; yading@11: return c; yading@11: } yading@11: yading@11: /** yading@11: * Locale-independent conversion of ASCII isxdigit. yading@11: */ yading@11: int av_isxdigit(int c); yading@11: yading@11: /** yading@11: * Locale-independent case-insensitive compare. yading@11: * @note This means only ASCII-range characters are case-insensitive yading@11: */ yading@11: int av_strcasecmp(const char *a, const char *b); yading@11: yading@11: /** yading@11: * Locale-independent case-insensitive compare. yading@11: * @note This means only ASCII-range characters are case-insensitive yading@11: */ yading@11: int av_strncasecmp(const char *a, const char *b, size_t n); yading@11: yading@11: yading@11: /** yading@11: * Thread safe basename. yading@11: * @param path the path, on DOS both \ and / are considered separators. yading@11: * @return pointer to the basename substring. yading@11: */ yading@11: const char *av_basename(const char *path); yading@11: yading@11: /** yading@11: * Thread safe dirname. yading@11: * @param path the path, on DOS both \ and / are considered separators. yading@11: * @return the path with the separator replaced by the string terminator or ".". yading@11: * @note the function may change the input string. yading@11: */ yading@11: const char *av_dirname(char *path); yading@11: yading@11: enum AVEscapeMode { yading@11: AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode. yading@11: AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping. yading@11: AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping. yading@11: }; yading@11: yading@11: /** yading@11: * Consider spaces special and escape them even in the middle of the yading@11: * string. yading@11: * yading@11: * This is equivalent to adding the whitespace characters to the special yading@11: * characters lists, except it is guaranteed to use the exact same list yading@11: * of whitespace characters as the rest of libavutil. yading@11: */ yading@11: #define AV_ESCAPE_FLAG_WHITESPACE 0x01 yading@11: yading@11: /** yading@11: * Escape only specified special characters. yading@11: * Without this flag, escape also any characters that may be considered yading@11: * special by av_get_token(), such as the single quote. yading@11: */ yading@11: #define AV_ESCAPE_FLAG_STRICT 0x02 yading@11: yading@11: /** yading@11: * Escape string in src, and put the escaped string in an allocated yading@11: * string in *dst, which must be freed with av_free(). yading@11: * yading@11: * @param dst pointer where an allocated string is put yading@11: * @param src string to escape, must be non-NULL yading@11: * @param special_chars string containing the special characters which yading@11: * need to be escaped, can be NULL yading@11: * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros. yading@11: * Any unknown value for mode will be considered equivalent to yading@11: * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without yading@11: * notice. yading@11: * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros yading@11: * @return the length of the allocated string, or a negative error code in case of error yading@11: * @see av_bprint_escape() yading@11: */ yading@11: int av_escape(char **dst, const char *src, const char *special_chars, yading@11: enum AVEscapeMode mode, int flags); yading@11: yading@11: /** yading@11: * @} yading@11: */ yading@11: yading@11: #endif /* AVUTIL_AVSTRING_H */