dvd-sector-copy.c, lib.[ch]: Improve and publish the number parsing functions.
[dvdrip] / lib.c
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;