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 
27 #include "config.h"
28 #include "common.h"
29 #include "mem.h"
30 #include "avstring.h"
31 #include "bprint.h"
32 
33 int av_strstart(const char *str, const char *pfx, const char **ptr)
34 {
35  while (*pfx && *pfx == *str) {
36  pfx++;
37  str++;
38  }
39  if (!*pfx && ptr)
40  *ptr = str;
41  return !*pfx;
42 }
43 
44 int av_stristart(const char *str, const char *pfx, const char **ptr)
45 {
46  while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
47  pfx++;
48  str++;
49  }
50  if (!*pfx && ptr)
51  *ptr = str;
52  return !*pfx;
53 }
54 
55 char *av_stristr(const char *s1, const char *s2)
56 {
57  if (!*s2)
58  return (char*)(intptr_t)s1;
59 
60  do
61  if (av_stristart(s1, s2, NULL))
62  return (char*)(intptr_t)s1;
63  while (*s1++);
64 
65  return NULL;
66 }
67 
68 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
69 {
70  size_t needle_len = strlen(needle);
71  if (!needle_len)
72  return (char*)haystack;
73  while (hay_length >= needle_len) {
74  hay_length--;
75  if (!memcmp(haystack, needle, needle_len))
76  return (char*)haystack;
77  haystack++;
78  }
79  return NULL;
80 }
81 
82 size_t av_strlcpy(char *dst, const char *src, size_t size)
83 {
84  size_t len = 0;
85  while (++len < size && *src)
86  *dst++ = *src++;
87  if (len <= size)
88  *dst = 0;
89  return len + strlen(src) - 1;
90 }
91 
92 size_t av_strlcat(char *dst, const char *src, size_t size)
93 {
94  size_t len = strlen(dst);
95  if (size <= len + 1)
96  return len + strlen(src);
97  return len + av_strlcpy(dst + len, src, size - len);
98 }
99 
100 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
101 {
102  int len = strlen(dst);
103  va_list vl;
104 
105  va_start(vl, fmt);
106  len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
107  va_end(vl);
108 
109  return len;
110 }
111 
112 char *av_asprintf(const char *fmt, ...)
113 {
114  char *p = NULL;
115  va_list va;
116  int len;
117 
118  va_start(va, fmt);
119  len = vsnprintf(NULL, 0, fmt, va);
120  va_end(va);
121  if (len < 0)
122  goto end;
123 
124  p = av_malloc(len + 1);
125  if (!p)
126  goto end;
127 
128  va_start(va, fmt);
129  len = vsnprintf(p, len + 1, fmt, va);
130  va_end(va);
131  if (len < 0)
132  av_freep(&p);
133 
134 end:
135  return p;
136 }
137 
138 char *av_d2str(double d)
139 {
140  char *str = av_malloc(16);
141  if (str)
142  snprintf(str, 16, "%f", d);
143  return str;
144 }
145 
146 #define WHITESPACES " \n\t"
147 
148 char *av_get_token(const char **buf, const char *term)
149 {
150  char *out = av_malloc(strlen(*buf) + 1);
151  char *ret = out, *end = out;
152  const char *p = *buf;
153  if (!out)
154  return NULL;
155  p += strspn(p, WHITESPACES);
156 
157  while (*p && !strspn(p, term)) {
158  char c = *p++;
159  if (c == '\\' && *p) {
160  *out++ = *p++;
161  end = out;
162  } else if (c == '\'') {
163  while (*p && *p != '\'')
164  *out++ = *p++;
165  if (*p) {
166  p++;
167  end = out;
168  }
169  } else {
170  *out++ = c;
171  }
172  }
173 
174  do
175  *out-- = 0;
176  while (out >= end && strspn(out, WHITESPACES));
177 
178  *buf = p;
179 
180  return ret;
181 }
182 
183 char *av_strtok(char *s, const char *delim, char **saveptr)
184 {
185  char *tok;
186 
187  if (!s && !(s = *saveptr))
188  return NULL;
189 
190  /* skip leading delimiters */
191  s += strspn(s, delim);
192 
193  /* s now points to the first non delimiter char, or to the end of the string */
194  if (!*s) {
195  *saveptr = NULL;
196  return NULL;
197  }
198  tok = s++;
199 
200  /* skip non delimiters */
201  s += strcspn(s, delim);
202  if (*s) {
203  *s = 0;
204  *saveptr = s+1;
205  } else {
206  *saveptr = NULL;
207  }
208 
209  return tok;
210 }
211 
212 int av_strcasecmp(const char *a, const char *b)
213 {
214  uint8_t c1, c2;
215  do {
216  c1 = av_tolower(*a++);
217  c2 = av_tolower(*b++);
218  } while (c1 && c1 == c2);
219  return c1 - c2;
220 }
221 
222 int av_strncasecmp(const char *a, const char *b, size_t n)
223 {
224  const char *end = a + n;
225  uint8_t c1, c2;
226  do {
227  c1 = av_tolower(*a++);
228  c2 = av_tolower(*b++);
229  } while (a < end && c1 && c1 == c2);
230  return c1 - c2;
231 }
232 
233 const char *av_basename(const 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  p = FFMAX3(p, q, d);
242 #endif
243 
244  if (!p)
245  return path;
246 
247  return p + 1;
248 }
249 
250 const char *av_dirname(char *path)
251 {
252  char *p = strrchr(path, '/');
253 
254 #if HAVE_DOS_PATHS
255  char *q = strrchr(path, '\\');
256  char *d = strchr(path, ':');
257 
258  d = d ? d + 1 : d;
259 
260  p = FFMAX3(p, q, d);
261 #endif
262 
263  if (!p)
264  return ".";
265 
266  *p = '\0';
267 
268  return path;
269 }
270 
271 int av_escape(char **dst, const char *src, const char *special_chars,
272  enum AVEscapeMode mode, int flags)
273 {
274  AVBPrint dstbuf;
275 
277  av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
278 
279  if (!av_bprint_is_complete(&dstbuf)) {
280  av_bprint_finalize(&dstbuf, NULL);
281  return AVERROR(ENOMEM);
282  } else {
283  av_bprint_finalize(&dstbuf, dst);
284  return dstbuf.len;
285  }
286 }
287 
288 int av_isdigit(int c)
289 {
290  return c >= '0' && c <= '9';
291 }
292 
293 int av_isgraph(int c)
294 {
295  return c > 32 && c < 127;
296 }
297 
298 int av_isspace(int c)
299 {
300  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
301  c == '\v';
302 }
303 
304 int av_isxdigit(int c)
305 {
306  c = av_tolower(c);
307  return av_isdigit(c) || (c >= 'a' && c <= 'f');
308 }
309 
310 #ifdef TEST
311 
312 int main(void)
313 {
314  int i;
315  const char *strings[] = {
316  "''",
317  "",
318  ":",
319  "\\",
320  "'",
321  " '' :",
322  " '' '' :",
323  "foo '' :",
324  "'foo'",
325  "foo ",
326  " ' foo ' ",
327  "foo\\",
328  "foo': blah:blah",
329  "foo\\: blah:blah",
330  "foo\'",
331  "'foo : ' :blahblah",
332  "\\ :blah",
333  " foo",
334  " foo ",
335  " foo \\ ",
336  "foo ':blah",
337  " foo bar : blahblah",
338  "\\f\\o\\o",
339  "'foo : \\ \\ ' : blahblah",
340  "'\\fo\\o:': blahblah",
341  "\\'fo\\o\\:': foo ' :blahblah"
342  };
343 
344  printf("Testing av_get_token()\n");
345  for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
346  const char *p = strings[i];
347  char *q;
348  printf("|%s|", p);
349  q = av_get_token(&p, ":");
350  printf(" -> |%s|", q);
351  printf(" + |%s|\n", p);
352  av_free(q);
353  }
354 
355  return 0;
356 }
357 
358 #endif /* TEST */
const char * s
Definition: avisynth_c.h:668
int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.c:304
#define c2
Definition: idct_sh4.c:27
const char * fmt
Definition: avisynth_c.h:669
int av_escape(char **dst, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape string in src, and put the escaped string in an allocated string in *dst, which must be freed ...
Definition: avstring.c:271
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:55
#define vsnprintf
Definition: snprintf.h:36
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:222
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.c:288
#define FF_ARRAY_ELEMS(a)
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:193
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
set threshold d
#define WHITESPACES
Definition: avstring.c:146
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:44
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:233
uint8_t
mode
Definition: f_perms.c:27
#define b
Definition: input.c:42
end end
static int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:216
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define AV_BPRINT_SIZE_UNLIMITED
Convenience macros for special values for av_bprint_init() size_max parameter.
Definition: bprint.h:89
#define s2
Definition: regdef.h:39
char * av_d2str(double d)
Convert a number to a av_malloced string.
Definition: avstring.c:138
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
Definition: bprint.c:68
static int av_bprint_is_complete(AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:166
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:298
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:82
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:148
int size
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:112
Buffer to print data progressively.
Definition: bprint.h:75
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:212
ret
Definition: avfilter.c:821
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
void * buf
Definition: avisynth_c.h:594
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:73
synthesis window for stochastic i
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:100
#define s1
Definition: regdef.h:38
#define snprintf
Definition: snprintf.h:34
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
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:92
static int flags
Definition: cpu.c:23
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:183
static int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:206
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:33
void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape the content in src and append it to dstbuf.
Definition: bprint.c:223
int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
Definition: avstring.c:293
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:68
common internal and external API header
static double c[64]
const char * av_dirname(char *path)
Thread safe dirname.
Definition: avstring.c:250
int len
printf("static const uint8_t my_array[100] = {\n")
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
AVEscapeMode
Definition: avstring.h:256
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int main(int argc, char **argv)
Definition: main.c:22
#define FFMAX3(a, b, c)
Definition: common.h:57
#define c1
Definition: idct_sh4.c:26