changeset 1027:7d79d41d152e libavutil

Move av_get_token() from libavfilter to libavutil.
author stefano
date Mon, 27 Sep 2010 16:23:43 +0000
parents 580d47a2f015
children 5dbb12a37c3d
files avstring.c avstring.h avutil.h
diffstat 3 files changed, 105 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/avstring.c	Mon Sep 27 10:34:03 2010 +0000
+++ b/avstring.c	Mon Sep 27 16:23:43 2010 +0000
@@ -97,3 +97,91 @@
     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 */
--- a/avstring.h	Mon Sep 27 10:34:03 2010 +0000
+++ b/avstring.h	Mon Sep 27 16:23:43 2010 +0000
@@ -114,4 +114,20 @@
  */
 char *av_d2str(double d);
 
+/**
+ * Unescape the given string until a non escaped terminating char,
+ * and return the token corresponding to the unescaped string.
+ *
+ * The normal \ and ' escaping is supported. Leading and trailing
+ * whitespaces are removed, unless they are escaped with '\' or are
+ * enclosed between ''.
+ *
+ * @param buf the buffer to parse, buf will be updated to point to the
+ * terminating char
+ * @param term a 0-terminated list of terminating chars
+ * @return the malloced unescaped string, which must be av_freed by
+ * the user, NULL in case of allocation failure
+ */
+char *av_get_token(const char **buf, const char *term);
+
 #endif /* AVUTIL_AVSTRING_H */
--- a/avutil.h	Mon Sep 27 10:34:03 2010 +0000
+++ b/avutil.h	Mon Sep 27 16:23:43 2010 +0000
@@ -40,7 +40,7 @@
 #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
 
 #define LIBAVUTIL_VERSION_MAJOR 50
-#define LIBAVUTIL_VERSION_MINOR 29
+#define LIBAVUTIL_VERSION_MINOR 30
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \