2 * librouteros - src/ros_parse.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 * Florian octo Forster <octo at verplant.org>
21 #ifndef _ISOC99_SOURCE
22 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
41 #include "routeros_api.h"
43 _Bool sstrtob (const char *str) /* {{{ */
48 if (strcasecmp ("true", str) == 0)
51 } /* }}} _Bool sstrtob */
53 unsigned int sstrtoui (const char *str) /* {{{ */
63 ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10);
64 if ((endptr == str) || (errno != 0))
68 } /* }}} unsigned int sstrtoui */
70 uint64_t sstrtoui64 (const char *str) /* {{{ */
80 ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10);
81 if ((endptr == str) || (errno != 0))
85 } /* }}} uint64_t sstrtoui64 */
87 double sstrtod (const char *str) /* {{{ */
97 ret = strtod (str, &endptr);
98 if ((endptr == str) || (errno != 0))
102 } /* }}} double sstrtod */
104 int sstrto_rx_tx_counters (const char *str, /* {{{ */
105 uint64_t *rx, uint64_t *tx)
110 if ((rx == NULL) || (tx == NULL))
122 *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
123 if ((endptr == str) || (errno != 0))
129 assert (endptr != NULL);
130 if ((*endptr != '/') && (*endptr != ','))
136 *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
137 if ((endptr == str) || (errno != 0))
145 } /* }}} int sstrto_rx_tx_counters */
147 /* have_hour is initially set to false and later set to true when the first
148 * colon is found. It is used to determine whether the number before the colon
149 * is hours or minutes. External code should use the sstrtodate() macro. */
150 uint64_t _sstrtodate (const char *str, _Bool have_hour) /* {{{ */
155 if ((str == NULL) || (*str == 0))
158 /* Example string: 6w6d18:33:07 */
161 ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10);
162 if ((endptr == str) || (errno != 0))
167 case 'y': ret *= 365 * 86400; break;
168 case 'w': ret *= 7 * 86400; break;
169 case 'd': ret *= 86400; break;
170 case ':': ret *= have_hour ? 60 : 3600; have_hour = true; break;
173 return (ret + _sstrtodate (endptr + 1, have_hour));
174 } /* }}} uint64_t _sstrtodate */
176 /* vim: set ts=2 sw=2 noet fdm=marker : */