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 module ffmpeg.libavutil.avstring;
21 
22 extern (C) @nogc nothrow:
23 
24 /**
25  * @addtogroup lavu_string
26  * @{
27  */
28 
29 /**
30  * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
31  * the address of the first character in str after the prefix.
32  *
33  * @param str input string
34  * @param pfx prefix to test
35  * @param ptr updated if the prefix is matched inside str
36  * @return non-zero if the prefix matches, zero otherwise
37  */
38 int av_strstart (const(char)* str, const(char)* pfx, const(char*)* ptr);
39 
40 /**
41  * Return non-zero if pfx is a prefix of str independent of case. If
42  * it is, *ptr is set to the address of the first character in str
43  * after the prefix.
44  *
45  * @param str input string
46  * @param pfx prefix to test
47  * @param ptr updated if the prefix is matched inside str
48  * @return non-zero if the prefix matches, zero otherwise
49  */
50 int av_stristart (const(char)* str, const(char)* pfx, const(char*)* ptr);
51 
52 /**
53  * Locate the first case-independent occurrence in the string haystack
54  * of the string needle.  A zero-length string needle is considered to
55  * match at the start of haystack.
56  *
57  * This function is a case-insensitive version of the standard strstr().
58  *
59  * @param haystack string to search in
60  * @param needle   string to search for
61  * @return         pointer to the located match within haystack
62  *                 or a null pointer if no match
63  */
64 char* av_stristr (const(char)* haystack, const(char)* needle);
65 
66 /**
67  * Locate the first occurrence of the string needle in the string haystack
68  * where not more than hay_length characters are searched. A zero-length
69  * string needle is considered to match at the start of haystack.
70  *
71  * This function is a length-limited version of the standard strstr().
72  *
73  * @param haystack   string to search in
74  * @param needle     string to search for
75  * @param hay_length length of string to search in
76  * @return           pointer to the located match within haystack
77  *                   or a null pointer if no match
78  */
79 char* av_strnstr (const(char)* haystack, const(char)* needle, size_t hay_length);
80 
81 /**
82  * Copy the string src to dst, but no more than size - 1 bytes, and
83  * null-terminate dst.
84  *
85  * This function is the same as BSD strlcpy().
86  *
87  * @param dst destination buffer
88  * @param src source string
89  * @param size size of destination buffer
90  * @return the length of src
91  *
92  * @warning since the return value is the length of src, src absolutely
93  * _must_ be a properly 0-terminated string, otherwise this will read beyond
94  * the end of the buffer and possibly crash.
95  */
96 size_t av_strlcpy (char* dst, const(char)* src, size_t size);
97 
98 /**
99  * Append the string src to the string dst, but to a total length of
100  * no more than size - 1 bytes, and null-terminate dst.
101  *
102  * This function is similar to BSD strlcat(), but differs when
103  * size <= strlen(dst).
104  *
105  * @param dst destination buffer
106  * @param src source string
107  * @param size size of destination buffer
108  * @return the total length of src and dst
109  *
110  * @warning since the return value use the length of src and dst, these
111  * absolutely _must_ be a properly 0-terminated strings, otherwise this
112  * will read beyond the end of the buffer and possibly crash.
113  */
114 size_t av_strlcat (char* dst, const(char)* src, size_t size);
115 
116 /**
117  * Append output to a string, according to a format. Never write out of
118  * the destination buffer, and always put a terminating 0 within
119  * the buffer.
120  * @param dst destination buffer (string to which the output is
121  *  appended)
122  * @param size total size of the destination buffer
123  * @param fmt printf-compatible format string, specifying how the
124  *  following parameters are used
125  * @return the length of the string that would have been generated
126  *  if enough space had been available
127  */
128 size_t av_strlcatf (char* dst, size_t size, const(char)* fmt, ...);
129 
130 /**
131  * Get the count of continuous non zero chars starting from the beginning.
132  *
133  * @param len maximum number of characters to check in the string, that
134  *            is the maximum value which is returned by the function
135  */
136 size_t av_strnlen (const(char)* s, size_t len);
137 
138 /**
139  * Print arguments following specified format into a large enough auto
140  * allocated buffer. It is similar to GNU asprintf().
141  * @param fmt printf-compatible format string, specifying how the
142  *            following parameters are used.
143  * @return the allocated string
144  * @note You have to free the string yourself with av_free().
145  */
146 char* av_asprintf (const(char)* fmt, ...);
147 
148 /**
149  * Convert a number to an av_malloced string.
150  * @deprecated  use av_asprintf() with "%f" or a more specific format
151  */
152 char* av_d2str (double d);
153 
154 /**
155  * Unescape the given string until a non escaped terminating char,
156  * and return the token corresponding to the unescaped string.
157  *
158  * The normal \ and ' escaping is supported. Leading and trailing
159  * whitespaces are removed, unless they are escaped with '\' or are
160  * enclosed between ''.
161  *
162  * @param buf the buffer to parse, buf will be updated to point to the
163  * terminating char
164  * @param term a 0-terminated list of terminating chars
165  * @return the malloced unescaped string, which must be av_freed by
166  * the user, NULL in case of allocation failure
167  */
168 char* av_get_token (const(char*)* buf, const(char)* term);
169 
170 /**
171  * Split the string into several tokens which can be accessed by
172  * successive calls to av_strtok().
173  *
174  * A token is defined as a sequence of characters not belonging to the
175  * set specified in delim.
176  *
177  * On the first call to av_strtok(), s should point to the string to
178  * parse, and the value of saveptr is ignored. In subsequent calls, s
179  * should be NULL, and saveptr should be unchanged since the previous
180  * call.
181  *
182  * This function is similar to strtok_r() defined in POSIX.1.
183  *
184  * @param s the string to parse, may be NULL
185  * @param delim 0-terminated list of token delimiters, must be non-NULL
186  * @param saveptr user-provided pointer which points to stored
187  * information necessary for av_strtok() to continue scanning the same
188  * string. saveptr is updated to point to the next character after the
189  * first delimiter found, or to NULL if the string was terminated
190  * @return the found token, or NULL when no token is found
191  */
192 char* av_strtok (char* s, const(char)* delim, char** saveptr);
193 
194 /**
195  * Locale-independent conversion of ASCII isdigit.
196  */
197 int av_isdigit (int c);
198 
199 /**
200  * Locale-independent conversion of ASCII isgraph.
201  */
202 int av_isgraph (int c);
203 
204 /**
205  * Locale-independent conversion of ASCII isspace.
206  */
207 int av_isspace (int c);
208 
209 /**
210  * Locale-independent conversion of ASCII characters to uppercase.
211  */
212 int av_toupper (int c);
213 
214 /**
215  * Locale-independent conversion of ASCII characters to lowercase.
216  */
217 int av_tolower (int c);
218 
219 /**
220  * Locale-independent conversion of ASCII isxdigit.
221  */
222 int av_isxdigit (int c);
223 
224 /**
225  * Locale-independent case-insensitive compare.
226  * @note This means only ASCII-range characters are case-insensitive
227  */
228 int av_strcasecmp (const(char)* a, const(char)* b);
229 
230 /**
231  * Locale-independent case-insensitive compare.
232  * @note This means only ASCII-range characters are case-insensitive
233  */
234 int av_strncasecmp (const(char)* a, const(char)* b, size_t n);
235 
236 /**
237  * Locale-independent strings replace.
238  * @note This means only ASCII-range characters are replace
239  */
240 char* av_strireplace (const(char)* str, const(char)* from, const(char)* to);
241 
242 /**
243  * Thread safe basename.
244  * @param path the string to parse, on DOS both \ and / are considered separators.
245  * @return pointer to the basename substring.
246  * If path does not contain a slash, the function returns a copy of path.
247  * If path is a NULL pointer or points to an empty string, a pointer
248  * to a string "." is returned.
249  */
250 const(char)* av_basename (const(char)* path);
251 
252 /**
253  * Thread safe dirname.
254  * @param path the string to parse, on DOS both \ and / are considered separators.
255  * @return A pointer to a string that's the parent directory of path.
256  * If path is a NULL pointer or points to an empty string, a pointer
257  * to a string "." is returned.
258  * @note the function may modify the contents of the path, so copies should be passed.
259  */
260 const(char)* av_dirname (char* path);
261 
262 /**
263  * Match instances of a name in a comma-separated list of names.
264  * List entries are checked from the start to the end of the names list,
265  * the first match ends further processing. If an entry prefixed with '-'
266  * matches, then 0 is returned. The "ALL" list entry is considered to
267  * match all names.
268  *
269  * @param name  Name to look for.
270  * @param names List of names.
271  * @return 1 on match, 0 otherwise.
272  */
273 int av_match_name (const(char)* name, const(char)* names);
274 
275 /**
276  * Append path component to the existing path.
277  * Path separator '/' is placed between when needed.
278  * Resulting string have to be freed with av_free().
279  * @param path      base path
280  * @param component component to be appended
281  * @return new path or NULL on error.
282  */
283 char* av_append_path_component (const(char)* path, const(char)* component);
284 
285 enum AVEscapeMode
286 {
287     AV_ESCAPE_MODE_AUTO = 0, ///< Use auto-selected escaping mode.
288     AV_ESCAPE_MODE_BACKSLASH = 1, ///< Use backslash escaping.
289     AV_ESCAPE_MODE_QUOTE = 2, ///< Use single-quote escaping.
290     AV_ESCAPE_MODE_XML = 3 ///< Use XML non-markup character data escaping.
291 }
292 
293 /**
294  * Consider spaces special and escape them even in the middle of the
295  * string.
296  *
297  * This is equivalent to adding the whitespace characters to the special
298  * characters lists, except it is guaranteed to use the exact same list
299  * of whitespace characters as the rest of libavutil.
300  */
301 enum AV_ESCAPE_FLAG_WHITESPACE = 1 << 0;
302 
303 /**
304  * Escape only specified special characters.
305  * Without this flag, escape also any characters that may be considered
306  * special by av_get_token(), such as the single quote.
307  */
308 enum AV_ESCAPE_FLAG_STRICT = 1 << 1;
309 
310 /**
311  * Within AV_ESCAPE_MODE_XML, additionally escape single quotes for single
312  * quoted attributes.
313  */
314 enum AV_ESCAPE_FLAG_XML_SINGLE_QUOTES = 1 << 2;
315 
316 /**
317  * Within AV_ESCAPE_MODE_XML, additionally escape double quotes for double
318  * quoted attributes.
319  */
320 enum AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES = 1 << 3;
321 
322 /**
323  * Escape string in src, and put the escaped string in an allocated
324  * string in *dst, which must be freed with av_free().
325  *
326  * @param dst           pointer where an allocated string is put
327  * @param src           string to escape, must be non-NULL
328  * @param special_chars string containing the special characters which
329  *                      need to be escaped, can be NULL
330  * @param mode          escape mode to employ, see AV_ESCAPE_MODE_* macros.
331  *                      Any unknown value for mode will be considered equivalent to
332  *                      AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
333  *                      notice.
334  * @param flags         flags which control how to escape, see AV_ESCAPE_FLAG_ macros
335  * @return the length of the allocated string, or a negative error code in case of error
336  * @see av_bprint_escape()
337  */
338 int av_escape (
339     char** dst,
340     const(char)* src,
341     const(char)* special_chars,
342     AVEscapeMode mode,
343     int flags);
344 
345 enum AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES = 1; ///< accept codepoints over 0x10FFFF
346 enum AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS = 2; ///< accept non-characters - 0xFFFE and 0xFFFF
347 enum AV_UTF8_FLAG_ACCEPT_SURROGATES = 4; ///< accept UTF-16 surrogates codes
348 enum AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES = 8; ///< exclude control codes not accepted by XML
349 
350 enum AV_UTF8_FLAG_ACCEPT_ALL = AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES | AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS | AV_UTF8_FLAG_ACCEPT_SURROGATES;
351 
352 /**
353  * Read and decode a single UTF-8 code point (character) from the
354  * buffer in *buf, and update *buf to point to the next byte to
355  * decode.
356  *
357  * In case of an invalid byte sequence, the pointer will be updated to
358  * the next byte after the invalid sequence and the function will
359  * return an error code.
360  *
361  * Depending on the specified flags, the function will also fail in
362  * case the decoded code point does not belong to a valid range.
363  *
364  * @note For speed-relevant code a carefully implemented use of
365  * GET_UTF8() may be preferred.
366  *
367  * @param codep   pointer used to return the parsed code in case of success.
368  *                The value in *codep is set even in case the range check fails.
369  * @param bufp    pointer to the address the first byte of the sequence
370  *                to decode, updated by the function to point to the
371  *                byte next after the decoded sequence
372  * @param buf_end pointer to the end of the buffer, points to the next
373  *                byte past the last in the buffer. This is used to
374  *                avoid buffer overreads (in case of an unfinished
375  *                UTF-8 sequence towards the end of the buffer).
376  * @param flags   a collection of AV_UTF8_FLAG_* flags
377  * @return >= 0 in case a sequence was successfully read, a negative
378  * value in case of invalid sequence
379  */
380 int av_utf8_decode (
381     int* codep,
382     const(ubyte*)* bufp,
383     const(ubyte)* buf_end,
384     uint flags);
385 
386 /**
387  * Check if a name is in a list.
388  * @returns 0 if not found, or the 1 based index where it has been found in the
389  *            list.
390  */
391 int av_match_list (const(char)* name, const(char)* list, char separator);
392 
393 /**
394  * See libc sscanf manual for more information.
395  * Locale-independent sscanf implementation.
396  */
397 int av_sscanf (const(char)* string, const(char)* format, ...);
398 
399 /**
400  * @}
401  */
402 
403 /* AVUTIL_AVSTRING_H */