You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
6.4KB

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