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 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 "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 s1;
58 
59  do {
60  if (av_stristart(s1, s2, NULL))
61  return 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_d2str(double d)
98 {
99  char *str= av_malloc(16);
100  if(str) snprintf(str, 16, "%f", d);
101  return str;
102 }
103 
104 #define WHITESPACES " \n\t"
105 
106 char *av_get_token(const char **buf, const char *term)
107 {
108  char *out = av_malloc(strlen(*buf) + 1);
109  char *ret= out, *end= out;
110  const char *p = *buf;
111  if (!out) return NULL;
112  p += strspn(p, WHITESPACES);
113 
114  while(*p && !strspn(p, term)) {
115  char c = *p++;
116  if(c == '\\' && *p){
117  *out++ = *p++;
118  end= out;
119  }else if(c == '\''){
120  while(*p && *p != '\'')
121  *out++ = *p++;
122  if(*p){
123  p++;
124  end= out;
125  }
126  }else{
127  *out++ = c;
128  }
129  }
130 
131  do{
132  *out-- = 0;
133  }while(out >= end && strspn(out, WHITESPACES));
134 
135  *buf = p;
136 
137  return ret;
138 }
139 
140 int av_strcasecmp(const char *a, const char *b)
141 {
142  uint8_t c1, c2;
143  do {
144  c1 = av_tolower(*a++);
145  c2 = av_tolower(*b++);
146  } while (c1 && c1 == c2);
147  return c1 - c2;
148 }
149 
150 int av_strncasecmp(const char *a, const char *b, size_t n)
151 {
152  const char *end = a + n;
153  uint8_t c1, c2;
154  do {
155  c1 = av_tolower(*a++);
156  c2 = av_tolower(*b++);
157  } while (a < end && c1 && c1 == c2);
158  return c1 - c2;
159 }
160 
161 const char *av_basename(const char *path)
162 {
163  char *p = strrchr(path, '/');
164 
165 #if HAVE_DOS_PATHS
166  char *q = strrchr(path, '\\');
167  char *d = strchr(path, ':');
168 
169  p = FFMAX3(p, q, d);
170 #endif
171 
172  if (!p)
173  return path;
174 
175  return p + 1;
176 }
177 
178 const char *av_dirname(char *path)
179 {
180  char *p = strrchr(path, '/');
181 
182 #if HAVE_DOS_PATHS
183  char *q = strrchr(path, '\\');
184  char *d = strchr(path, ':');
185 
186  d = d ? d + 1 : d;
187 
188  p = FFMAX3(p, q, d);
189 #endif
190 
191  if (!p)
192  return ".";
193 
194  *p = '\0';
195 
196  return path;
197 }
198 
199 
200 #ifdef TEST
201 
202 #include "common.h"
203 
204 int main(void)
205 {
206  int i;
207 
208  printf("Testing av_get_token()\n");
209  {
210  const char *strings[] = {
211  "''",
212  "",
213  ":",
214  "\\",
215  "'",
216  " '' :",
217  " '' '' :",
218  "foo '' :",
219  "'foo'",
220  "foo ",
221  " ' foo ' ",
222  "foo\\",
223  "foo': blah:blah",
224  "foo\\: blah:blah",
225  "foo\'",
226  "'foo : ' :blahblah",
227  "\\ :blah",
228  " foo",
229  " foo ",
230  " foo \\ ",
231  "foo ':blah",
232  " foo bar : blahblah",
233  "\\f\\o\\o",
234  "'foo : \\ \\ ' : blahblah",
235  "'\\fo\\o:': blahblah",
236  "\\'fo\\o\\:': foo ' :blahblah"
237  };
238 
239  for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
240  const char *p = strings[i], *q;
241  printf("|%s|", p);
242  q = av_get_token(&p, ":");
243  printf(" -> |%s|", q);
244  printf(" + |%s|\n", p);
245  av_free(q);
246  }
247  }
248 
249  return 0;
250 }
251 
252 #endif /* TEST */