From: Martin Mares Date: Thu, 16 Feb 2012 19:52:15 +0000 (+0100) Subject: Time: Added a module for parsing timeouts X-Git-Tag: v5.0~34 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=89a18a431ad15d784bbfb249683fb93cd8b7cd13;p=libucw.git Time: Added a module for parsing timeouts --- diff --git a/ucw/Makefile b/ucw/Makefile index 1b2760ef..584ae85f 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -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 index 00000000..1126011a --- /dev/null +++ b/ucw/time-conf.c @@ -0,0 +1,71 @@ +/* + * UCW Library -- A Parser of Time Intervals + * + * (c) 2012 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include +#include +#include +#include + +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 + +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 diff --git a/ucw/time.h b/ucw/time.h index 0684bec1..f9b3f4da 100644 --- a/ucw/time.h +++ b/ucw/time.h @@ -24,6 +24,16 @@ 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 <>. 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 index 00000000..f4c4c33c --- /dev/null +++ b/ucw/time.t @@ -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