avstring.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #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 s1;
  53. do
  54. if (av_stristart(s1, s2, NULL))
  55. return 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 haystack;
  64. while (hay_length >= needle_len) {
  65. hay_length--;
  66. if (!memcmp(haystack, needle, needle_len))
  67. return 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_d2str(double d)
  98. {
  99. char *str = av_malloc(16);
  100. if (str)
  101. snprintf(str, 16, "%f", d);
  102. return str;
  103. }
  104. #define WHITESPACES " \n\t"
  105. char *av_get_token(const char **buf, const char *term)
  106. {
  107. char *out = av_malloc(strlen(*buf) + 1);
  108. char *ret = out, *end = out;
  109. const char *p = *buf;
  110. if (!out)
  111. return NULL;
  112. p += strspn(p, WHITESPACES);
  113. while (*p && !strspn(p, term)) {
  114. char c = *p++;
  115. if (c == '\\' && *p) {
  116. *out++ = *p++;
  117. end = out;
  118. } else if (c == '\'') {
  119. while (*p && *p != '\'')
  120. *out++ = *p++;
  121. if (*p) {
  122. p++;
  123. end = out;
  124. }
  125. } else {
  126. *out++ = c;
  127. }
  128. }
  129. do
  130. *out-- = 0;
  131. while (out >= end && strspn(out, WHITESPACES));
  132. *buf = p;
  133. return ret;
  134. }
  135. int av_strcasecmp(const char *a, const char *b)
  136. {
  137. uint8_t c1, c2;
  138. do {
  139. c1 = av_tolower(*a++);
  140. c2 = av_tolower(*b++);
  141. } while (c1 && c1 == c2);
  142. return c1 - c2;
  143. }
  144. int av_strncasecmp(const char *a, const char *b, size_t n)
  145. {
  146. const char *end = a + n;
  147. uint8_t c1, c2;
  148. do {
  149. c1 = av_tolower(*a++);
  150. c2 = av_tolower(*b++);
  151. } while (a < end && c1 && c1 == c2);
  152. return c1 - c2;
  153. }
  154. const char *av_basename(const char *path)
  155. {
  156. char *p = strrchr(path, '/');
  157. #if HAVE_DOS_PATHS
  158. char *q = strrchr(path, '\\');
  159. char *d = strchr(path, ':');
  160. p = FFMAX3(p, q, d);
  161. #endif
  162. if (!p)
  163. return path;
  164. return p + 1;
  165. }
  166. const char *av_dirname(char *path)
  167. {
  168. char *p = strrchr(path, '/');
  169. #if HAVE_DOS_PATHS
  170. char *q = strrchr(path, '\\');
  171. char *d = strchr(path, ':');
  172. d = d ? d + 1 : d;
  173. p = FFMAX3(p, q, d);
  174. #endif
  175. if (!p)
  176. return ".";
  177. *p = '\0';
  178. return path;
  179. }
  180. int av_isdigit(int c)
  181. {
  182. return c >= '0' && c <= '9';
  183. }
  184. int av_isgraph(int c)
  185. {
  186. return c > 32 && c < 127;
  187. }
  188. int av_isspace(int c)
  189. {
  190. return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
  191. c == '\v';
  192. }
  193. int av_isxdigit(int c)
  194. {
  195. c = av_tolower(c);
  196. return av_isdigit(c) || (c >= 'a' && c <= 'f');
  197. }
  198. int av_match_name(const char *name, const char *names)
  199. {
  200. const char *p;
  201. int len, namelen;
  202. if (!name || !names)
  203. return 0;
  204. namelen = strlen(name);
  205. while ((p = strchr(names, ','))) {
  206. len = FFMAX(p - names, namelen);
  207. if (!av_strncasecmp(name, names, len))
  208. return 1;
  209. names = p + 1;
  210. }
  211. return !av_strcasecmp(name, names);
  212. }
  213. #ifdef TEST
  214. int main(void)
  215. {
  216. int i;
  217. const char *strings[] = {
  218. "''",
  219. "",
  220. ":",
  221. "\\",
  222. "'",
  223. " '' :",
  224. " '' '' :",
  225. "foo '' :",
  226. "'foo'",
  227. "foo ",
  228. " ' foo ' ",
  229. "foo\\",
  230. "foo': blah:blah",
  231. "foo\\: blah:blah",
  232. "foo\'",
  233. "'foo : ' :blahblah",
  234. "\\ :blah",
  235. " foo",
  236. " foo ",
  237. " foo \\ ",
  238. "foo ':blah",
  239. " foo bar : blahblah",
  240. "\\f\\o\\o",
  241. "'foo : \\ \\ ' : blahblah",
  242. "'\\fo\\o:': blahblah",
  243. "\\'fo\\o\\:': foo ' :blahblah"
  244. };
  245. printf("Testing av_get_token()\n");
  246. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  247. const char *p = strings[i];
  248. char *q;
  249. printf("|%s|", p);
  250. q = av_get_token(&p, ":");
  251. printf(" -> |%s|", q);
  252. printf(" + |%s|\n", p);
  253. av_free(q);
  254. }
  255. return 0;
  256. }
  257. #endif /* TEST */