From: Florian Forster Date: Wed, 23 Dec 2009 16:37:59 +0000 (+0100) Subject: src/ros_parse.[ch]: Added functions to parse the uptime string into seconds. X-Git-Tag: librouteros-1.1.0~7 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=1ac209036249e18ebb9cb42d3afeebdd14d5eed1;p=routeros-api.git src/ros_parse.[ch]: Added functions to parse the uptime string into seconds. --- diff --git a/src/ros_parse.c b/src/ros_parse.c index b9d7e99..95126ef 100644 --- a/src/ros_parse.c +++ b/src/ros_parse.c @@ -145,4 +145,33 @@ int sstrto_rx_tx_counters (const char *str, /* {{{ */ return (0); } /* }}} int sstrto_rx_tx_counters */ +/* have_hour is initially set to false and later set to true when the first + * colon is found. It is used to determine whether the number before the colon + * is hours or minutes. External code should use the sstrtodate() macro. */ +uint64_t _sstrtodate (const char *str, _Bool have_hour) /* {{{ */ +{ + uint64_t ret; + char *endptr; + + if ((str == NULL) || (*str == 0)) + return (0); + + /* Example string: 6w6d18:33:07 */ + errno = 0; + endptr = NULL; + ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10); + if ((endptr == str) || (errno != 0)) + return (0); + + switch (*endptr) + { + case 'y': ret *= 365 * 86400; break; + case 'w': ret *= 7 * 86400; break; + case 'd': ret *= 86400; break; + case ':': ret *= have_hour ? 60 : 3600; have_hour = true; break; + } + + return (ret + _sstrtodate (endptr + 1, have_hour)); +} /* }}} uint64_t _sstrtodate */ + /* vim: set ts=2 sw=2 noet fdm=marker : */ diff --git a/src/ros_parse.h b/src/ros_parse.h index a39ac89..75473a4 100644 --- a/src/ros_parse.h +++ b/src/ros_parse.h @@ -31,6 +31,9 @@ double sstrtod (const char *str); int sstrto_rx_tx_counters (const char *str, uint64_t *rx, uint64_t *tx); +uint64_t _sstrtodate (const char *str, _Bool have_hour); +#define sstrtodate(str) _sstrtodate((str), 0) + #endif /* ROS_PARSE_H */ /* vim: set ts=2 sw=2 noet fdm=marker : */