dvd-sector-copy.c, lib.[ch]: Improve and publish the number parsing functions.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 19 Mar 2022 14:08:07 +0000 (14:08 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 19 Mar 2022 14:08:07 +0000 (14:08 +0000)
dvd-sector-copy.c
lib.c
lib.h

index f0800c8..4c19389 100644 (file)
@@ -861,38 +861,6 @@ static int read_line(FILE *fp, struct buf *b)
   return (0);
 }
 
-static double parse_float(const char **p_inout, double min, double max,
-                         const char *what)
-{
-  const char *p;
-  char *q;
-  double x;
-  int err;
-
-  err = errno; errno = 0;
-  p = *p_inout;
-  x = strtod(p, &q);
-  if (errno || x < min || x > max) bail("bad %s `%s'", what, p);
-  *p_inout = q; errno = err;
-  return (x);
-}
-
-static long parse_int(const char **p_inout, long min, long max,
-                     const char *what)
-{
-  const char *p;
-  char *q;
-  long x;
-  int err;
-
-  err = errno; errno = 0;
-  p = *p_inout;
-  x = strtoul(p, &q, 0);
-  if (errno || x < min || x > max) bail("bad %s `%s'", what, p);
-  *p_inout = q; errno = err;
-  return (x);
-}
-
 #define PRF_HYPHEN 1u
 static int parse_range(const char *p, unsigned f,
                       secaddr *start_out, secaddr *end_out)
@@ -1012,25 +980,33 @@ int main(int argc, char *argv[])
        (STRNCMP(p, ==, s "=", sizeof(s)) && (p += sizeof(s), 1))
        for (;;) {
          if (SKIP_PREFIX("cf"))
-           clear_factor = parse_float(&p, 0, DBL_MAX, "clear factor");
+           clear_factor = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
+                                      "clear factor");
          else if (SKIP_PREFIX("cmin"))
-           clear_min = parse_int(&p, 1, SECLIMIT, "clear minimum");
+           clear_min = parse_int(&p, PNF_JUNK, 1, SECLIMIT,
+                                 "clear minimum");
          else if (SKIP_PREFIX("cmax"))
-           clear_max = parse_int(&p, 1, SECLIMIT, "clear maximum");
+           clear_max = parse_int(&p, PNF_JUNK, 1, SECLIMIT,
+                                 "clear maximum");
          else if (SKIP_PREFIX("sf"))
-           step_factor = parse_float(&p, 0, DBL_MAX, "step factor");
+           step_factor = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
+                                     "step factor");
          else if (SKIP_PREFIX("smin"))
-           step_min = parse_int(&p, 1, SECLIMIT - 1, "step minimum");
+           step_min = parse_int(&p, PNF_JUNK, 1, SECLIMIT - 1,
+                                "step minimum");
          else if (SKIP_PREFIX("smax"))
-           step_max = parse_int(&p, 1, SECLIMIT - 1, "step maximum");
+           step_max = parse_int(&p, PNF_JUNK, 1, SECLIMIT - 1,
+                                "step maximum");
          else if (SKIP_PREFIX("retry"))
-           max_retries = parse_int(&p, 0, INT_MAX, "retries");
+           max_retries = parse_int(&p, PNF_JUNK, 0, INT_MAX, "retries");
          else if (SKIP_PREFIX("alpha"))
-           alpha = parse_float(&p, 0, 1, "average decay factor");
+           alpha = parse_float(&p, PNF_JUNK, 0, 1, "average decay factor");
          else if (SKIP_PREFIX("_badwait"))
-           bad_block_delay = parse_float(&p, 0, DBL_MAX, "bad-block delay");
+           bad_block_delay = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
+                                         "bad-block delay");
          else if (SKIP_PREFIX("_blkwait"))
-           good_block_delay = parse_float(&p, 0, DBL_MAX, "good block delay");
+           good_block_delay = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
+                                          "good block delay");
          else
            bail("unknown bad blocks parameter `%s'", p);
          if (!*p) break;
diff --git a/lib.c b/lib.c
index 521af8d..4f2950c 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -33,6 +33,40 @@ void bail_syserr(int err, const char *fmt, ...)
   exit(2);
 }
 
+double parse_float(const char **p_inout, unsigned f,
+                  double min, double max, const char *what)
+{
+  const char *p;
+  char *q;
+  double x;
+  int err;
+
+  err = errno; errno = 0;
+  p = *p_inout;
+  x = strtod(p, &q);
+  if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q))
+    bail("bad %s `%s'", what, p);
+  *p_inout = q; errno = err;
+  return (x);
+}
+
+long parse_int(const char **p_inout, unsigned f,
+              long min, long max, const char *what)
+{
+  const char *p;
+  char *q;
+  long x;
+  int err;
+
+  err = errno; errno = 0;
+  p = *p_inout;
+  x = strtoul(p, &q, 0);
+  if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q))
+    bail("bad %s `%s'", what, p);
+  *p_inout = q; errno = err;
+  return (x);
+}
+
 void sit(double t)
 {
   struct timeval tv;
diff --git a/lib.h b/lib.h
index 8a63e58..ceba762 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -73,6 +73,12 @@ extern PRINTF_LIKE(1, 2) NORETURN void bail(const char *fmt, ...);
 extern PRINTF_LIKE(2, 3) NORETURN
   void bail_syserr(int err, const char *fmt, ...);
 
+#define PNF_JUNK 1u
+extern double parse_float(const char **p_inout, unsigned f,
+                         double min, double max, const char *what);
+extern long parse_int(const char **p_inout, unsigned f,
+                     long min, long max, const char *what);
+
 extern void sit(double t);
 
 extern void carefully_write(int fd, const void *buf, size_t sz);