Browse code

av_get_token() based on a patch by Stefano Sabatini

Originally committed as revision 18725 to svn://svn.ffmpeg.org/ffmpeg/trunk

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