[FFmpeg-devel] [PATCH] cmdutils: add parse_flags() function

Stefano Sabatini stefasab at gmail.com
Thu Nov 14 11:58:10 CET 2013


The function is required by the pending ffprobe validation patch.
---
 cmdutils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cmdutils.h | 20 ++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/cmdutils.c b/cmdutils.c
index b10a552..9b344a3 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -1982,3 +1982,57 @@ void *grow_array(void *array, int elem_size, int *size, int new_size)
     }
     return array;
 }
+
+int parse_flags(unsigned int *flags, const char *buf,
+                const struct flag_descriptor *flag_descs)
+{
+    char flag_name[128];
+    const struct flag_descriptor *flag_desc;
+    *flags = 0;
+
+    while (*buf) {
+        char op = *buf == '+' || *buf == '-' ? *buf++ : '+';
+        int len = strcspn(buf, "+-");
+        int flag_value;
+
+        av_strlcpy(flag_name, buf, len+1);
+        buf += len;
+
+        for (flag_desc = flag_descs; flag_desc->name; flag_desc++) {
+            if (!strcmp(flag_desc->name, flag_name)) {
+                flag_value = flag_desc->value;
+                break;
+            }
+        }
+        if (!flag_desc->name) {
+            if      (!strcmp(flag_name, "none"))
+                flag_value = 0;
+            else if (!strcmp(flag_name, "all"))
+                flag_value = ~0;
+            else {
+                fprintf(stderr, "Unknown flag: '%s'\n", flag_name);
+                return AVERROR(EINVAL);
+            }
+        }
+
+        if (op == '+')
+            *flags = *flags |  flag_value;
+        else
+            *flags = *flags & ~flag_value;
+    }
+
+    return 0;
+}
+
+char *get_flags_string(char *buf, size_t buf_size, int flags,
+                       const struct flag_descriptor *flag_descs)
+{
+    const struct flag_descriptor *flag_desc;
+    *buf = 0;
+
+    for (flag_desc = flag_descs; flag_desc->name; flag_desc++)
+        if ((flags & flag_desc->value) ==  flag_desc->value)
+            av_strlcatf(buf, buf_size, "+%s", flag_desc->name);
+
+    return buf;
+}
diff --git a/cmdutils.h b/cmdutils.h
index 2d2afc8..0138bc8 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -568,4 +568,24 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
     char name[128];\
     av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
 
+struct flag_descriptor {
+    unsigned int value;
+    const char *name;
+};
+
+/**
+ * Parse flags in buf, and store in *flags the value of the string in
+ * buf representing a collection of flags.
+ *
+ * @return 0 in case of successfull parsing, a negative value in case of error
+ */
+int parse_flags(unsigned int *flags, const char *buf,
+                const struct flag_descriptor *flag_descs);
+
+/**
+ * Return a string representing the flags stored in flags.
+ */
+char *get_flags_string(char *buf, size_t buf_size, int flags,
+                       const struct flag_descriptor *flag_descs);
+
 #endif /* CMDUTILS_H */
-- 
1.8.1.2



More information about the ffmpeg-devel mailing list