]> git.sesse.net Git - ffmpeg/blob - libavutil/avstring.c
avutil: add yuva422p and yuva444p formats
[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 Libav.
6  *
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.
11  *
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.
16  *
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
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 s1;
56
57     do {
58         if (av_stristart(s1, s2, NULL))
59             return 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_d2str(double d)
96 {
97     char *str= av_malloc(16);
98     if(str) snprintf(str, 16, "%f", d);
99     return str;
100 }
101
102 #define WHITESPACES " \n\t"
103
104 char *av_get_token(const char **buf, const char *term)
105 {
106     char *out = av_malloc(strlen(*buf) + 1);
107     char *ret= out, *end= out;
108     const char *p = *buf;
109     if (!out) return NULL;
110     p += strspn(p, WHITESPACES);
111
112     while(*p && !strspn(p, term)) {
113         char c = *p++;
114         if(c == '\\' && *p){
115             *out++ = *p++;
116             end= out;
117         }else if(c == '\''){
118             while(*p && *p != '\'')
119                 *out++ = *p++;
120             if(*p){
121                 p++;
122                 end= out;
123             }
124         }else{
125             *out++ = c;
126         }
127     }
128
129     do{
130         *out-- = 0;
131     }while(out >= end && strspn(out, WHITESPACES));
132
133     *buf = p;
134
135     return ret;
136 }
137
138 int av_strcasecmp(const char *a, const char *b)
139 {
140     uint8_t c1, c2;
141     do {
142         c1 = av_tolower(*a++);
143         c2 = av_tolower(*b++);
144     } while (c1 && c1 == c2);
145     return c1 - c2;
146 }
147
148 int av_strncasecmp(const char *a, const char *b, size_t n)
149 {
150     const char *end = a + n;
151     uint8_t c1, c2;
152     do {
153         c1 = av_tolower(*a++);
154         c2 = av_tolower(*b++);
155     } while (a < end && c1 && c1 == c2);
156     return c1 - c2;
157 }
158
159 #ifdef TEST
160
161 #include "common.h"
162 #undef printf
163
164 int main(void)
165 {
166     int i;
167
168     printf("Testing av_get_token()\n");
169     {
170         const char *strings[] = {
171             "''",
172             "",
173             ":",
174             "\\",
175             "'",
176             "    ''    :",
177             "    ''  ''  :",
178             "foo   '' :",
179             "'foo'",
180             "foo     ",
181             "  '  foo  '  ",
182             "foo\\",
183             "foo':  blah:blah",
184             "foo\\:  blah:blah",
185             "foo\'",
186             "'foo :  '  :blahblah",
187             "\\ :blah",
188             "     foo",
189             "      foo       ",
190             "      foo     \\ ",
191             "foo ':blah",
192             " foo   bar    :   blahblah",
193             "\\f\\o\\o",
194             "'foo : \\ \\  '   : blahblah",
195             "'\\fo\\o:': blahblah",
196             "\\'fo\\o\\:':  foo  '  :blahblah"
197         };
198
199         for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
200             const char *p= strings[i];
201             printf("|%s|", p);
202             printf(" -> |%s|", av_get_token(&p, ":"));
203             printf(" + |%s|\n", p);
204         }
205     }
206
207     return 0;
208 }
209
210 #endif /* TEST */