avstring.h 4.56 KB
Newer Older
Måns Rullgård's avatar
Måns Rullgård committed
1 2 3
/*
 * Copyright (c) 2007 Mans Rullgard
 *
4
 * This file is part of Libav.
Måns Rullgård's avatar
Måns Rullgård committed
5
 *
6
 * Libav is free software; you can redistribute it and/or
Måns Rullgård's avatar
Måns Rullgård committed
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
Måns Rullgård's avatar
Måns Rullgård committed
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
Måns Rullgård's avatar
Måns Rullgård committed
18 19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22
#ifndef AVUTIL_AVSTRING_H
#define AVUTIL_AVSTRING_H
Måns Rullgård's avatar
Måns Rullgård committed
23 24 25 26 27 28 29 30 31

#include <stddef.h>

/**
 * 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
32
 * @param ptr updated if the prefix is matched inside str
Måns Rullgård's avatar
Måns Rullgård committed
33 34 35 36 37 38 39 40 41 42 43
 * @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
44
 * @param ptr updated if the prefix is matched inside str
Måns Rullgård's avatar
Måns Rullgård committed
45 46 47 48
 * @return non-zero if the prefix matches, zero otherwise
 */
int av_stristart(const char *str, const char *pfx, const char **ptr);

Måns Rullgård's avatar
Måns Rullgård committed
49
/**
50 51 52
 * 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.
Måns Rullgård's avatar
Måns Rullgård committed
53 54 55
 *
 * This function is a case-insensitive version of the standard strstr().
 *
56 57 58 59
 * @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
Måns Rullgård's avatar
Måns Rullgård committed
60
 */
61
char *av_stristr(const char *haystack, const char *needle);
Måns Rullgård's avatar
Måns Rullgård committed
62

Måns Rullgård's avatar
Måns Rullgård committed
63 64
/**
 * Copy the string src to dst, but no more than size - 1 bytes, and
65
 * null-terminate dst.
Måns Rullgård's avatar
Måns Rullgård committed
66 67 68 69 70 71 72
 *
 * 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
73 74 75 76
 *
 * WARNING: since the return value is the length of src, src absolutely
 * _must_ be a properly 0-terminated string, otherwise this will read beyond
 * the end of the buffer and possibly crash.
Måns Rullgård's avatar
Måns Rullgård committed
77 78 79 80 81
 */
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
82
 * no more than size - 1 bytes, and null-terminate dst.
Måns Rullgård's avatar
Måns Rullgård committed
83 84 85 86 87 88 89 90
 *
 * 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
91 92 93 94
 *
 * 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.
Måns Rullgård's avatar
Måns Rullgård committed
95 96 97
 */
size_t av_strlcat(char *dst, const char *src, size_t size);

98 99
/**
 * Append output to a string, according to a format. Never write out of
100
 * the destination buffer, and always put a terminating 0 within
101 102 103 104 105 106 107 108 109 110 111
 * 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
 */
size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...);

112 113 114 115 116
/**
 * Convert a number to a av_malloced string.
 */
char *av_d2str(double d);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * 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);

133
#endif /* AVUTIL_AVSTRING_H */