avstring.h 6.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2007 Mans Rullgard
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22
#ifndef AVUTIL_AVSTRING_H
#define AVUTIL_AVSTRING_H
23 24

#include <stddef.h>
25
#include "attributes.h"
26

27 28 29 30 31
/**
 * @addtogroup lavu_string
 * @{
 */

32 33 34 35 36 37
/**
 * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
 * the address of the first character in str after the prefix.
 *
 * @param str input string
 * @param pfx prefix to test
38
 * @param ptr updated if the prefix is matched inside str
39 40 41 42 43 44 45 46 47 48 49
 * @return non-zero if the prefix matches, zero otherwise
 */
int av_strstart(const char *str, const char *pfx, const char **ptr);

/**
 * Return non-zero if pfx is a prefix of str independent of case. If
 * it is, *ptr is set to the address of the first character in str
 * after the prefix.
 *
 * @param str input string
 * @param pfx prefix to test
50
 * @param ptr updated if the prefix is matched inside str
51 52 53 54
 * @return non-zero if the prefix matches, zero otherwise
 */
int av_stristart(const char *str, const char *pfx, const char **ptr);

55
/**
56 57 58
 * Locate the first case-independent occurrence in the string haystack
 * of the string needle.  A zero-length string needle is considered to
 * match at the start of haystack.
59 60 61
 *
 * This function is a case-insensitive version of the standard strstr().
 *
62 63 64 65
 * @param haystack string to search in
 * @param needle   string to search for
 * @return         pointer to the located match within haystack
 *                 or a null pointer if no match
66
 */
67
char *av_stristr(const char *haystack, const char *needle);
68

69 70
/**
 * Copy the string src to dst, but no more than size - 1 bytes, and
71
 * null-terminate dst.
72 73 74 75 76 77 78
 *
 * This function is the same as BSD strlcpy().
 *
 * @param dst destination buffer
 * @param src source string
 * @param size size of destination buffer
 * @return the length of src
79
 *
80
 * @warning since the return value is the length of src, src absolutely
81 82
 * _must_ be a properly 0-terminated string, otherwise this will read beyond
 * the end of the buffer and possibly crash.
83 84 85 86 87
 */
size_t av_strlcpy(char *dst, const char *src, size_t size);

/**
 * Append the string src to the string dst, but to a total length of
88
 * no more than size - 1 bytes, and null-terminate dst.
89 90 91 92 93 94 95 96
 *
 * This function is similar to BSD strlcat(), but differs when
 * size <= strlen(dst).
 *
 * @param dst destination buffer
 * @param src source string
 * @param size size of destination buffer
 * @return the total length of src and dst
97
 *
98 99 100
 * @warning since the return value use the length of src and dst, these
 * absolutely _must_ be a properly 0-terminated strings, otherwise this
 * will read beyond the end of the buffer and possibly crash.
101 102 103
 */
size_t av_strlcat(char *dst, const char *src, size_t size);

104 105
/**
 * Append output to a string, according to a format. Never write out of
106
 * the destination buffer, and always put a terminating 0 within
107 108 109 110 111 112 113 114 115
 * the buffer.
 * @param dst destination buffer (string to which the output is
 *  appended)
 * @param size total size of the destination buffer
 * @param fmt printf-compatible format string, specifying how the
 *  following parameters are used
 * @return the length of the string that would have been generated
 *  if enough space had been available
 */
116
size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
117

118 119 120 121 122 123 124 125 126 127
/**
 * Print arguments following specified format into a large enough auto
 * allocated buffer. It is similar to GNU asprintf().
 * @param fmt printf-compatible format string, specifying how the
 *            following parameters are used.
 * @return the allocated string
 * @note You have to free the string yourself with av_free().
 */
char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);

128 129 130 131 132
/**
 * Convert a number to a av_malloced string.
 */
char *av_d2str(double d);

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/**
 * Unescape the given string until a non escaped terminating char,
 * and return the token corresponding to the unescaped string.
 *
 * The normal \ and ' escaping is supported. Leading and trailing
 * whitespaces are removed, unless they are escaped with '\' or are
 * enclosed between ''.
 *
 * @param buf the buffer to parse, buf will be updated to point to the
 * terminating char
 * @param term a 0-terminated list of terminating chars
 * @return the malloced unescaped string, which must be av_freed by
 * the user, NULL in case of allocation failure
 */
char *av_get_token(const char **buf, const char *term);

Stefano Sabatini's avatar
Stefano Sabatini committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/**
 * Split the string into several tokens which can be accessed by
 * successive calls to av_strtok().
 *
 * A token is defined as a sequence of characters not belonging to the
 * set specified in delim.
 *
 * On the first call to av_strtok(), s should point to the string to
 * parse, and the value of saveptr is ignored. In subsequent calls, s
 * should be NULL, and saveptr should be unchanged since the previous
 * call.
 *
 * This function is similar to strtok_r() defined in POSIX.1.
 *
 * @param s the string to parse, may be NULL
 * @param delim 0-terminated list of token delimiters, must be non-NULL
 * @param saveptr user-provided pointer which points to stored
 * information necessary for av_strtok() to continue scanning the same
 * string. saveptr is updated to point to the next character after the
 * first delimiter found, or to NULL if the string was terminated
 * @return the found token, or NULL when no token is found
 */
char *av_strtok(char *s, const char *delim, char **saveptr);

173
/**
174
 * Locale-independent conversion of ASCII characters to uppercase.
175 176 177 178 179 180 181 182 183
 */
static inline int av_toupper(int c)
{
    if (c >= 'a' && c <= 'z')
        c ^= 0x20;
    return c;
}

/**
184
 * Locale-independent conversion of ASCII characters to lowercase.
185 186 187 188 189 190 191 192
 */
static inline int av_tolower(int c)
{
    if (c >= 'A' && c <= 'Z')
        c ^= 0x20;
    return c;
}

193
/**
194
 * Locale-independent case-insensitive compare.
195
 * @note This means only ASCII-range characters are case-insensitive
196 197 198 199
 */
int av_strcasecmp(const char *a, const char *b);

/**
200
 * Locale-independent case-insensitive compare.
201
 * @note This means only ASCII-range characters are case-insensitive
202 203 204
 */
int av_strncasecmp(const char *a, const char *b, size_t n);

205 206 207 208
/**
 * @}
 */

209
#endif /* AVUTIL_AVSTRING_H */