src/ros_parse.[ch]: Move parsing functions into a separate module.
[routeros-api.git] / src / ros_parse.c
1 /**
2  * librouteros - src/ros_parse.c
3  * Copyright (C) 2009  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <inttypes.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <math.h>
39 #include <errno.h>
40 #include <assert.h>
41
42 #include "routeros_api.h"
43
44 _Bool sstrtob (const char *str) /* {{{ */
45 {
46         if (str == NULL)
47                 return (false);
48
49         if (strcasecmp ("true", str) == 0)
50                 return (true);
51         return (false);
52 } /* }}} _Bool sstrtob */
53
54 unsigned int sstrtoui (const char *str) /* {{{ */
55 {
56         unsigned int ret;
57         char *endptr;
58
59         if (str == NULL)
60                 return (0);
61
62         errno = 0;
63         endptr = NULL;
64         ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10);
65         if ((endptr == str) || (errno != 0))
66                 return (0);
67
68         return (ret);
69 } /* }}} unsigned int sstrtoui */
70
71 double sstrtod (const char *str) /* {{{ */
72 {
73         double ret;
74         char *endptr;
75
76         if (str == NULL)
77                 return (NAN);
78
79         errno = 0;
80         endptr = NULL;
81         ret = strtod (str, &endptr);
82         if ((endptr == str) || (errno != 0))
83                 return (NAN);
84
85         return (ret);
86 } /* }}} double sstrtod */
87
88 int sstrto_rx_tx_counters (const char *str, /* {{{ */
89                 uint64_t *rx, uint64_t *tx)
90 {
91         const char *ptr;
92         char *endptr;
93
94         if ((rx == NULL) || (tx == NULL))
95                 return (EINVAL);
96
97         *rx = 0;
98         *tx = 0;
99
100         if (str == NULL)
101                 return (EINVAL);
102
103         ptr = str;
104         errno = 0;
105         endptr = NULL;
106         *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
107         if ((endptr == str) || (errno != 0))
108         {
109                 *rx = 0;
110                 return (EIO);
111         }
112
113         assert (endptr != NULL);
114         if ((*endptr != '/') && (*endptr != ','))
115                 return (EIO);
116
117         ptr = endptr + 1;
118         errno = 0;
119         endptr = NULL;
120         *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
121         if ((endptr == str) || (errno != 0))
122         {
123                 *rx = 0;
124                 *tx = 0;
125                 return (EIO);
126         }
127
128         return (0);
129 } /* }}} int sstrto_rx_tx_counters */
130
131 /* vim: set ts=2 sw=2 noet fdm=marker : */