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.

111 lines
2.5KB

  1. /*
  2. * copyright (c) 2009 Stefano Sabatini
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file libavfilter/parseutils.c
  21. * parsing utils
  22. */
  23. #include <string.h>
  24. #include "libavutil/avutil.h"
  25. #include "parseutils.h"
  26. #define WHITESPACES " \n\t"
  27. char *av_get_token(const char **buf, const char *term)
  28. {
  29. char *out = av_malloc(strlen(*buf) + 1);
  30. char *ret= out, *end= out;
  31. const char *p = *buf;
  32. p += strspn(p, WHITESPACES);
  33. while(*p && !strspn(p, term)) {
  34. char c = *p++;
  35. if(c == '\\' && *p){
  36. *out++ = *p++;
  37. end= out;
  38. }else if(c == '\''){
  39. while(*p && *p != '\'')
  40. *out++ = *p++;
  41. if(*p){
  42. p++;
  43. end= out;
  44. }
  45. }else{
  46. *out++ = c;
  47. }
  48. }
  49. do{
  50. *out-- = 0;
  51. }while(out >= end && strspn(out, WHITESPACES));
  52. *buf = p;
  53. return ret;
  54. }
  55. #ifdef TEST
  56. #undef printf
  57. int main(void)
  58. {
  59. int i;
  60. const char *strings[] = {
  61. "''",
  62. "",
  63. ":",
  64. "\\",
  65. "'",
  66. " '' :",
  67. " '' '' :",
  68. "foo '' :",
  69. "'foo'",
  70. "foo ",
  71. "foo\\",
  72. "foo': blah:blah",
  73. "foo\\: blah:blah",
  74. "foo\'",
  75. "'foo : ' :blahblah",
  76. "\\ :blah",
  77. " foo",
  78. " foo ",
  79. " foo \\ ",
  80. "foo ':blah",
  81. " foo bar : blahblah",
  82. "\\f\\o\\o",
  83. "'foo : \\ \\ ' : blahblah",
  84. "'\\fo\\o:': blahblah",
  85. "\\'fo\\o\\:': foo ' :blahblah"
  86. };
  87. for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
  88. const char *p= strings[i];
  89. printf("|%s|", p);
  90. printf(" -> |%s|", av_get_token(&p, ":"));
  91. printf(" + |%s|\n", p);
  92. }
  93. return 0;
  94. }
  95. #endif