]> git.sesse.net Git - ffmpeg/blob - libavutil/avstring.c
Merge commit '5c7bf2dddee5bdfa247ff0d57cb8a37d19077f66'
[ffmpeg] / libavutil / avstring.c
1 /*
2  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3  * Copyright (c) 2007 Mans Rullgard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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.
11  *
12  * FFmpeg 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "avstring.h"
28 #include "mem.h"
29
30 int av_strstart(const char *str, const char *pfx, const char **ptr)
31 {
32     while (*pfx && *pfx == *str) {
33         pfx++;
34         str++;
35     }
36     if (!*pfx && ptr)
37         *ptr = str;
38     return !*pfx;
39 }
40
41 int av_stristart(const char *str, const char *pfx, const char **ptr)
42 {
43     while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
44         pfx++;
45         str++;
46     }
47     if (!*pfx && ptr)
48         *ptr = str;
49     return !*pfx;
50 }
51
52 char *av_stristr(const char *s1, const char *s2)
53 {
54     if (!*s2)
55         return (char*)(intptr_t)s1;
56
57     do {
58         if (av_stristart(s1, s2, NULL))
59             return (char*)(intptr_t)s1;
60     } while (*s1++);
61
62     return NULL;
63 }
64
65 size_t av_strlcpy(char *dst, const char *src, size_t size)
66 {
67     size_t len = 0;
68     while (++len < size && *src)
69         *dst++ = *src++;
70     if (len <= size)
71         *dst = 0;
72     return len + strlen(src) - 1;
73 }
74
75 size_t av_strlcat(char *dst, const char *src, size_t size)
76 {
77     size_t len = strlen(dst);
78     if (size <= len + 1)
79         return len + strlen(src);
80     return len + av_strlcpy(dst + len, src, size - len);
81 }
82
83 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
84 {
85     int len = strlen(dst);
86     va_list vl;
87
88     va_start(vl, fmt);
89     len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
90     va_end(vl);
91
92     return len;
93 }
94
95 char *av_asprintf(const char *fmt, ...)
96 {
97     char *p = NULL;
98     va_list va;
99     int len;
100
101     va_start(va, fmt);
102     len = vsnprintf(NULL, 0, fmt, va);
103     va_end(va);
104     if (len < 0)
105         goto end;
106
107     p = av_malloc(len + 1);
108     if (!p)
109         goto end;
110
111     va_start(va, fmt);
112     len = vsnprintf(p, len + 1, fmt, va);
113     va_end(va);
114     if (len < 0)
115         av_freep(&p);
116
117 end:
118     return p;
119 }
120
121 char *av_d2str(double d)
122 {
123     char *str= av_malloc(16);
124     if(str) snprintf(str, 16, "%f", d);
125     return str;
126 }
127
128 #define WHITESPACES " \n\t"
129
130 char *av_get_token(const char **buf, const char *term)
131 {
132     char *out = av_malloc(strlen(*buf) + 1);
133     char *ret= out, *end= out;
134     const char *p = *buf;
135     if (!out) return NULL;
136     p += strspn(p, WHITESPACES);
137
138     while(*p && !strspn(p, term)) {
139         char c = *p++;
140         if(c == '\\' && *p){
141             *out++ = *p++;
142             end= out;
143         }else if(c == '\''){
144             while(*p && *p != '\'')
145                 *out++ = *p++;
146             if(*p){
147                 p++;
148                 end= out;
149             }
150         }else{
151             *out++ = c;
152         }
153     }
154
155     do{
156         *out-- = 0;
157     }while(out >= end && strspn(out, WHITESPACES));
158
159     *buf = p;
160
161     return ret;
162 }
163
164 char *av_strtok(char *s, const char *delim, char **saveptr)
165 {
166     char *tok;
167
168     if (!s && !(s = *saveptr))
169         return NULL;
170
171     /* skip leading delimiters */
172     s += strspn(s, delim);
173
174     /* s now points to the first non delimiter char, or to the end of the string */
175     if (!*s) {
176         *saveptr = NULL;
177         return NULL;
178     }
179     tok = s++;
180
181     /* skip non delimiters */
182     s += strcspn(s, delim);
183     if (*s) {
184         *s = 0;
185         *saveptr = s+1;
186     } else {
187         *saveptr = NULL;
188     }
189
190     return tok;
191 }
192
193 int av_strcasecmp(const char *a, const char *b)
194 {
195     uint8_t c1, c2;
196     do {
197         c1 = av_tolower(*a++);
198         c2 = av_tolower(*b++);
199     } while (c1 && c1 == c2);
200     return c1 - c2;
201 }
202
203 int av_strncasecmp(const char *a, const char *b, size_t n)
204 {
205     const char *end = a + n;
206     uint8_t c1, c2;
207     do {
208         c1 = av_tolower(*a++);
209         c2 = av_tolower(*b++);
210     } while (a < end && c1 && c1 == c2);
211     return c1 - c2;
212 }
213
214 #ifdef TEST
215
216 #include "common.h"
217 #undef printf
218
219 int main(void)
220 {
221     int i;
222
223     printf("Testing av_get_token()\n");
224     {
225         const char *strings[] = {
226             "''",
227             "",
228             ":",
229             "\\",
230             "'",
231             "    ''    :",
232             "    ''  ''  :",
233             "foo   '' :",
234             "'foo'",
235             "foo     ",
236             "  '  foo  '  ",
237             "foo\\",
238             "foo':  blah:blah",
239             "foo\\:  blah:blah",
240             "foo\'",
241             "'foo :  '  :blahblah",
242             "\\ :blah",
243             "     foo",
244             "      foo       ",
245             "      foo     \\ ",
246             "foo ':blah",
247             " foo   bar    :   blahblah",
248             "\\f\\o\\o",
249             "'foo : \\ \\  '   : blahblah",
250             "'\\fo\\o:': blahblah",
251             "\\'fo\\o\\:':  foo  '  :blahblah"
252         };
253
254         for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
255             const char *p = strings[i];
256             char *q;
257             printf("|%s|", p);
258             q = av_get_token(&p, ":");
259             printf(" -> |%s|", q);
260             printf(" + |%s|\n", p);
261             av_free(q);
262         }
263     }
264
265     return 0;
266 }
267
268 #endif /* TEST */