Skip to content
Snippets Groups Projects
Commit 4a91014b authored by Andreas Gattringer's avatar Andreas Gattringer
Browse files

moved string conversion to cats_strings library

parent 39d26a9c
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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
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
......@@ -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"
......
/*
* 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;
}
/*
* 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
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment