avstring.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  3. * Copyright (c) 2007 Mans Rullgard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 (char*)(intptr_t)s1;
  54. do {
  55. if (av_stristart(s1, s2, NULL))
  56. return (char*)(intptr_t)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_asprintf(const char *fmt, ...)
  86. {
  87. char *p = NULL;
  88. va_list va;
  89. int len;
  90. va_start(va, fmt);
  91. len = vsnprintf(NULL, 0, fmt, va);
  92. va_end(va);
  93. if (len < 0)
  94. goto end;
  95. p = av_malloc(len + 1);
  96. if (!p)
  97. goto end;
  98. va_start(va, fmt);
  99. len = vsnprintf(p, len + 1, fmt, va);
  100. va_end(va);
  101. if (len < 0)
  102. av_freep(&p);
  103. end:
  104. return p;
  105. }
  106. char *av_d2str(double d)
  107. {
  108. char *str= av_malloc(16);
  109. if(str) snprintf(str, 16, "%f", d);
  110. return str;
  111. }
  112. #define WHITESPACES " \n\t"
  113. char *av_get_token(const char **buf, const char *term)
  114. {
  115. char *out = av_malloc(strlen(*buf) + 1);
  116. char *ret= out, *end= out;
  117. const char *p = *buf;
  118. if (!out) return NULL;
  119. p += strspn(p, WHITESPACES);
  120. while(*p && !strspn(p, term)) {
  121. char c = *p++;
  122. if(c == '\\' && *p){
  123. *out++ = *p++;
  124. end= out;
  125. }else if(c == '\''){
  126. while(*p && *p != '\'')
  127. *out++ = *p++;
  128. if(*p){
  129. p++;
  130. end= out;
  131. }
  132. }else{
  133. *out++ = c;
  134. }
  135. }
  136. do{
  137. *out-- = 0;
  138. }while(out >= end && strspn(out, WHITESPACES));
  139. *buf = p;
  140. return ret;
  141. }
  142. char *av_strtok(char *s, const char *delim, char **saveptr)
  143. {
  144. char *tok;
  145. if (!s && !(s = *saveptr))
  146. return NULL;
  147. /* skip leading delimiters */
  148. s += strspn(s, delim);
  149. /* s now points to the first non delimiter char, or to the end of the string */
  150. if (!*s) {
  151. *saveptr = NULL;
  152. return NULL;
  153. }
  154. tok = s++;
  155. /* skip non delimiters */
  156. s += strcspn(s, delim);
  157. if (*s) {
  158. *s = 0;
  159. *saveptr = s+1;
  160. } else {
  161. *saveptr = NULL;
  162. }
  163. return tok;
  164. }
  165. int av_strcasecmp(const char *a, const char *b)
  166. {
  167. uint8_t c1, c2;
  168. do {
  169. c1 = av_tolower(*a++);
  170. c2 = av_tolower(*b++);
  171. } while (c1 && c1 == c2);
  172. return c1 - c2;
  173. }
  174. int av_strncasecmp(const char *a, const char *b, size_t n)
  175. {
  176. const char *end = a + n;
  177. uint8_t c1, c2;
  178. do {
  179. c1 = av_tolower(*a++);
  180. c2 = av_tolower(*b++);
  181. } while (a < end && c1 && c1 == c2);
  182. return c1 - c2;
  183. }
  184. const char *av_basename(const char *path)
  185. {
  186. char *p = strrchr(path, '/');
  187. #if HAVE_DOS_PATHS
  188. char *q = strrchr(path, '\\');
  189. char *d = strchr(path, ':');
  190. p = FFMAX3(p, q, d);
  191. #endif
  192. if (!p)
  193. return path;
  194. return p + 1;
  195. }
  196. const char *av_dirname(char *path)
  197. {
  198. char *p = strrchr(path, '/');
  199. #if HAVE_DOS_PATHS
  200. char *q = strrchr(path, '\\');
  201. char *d = strchr(path, ':');
  202. d = d ? d + 1 : d;
  203. p = FFMAX3(p, q, d);
  204. #endif
  205. if (!p)
  206. return ".";
  207. *p = '\0';
  208. return path;
  209. }
  210. #ifdef TEST
  211. #include "common.h"
  212. int main(void)
  213. {
  214. int i;
  215. printf("Testing av_get_token()\n");
  216. {
  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. for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
  246. const char *p = strings[i];
  247. char *q;
  248. printf("|%s|", p);
  249. q = av_get_token(&p, ":");
  250. printf(" -> |%s|", q);
  251. printf(" + |%s|\n", p);
  252. av_free(q);
  253. }
  254. }
  255. return 0;
  256. }
  257. #endif /* TEST */