From 0444981b9b06a05776b2081889c0f7916aa718f6 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 27 Nov 2009 09:41:00 +0100 Subject: [PATCH] src/ros_parse.[ch]: Move parsing functions into a separate module. --- src/Makefile.am | 5 +- src/interface.c | 82 ++++------------------------- src/registration_table.c | 70 +++++-------------------- src/ros_parse.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++ src/ros_parse.h | 35 +++++++++++++ 5 files changed, 192 insertions(+), 131 deletions(-) create mode 100644 src/ros_parse.c create mode 100644 src/ros_parse.h diff --git a/src/Makefile.am b/src/Makefile.am index 89d6c07..4d56eb5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,10 @@ librouteros_la_LIBADD = -lgcrypt if BUILD_WITH_LIBSOCKET librouteros_la_LIBADD += -lsocket endif -librouteros_la_SOURCES = main.c registration_table.c interface.c routeros_api.h routeros_version.h +librouteros_la_SOURCES = main.c routeros_api.h routeros_version.h \ + ros_parse.c ros_parse.h \ + registration_table.c \ + interface.c bin_PROGRAMS = ros diff --git a/src/interface.c b/src/interface.c index de62eeb..3ee105f 100644 --- a/src/interface.c +++ b/src/interface.c @@ -40,13 +40,14 @@ #include #include "routeros_api.h" +#include "ros_parse.h" /* * Private data types */ struct rt_internal_data_s { - ros_interface_handler handler; + ros_interface_handler_t handler; void *user_data; }; typedef struct rt_internal_data_s rt_internal_data_t; @@ -54,69 +55,6 @@ typedef struct rt_internal_data_s rt_internal_data_t; /* * Private functions */ -static unsigned int sstrtoui (const char *str) /* {{{ */ -{ - unsigned int ret; - char *endptr; - - if (str == NULL) - return (0); - - errno = 0; - endptr = NULL; - ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10); - if ((endptr == str) || (errno != 0)) - return (0); - - return (ret); -} /* }}} unsigned int sstrtoui */ - -static _Bool sstrtob (const char *str) /* {{{ */ -{ - if (str == NULL) - return (false); - - if (strcasecmp ("true", str) == 0) - return (true); - return (false); -} /* }}} _Bool sstrtob */ - -static int string_to_rx_tx_counters (const char *str, /* {{{ */ - uint64_t *rx, uint64_t *tx) -{ - const char *ptr; - char *endptr; - - if ((rx == NULL) || (tx == NULL)) - return (EINVAL); - - *rx = 0; - *tx = 0; - - if (str == NULL) - return (EINVAL); - - ptr = str; - errno = 0; - endptr = NULL; - *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); - if ((endptr == str) || (errno != 0)) - return (EIO); - - assert (endptr != NULL); - if (*endptr != '/') - return (EIO); - - ptr = endptr + 1; - errno = 0; - endptr = NULL; - *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); - if ((endptr == str) || (errno != 0)) - return (EIO); - - return (0); -} /* }}} int string_to_rx_tx_counters */ - static ros_interface_t *rt_reply_to_interface (const ros_reply_t *r) /* {{{ */ { ros_interface_t *ret; @@ -136,13 +74,13 @@ static ros_interface_t *rt_reply_to_interface (const ros_reply_t *r) /* {{{ */ ret->type = ros_reply_param_val_by_key (r, "type"); ret->comment = ros_reply_param_val_by_key (r, "comment"); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"), &ret->rx_packets, &ret->tx_packets); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"), &ret->rx_bytes, &ret->tx_bytes); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "errors"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "errors"), &ret->rx_errors, &ret->tx_errors); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "drops"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "drops"), &ret->rx_drops, &ret->tx_drops); ret->mtu = sstrtoui (ros_reply_param_val_by_key (r, "mtu")); @@ -157,14 +95,14 @@ static ros_interface_t *rt_reply_to_interface (const ros_reply_t *r) /* {{{ */ return (ret); } /* }}} ros_interface_t *rt_reply_to_interface */ -static void if_interface_free (ros_interface_t *r) /* {{{ */ +static void if_interface_free (const ros_interface_t *r) /* {{{ */ { - ros_interface_t *next; + const ros_interface_t *next; while (r != NULL) { next = r->next; - free (r); + free ((void *) r); r = next; } } /* }}} void if_interface_free */ @@ -193,7 +131,7 @@ static int if_internal_handler (ros_connection_t *c, /* {{{ */ * Public functions */ int ros_interface (ros_connection_t *c, /* {{{ */ - ros_interface_handler handler, void *user_data) + ros_interface_handler_t handler, void *user_data) { rt_internal_data_t data; diff --git a/src/registration_table.c b/src/registration_table.c index f016a85..e5c82aa 100644 --- a/src/registration_table.c +++ b/src/registration_table.c @@ -38,6 +38,7 @@ #include #include "routeros_api.h" +#include "ros_parse.h" /* Status: re @@ -86,7 +87,7 @@ Status: done */ struct rt_internal_data_s { - ros_registration_table_handler handler; + ros_registration_table_handler_t handler; void *user_data; }; typedef struct rt_internal_data_s rt_internal_data_t; @@ -94,53 +95,6 @@ typedef struct rt_internal_data_s rt_internal_data_t; /* * Private functions */ -static double sstrtod (const char *str) /* {{{ */ -{ - double ret; - char *endptr; - - if (str == NULL) - return (NAN); - - errno = 0; - endptr = NULL; - ret = strtod (str, &endptr); - if ((endptr == str) || (errno != 0)) - return (NAN); - - return (ret); -} /* }}} double sstrtod */ - -static int string_to_rx_tx_counters (const char *str, /* {{{ */ - uint64_t *rx, uint64_t *tx) -{ - const char *ptr; - char *endptr; - - if ((str == NULL) || (rx == NULL) || (tx == NULL)) - return (EINVAL); - - ptr = str; - errno = 0; - endptr = NULL; - *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); - if ((endptr == str) || (errno != 0)) - return (EIO); - - assert (endptr != NULL); - if (*endptr != ',') - return (EIO); - - ptr = endptr + 1; - errno = 0; - endptr = NULL; - *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); - if ((endptr == str) || (errno != 0)) - return (EIO); - - return (0); -} /* }}} int string_to_rx_tx_counters */ - static ros_registration_table_t *rt_reply_to_regtable (const ros_reply_t *r) /* {{{ */ { ros_registration_table_t *ret; @@ -161,17 +115,17 @@ static ros_registration_table_t *rt_reply_to_regtable (const ros_reply_t *r) /* ret->rx_rate = sstrtod (ros_reply_param_val_by_key (r, "rx-rate")); ret->tx_rate = sstrtod (ros_reply_param_val_by_key (r, "tx-rate")); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"), &ret->rx_packets, &ret->tx_packets); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"), &ret->rx_bytes, &ret->tx_bytes); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "frames"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "frames"), &ret->rx_frames, &ret->tx_frames); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "frame-bytes"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "frame-bytes"), &ret->rx_frame_bytes, &ret->tx_frame_bytes); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frames"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frames"), &ret->rx_hw_frames, &ret->tx_hw_frames); - string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frame-bytes"), + sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frame-bytes"), &ret->rx_hw_frame_bytes, &ret->tx_hw_frame_bytes); ret->rx_signal_strength = sstrtod (ros_reply_param_val_by_key (r, "signal-strength")); @@ -186,14 +140,14 @@ static ros_registration_table_t *rt_reply_to_regtable (const ros_reply_t *r) /* return (ret); } /* }}} ros_registration_table_t *rt_reply_to_regtable */ -static void rt_regtable_free (ros_registration_table_t *r) /* {{{ */ +static void rt_regtable_free (const ros_registration_table_t *r) /* {{{ */ { - ros_registration_table_t *next; + const ros_registration_table_t *next; while (r != NULL) { next = r->next; - free (r); + free ((void *) r); r = next; } } /* }}} void rt_regtable_free */ @@ -222,7 +176,7 @@ static int rt_internal_handler (ros_connection_t *c, /* {{{ */ * Public functions */ int ros_registration_table (ros_connection_t *c, /* {{{ */ - ros_registration_table_handler handler, void *user_data) + ros_registration_table_handler_t handler, void *user_data) { rt_internal_data_t data; diff --git a/src/ros_parse.c b/src/ros_parse.c new file mode 100644 index 0000000..d236739 --- /dev/null +++ b/src/ros_parse.c @@ -0,0 +1,131 @@ +/** + * librouteros - src/ros_parse.c + * Copyright (C) 2009 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#ifndef _ISOC99_SOURCE +# define _ISOC99_SOURCE +#endif + +#ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200112L +#endif + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "routeros_api.h" + +_Bool sstrtob (const char *str) /* {{{ */ +{ + if (str == NULL) + return (false); + + if (strcasecmp ("true", str) == 0) + return (true); + return (false); +} /* }}} _Bool sstrtob */ + +unsigned int sstrtoui (const char *str) /* {{{ */ +{ + unsigned int ret; + char *endptr; + + if (str == NULL) + return (0); + + errno = 0; + endptr = NULL; + ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10); + if ((endptr == str) || (errno != 0)) + return (0); + + return (ret); +} /* }}} unsigned int sstrtoui */ + +double sstrtod (const char *str) /* {{{ */ +{ + double ret; + char *endptr; + + if (str == NULL) + return (NAN); + + errno = 0; + endptr = NULL; + ret = strtod (str, &endptr); + if ((endptr == str) || (errno != 0)) + return (NAN); + + return (ret); +} /* }}} double sstrtod */ + +int sstrto_rx_tx_counters (const char *str, /* {{{ */ + uint64_t *rx, uint64_t *tx) +{ + const char *ptr; + char *endptr; + + if ((rx == NULL) || (tx == NULL)) + return (EINVAL); + + *rx = 0; + *tx = 0; + + if (str == NULL) + return (EINVAL); + + ptr = str; + errno = 0; + endptr = NULL; + *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); + if ((endptr == str) || (errno != 0)) + { + *rx = 0; + return (EIO); + } + + assert (endptr != NULL); + if ((*endptr != '/') && (*endptr != ',')) + return (EIO); + + ptr = endptr + 1; + errno = 0; + endptr = NULL; + *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10); + if ((endptr == str) || (errno != 0)) + { + *rx = 0; + *tx = 0; + return (EIO); + } + + return (0); +} /* }}} int sstrto_rx_tx_counters */ + +/* vim: set ts=2 sw=2 noet fdm=marker : */ diff --git a/src/ros_parse.h b/src/ros_parse.h new file mode 100644 index 0000000..c68f0f2 --- /dev/null +++ b/src/ros_parse.h @@ -0,0 +1,35 @@ +/** + * librouteros - src/ros_parse.h + * Copyright (C) 2009 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#ifndef ROS_PARSE_H +#define ROS_PARSE_H 1 + +_Bool sstrtob (const char *str); + +unsigned int sstrtoui (const char *str); + +double sstrtod (const char *str); + +int sstrto_rx_tx_counters (const char *str, uint64_t *rx, uint64_t *tx); + +#endif /* ROS_PARSE_H */ + +/* vim: set ts=2 sw=2 noet fdm=marker : */ -- 2.11.0