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.

232 lines
4.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 "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_match_name(const char *name, const char *names)
  181. {
  182. const char *p;
  183. int len, namelen;
  184. if (!name || !names)
  185. return 0;
  186. namelen = strlen(name);
  187. while ((p = strchr(names, ','))) {
  188. len = FFMAX(p - names, namelen);
  189. if (!av_strncasecmp(name, names, len))
  190. return 1;
  191. names = p + 1;
  192. }
  193. return !av_strcasecmp(name, names);
  194. }