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.

286 lines
5.8KB

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