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