From 372e2884089e6a17be6d0028386ea2099dd86268 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Mon, 27 Sep 2010 16:23:43 +0000 Subject: Move av_get_token() from libavfilter to libavutil. Originally committed as revision 25225 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavutil/avstring.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'libavutil/avstring.c') diff --git a/libavutil/avstring.c b/libavutil/avstring.c index 4844e28db2..b573dd6761 100644 --- a/libavutil/avstring.c +++ b/libavutil/avstring.c @@ -97,3 +97,91 @@ char *av_d2str(double d) if(str) snprintf(str, 16, "%f", d); return str; } + +#define WHITESPACES " \n\t" + +char *av_get_token(const char **buf, const char *term) +{ + char *out = av_malloc(strlen(*buf) + 1); + char *ret= out, *end= out; + const char *p = *buf; + if (!out) return NULL; + p += strspn(p, WHITESPACES); + + while(*p && !strspn(p, term)) { + char c = *p++; + if(c == '\\' && *p){ + *out++ = *p++; + end= out; + }else if(c == '\''){ + while(*p && *p != '\'') + *out++ = *p++; + if(*p){ + p++; + end= out; + } + }else{ + *out++ = c; + } + } + + do{ + *out-- = 0; + }while(out >= end && strspn(out, WHITESPACES)); + + *buf = p; + + return ret; +} + +#ifdef TEST + +#undef printf + +int main(void) +{ + int i; + + printf("Testing av_get_token()\n"); + { + const char *strings[] = { + "''", + "", + ":", + "\\", + "'", + " '' :", + " '' '' :", + "foo '' :", + "'foo'", + "foo ", + " ' foo ' ", + "foo\\", + "foo': blah:blah", + "foo\\: blah:blah", + "foo\'", + "'foo : ' :blahblah", + "\\ :blah", + " foo", + " foo ", + " foo \\ ", + "foo ':blah", + " foo bar : blahblah", + "\\f\\o\\o", + "'foo : \\ \\ ' : blahblah", + "'\\fo\\o:': blahblah", + "\\'fo\\o\\:': foo ' :blahblah" + }; + + for (i=0; i < FF_ARRAY_ELEMS(strings); i++) { + const char *p= strings[i]; + printf("|%s|", p); + printf(" -> |%s|", av_get_token(&p, ":")); + printf(" + |%s|\n", p); + } + } + + return 0; +} + +#endif /* TEST */ -- cgit v1.2.3