avstring.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 s1;
  51. do {
  52. if (av_stristart(s1, s2, NULL))
  53. return 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_d2str(double d)
  83. {
  84. char *str= av_malloc(16);
  85. if(str) snprintf(str, 16, "%f", d);
  86. return str;
  87. }
  88. #define WHITESPACES " \n\t"
  89. char *av_get_token(const char **buf, const char *term)
  90. {
  91. char *out = av_malloc(strlen(*buf) + 1);
  92. char *ret= out, *end= out;
  93. const char *p = *buf;
  94. if (!out) return NULL;
  95. p += strspn(p, WHITESPACES);
  96. while(*p && !strspn(p, term)) {
  97. char c = *p++;
  98. if(c == '\\' && *p){
  99. *out++ = *p++;
  100. end= out;
  101. }else if(c == '\''){
  102. while(*p && *p != '\'')
  103. *out++ = *p++;
  104. if(*p){
  105. p++;
  106. end= out;
  107. }
  108. }else{
  109. *out++ = c;
  110. }
  111. }
  112. do{
  113. *out-- = 0;
  114. }while(out >= end && strspn(out, WHITESPACES));
  115. *buf = p;
  116. return ret;
  117. }
  118. #ifdef TEST
  119. #undef printf
  120. int main(void)
  121. {
  122. int i;
  123. printf("Testing av_get_token()\n");
  124. {
  125. const char *strings[] = {
  126. "''",
  127. "",
  128. ":",
  129. "\\",
  130. "'",
  131. " '' :",
  132. " '' '' :",
  133. "foo '' :",
  134. "'foo'",
  135. "foo ",
  136. " ' foo ' ",
  137. "foo\\",
  138. "foo': blah:blah",
  139. "foo\\: blah:blah",
  140. "foo\'",
  141. "'foo : ' :blahblah",
  142. "\\ :blah",
  143. " foo",
  144. " foo ",
  145. " foo \\ ",
  146. "foo ':blah",
  147. " foo bar : blahblah",
  148. "\\f\\o\\o",
  149. "'foo : \\ \\ ' : blahblah",
  150. "'\\fo\\o:': blahblah",
  151. "\\'fo\\o\\:': foo ' :blahblah"
  152. };
  153. for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
  154. const char *p= strings[i];
  155. printf("|%s|", p);
  156. printf(" -> |%s|", av_get_token(&p, ":"));
  157. printf(" + |%s|\n", p);
  158. }
  159. }
  160. return 0;
  161. }
  162. #endif /* TEST */