avstring.c 7.2 KB

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