From 4a91014b6655a1d42622b78a2f0bb08a08574e3f Mon Sep 17 00:00:00 2001 From: Andreas Gattringer <andreas.gattringer@univie.ac.at> Date: Wed, 11 Jan 2023 17:24:52 +0100 Subject: [PATCH] moved string conversion to cats_strings library --- src/cats/data/cats_datatypes.c | 121 --------------------- src/cats/data/cats_datatypes.h | 10 +- src/cats_strings/CMakeLists.txt | 4 +- src/cats_strings/cats_strings.h | 2 +- src/cats_strings/string_converters.c | 152 +++++++++++++++++++++++++++ src/cats_strings/string_converters.h | 37 +++++++ src/cats_strings/string_helpers.c | 2 +- 7 files changed, 194 insertions(+), 134 deletions(-) create mode 100644 src/cats_strings/string_converters.c create mode 100644 src/cats_strings/string_converters.h diff --git a/src/cats/data/cats_datatypes.c b/src/cats/data/cats_datatypes.c index f1fabcc..c0817ac 100644 --- a/src/cats/data/cats_datatypes.c +++ b/src/cats/data/cats_datatypes.c @@ -30,124 +30,3 @@ #include "cats_datatypes.h" #include "logging.h" - -const char *true_values[] = {"1", "y", "t"}; -const char *false_values[] = {"0", "n", "f"}; -const int true_counts = (int) (sizeof(true_values) / sizeof(char *)); -const int false_counts = (int) (sizeof(false_values) / sizeof(char *)); - - -bool string_to_double(char *string, double *value) -{ - if (string == NULL || strlen(string) == 0) return false; - - char *end_pointer = NULL; - errno = 0; - - double converted = strtod(string, &end_pointer); - - if (strlen(end_pointer) != 0) { - log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'double': '%s'", - __func__, string, - end_pointer); - - } - - if (errno == 0) { - *value = converted; - return true; - } - return false; -} - - -bool string_to_float(char *string, float *value) -{ - if (string == NULL || strlen(string) == 0) return false; - - char *end_pointer = NULL; - errno = 0; - - float converted = strtof(string, &end_pointer); - - if (strlen(end_pointer) != 0) { - log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'float': '%s'", - __func__, string, - end_pointer); - return false; - } - - if (errno == 0) { - *value = converted; - return true; - } - return false; -} - - -bool string_to_long_double(char *string, long double *value) -{ - if (string == NULL || strlen(string) == 0) return false; - - char *end_pointer = NULL; - errno = 0; - - long double converted = strtold(string, &end_pointer); - - if (strlen(end_pointer) != 0) { - log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'long double': '%s'", - __func__, string, end_pointer); - return false; - } - - if (errno == 0) { - *value = (cats_dt_rates) converted; - return true; - } - return false; -} - - -bool string_to_bool(char *string, bool *value) -{ - if (string == NULL) return false; - - for (int i = 0; i < true_counts; i++) { - if (!strncmp(string, true_values[i], 1)) { - *value = true; - return true; - } - } - - for (int i = 0; i < false_counts; i++) { - if (!strncmp(string, false_values[i], 1)) { - *value = false; - return true; - } - } - - return false; -} - - -bool string_to_integer(char *string, int32_t *value) -{ - if (string == NULL || strlen(string) == 0) return false; - - errno = 0; - char *end_pointer = NULL; - long converted = strtol(string, &end_pointer, 10); - - if (strlen(end_pointer) != 0) { - log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to integer %d: '%s'", - __func__, string, converted, end_pointer); - return false; - } - - if (errno == 0) { - *value = (int32_t) converted; - return true; - } - return false; -} - diff --git a/src/cats/data/cats_datatypes.h b/src/cats/data/cats_datatypes.h index 7d4911a..9325eba 100644 --- a/src/cats/data/cats_datatypes.h +++ b/src/cats/data/cats_datatypes.h @@ -65,14 +65,6 @@ struct cats_coordinates { // conversion functions -bool string_to_float(char *string, float *value); - -bool string_to_bool(char *string, bool *value); - -bool string_to_double(char *string, double *value); - -bool string_to_integer(char *string, int32_t *value); - -bool string_to_long_double(char *string, long double *value); +#include <cats_strings/string_converters.h> #endif \ No newline at end of file diff --git a/src/cats_strings/CMakeLists.txt b/src/cats_strings/CMakeLists.txt index 7a691c1..8079a56 100644 --- a/src/cats_strings/CMakeLists.txt +++ b/src/cats_strings/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(cats_strings STATIC "" ../cats_defs.h ../cats_windows.h) +add_library(cats_strings STATIC "" ../cats_defs.h ../cats_windows.h string_converters.c string_converters.h) target_sources(cats_strings PRIVATE @@ -14,4 +14,4 @@ target_sources(cats_strings ) set_property(TARGET cats_strings PROPERTY POSITION_INDEPENDENT_CODE ON) -target_link_libraries(cats_logging) \ No newline at end of file +target_link_libraries(cats_logging cats_time) \ No newline at end of file diff --git a/src/cats_strings/cats_strings.h b/src/cats_strings/cats_strings.h index f1a5b4b..27cdaff 100644 --- a/src/cats_strings/cats_strings.h +++ b/src/cats_strings/cats_strings.h @@ -23,7 +23,7 @@ #ifndef CATS_STRING_H_ #define CATS_STRING_H_ - +#include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include "data/error.h" diff --git a/src/cats_strings/string_converters.c b/src/cats_strings/string_converters.c new file mode 100644 index 0000000..28836ed --- /dev/null +++ b/src/cats_strings/string_converters.c @@ -0,0 +1,152 @@ +/* + * SPDX-License-Identifier: GPL-3.0-or-later + * + * string_converters.c + * + * Copyright (C) 2011-2022, University of Vienna and Vienna Institute for Nature Conservation & Analyses, Andreas Gattringer. + * + * 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; either version 3 of the License, or (at + * your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + + +#include <stdbool.h> +#include <stddef.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> +#include "string_converters.h" +#include "logging/logging.h" + +const char *true_values[] = {"1", "y", "t"}; +const char *false_values[] = {"0", "n", "f"}; +const int true_counts = (int) (sizeof(true_values) / sizeof(char *)); +const int false_counts = (int) (sizeof(false_values) / sizeof(char *)); + + +bool string_to_double(char *string, double *value) +{ + if (string == NULL || strlen(string) == 0) return false; + + char *end_pointer = NULL; + errno = 0; + + double converted = strtod(string, &end_pointer); + + if (strlen(end_pointer) != 0) { + log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'double': '%s'", + __func__, string, + end_pointer); + + } + + if (errno == 0) { + *value = converted; + return true; + } + return false; +} + + +bool string_to_float(char *string, float *value) +{ + if (string == NULL || strlen(string) == 0) return false; + + char *end_pointer = NULL; + errno = 0; + + float converted = strtof(string, &end_pointer); + + if (strlen(end_pointer) != 0) { + log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'float': '%s'", + __func__, string, + end_pointer); + return false; + } + + if (errno == 0) { + *value = converted; + return true; + } + return false; +} + + +bool string_to_long_double(char *string, long double *value) +{ + if (string == NULL || strlen(string) == 0) return false; + + char *end_pointer = NULL; + errno = 0; + + long double converted = strtold(string, &end_pointer); + + if (strlen(end_pointer) != 0) { + log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to 'long double': '%s'", + __func__, string, end_pointer); + return false; + } + + if (errno == 0) { + *value = (long double) converted; + return true; + } + return false; +} + + +bool string_to_bool(char *string, bool *value) +{ + if (string == NULL) return false; + + for (int i = 0; i < true_counts; i++) { + if (!strncmp(string, true_values[i], 1)) { + *value = true; + return true; + } + } + + for (int i = 0; i < false_counts; i++) { + if (!strncmp(string, false_values[i], 1)) { + *value = false; + return true; + } + } + + return false; +} + + +bool string_to_integer(char *string, int32_t *value) +{ + if (string == NULL || strlen(string) == 0) return false; + + errno = 0; + char *end_pointer = NULL; + long converted = strtol(string, &end_pointer, 10); + + if (strlen(end_pointer) != 0) { + log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to integer %d: '%s'", + __func__, string, converted, end_pointer); + return false; + } + + if (errno == 0) { + *value = (int32_t) converted; + return true; + } + return false; +} + diff --git a/src/cats_strings/string_converters.h b/src/cats_strings/string_converters.h new file mode 100644 index 0000000..68a6aa2 --- /dev/null +++ b/src/cats_strings/string_converters.h @@ -0,0 +1,37 @@ +/* + * SPDX-License-Identifier: GPL-3.0-or-later + * + * string_converters.h + * + * Copyright (C) 2011-2022, University of Vienna and Vienna Institute for Nature Conservation & Analyses, Andreas Gattringer. + * + * 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; either version 3 of the License, or (at + * your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + + + +#ifndef CATS_STRING_CONVERTERS_H +#define CATS_STRING_CONVERTERS_H +bool string_to_float(char *string, float *value); + +bool string_to_bool(char *string, bool *value); + +bool string_to_double(char *string, double *value); + +bool string_to_integer(char *string, int32_t *value); + +bool string_to_long_double(char *string, long double *value); +#endif //CATS_STRING_CONVERTERS_H diff --git a/src/cats_strings/string_helpers.c b/src/cats_strings/string_helpers.c index 2512f66..14bd4fa 100644 --- a/src/cats_strings/string_helpers.c +++ b/src/cats_strings/string_helpers.c @@ -123,7 +123,7 @@ char *replace_substring(const char *original, const char *search, const char *re log_message(LOG_ERROR, "%s: at least one NULL argument supplied: original '%s', search '%s', replacement '%s' ", __func__, original, search, replacement); - exit_cats(EXIT_FAILURE); + exit(EXIT_FAILURE); } size_t search_len = strlen(search); -- GitLab