diff --git a/src/cats/data/cats_datatypes.c b/src/cats/data/cats_datatypes.c index f1fabcc09055222cb81941f8ae8487a43418cf7c..c0817acfd282833ac0edf5e288bfebbf48259e67 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 7d4911a6537143e84a51507262731c5d9be6d53f..9325eba692b455c27bcea5be0870f00e03384560 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 7a691c1cc774ccb987dec8f5b7ae1a185cee49e5..8079a56dffc942207f8dae65f753b51a88a7cefc 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 f1a5b4b1dd63378c076b0ccbc334da8612d867e5..27cdaff7869c746300bf4c0ac48463109db65d7a 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 0000000000000000000000000000000000000000..28836ed59b095b706931baf172fd3a3867fc2ef1 --- /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 0000000000000000000000000000000000000000..68a6aa2cece33da6e7d798213b64d4cf5b501c52 --- /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 2512f66f97987932e51b408ce7db831c450ae93a..14bd4faae6ba4b1189208fdd643832711caacdc7 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);