2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3 * Copyright (c) 2007 Mans Rullgard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 int av_strstart(const char *str, const char *pfx, const char **ptr)
34 while (*pfx && *pfx == *str) {
43 int av_stristart(const char *str, const char *pfx, const char **ptr)
45 while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
54 char *av_stristr(const char *s1, const char *s2)
60 if (av_stristart(s1, s2, NULL))
67 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
69 size_t needle_len = strlen(needle);
72 while (hay_length >= needle_len) {
74 if (!memcmp(haystack, needle, needle_len))
81 size_t av_strlcpy(char *dst, const char *src, size_t size)
84 while (++len < size && *src)
88 return len + strlen(src) - 1;
91 size_t av_strlcat(char *dst, const char *src, size_t size)
93 size_t len = strlen(dst);
95 return len + strlen(src);
96 return len + av_strlcpy(dst + len, src, size - len);
99 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
101 int len = strlen(dst);
105 len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
111 char *av_d2str(double d)
113 char *str = av_malloc(16);
115 snprintf(str, 16, "%f", d);
119 #define WHITESPACES " \n\t"
121 char *av_get_token(const char **buf, const char *term)
123 char *out = av_malloc(strlen(*buf) + 1);
124 char *ret = out, *end = out;
125 const char *p = *buf;
128 p += strspn(p, WHITESPACES);
130 while (*p && !strspn(p, term)) {
132 if (c == '\\' && *p) {
135 } else if (c == '\'') {
136 while (*p && *p != '\'')
149 while (out >= end && strspn(out, WHITESPACES));
156 int av_strcasecmp(const char *a, const char *b)
160 c1 = av_tolower(*a++);
161 c2 = av_tolower(*b++);
162 } while (c1 && c1 == c2);
166 int av_strncasecmp(const char *a, const char *b, size_t n)
168 const char *end = a + n;
171 c1 = av_tolower(*a++);
172 c2 = av_tolower(*b++);
173 } while (a < end && c1 && c1 == c2);
177 const char *av_basename(const char *path)
179 char *p = strrchr(path, '/');
182 char *q = strrchr(path, '\\');
183 char *d = strchr(path, ':');
194 const char *av_dirname(char *path)
196 char *p = strrchr(path, '/');
199 char *q = strrchr(path, '\\');
200 char *d = strchr(path, ':');
215 int av_isdigit(int c)
217 return c >= '0' && c <= '9';
220 int av_isgraph(int c)
222 return c > 32 && c < 127;
225 int av_isspace(int c)
227 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
231 int av_isxdigit(int c)
234 return av_isdigit(c) || (c >= 'a' && c <= 'f');
242 const char *strings[] = {
258 "'foo : ' :blahblah",
264 " foo bar : blahblah",
266 "'foo : \\ \\ ' : blahblah",
267 "'\\fo\\o:': blahblah",
268 "\\'fo\\o\\:': foo ' :blahblah"
271 printf("Testing av_get_token()\n");
272 for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
273 const char *p = strings[i], *q;
275 q = av_get_token(&p, ":");
276 printf(" -> |%s|", q);
277 printf(" + |%s|\n", p);