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_match_name(const char *name, const char *names)
216 {
217  const char *p;
218  int len, namelen;
219 
220  if (!name || !names)
221  return 0;
222 
223  namelen = strlen(name);
224  while ((p = strchr(names, ','))) {
225  len = FFMAX(p - names, namelen);
226  if (!av_strncasecmp(name, names, len))
227  return 1;
228  names = p + 1;
229  }
230  return !av_strcasecmp(name, names);
231 }
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
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
#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
#define src
Definition: vp8dsp.c:254
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:192
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:215
#define FFMAX(a, b)
Definition: common.h:64
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
const char * name
Definition: qsvenc.c:44
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
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:182
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
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
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
FILE * out
Definition: movenc.c:54
#define FFMAX3(a, b, c)
Definition: common.h:65