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.

253 lines
5.2KB

  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 "avstring.h"
  27. #include "config.h"
  28. #include "common.h"
  29. #include "mem.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) snprintf(str, 16, "%f", d);
  89. return str;
  90. }
  91. #define WHITESPACES " \n\t"
  92. char *av_get_token(const char **buf, const char *term)
  93. {
  94. char *out = av_malloc(strlen(*buf) + 1);
  95. char *ret= out, *end= out;
  96. const char *p = *buf;
  97. if (!out) return NULL;
  98. p += strspn(p, WHITESPACES);
  99. while(*p && !strspn(p, term)) {
  100. char c = *p++;
  101. if(c == '\\' && *p){
  102. *out++ = *p++;
  103. end= out;
  104. }else if(c == '\''){
  105. while(*p && *p != '\'')
  106. *out++ = *p++;
  107. if(*p){
  108. p++;
  109. end= out;
  110. }
  111. }else{
  112. *out++ = c;
  113. }
  114. }
  115. do{
  116. *out-- = 0;
  117. }while(out >= end && strspn(out, WHITESPACES));
  118. *buf = p;
  119. return ret;
  120. }
  121. int av_strcasecmp(const char *a, const char *b)
  122. {
  123. uint8_t c1, c2;
  124. do {
  125. c1 = av_tolower(*a++);
  126. c2 = av_tolower(*b++);
  127. } while (c1 && c1 == c2);
  128. return c1 - c2;
  129. }
  130. int av_strncasecmp(const char *a, const char *b, size_t n)
  131. {
  132. const char *end = a + n;
  133. uint8_t c1, c2;
  134. do {
  135. c1 = av_tolower(*a++);
  136. c2 = av_tolower(*b++);
  137. } while (a < end && c1 && c1 == c2);
  138. return c1 - c2;
  139. }
  140. const char *av_basename(const char *path)
  141. {
  142. char *p = strrchr(path, '/');
  143. #if HAVE_DOS_PATHS
  144. char *q = strrchr(path, '\\');
  145. char *d = strchr(path, ':');
  146. p = FFMAX3(p, q, d);
  147. #endif
  148. if (!p)
  149. return path;
  150. return p + 1;
  151. }
  152. const char *av_dirname(char *path)
  153. {
  154. char *p = strrchr(path, '/');
  155. #if HAVE_DOS_PATHS
  156. char *q = strrchr(path, '\\');
  157. char *d = strchr(path, ':');
  158. d = d ? d + 1 : d;
  159. p = FFMAX3(p, q, d);
  160. #endif
  161. if (!p)
  162. return ".";
  163. *p = '\0';
  164. return path;
  165. }
  166. #ifdef TEST
  167. #include "common.h"
  168. int main(void)
  169. {
  170. int i;
  171. printf("Testing av_get_token()\n");
  172. {
  173. const char *strings[] = {
  174. "''",
  175. "",
  176. ":",
  177. "\\",
  178. "'",
  179. " '' :",
  180. " '' '' :",
  181. "foo '' :",
  182. "'foo'",
  183. "foo ",
  184. " ' foo ' ",
  185. "foo\\",
  186. "foo': blah:blah",
  187. "foo\\: blah:blah",
  188. "foo\'",
  189. "'foo : ' :blahblah",
  190. "\\ :blah",
  191. " foo",
  192. " foo ",
  193. " foo \\ ",
  194. "foo ':blah",
  195. " foo bar : blahblah",
  196. "\\f\\o\\o",
  197. "'foo : \\ \\ ' : blahblah",
  198. "'\\fo\\o:': blahblah",
  199. "\\'fo\\o\\:': foo ' :blahblah"
  200. };
  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. }
  210. return 0;
  211. }
  212. #endif /* TEST */