avstring.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "avstring.h"
  26. #include "mem.h"
  27. int av_strstart(const char *str, const char *pfx, const char **ptr)
  28. {
  29. while (*pfx && *pfx == *str) {
  30. pfx++;
  31. str++;
  32. }
  33. if (!*pfx && ptr)
  34. *ptr = str;
  35. return !*pfx;
  36. }
  37. int av_stristart(const char *str, const char *pfx, const char **ptr)
  38. {
  39. while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
  40. pfx++;
  41. str++;
  42. }
  43. if (!*pfx && ptr)
  44. *ptr = str;
  45. return !*pfx;
  46. }
  47. char *av_stristr(const char *s1, const char *s2)
  48. {
  49. if (!*s2)
  50. return (char*)(intptr_t)s1;
  51. do {
  52. if (av_stristart(s1, s2, NULL))
  53. return (char*)(intptr_t)s1;
  54. } while (*s1++);
  55. return NULL;
  56. }
  57. size_t av_strlcpy(char *dst, const char *src, size_t size)
  58. {
  59. size_t len = 0;
  60. while (++len < size && *src)
  61. *dst++ = *src++;
  62. if (len <= size)
  63. *dst = 0;
  64. return len + strlen(src) - 1;
  65. }
  66. size_t av_strlcat(char *dst, const char *src, size_t size)
  67. {
  68. size_t len = strlen(dst);
  69. if (size <= len + 1)
  70. return len + strlen(src);
  71. return len + av_strlcpy(dst + len, src, size - len);
  72. }
  73. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  74. {
  75. int len = strlen(dst);
  76. va_list vl;
  77. va_start(vl, fmt);
  78. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  79. va_end(vl);
  80. return len;
  81. }
  82. char *av_asprintf(const char *fmt, ...)
  83. {
  84. char *p = NULL;
  85. va_list va;
  86. int len;
  87. va_start(va, fmt);
  88. len = vsnprintf(NULL, 0, fmt, va);
  89. va_end(va);
  90. if (len < 0)
  91. goto end;
  92. p = av_malloc(len + 1);
  93. if (!p)
  94. goto end;
  95. va_start(va, fmt);
  96. len = vsnprintf(p, len + 1, fmt, va);
  97. va_end(va);
  98. if (len < 0)
  99. av_freep(&p);
  100. end:
  101. return p;
  102. }
  103. char *av_d2str(double d)
  104. {
  105. char *str= av_malloc(16);
  106. if(str) snprintf(str, 16, "%f", d);
  107. return str;
  108. }
  109. #define WHITESPACES " \n\t"
  110. char *av_get_token(const char **buf, const char *term)
  111. {
  112. char *out = av_malloc(strlen(*buf) + 1);
  113. char *ret= out, *end= out;
  114. const char *p = *buf;
  115. if (!out) return NULL;
  116. p += strspn(p, WHITESPACES);
  117. while(*p && !strspn(p, term)) {
  118. char c = *p++;
  119. if(c == '\\' && *p){
  120. *out++ = *p++;
  121. end= out;
  122. }else if(c == '\''){
  123. while(*p && *p != '\'')
  124. *out++ = *p++;
  125. if(*p){
  126. p++;
  127. end= out;
  128. }
  129. }else{
  130. *out++ = c;
  131. }
  132. }
  133. do{
  134. *out-- = 0;
  135. }while(out >= end && strspn(out, WHITESPACES));
  136. *buf = p;
  137. return ret;
  138. }
  139. char *av_strtok(char *s, const char *delim, char **saveptr)
  140. {
  141. char *tok;
  142. if (!s && !(s = *saveptr))
  143. return NULL;
  144. /* skip leading delimiters */
  145. s += strspn(s, delim);
  146. /* s now points to the first non delimiter char, or to the end of the string */
  147. if (!*s) {
  148. *saveptr = NULL;
  149. return NULL;
  150. }
  151. tok = s++;
  152. /* skip non delimiters */
  153. s += strcspn(s, delim);
  154. if (*s) {
  155. *s = 0;
  156. *saveptr = s+1;
  157. } else {
  158. *saveptr = NULL;
  159. }
  160. return tok;
  161. }
  162. int av_strcasecmp(const char *a, const char *b)
  163. {
  164. uint8_t c1, c2;
  165. do {
  166. c1 = av_tolower(*a++);
  167. c2 = av_tolower(*b++);
  168. } while (c1 && c1 == c2);
  169. return c1 - c2;
  170. }
  171. int av_strncasecmp(const char *a, const char *b, size_t n)
  172. {
  173. const char *end = a + n;
  174. uint8_t c1, c2;
  175. do {
  176. c1 = av_tolower(*a++);
  177. c2 = av_tolower(*b++);
  178. } while (a < end && c1 && c1 == c2);
  179. return c1 - c2;
  180. }
  181. #ifdef TEST
  182. #undef printf
  183. int main(void)
  184. {
  185. int i;
  186. printf("Testing av_get_token()\n");
  187. {
  188. const char *strings[] = {
  189. "''",
  190. "",
  191. ":",
  192. "\\",
  193. "'",
  194. " '' :",
  195. " '' '' :",
  196. "foo '' :",
  197. "'foo'",
  198. "foo ",
  199. " ' foo ' ",
  200. "foo\\",
  201. "foo': blah:blah",
  202. "foo\\: blah:blah",
  203. "foo\'",
  204. "'foo : ' :blahblah",
  205. "\\ :blah",
  206. " foo",
  207. " foo ",
  208. " foo \\ ",
  209. "foo ':blah",
  210. " foo bar : blahblah",
  211. "\\f\\o\\o",
  212. "'foo : \\ \\ ' : blahblah",
  213. "'\\fo\\o:': blahblah",
  214. "\\'fo\\o\\:': foo ' :blahblah"
  215. };
  216. for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
  217. const char *p= strings[i];
  218. printf("|%s|", p);
  219. printf(" -> |%s|", av_get_token(&p, ":"));
  220. printf(" + |%s|\n", p);
  221. }
  222. }
  223. return 0;
  224. }
  225. #endif /* TEST */