Originally committed as revision 18725 to svn://svn.ffmpeg.org/ffmpeg/trunk
Unified · Split
| @@ -0,0 +1,110 @@ | ||
| 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 | +/** | |
| 21 | + * @file libavfilter/parseutils.c | |
| 22 | + * parsing utils | |
| 23 | + */ | |
| 24 | + | |
| 25 | +#include <string.h> | |
| 26 | +#include "libavutil/avutil.h" | |
| 27 | +#include "parseutils.h" | |
| 28 | + | |
| 29 | +#define WHITESPACES " \n\t" | |
| 30 | + | |
| 31 | +char *av_get_token(const char **buf, const char *term) | |
| 32 | +{ | |
| 33 | + char *out = av_malloc(strlen(*buf) + 1); | |
| 34 | + char *ret= out, *end= out; | |
| 35 | + const char *p = *buf; | |
| 36 | + p += strspn(p, WHITESPACES); | |
| 37 | + | |
| 38 | + while(*p && !strspn(p, term)) { | |
| 39 | + char c = *p++; | |
| 40 | + if(c == '\\' && *p){ | |
| 41 | + *out++ = *p++; | |
| 42 | + end= out; | |
| 43 | + }else if(c == '\''){ | |
| 44 | + while(*p && *p != '\'') | |
| 45 | + *out++ = *p++; | |
| 46 | + if(*p){ | |
| 47 | + p++; | |
| 48 | + end= out; | |
| 49 | + } | |
| 50 | + }else{ | |
| 51 | + *out++ = c; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + do{ | |
| 56 | + *out-- = 0; | |
| 57 | + }while(out >= end && strspn(out, WHITESPACES)); | |
| 58 | + | |
| 59 | + *buf = p; | |
| 60 | + | |
| 61 | + return ret; | |
| 62 | +} | |
| 63 | + | |
| 64 | +#ifdef TEST | |
| 65 | + | |
| 66 | +#undef printf | |
| 67 | + | |
| 68 | +int main() | |
| 69 | +{ | |
| 70 | + int i; | |
| 71 | + | |
| 72 | + const char *strings[] = { | |
| 73 | + "''", | |
| 74 | + "", | |
| 75 | + ":", | |
| 76 | + "\\", | |
| 77 | + "'", | |
| 78 | + " '' :", | |
| 79 | + " '' '' :", | |
| 80 | + "foo '' :", | |
| 81 | + "'foo'", | |
| 82 | + "foo ", | |
| 83 | + "foo\\", | |
| 84 | + "foo': blah:blah", | |
| 85 | + "foo\\: blah:blah", | |
| 86 | + "foo\'", | |
| 87 | + "'foo : ' :blahblah", | |
| 88 | + "\\ :blah", | |
| 89 | + " foo", | |
| 90 | + " foo ", | |
| 91 | + " foo \\ ", | |
| 92 | + "foo ':blah", | |
| 93 | + " foo bar : blahblah", | |
| 94 | + "\\f\\o\\o", | |
| 95 | + "'foo : \\ \\ ' : blahblah", | |
| 96 | + "'\\fo\\o:': blahblah", | |
| 97 | + "\\'fo\\o\\:': foo ' :blahblah" | |
| 98 | + }; | |
| 99 | + | |
| 100 | + for (i=0; i < FF_ARRAY_ELEMS(strings); i++) { | |
| 101 | + const char *p= strings[i]; | |
| 102 | + printf("|%s|", p); | |
| 103 | + printf(" -> |%s|", av_get_token(&p, ":")); | |
| 104 | + printf(" + |%s|\n", p); | |
| 105 | + } | |
| 106 | + | |
| 107 | + return 0; | |
| 108 | +} | |
| 109 | + | |
| 110 | +#endif | |
| @@ -0,0 +1,43 @@ | ||
| 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 | +/** | |
| 21 | + * @file libavfilter/parseutils.h | |
| 22 | + * parsing utils | |
| 23 | + */ | |
| 24 | + | |
| 25 | +#ifndef AVFILTER_PARSEUTILS_H | |
| 26 | +#define AVFILTER_PARSEUTILS_H | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * Unescapes the given string until a non escaped terminating char, | |
| 30 | + * and returns the token corresponding to the unescaped string. | |
| 31 | + * | |
| 32 | + * The normal \ and ' escaping is supported. Leading and trailing | |
| 33 | + * whitespaces are removed. | |
| 34 | + * | |
| 35 | + * @param term a 0-terminated list of terminating chars | |
| 36 | + * @param buf the buffer to parse, buf will be updated to point to the | |
| 37 | + * terminating char | |
| 38 | + * @return the malloced unescaped string, which must be av_freed by | |
| 39 | + * the user | |
| 40 | + */ | |
| 41 | +char *av_get_token(const char **buf, const char *term); | |
| 42 | + | |
| 43 | +#endif /* AVFILTER_PARSEUTILS_H */ | |