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.

250 lines
5.1KB

  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. size_t av_strlcpy(char *dst, const char *src, size_t size)
  61. {
  62. size_t len = 0;
  63. while (++len < size && *src)
  64. *dst++ = *src++;
  65. if (len <= size)
  66. *dst = 0;
  67. return len + strlen(src) - 1;
  68. }
  69. size_t av_strlcat(char *dst, const char *src, size_t size)
  70. {
  71. size_t len = strlen(dst);
  72. if (size <= len + 1)
  73. return len + strlen(src);
  74. return len + av_strlcpy(dst + len, src, size - len);
  75. }
  76. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  77. {
  78. int len = strlen(dst);
  79. va_list vl;
  80. va_start(vl, fmt);
  81. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  82. va_end(vl);
  83. return len;
  84. }
  85. char *av_d2str(double d)
  86. {
  87. char *str = av_malloc(16);
  88. if (str)
  89. snprintf(str, 16, "%f", d);
  90. return str;
  91. }
  92. #define WHITESPACES " \n\t"
  93. char *av_get_token(const char **buf, const char *term)
  94. {
  95. char *out = av_malloc(strlen(*buf) + 1);
  96. char *ret = out, *end = out;
  97. const char *p = *buf;
  98. if (!out)
  99. return NULL;
  100. p += strspn(p, WHITESPACES);
  101. while (*p && !strspn(p, term)) {
  102. char c = *p++;
  103. if (c == '\\' && *p) {
  104. *out++ = *p++;
  105. end = out;
  106. } else if (c == '\'') {
  107. while (*p && *p != '\'')
  108. *out++ = *p++;
  109. if (*p) {
  110. p++;
  111. end = out;
  112. }
  113. } else {
  114. *out++ = c;
  115. }
  116. }
  117. do
  118. *out-- = 0;
  119. while (out >= end && strspn(out, WHITESPACES));
  120. *buf = p;
  121. return ret;
  122. }
  123. int av_strcasecmp(const char *a, const char *b)
  124. {
  125. uint8_t c1, c2;
  126. do {
  127. c1 = av_tolower(*a++);
  128. c2 = av_tolower(*b++);
  129. } while (c1 && c1 == c2);
  130. return c1 - c2;
  131. }
  132. int av_strncasecmp(const char *a, const char *b, size_t n)
  133. {
  134. const char *end = a + n;
  135. uint8_t c1, c2;
  136. do {
  137. c1 = av_tolower(*a++);
  138. c2 = av_tolower(*b++);
  139. } while (a < end && c1 && c1 == c2);
  140. return c1 - c2;
  141. }
  142. const char *av_basename(const char *path)
  143. {
  144. char *p = strrchr(path, '/');
  145. #if HAVE_DOS_PATHS
  146. char *q = strrchr(path, '\\');
  147. char *d = strchr(path, ':');
  148. p = FFMAX3(p, q, d);
  149. #endif
  150. if (!p)
  151. return path;
  152. return p + 1;
  153. }
  154. const char *av_dirname(char *path)
  155. {
  156. char *p = strrchr(path, '/');
  157. #if HAVE_DOS_PATHS
  158. char *q = strrchr(path, '\\');
  159. char *d = strchr(path, ':');
  160. d = d ? d + 1 : d;
  161. p = FFMAX3(p, q, d);
  162. #endif
  163. if (!p)
  164. return ".";
  165. *p = '\0';
  166. return path;
  167. }
  168. #ifdef TEST
  169. int main(void)
  170. {
  171. int i;
  172. const char *strings[] = {
  173. "''",
  174. "",
  175. ":",
  176. "\\",
  177. "'",
  178. " '' :",
  179. " '' '' :",
  180. "foo '' :",
  181. "'foo'",
  182. "foo ",
  183. " ' foo ' ",
  184. "foo\\",
  185. "foo': blah:blah",
  186. "foo\\: blah:blah",
  187. "foo\'",
  188. "'foo : ' :blahblah",
  189. "\\ :blah",
  190. " foo",
  191. " foo ",
  192. " foo \\ ",
  193. "foo ':blah",
  194. " foo bar : blahblah",
  195. "\\f\\o\\o",
  196. "'foo : \\ \\ ' : blahblah",
  197. "'\\fo\\o:': blahblah",
  198. "\\'fo\\o\\:': foo ' :blahblah"
  199. };
  200. printf("Testing av_get_token()\n");
  201. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  202. const char *p = strings[i], *q;
  203. printf("|%s|", p);
  204. q = av_get_token(&p, ":");
  205. printf(" -> |%s|", q);
  206. printf(" + |%s|\n", p);
  207. av_free(q);
  208. }
  209. return 0;
  210. }
  211. #endif /* TEST */