Libav
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 
27 #include "config.h"
28 #include "common.h"
29 #include "mem.h"
30 #include "avstring.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 && av_toupper((unsigned)*pfx) == av_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 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
68 {
69  size_t needle_len = strlen(needle);
70  if (!needle_len)
71  return haystack;
72  while (hay_length >= needle_len) {
73  hay_length--;
74  if (!memcmp(haystack, needle, needle_len))
75  return haystack;
76  haystack++;
77  }
78  return NULL;
79 }
80 
81 size_t av_strlcpy(char *dst, const char *src, size_t size)
82 {
83  size_t len = 0;
84  while (++len < size && *src)
85  *dst++ = *src++;
86  if (len <= size)
87  *dst = 0;
88  return len + strlen(src) - 1;
89 }
90 
91 size_t av_strlcat(char *dst, const char *src, size_t size)
92 {
93  size_t len = strlen(dst);
94  if (size <= len + 1)
95  return len + strlen(src);
96  return len + av_strlcpy(dst + len, src, size - len);
97 }
98 
99 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
100 {
101  int len = strlen(dst);
102  va_list vl;
103 
104  va_start(vl, fmt);
105  len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
106  va_end(vl);
107 
108  return len;
109 }
110 
111 char *av_d2str(double d)
112 {
113  char *str = av_malloc(16);
114  if (str)
115  snprintf(str, 16, "%f", d);
116  return str;
117 }
118 
119 #define WHITESPACES " \n\t"
120 
121 char *av_get_token(const char **buf, const char *term)
122 {
123  char *out = av_malloc(strlen(*buf) + 1);
124  char *ret = out, *end = out;
125  const char *p = *buf;
126  if (!out)
127  return NULL;
128  p += strspn(p, WHITESPACES);
129 
130  while (*p && !strspn(p, term)) {
131  char c = *p++;
132  if (c == '\\' && *p) {
133  *out++ = *p++;
134  end = out;
135  } else if (c == '\'') {
136  while (*p && *p != '\'')
137  *out++ = *p++;
138  if (*p) {
139  p++;
140  end = out;
141  }
142  } else {
143  *out++ = c;
144  }
145  }
146 
147  do
148  *out-- = 0;
149  while (out >= end && strspn(out, WHITESPACES));
150 
151  *buf = p;
152 
153  return ret;
154 }
155 
156 int av_strcasecmp(const char *a, const char *b)
157 {
158  uint8_t c1, c2;
159  do {
160  c1 = av_tolower(*a++);
161  c2 = av_tolower(*b++);
162  } while (c1 && c1 == c2);
163  return c1 - c2;
164 }
165 
166 int av_strncasecmp(const char *a, const char *b, size_t n)
167 {
168  const char *end = a + n;
169  uint8_t c1, c2;
170  do {
171  c1 = av_tolower(*a++);
172  c2 = av_tolower(*b++);
173  } while (a < end && c1 && c1 == c2);
174  return c1 - c2;
175 }
176 
177 const char *av_basename(const char *path)
178 {
179  char *p = strrchr(path, '/');
180 
181 #if HAVE_DOS_PATHS
182  char *q = strrchr(path, '\\');
183  char *d = strchr(path, ':');
184 
185  p = FFMAX3(p, q, d);
186 #endif
187 
188  if (!p)
189  return path;
190 
191  return p + 1;
192 }
193 
194 const char *av_dirname(char *path)
195 {
196  char *p = strrchr(path, '/');
197 
198 #if HAVE_DOS_PATHS
199  char *q = strrchr(path, '\\');
200  char *d = strchr(path, ':');
201 
202  d = d ? d + 1 : d;
203 
204  p = FFMAX3(p, q, d);
205 #endif
206 
207  if (!p)
208  return ".";
209 
210  *p = '\0';
211 
212  return path;
213 }
214 
215 int av_isdigit(int c)
216 {
217  return c >= '0' && c <= '9';
218 }
219 
220 int av_isgraph(int c)
221 {
222  return c > 32 && c < 127;
223 }
224 
225 int av_isspace(int c)
226 {
227  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
228  c == '\v';
229 }
230 
231 int av_isxdigit(int c)
232 {
233  c = av_tolower(c);
234  return av_isdigit(c) || (c >= 'a' && c <= 'f');
235 }
236 
237 int av_match_name(const char *name, const char *names)
238 {
239  const char *p;
240  int len, namelen;
241 
242  if (!name || !names)
243  return 0;
244 
245  namelen = strlen(name);
246  while ((p = strchr(names, ','))) {
247  len = FFMAX(p - names, namelen);
248  if (!av_strncasecmp(name, names, len))
249  return 1;
250  names = p + 1;
251  }
252  return !av_strcasecmp(name, names);
253 }
254 
255 
256 
257 #ifdef TEST
258 
259 int main(void)
260 {
261  int i;
262  const char *strings[] = {
263  "''",
264  "",
265  ":",
266  "\\",
267  "'",
268  " '' :",
269  " '' '' :",
270  "foo '' :",
271  "'foo'",
272  "foo ",
273  " ' foo ' ",
274  "foo\\",
275  "foo': blah:blah",
276  "foo\\: blah:blah",
277  "foo\'",
278  "'foo : ' :blahblah",
279  "\\ :blah",
280  " foo",
281  " foo ",
282  " foo \\ ",
283  "foo ':blah",
284  " foo bar : blahblah",
285  "\\f\\o\\o",
286  "'foo : \\ \\ ' : blahblah",
287  "'\\fo\\o:': blahblah",
288  "\\'fo\\o\\:': foo ' :blahblah"
289  };
290 
291  printf("Testing av_get_token()\n");
292  for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
293  const char *p = strings[i];
294  char *q;
295  printf("|%s|", p);
296  q = av_get_token(&p, ":");
297  printf(" -> |%s|", q);
298  printf(" + |%s|\n", p);
299  av_free(q);
300  }
301 
302  return 0;
303 }
304 
305 #endif /* TEST */
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int size
int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.c:231
memory handling functions
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:54
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:166
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.c:215
#define FF_ARRAY_ELEMS(a)
#define WHITESPACES
Definition: avstring.c:119
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
Definition: avstring.c:43
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:177
uint8_t
#define b
Definition: input.c:52
const char * name
static int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:182
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int main(int argc, char **argv)
Definition: avconv.c:2608
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition: avstring.c:237
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:225
#define FFMAX(a, b)
Definition: common.h:55
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:121
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
AVFifoBuffer ** buf
single buffer for interleaved, per-channel buffers for planar
Definition: audio_fifo.c:35
NULL
Definition: eval.c:55
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
char * av_d2str(double d)
Convert a number to a av_malloced string.
Definition: avstring.c:111
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
static int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:172
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:32
int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
Definition: avstring.c:220
char * av_strnstr(const char *haystack, const char *needle, size_t hay_length)
Locate the first occurrence of the string needle in the string haystack where not more than hay_lengt...
Definition: avstring.c:67
common internal and external API header
const char * av_dirname(char *path)
Thread safe dirname.
Definition: avstring.c:194
int len
#define FFMAX3(a, b, c)
Definition: common.h:56