[FFmpeg-devel] [PATCH 05/11] Implement set_option() function.

Stefano Sabatini stefano.sabatini-lala
Fri Nov 12 19:31:43 CET 2010


---
 cmdutils.c |   71 +++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index c9c4ab8..575e32b 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -141,25 +141,12 @@ static const OptionDef* find_option(const OptionDef *po, const char *name){
     return po;
 }
 
-void parse_options(int argc, char **argv, const OptionDef *options,
-                   void (* parse_arg_function)(const char*))
+static int set_option(const char *ctx, const OptionDef **optdef,
+                      const char *opt, const char *arg, const OptionDef *options)
 {
-    const char *opt, *arg;
-    int optindex, handleoptions=1;
     const OptionDef *po;
+    int ret, bool_val = 1;
 
-    /* parse options */
-    optindex = 1;
-    while (optindex < argc) {
-        opt = argv[optindex++];
-
-        if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
-            int bool_val = 1;
-            if (opt[1] == '-' && opt[2] == '\0') {
-                handleoptions = 0;
-                continue;
-            }
-            opt++;
             po= find_option(options, opt);
             if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
                 /* handle 'no' bool option */
@@ -170,18 +157,16 @@ void parse_options(int argc, char **argv, const OptionDef *options,
             }
             if (!po->name)
                 po= find_option(options, "default");
+            *optdef = po;
+            if (po->flags & HAS_ARG && !arg) {
+                    fprintf(stderr, "%s: missing argument for option '%s'\n", ctx, opt);
+                    return AVERROR(EINVAL);
+                }
+
             if (!po->name) {
 unknown_opt:
-                fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
-                exit(1);
-            }
-            arg = NULL;
-            if (po->flags & HAS_ARG) {
-                arg = argv[optindex++];
-                if (!arg) {
-                    fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
-                    exit(1);
-                }
+                fprintf(stderr, "%s: unrecognized option '%s'\n", ctx, opt);
+                return AVERROR(EINVAL);
             }
             if (po->flags & OPT_STRING) {
                 char *str;
@@ -196,15 +181,43 @@ unknown_opt:
             } else if (po->flags & OPT_FLOAT) {
                 *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -1.0/0.0, 1.0/0.0);
             } else if (po->flags & OPT_FUNC2) {
-                if (po->u.func2_arg(opt, arg) < 0) {
-                    fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
-                    exit(1);
+                if ((ret = po->u.func2_arg(opt, arg)) < 0) {
+                    fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", ctx, arg, opt);
+                    return ret;
                 }
             } else {
                 po->u.func_arg(arg);
             }
             if(po->flags & OPT_EXIT)
                 exit(0);
+
+            return 0;
+}
+
+void parse_options(int argc, char **argv, const OptionDef *options,
+                   void (* parse_arg_function)(const char*))
+{
+    const char *opt, *arg;
+    int optindex, handleoptions=1;
+    const OptionDef *po;
+
+    /* parse options */
+    optindex = 1;
+    while (optindex < argc) {
+        opt = argv[optindex++];
+
+        if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
+            if (opt[1] == '-' && opt[2] == '\0') {
+                handleoptions = 0;
+                continue;
+            }
+            opt++;
+
+            arg = argv[optindex];
+            if (set_option(argv[0], &po, opt, arg, options) < 0)
+                exit(1);
+            if (po->flags & HAS_ARG)
+                optindex++;
         } else {
             if (parse_arg_function)
                 parse_arg_function(opt);
-- 
1.7.1




More information about the ffmpeg-devel mailing list