From f39fde3f5632f356334bcdfe3ec120ebbd0e5566 Mon Sep 17 00:00:00 2001 From: Volker Jacht Date: Sat, 12 Dec 2020 23:30:38 +0100 Subject: [PATCH] Strip whitespaces from input strings --- src/mma.c | 21 ++++++++++++--------- src/string_list.c | 39 +++++++++++++++++++++++++++++++++++++++ src/string_list.h | 1 + tests/mma.F90 | 15 ++++++++++++++- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/mma.c b/src/mma.c index e33863b..252733a 100644 --- a/src/mma.c +++ b/src/mma.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include @@ -36,18 +36,17 @@ static int mma_reset_() { int mma_set_name(const char* name) { assert(name); free(_name); - _name = malloc(sizeof(char) * strlen(name) + 1); - if (!_name) { - return 1; - } - memcpy(_name, name, sizeof(char) * strlen(name) + 1); - return 0; + _name = NULL; + return string_trim(name, &_name); } int mma_comm_get(const char* comm_name, struct mma_comm** comm) { if (comm_array_size) { /* check if mma_initialize() has been called */ - int index = string_list_index_of(comm_name_list, comm_name); + char* comm_name_ = NULL; + string_trim(comm_name, &comm_name_); + int index = string_list_index_of(comm_name_list, comm_name_); + free(comm_name_); if (index == -1) { /* communicator name does not exist */ *comm = NULL; @@ -68,7 +67,11 @@ int mma_comm_register(const char* comm_name) { if (!comm_name_list) { string_list_create(&comm_name_list); } - return string_list_add(comm_name_list, comm_name); + char* comm_name_ = NULL; + string_trim(comm_name, &comm_name_); + int result = string_list_add(comm_name_list, comm_name_); + free(comm_name_); + return result; } int mma_print() { diff --git a/src/string_list.c b/src/string_list.c index 83a42e6..e355b08 100644 --- a/src/string_list.c +++ b/src/string_list.c @@ -1,6 +1,7 @@ #include "string_list.h" #include +#include #include #include #include @@ -121,3 +122,41 @@ char* string_list_get(struct string_list* list, int index) { } return NULL; } + +int string_trim(const char* string, char** trimmed_string) { + /* valid reference */ + assert(string); + /* valid reference to unallocated string */ + assert(trimmed_string); + assert(!*trimmed_string); + + size_t string_length = strlen(string); + size_t trimmed_string_length = 0; + size_t start = 0; + size_t end = 0; + + /* left trim */ + for (size_t i = 0; i < string_length; i++) { + if (!isspace(string[i])) { + start = i; + break; + } + } + + /* right trim */ + for (size_t i = string_length; i > start; i--) { + if (!isspace(string[i - 1])) { + end = i; + break; + } + } + + /* allocate and copy trimmed string */ + trimmed_string_length = end - start; + *trimmed_string = malloc(sizeof(char) * (trimmed_string_length + 1)); + if (!*trimmed_string) { + return 1; + } + memcpy(*trimmed_string, &string[start], sizeof(char) * trimmed_string_length); + (*trimmed_string)[trimmed_string_length] = '\0'; +} diff --git a/src/string_list.h b/src/string_list.h index fcfa4bc..e646334 100644 --- a/src/string_list.h +++ b/src/string_list.h @@ -19,5 +19,6 @@ void string_list_print(struct string_list*); int string_list_size(struct string_list*); int string_list_index_of(struct string_list*, const char*); char* string_list_get(struct string_list*, int); +int string_trim(const char* string, char** trimmed_string); #endif diff --git a/tests/mma.F90 b/tests/mma.F90 index 3b6db88..6de9601 100644 --- a/tests/mma.F90 +++ b/tests/mma.F90 @@ -1,16 +1,29 @@ #include "mma/mma.fmod" +#define assert(value) if (.not. value) then ; write(*,*) value ; call exit(1) ; end if + program main use mma implicit none type(mma_comm), pointer :: comm integer :: ierror - call mma_comm_register("test", ierror) + call mma_comm_register("test ", ierror) + assert(ierror == 0) call mma_print(ierror) + assert(ierror == 0) call mma_initialize(ierror) + assert(ierror == 0) call mma_comm_get("test", comm, ierror) + assert(ierror == 0) + call mma_comm_get("test ", comm, ierror) + assert(ierror == 0) + call mma_comm_get(" test ", comm, ierror) + assert(ierror == 0) call mma_print(ierror) + assert(ierror == 0) call mma_comm_register("test", ierror) + assert(ierror /= 0) call mma_finalize(ierror) + assert(ierror == 0) end program