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.

213 lines
4.7KB

  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 "mem.h"
  28. int av_strstart(const char *str, const char *pfx, const char **ptr)
  29. {
  30. while (*pfx && *pfx == *str) {
  31. pfx++;
  32. str++;
  33. }
  34. if (!*pfx && ptr)
  35. *ptr = str;
  36. return !*pfx;
  37. }
  38. int av_stristart(const char *str, const char *pfx, const char **ptr)
  39. {
  40. while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
  41. pfx++;
  42. str++;
  43. }
  44. if (!*pfx && ptr)
  45. *ptr = str;
  46. return !*pfx;
  47. }
  48. char *av_stristr(const char *s1, const char *s2)
  49. {
  50. if (!*s2)
  51. return s1;
  52. do {
  53. if (av_stristart(s1, s2, NULL))
  54. return s1;
  55. } while (*s1++);
  56. return NULL;
  57. }
  58. size_t av_strlcpy(char *dst, const char *src, size_t size)
  59. {
  60. size_t len = 0;
  61. while (++len < size && *src)
  62. *dst++ = *src++;
  63. if (len <= size)
  64. *dst = 0;
  65. return len + strlen(src) - 1;
  66. }
  67. size_t av_strlcat(char *dst, const char *src, size_t size)
  68. {
  69. size_t len = strlen(dst);
  70. if (size <= len + 1)
  71. return len + strlen(src);
  72. return len + av_strlcpy(dst + len, src, size - len);
  73. }
  74. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  75. {
  76. int len = strlen(dst);
  77. va_list vl;
  78. va_start(vl, fmt);
  79. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  80. va_end(vl);
  81. return len;
  82. }
  83. char *av_d2str(double d)
  84. {
  85. char *str= av_malloc(16);
  86. if(str) snprintf(str, 16, "%f", d);
  87. return str;
  88. }
  89. #define WHITESPACES " \n\t"
  90. char *av_get_token(const char **buf, const char *term)
  91. {
  92. char *out = av_malloc(strlen(*buf) + 1);
  93. char *ret= out, *end= out;
  94. const char *p = *buf;
  95. if (!out) return NULL;
  96. p += strspn(p, WHITESPACES);
  97. while(*p && !strspn(p, term)) {
  98. char c = *p++;
  99. if(c == '\\' && *p){
  100. *out++ = *p++;
  101. end= out;
  102. }else if(c == '\''){
  103. while(*p && *p != '\'')
  104. *out++ = *p++;
  105. if(*p){
  106. p++;
  107. end= out;
  108. }
  109. }else{
  110. *out++ = c;
  111. }
  112. }
  113. do{
  114. *out-- = 0;
  115. }while(out >= end && strspn(out, WHITESPACES));
  116. *buf = p;
  117. return ret;
  118. }
  119. int av_strcasecmp(const char *a, const char *b)
  120. {
  121. uint8_t c1, c2;
  122. do {
  123. c1 = av_tolower(*a++);
  124. c2 = av_tolower(*b++);
  125. } while (c1 && c1 == c2);
  126. return c1 - c2;
  127. }
  128. int av_strncasecmp(const char *a, const char *b, size_t n)
  129. {
  130. const char *end = a + n;
  131. uint8_t c1, c2;
  132. do {
  133. c1 = av_tolower(*a++);
  134. c2 = av_tolower(*b++);
  135. } while (a < end && c1 && c1 == c2);
  136. return c1 - c2;
  137. }
  138. #ifdef TEST
  139. #include "common.h"
  140. #undef printf
  141. int main(void)
  142. {
  143. int i;
  144. printf("Testing av_get_token()\n");
  145. {
  146. const char *strings[] = {
  147. "''",
  148. "",
  149. ":",
  150. "\\",
  151. "'",
  152. " '' :",
  153. " '' '' :",
  154. "foo '' :",
  155. "'foo'",
  156. "foo ",
  157. " ' foo ' ",
  158. "foo\\",
  159. "foo': blah:blah",
  160. "foo\\: blah:blah",
  161. "foo\'",
  162. "'foo : ' :blahblah",
  163. "\\ :blah",
  164. " foo",
  165. " foo ",
  166. " foo \\ ",
  167. "foo ':blah",
  168. " foo bar : blahblah",
  169. "\\f\\o\\o",
  170. "'foo : \\ \\ ' : blahblah",
  171. "'\\fo\\o:': blahblah",
  172. "\\'fo\\o\\:': foo ' :blahblah"
  173. };
  174. for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
  175. const char *p = strings[i], *q;
  176. printf("|%s|", p);
  177. q = av_get_token(&p, ":");
  178. printf(" -> |%s|", q);
  179. printf(" + |%s|\n", p);
  180. av_free(q);
  181. }
  182. }
  183. return 0;
  184. }
  185. #endif /* TEST */