]> mj.ucw.cz Git - libucw.git/commitdiff
Time: Added a module for parsing timeouts
authorMartin Mares <mj@ucw.cz>
Thu, 16 Feb 2012 19:52:15 +0000 (20:52 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 16 Feb 2012 19:52:15 +0000 (20:52 +0100)
ucw/Makefile
ucw/time-conf.c [new file with mode: 0644]
ucw/time.h
ucw/time.t [new file with mode: 0644]

index 1b2760ef1a581a3289de86b7b592e16a5bd9ffd3..584ae85fc6cc5cc44bfc8af2fb979396f96d4a26 100644 (file)
@@ -20,7 +20,7 @@ LIBUCW_MODS= \
        char-cat char-upper char-lower unicode stkstring \
        wildmatch regex \
        prime primetable random \
-       time-stamp time-timer \
+       time-stamp time-timer time-conf \
        bit-ffs bit-fls \
        url \
        mainloop main-block main-rec \
@@ -114,7 +114,7 @@ TESTS+=$(addprefix $(o)/ucw/,regex.test unicode.test hash-test.test mempool.test
     fb-socket.test trie-test.test string.test sha1.test asort-test.test binheap-test.test \
     redblack-test.test fb-file.test fb-grow.test fb-pool.test fb-atomic.test \
     fb-limfd.test fb-temp.test fb-mem.test fb-buffer.test fb-mmap.test url.test strtonum-test.test \
-    gary.test)
+    gary.test time.test)
 
 $(o)/ucw/regex.test: $(o)/ucw/regex-t
 $(o)/ucw/unicode.test: $(o)/ucw/unicode-t
@@ -139,6 +139,7 @@ $(addprefix $(o)/ucw/fb-,file.test grow.test pool.test socket.test atomic.test \
        limfd.test temp.test mem.test buffer.test mmap.test): %.test: %-t
 $(o)/ucw/url.test: $(o)/ucw/url-t
 $(o)/ucw/gary.test: $(o)/ucw/gary-t
+$(o)/ucw/time.test: $(o)/ucw/time-conf-t
 
 ifdef CONFIG_UCW_THREADS
 TESTS+=$(addprefix $(o)/ucw/,asio.test)
diff --git a/ucw/time-conf.c b/ucw/time-conf.c
new file mode 100644 (file)
index 0000000..1126011
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *     UCW Library -- A Parser of Time Intervals
+ *
+ *     (c) 2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+#include <ucw/conf.h>
+#include <ucw/time.h>
+#include <ucw/fastbuf.h>
+
+static char *
+timestamp_parser(char *c, void *ptr)
+{
+  timestamp_t *ts = ptr;
+  double d;
+
+  char *err = cf_parse_double(c, &d);
+  if (err)
+    return err;
+  if (d > 1e16)
+    return "Time too large";
+
+  *ts = (timestamp_t)(d*1000 + 0.5);
+  if (!*ts && d)
+    return "Non-zero time rounds down to zero milliseconds";
+
+  return NULL;
+}
+
+static void
+timestamp_dumper(struct fastbuf *fb, void *ptr)
+{
+  timestamp_t *ts = ptr;
+  bprintf(fb, "%jd", (intmax_t) *ts);
+}
+
+struct cf_user_type timestamp_type = {
+  .size = sizeof(timestamp_t),
+  .name = "timestamp",
+  .parser = timestamp_parser,
+  .dumper = timestamp_dumper
+};
+
+#ifdef TEST
+
+#include <stdio.h>
+
+int main(void)
+{
+  char line[256];
+
+  while (fgets(line, sizeof(line), stdin))
+    {
+      char *nl = strchr(line, '\n');
+      if (nl)
+       *nl = 0;
+      timestamp_t t = -1;
+      char *err = timestamp_parser(line, &t);
+      if (err)
+       puts(err);
+      else
+       printf("%jd\n", (intmax_t) t);
+    }
+  return 0;
+}
+
+#endif
index 0684bec16bf187a7e3b65b2f61488955fdc0806c..f9b3f4daac3e5e9578c95a6124e50f4042e3c38b 100644 (file)
 
 timestamp_t get_timestamp(void);               /** Get current time as a millisecond timestamp. **/
 
+/* time-conf.c */
+
+/**
+ * A user type for parsing of time intervals in configuration files.
+ * It is specified as fractional seconds and internally converted to
+ * a <<basics:type_timestamp_t,`timestamp_t`>>. When conversion of
+ * a non-zero value yields zero, an error is raised.
+ **/
+extern struct cf_user_type timestamp_type;
+
 /***
  * === Timers
  *
diff --git a/ucw/time.t b/ucw/time.t
new file mode 100644 (file)
index 0000000..f4c4c33
--- /dev/null
@@ -0,0 +1,24 @@
+# Tests for time-conf
+
+Name:  Parser normal
+Run:   ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In:    123
+       0.23
+       1e2
+       3k
+       1d
+Out:   123000
+       230
+       100000
+       3000000
+       86400000
+
+Name:  Parser underflow
+Run:   ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In:    0.0001
+Out:   Non-zero time rounds down to zero milliseconds
+
+Name:  Parser overflow
+Run:   ../obj/ucw/time-conf-t 'ftp://example.com/other'
+In:    1e30
+Out:   Time too large