FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avstring.c
Go to the documentation of this file.
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 "config.h"
29 #include "common.h"
30 #include "mem.h"
31 
32 int av_strstart(const char *str, const char *pfx, const char **ptr)
33 {
34  while (*pfx && *pfx == *str) {
35  pfx++;
36  str++;
37  }
38  if (!*pfx && ptr)
39  *ptr = str;
40  return !*pfx;
41 }
42 
43 int av_stristart(const char *str, const char *pfx, const char **ptr)
44 {
45  while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
46  pfx++;
47  str++;
48  }
49  if (!*pfx && ptr)
50  *ptr = str;
51  return !*pfx;
52 }
53 
54 char *av_stristr(const char *s1, const char *s2)
55 {
56  if (!*s2)
57  return (char*)(intptr_t)s1;
58 
59  do {
60  if (av_stristart(s1, s2, NULL))
61  return (char*)(intptr_t)s1;
62  } while (*s1++);
63 
64  return NULL;
65 }
66 
67 size_t av_strlcpy(char *dst, const char *src, size_t size)
68 {
69  size_t len = 0;
70  while (++len < size && *src)
71  *dst++ = *src++;
72  if (len <= size)
73  *dst = 0;
74  return len + strlen(src) - 1;
75 }
76 
77 size_t av_strlcat(char *dst, const char *src, size_t size)
78 {
79  size_t len = strlen(dst);
80  if (size <= len + 1)
81  return len + strlen(src);
82  return len + av_strlcpy(dst + len, src, size - len);
83 }
84 
85 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
86 {
87  int len = strlen(dst);
88  va_list vl;
89 
90  va_start(vl, fmt);
91  len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
92  va_end(vl);
93 
94  return len;
95 }
96 
97 char *av_asprintf(const char *fmt, ...)
98 {
99  char *p = NULL;
100  va_list va;
101  int len;
102 
103  va_start(va, fmt);
104  len = vsnprintf(NULL, 0, fmt, va);
105  va_end(va);
106  if (len < 0)
107  goto end;
108 
109  p = av_malloc(len + 1);
110  if (!p)
111  goto end;
112 
113  va_start(va, fmt);
114  len = vsnprintf(p, len + 1, fmt, va);
115  va_end(va);
116  if (len < 0)
117  av_freep(&p);
118 
119 end:
120  return p;
121 }
122 
123 char *av_d2str(double d)
124 {
125  char *str= av_malloc(16);
126  if(str) snprintf(str, 16, "%f", d);
127  return str;
128 }
129 
130 #define WHITESPACES " \n\t"
131 
132 char *av_get_token(const char **buf, const char *term)
133 {
134  char *out = av_malloc(strlen(*buf) + 1);
135  char *ret= out, *end= out;
136  const char *p = *buf;
137  if (!out) return NULL;
138  p += strspn(p, WHITESPACES);
139 
140  while(*p && !strspn(p, term)) {
141  char c = *p++;
142  if(c == '\\' && *p){
143  *out++ = *p++;
144  end= out;
145  }else if(c == '\''){
146  while(*p && *p != '\'')
147  *out++ = *p++;
148  if(*p){
149  p++;
150  end= out;
151  }
152  }else{
153  *out++ = c;
154  }
155  }
156 
157  do{
158  *out-- = 0;
159  }while(out >= end && strspn(out, WHITESPACES));
160 
161  *buf = p;
162 
163  return ret;
164 }
165 
166 char *av_strtok(char *s, const char *delim, char **saveptr)
167 {
168  char *tok;
169 
170  if (!s && !(s = *saveptr))
171  return NULL;
172 
173  /* skip leading delimiters */
174  s += strspn(s, delim);
175 
176  /* s now points to the first non delimiter char, or to the end of the string */
177  if (!*s) {
178  *saveptr = NULL;
179  return NULL;
180  }
181  tok = s++;
182 
183  /* skip non delimiters */
184  s += strcspn(s, delim);
185  if (*s) {
186  *s = 0;
187  *saveptr = s+1;
188  } else {
189  *saveptr = NULL;
190  }
191 
192  return tok;
193 }
194 
195 int av_strcasecmp(const char *a, const char *b)
196 {
197  uint8_t c1, c2;
198  do {
199  c1 = av_tolower(*a++);
200  c2 = av_tolower(*b++);
201  } while (c1 && c1 == c2);
202  return c1 - c2;
203 }
204 
205 int av_strncasecmp(const char *a, const char *b, size_t n)
206 {
207  const char *end = a + n;
208  uint8_t c1, c2;
209  do {
210  c1 = av_tolower(*a++);
211  c2 = av_tolower(*b++);
212  } while (a < end && c1 && c1 == c2);
213  return c1 - c2;
214 }
215 
216 const char *av_basename(const char *path)
217 {
218  char *p = strrchr(path, '/');
219 
220 #if HAVE_DOS_PATHS
221  char *q = strrchr(path, '\\');
222  char *d = strchr(path, ':');
223 
224  p = FFMAX3(p, q, d);
225 #endif
226 
227  if (!p)
228  return path;
229 
230  return p + 1;
231 }
232 
233 const char *av_dirname(char *path)
234 {
235  char *p = strrchr(path, '/');
236 
237 #if HAVE_DOS_PATHS
238  char *q = strrchr(path, '\\');
239  char *d = strchr(path, ':');
240 
241  d = d ? d + 1 : d;
242 
243  p = FFMAX3(p, q, d);
244 #endif
245 
246  if (!p)
247  return ".";
248 
249  *p = '\0';
250 
251  return path;
252 }
253 
254 
255 #ifdef TEST
256 
257 #include "common.h"
258 
259 int main(void)
260 {
261  int i;
262 
263  printf("Testing av_get_token()\n");
264  {
265  const char *strings[] = {
266  "''",
267  "",
268  ":",
269  "\\",
270  "'",
271  " '' :",
272  " '' '' :",
273  "foo '' :",
274  "'foo'",
275  "foo ",
276  " ' foo ' ",
277  "foo\\",
278  "foo': blah:blah",
279  "foo\\: blah:blah",
280  "foo\'",
281  "'foo : ' :blahblah",
282  "\\ :blah",
283  " foo",
284  " foo ",
285  " foo \\ ",
286  "foo ':blah",
287  " foo bar : blahblah",
288  "\\f\\o\\o",
289  "'foo : \\ \\ ' : blahblah",
290  "'\\fo\\o:': blahblah",
291  "\\'fo\\o\\:': foo ' :blahblah"
292  };
293 
294  for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
295  const char *p = strings[i];
296  char *q;
297  printf("|%s|", p);
298  q = av_get_token(&p, ":");
299  printf(" -> |%s|", q);
300  printf(" + |%s|\n", p);
301  av_free(q);
302  }
303  }
304 
305  return 0;
306 }
307 
308 #endif /* TEST */