diff options
author | Stefano Sabatini <stefano.sabatini-lala@poste.it> | 2010-09-27 16:23:43 +0000 |
---|---|---|
committer | Stefano Sabatini <stefano.sabatini-lala@poste.it> | 2010-09-27 16:23:43 +0000 |
commit | 372e2884089e6a17be6d0028386ea2099dd86268 (patch) | |
tree | d65753e450b0613788dde3e5f23a116a157cd1c7 /libavutil/avstring.c | |
parent | 4a94cfea02f4c3da43d2f2ed25f14ee461a1a315 (diff) | |
download | ffmpeg-372e2884089e6a17be6d0028386ea2099dd86268.tar.gz |
Move av_get_token() from libavfilter to libavutil.
Originally committed as revision 25225 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavutil/avstring.c')
-rw-r--r-- | libavutil/avstring.c | 88 |
1 files changed, 88 insertions, 0 deletions
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 */ |