2 * librouteros - src/ros_parse.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
42 #include "routeros_api.h"
44 _Bool sstrtob (const char *str) /* {{{ */
49 if (strcasecmp ("true", str) == 0)
52 } /* }}} _Bool sstrtob */
54 unsigned int sstrtoui (const char *str) /* {{{ */
64 ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10);
65 if ((endptr == str) || (errno != 0))
69 } /* }}} unsigned int sstrtoui */
71 uint64_t sstrtoui64 (const char *str) /* {{{ */
81 ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10);
82 if ((endptr == str) || (errno != 0))
86 } /* }}} uint64_t sstrtoui64 */
88 double sstrtod (const char *str) /* {{{ */
98 ret = strtod (str, &endptr);
99 if ((endptr == str) || (errno != 0))
103 } /* }}} double sstrtod */
105 int sstrto_rx_tx_counters (const char *str, /* {{{ */
106 uint64_t *rx, uint64_t *tx)
111 if ((rx == NULL) || (tx == NULL))
123 *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
124 if ((endptr == str) || (errno != 0))
130 assert (endptr != NULL);
131 if ((*endptr != '/') && (*endptr != ','))
137 *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
138 if ((endptr == str) || (errno != 0))
146 } /* }}} int sstrto_rx_tx_counters */
148 /* have_hour is initially set to false and later set to true when the first
149 * colon is found. It is used to determine whether the number before the colon
150 * is hours or minutes. External code should use the sstrtodate() macro. */
151 uint64_t _sstrtodate (const char *str, _Bool have_hour) /* {{{ */
156 if ((str == NULL) || (*str == 0))
159 /* Example string: 6w6d18:33:07 */
162 ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10);
163 if ((endptr == str) || (errno != 0))
168 case 'y': ret *= 365 * 86400; break;
169 case 'w': ret *= 7 * 86400; break;
170 case 'd': ret *= 86400; break;
171 case ':': ret *= have_hour ? 60 : 3600; have_hour = true; break;
174 return (ret + _sstrtodate (endptr + 1, have_hour));
175 } /* }}} uint64_t _sstrtodate */
177 /* vim: set ts=2 sw=2 noet fdm=marker : */