[FFmpeg-devel] [PATCH] lavu/opt: factorize internal function parse_key_value_pair()

Stefano Sabatini stefasab at gmail.com
Sun Apr 29 21:42:13 CEST 2012


Create an internal ff_parse_key_value_pair() function, which will be
useful for sharing common code with a new routine.
---
 libavutil/internal.h |   16 ++++++++++++++++
 libavutil/opt.c      |   35 +++++++++++++++++++++++------------
 2 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/libavutil/internal.h b/libavutil/internal.h
index 5d37da7..85240dc 100644
--- a/libavutil/internal.h
+++ b/libavutil/internal.h
@@ -246,4 +246,20 @@ static av_always_inline void emms_c(void)
 #define emms_c()
 #endif /* HAVE_MMX */
 
+/**
+ * Parse the buffer in *buf and get a key/value pair.
+ *
+ * @param buf the string to parse, buf will be updated to point at the
+ * separator just after the parsed key/value pair
+ * @param key_val_sep a 0-terminated list of characters used to
+ * separate key from value
+ * @param pairs_sep a 0-terminated list of characters used to separate
+ * two pairs from each other
+ * @return 0 if the key/value pair has been successfully parsed and
+ * set, or a negative value corresponding to an AVERROR code in case
+ * of error
+ */
+int ff_parse_key_value_pair(char **key, char **val, const char **buf,
+                            const char *key_val_sep, const char *pairs_sep);
+
 #endif /* AVUTIL_INTERNAL_H */
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 2f8be3b..60ae1dc 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -651,6 +651,20 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
     }
 }
 
+int ff_parse_key_value_pair(char **key, char **val, const char **buf,
+                            const char *key_val_sep, const char *pairs_sep)
+{
+    *key = av_get_token(buf, key_val_sep);
+
+    if (**key && strspn(*buf, key_val_sep)) {
+        (*buf)++;
+        *val = av_get_token(buf, pairs_sep);
+        return 0;
+    } else {
+        return AVERROR(EINVAL);
+    }
+}
+
 /**
  * Store the value in the field in ctx that is named like key.
  * ctx must be an AVClass context, storing is done using AVOptions.
@@ -668,20 +682,16 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
  * the error code issued by av_opt_set() if the key/value pair
  * cannot be set
  */
-static int parse_key_value_pair(void *ctx, const char **buf,
-                                const char *key_val_sep, const char *pairs_sep)
+static int set_key_value_pair(void *ctx, const char **buf,
+                              const char *key_val_sep, const char *pairs_sep)
 {
-    char *key = av_get_token(buf, key_val_sep);
-    char *val;
+    char *key = NULL, *val = NULL;
     int ret;
 
-    if (*key && strspn(*buf, key_val_sep)) {
-        (*buf)++;
-        val = av_get_token(buf, pairs_sep);
-    } else {
-        av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
-        av_free(key);
-        return AVERROR(EINVAL);
+    ret = ff_parse_key_value_pair(&key, &val, buf, key_val_sep, pairs_sep);
+    if (ret < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key in '%s'\n", key);
+        goto end;
     }
 
     av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
@@ -690,6 +700,7 @@ static int parse_key_value_pair(void *ctx, const char **buf,
     if (ret == AVERROR_OPTION_NOT_FOUND)
         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
 
+end:
     av_free(key);
     av_free(val);
     return ret;
@@ -704,7 +715,7 @@ int av_set_options_string(void *ctx, const char *opts,
         return 0;
 
     while (*opts) {
-        if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
+        if ((ret = set_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
             return ret;
         count++;
 
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list