FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avstring.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Mans Rullgard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVUTIL_AVSTRING_H
22 #define AVUTIL_AVSTRING_H
23 
24 #include <stddef.h>
25 #include "attributes.h"
26 
27 /**
28  * @addtogroup lavu_string
29  * @{
30  */
31 
32 /**
33  * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
34  * the address of the first character in str after the prefix.
35  *
36  * @param str input string
37  * @param pfx prefix to test
38  * @param ptr updated if the prefix is matched inside str
39  * @return non-zero if the prefix matches, zero otherwise
40  */
41 int av_strstart(const char *str, const char *pfx, const char **ptr);
42 
43 /**
44  * Return non-zero if pfx is a prefix of str independent of case. If
45  * it is, *ptr is set to the address of the first character in str
46  * after the prefix.
47  *
48  * @param str input string
49  * @param pfx prefix to test
50  * @param ptr updated if the prefix is matched inside str
51  * @return non-zero if the prefix matches, zero otherwise
52  */
53 int av_stristart(const char *str, const char *pfx, const char **ptr);
54 
55 /**
56  * Locate the first case-independent occurrence in the string haystack
57  * of the string needle. A zero-length string needle is considered to
58  * match at the start of haystack.
59  *
60  * This function is a case-insensitive version of the standard strstr().
61  *
62  * @param haystack string to search in
63  * @param needle string to search for
64  * @return pointer to the located match within haystack
65  * or a null pointer if no match
66  */
67 char *av_stristr(const char *haystack, const char *needle);
68 
69 /**
70  * Locate the first occurrence of the string needle in the string haystack
71  * where not more than hay_length characters are searched. A zero-length
72  * string needle is considered to match at the start of haystack.
73  *
74  * This function is a length-limited version of the standard strstr().
75  *
76  * @param haystack string to search in
77  * @param needle string to search for
78  * @param hay_length length of string to search in
79  * @return pointer to the located match within haystack
80  * or a null pointer if no match
81  */
82 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length);
83 
84 /**
85  * Copy the string src to dst, but no more than size - 1 bytes, and
86  * null-terminate dst.
87  *
88  * This function is the same as BSD strlcpy().
89  *
90  * @param dst destination buffer
91  * @param src source string
92  * @param size size of destination buffer
93  * @return the length of src
94  *
95  * @warning since the return value is the length of src, src absolutely
96  * _must_ be a properly 0-terminated string, otherwise this will read beyond
97  * the end of the buffer and possibly crash.
98  */
99 size_t av_strlcpy(char *dst, const char *src, size_t size);
100 
101 /**
102  * Append the string src to the string dst, but to a total length of
103  * no more than size - 1 bytes, and null-terminate dst.
104  *
105  * This function is similar to BSD strlcat(), but differs when
106  * size <= strlen(dst).
107  *
108  * @param dst destination buffer
109  * @param src source string
110  * @param size size of destination buffer
111  * @return the total length of src and dst
112  *
113  * @warning since the return value use the length of src and dst, these
114  * absolutely _must_ be a properly 0-terminated strings, otherwise this
115  * will read beyond the end of the buffer and possibly crash.
116  */
117 size_t av_strlcat(char *dst, const char *src, size_t size);
118 
119 /**
120  * Append output to a string, according to a format. Never write out of
121  * the destination buffer, and always put a terminating 0 within
122  * the buffer.
123  * @param dst destination buffer (string to which the output is
124  * appended)
125  * @param size total size of the destination buffer
126  * @param fmt printf-compatible format string, specifying how the
127  * following parameters are used
128  * @return the length of the string that would have been generated
129  * if enough space had been available
130  */
131 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
132 
133 /**
134  * Print arguments following specified format into a large enough auto
135  * allocated buffer. It is similar to GNU asprintf().
136  * @param fmt printf-compatible format string, specifying how the
137  * following parameters are used.
138  * @return the allocated string
139  * @note You have to free the string yourself with av_free().
140  */
141 char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
142 
143 /**
144  * Convert a number to a av_malloced string.
145  */
146 char *av_d2str(double d);
147 
148 /**
149  * Unescape the given string until a non escaped terminating char,
150  * and return the token corresponding to the unescaped string.
151  *
152  * The normal \ and ' escaping is supported. Leading and trailing
153  * whitespaces are removed, unless they are escaped with '\' or are
154  * enclosed between ''.
155  *
156  * @param buf the buffer to parse, buf will be updated to point to the
157  * terminating char
158  * @param term a 0-terminated list of terminating chars
159  * @return the malloced unescaped string, which must be av_freed by
160  * the user, NULL in case of allocation failure
161  */
162 char *av_get_token(const char **buf, const char *term);
163 
164 /**
165  * Split the string into several tokens which can be accessed by
166  * successive calls to av_strtok().
167  *
168  * A token is defined as a sequence of characters not belonging to the
169  * set specified in delim.
170  *
171  * On the first call to av_strtok(), s should point to the string to
172  * parse, and the value of saveptr is ignored. In subsequent calls, s
173  * should be NULL, and saveptr should be unchanged since the previous
174  * call.
175  *
176  * This function is similar to strtok_r() defined in POSIX.1.
177  *
178  * @param s the string to parse, may be NULL
179  * @param delim 0-terminated list of token delimiters, must be non-NULL
180  * @param saveptr user-provided pointer which points to stored
181  * information necessary for av_strtok() to continue scanning the same
182  * string. saveptr is updated to point to the next character after the
183  * first delimiter found, or to NULL if the string was terminated
184  * @return the found token, or NULL when no token is found
185  */
186 char *av_strtok(char *s, const char *delim, char **saveptr);
187 
188 /**
189  * Locale-independent conversion of ASCII isdigit.
190  */
191 static inline int av_isdigit(int c)
192 {
193  return c >= '0' && c <= '9';
194 }
195 
196 /**
197  * Locale-independent conversion of ASCII isgraph.
198  */
199 static inline int av_isgraph(int c)
200 {
201  return c > 32 && c < 127;
202 }
203 
204 /**
205  * Locale-independent conversion of ASCII isspace.
206  */
207 static inline int av_isspace(int c)
208 {
209  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
210 }
211 
212 /**
213  * Locale-independent conversion of ASCII characters to uppercase.
214  */
215 static inline int av_toupper(int c)
216 {
217  if (c >= 'a' && c <= 'z')
218  c ^= 0x20;
219  return c;
220 }
221 
222 /**
223  * Locale-independent conversion of ASCII characters to lowercase.
224  */
225 static inline int av_tolower(int c)
226 {
227  if (c >= 'A' && c <= 'Z')
228  c ^= 0x20;
229  return c;
230 }
231 
232 /**
233  * Locale-independent conversion of ASCII isxdigit.
234  */
235 static inline int av_isxdigit(int c)
236 {
237  c = av_tolower(c);
238  return av_isdigit(c) || (c >= 'a' && c <= 'z');
239 }
240 
241 /**
242  * Locale-independent case-insensitive compare.
243  * @note This means only ASCII-range characters are case-insensitive
244  */
245 int av_strcasecmp(const char *a, const char *b);
246 
247 /**
248  * Locale-independent case-insensitive compare.
249  * @note This means only ASCII-range characters are case-insensitive
250  */
251 int av_strncasecmp(const char *a, const char *b, size_t n);
252 
253 
254 /**
255  * Thread safe basename.
256  * @param path the path, on DOS both \ and / are considered separators.
257  * @return pointer to the basename substring.
258  */
259 const char *av_basename(const char *path);
260 
261 /**
262  * Thread safe dirname.
263  * @param path the path, on DOS both \ and / are considered separators.
264  * @return the path with the separator replaced by the string terminator or ".".
265  * @note the function may change the input string.
266  */
267 const char *av_dirname(char *path);
268 
270  AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.
271  AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
272  AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping.
273 };
274 
275 /**
276  * Consider spaces special and escape them even in the middle of the
277  * string.
278  *
279  * This is equivalent to adding the whitespace characters to the special
280  * characters lists, except it is guaranteed to use the exact same list
281  * of whitespace characters as the rest of libavutil.
282  */
283 #define AV_ESCAPE_FLAG_WHITESPACE 0x01
284 
285 /**
286  * Escape only specified special characters.
287  * Without this flag, escape also any characters that may be considered
288  * special by av_get_token(), such as the single quote.
289  */
290 #define AV_ESCAPE_FLAG_STRICT 0x02
291 
292 /**
293  * Escape string in src, and put the escaped string in an allocated
294  * string in *dst, which must be freed with av_free().
295  *
296  * @param dst pointer where an allocated string is put
297  * @param src string to escape, must be non-NULL
298  * @param special_chars string containing the special characters which
299  * need to be escaped, can be NULL
300  * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
301  * Any unknown value for mode will be considered equivalent to
302  * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
303  * notice.
304  * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros
305  * @return the length of the allocated string, or a negative error code in case of error
306  * @see av_bprint_escape()
307  */
308 int av_escape(char **dst, const char *src, const char *special_chars,
309  enum AVEscapeMode mode, int flags);
310 
311 /**
312  * @}
313  */
314 
315 #endif /* AVUTIL_AVSTRING_H */