[FFmpeg-devel] [PATCH 3/4] Implement parse_number() and ffserver.c:parse_ffconfig_integer().

Stefano Sabatini stefano.sabatini-lala
Sun Apr 11 20:32:26 CEST 2010


parse_number() is used in the new defined function
ffserver.c:parse_ffconfig_integer(), which simplifies and makes more
robust integer parsing in parse_ffconfig().

---
 cmdutils.c |   14 +++++++++++-
 cmdutils.h |   14 ++++++++++--
 ffserver.c |   65 +++++++++++++++++++++++++++--------------------------------
 3 files changed, 53 insertions(+), 40 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index 7e652a1..83f21a0 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -54,7 +54,7 @@ struct SwsContext *sws_opts;
 
 const int this_year = 2010;
 
-double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
+double parse_number(const char *context, const char *numstr, int type, double min, double max)
 {
     char *tail;
     const char *error;
@@ -67,8 +67,18 @@ double parse_number_or_die(const char *context, const char *numstr, int type, do
         error= "Expected int64 for %s but found %s\n";
     else
         return d;
+
     fprintf(stderr, error, context, numstr, min, max);
-    exit(1);
+    return NAN;
+}
+
+double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
+{
+    double d = parse_number(context, numstr, type, min, max);
+    if (isnan(d))
+        exit(1);
+    else
+        return d;
 }
 
 int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
diff --git a/cmdutils.h b/cmdutils.h
index ad8cefd..7b46fb1 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -61,9 +61,8 @@ int opt_loglevel(const char *opt, const char *arg);
 int opt_timelimit(const char *opt, const char *arg);
 
 /**
- * Parses a string and returns its corresponding value as a double.
- * Exits from the application if the string cannot be correctly
- * parsed or the corresponding value is invalid.
+ * Parses a string and returns its corresponding value as a double,
+ * NAN if the number cannot be parsed or is invalid.
  *
  * @param context the context of the value to be set (e.g. the
  * corresponding commandline option name)
@@ -73,6 +72,15 @@ int opt_timelimit(const char *opt, const char *arg);
  * @param min the minimum valid accepted value
  * @param max the maximum valid accepted value
  */
+double parse_number(const char *context, const char *numstr, int type, double min, double max);
+
+/**
+ * Parses a string and returns its corresponding value as a double.
+ * Exits from the application if the string cannot be correctly
+ * parsed or the corresponding value is invalid.
+ *
+ * @see parse_number()
+ */
 double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max);
 
 /**
diff --git a/ffserver.c b/ffserver.c
index 6bdd9cf..4404511 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -3961,6 +3961,22 @@ static void report_config_error(const char *filename, int line_num, int *errors,
     (*errors)++;
 }
 
+static int parse_ffconfig_integer(int64_t *res, const char *opt, const char *arg,
+                                  int64_t min, int64_t max,
+                                  const char *filename, int line_num, int *errors)
+{
+    double d;
+
+    d = parse_number(opt, arg, OPT_INT64, min, max);
+    if (isnan(d)) {
+        report_config_error(filename, line_num, errors, "Invalid value for parameter '%s': %s\n", opt, arg);
+        return AVERROR(EINVAL);
+    } else {
+        *res = (int64_t)d;
+        return 0;
+    }
+}
+
 static int parse_ffconfig(const char *filename)
 {
     FILE *f;
@@ -3968,7 +3984,8 @@ static int parse_ffconfig(const char *filename)
     char cmd[64];
     char arg[1024];
     const char *p;
-    int val, errors, line_num;
+    int64_t val;
+    int errors, line_num;
     FFStream **last_stream, *stream, *redirect;
     FFStream **last_feed, *feed, *s;
     AVCodecContext audio_enc, video_enc;
@@ -4008,10 +4025,7 @@ static int parse_ffconfig(const char *filename)
 
         if (!strcasecmp(cmd, "Port")) {
             get_arg(arg, sizeof(arg), &p);
-            val = atoi(arg);
-            if (val < 1 || val > 65536) {
-                VERROR("Invalid_port: %s", arg);
-            }
+            if (parse_ffconfig_integer(&val, cmd, arg, 1, 65536, filename, line_num, &errors) >= 0)
             my_http_addr.sin_port = htons(val);
         } else if (!strcasecmp(cmd, "BindAddress")) {
             get_arg(arg, sizeof(arg), &p);
@@ -4022,10 +4036,7 @@ static int parse_ffconfig(const char *filename)
             ffserver_daemon = 0;
         } else if (!strcasecmp(cmd, "RTSPPort")) {
             get_arg(arg, sizeof(arg), &p);
-            val = atoi(arg);
-            if (val < 1 || val > 65536) {
-                VERROR("%s:%d: Invalid port: %s\n", arg);
-            }
+            if (parse_ffconfig_integer(&val, cmd, arg, 1, 65536, filename, line_num, &errors) >= 0)
             my_rtsp_addr.sin_port = htons(atoi(arg));
         } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
             get_arg(arg, sizeof(arg), &p);
@@ -4034,27 +4045,17 @@ static int parse_ffconfig(const char *filename)
             }
         } else if (!strcasecmp(cmd, "MaxHTTPConnections")) {
             get_arg(arg, sizeof(arg), &p);
-            val = atoi(arg);
-            if (val < 1 || val > 65536) {
-                VERROR("Invalid MaxHTTPConnections: %s", arg);
-            }
+            if (parse_ffconfig_integer(&val, cmd, arg, 1, 65536, filename, line_num, &errors) >= 0)
             nb_max_http_connections = val;
         } else if (!strcasecmp(cmd, "MaxClients")) {
             get_arg(arg, sizeof(arg), &p);
             val = atoi(arg);
-            if (val < 1 || val > nb_max_http_connections) {
-                VERROR("Invalid MaxClients: %s\n", arg);
-            } else {
+            if (parse_ffconfig_integer(&val, cmd, arg, 1, nb_max_http_connections, filename, line_num, &errors) >= 0)
                 nb_max_connections = val;
-            }
         } else if (!strcasecmp(cmd, "MaxBandwidth")) {
-            int64_t llval;
             get_arg(arg, sizeof(arg), &p);
-            llval = atoll(arg);
-            if (llval < 10 || llval > 10000000) {
-                VERROR("Invalid MaxBandwidth: %s\n", arg);
-            } else
-                max_bandwidth = llval;
+            if (parse_ffconfig_integer(&val, cmd, arg, 10, 10000000, filename, line_num, &errors) >= 0)
+                max_bandwidth = val;
         } else if (!strcasecmp(cmd, "CustomLog")) {
             if (!ffserver_debug)
                 get_arg(logfilename, sizeof(logfilename), &p);
@@ -4408,26 +4409,20 @@ static int parse_ffconfig(const char *filename)
         } else if (!strcasecmp(cmd, "VideoQDiff")) {
             get_arg(arg, sizeof(arg), &p);
             if (stream) {
-                video_enc.max_qdiff = atoi(arg);
-                if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
-                    ERROR("VideoQDiff out of range\n");
-                }
+                if (parse_ffconfig_integer(&val, cmd, arg, 1, 31, filename, line_num, &errors) >= 0)
+                    video_enc.max_qdiff = val;
             }
         } else if (!strcasecmp(cmd, "VideoQMax")) {
             get_arg(arg, sizeof(arg), &p);
             if (stream) {
-                video_enc.qmax = atoi(arg);
-                if (video_enc.qmax < 1 || video_enc.qmax > 31) {
-                    ERROR("VideoQMax out of range\n");
-                }
+                if (parse_ffconfig_integer(&val, cmd, arg, 1, 31, filename, line_num, &errors) >= 0)
+                    video_enc.qmax = val;
             }
         } else if (!strcasecmp(cmd, "VideoQMin")) {
             get_arg(arg, sizeof(arg), &p);
             if (stream) {
-                video_enc.qmin = atoi(arg);
-                if (video_enc.qmin < 1 || video_enc.qmin > 31) {
-                    ERROR("VideoQMin out of range\n");
-                }
+                if (parse_ffconfig_integer(&val, cmd, arg, 1, 31, filename, line_num, &errors) >= 0)
+                    video_enc.qmin = val;
             }
         } else if (!strcasecmp(cmd, "LumaElim")) {
             get_arg(arg, sizeof(arg), &p);
-- 
1.7.0




More information about the ffmpeg-devel mailing list