Skip to content

Commit

Permalink
Strip whitespaces from input strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kooky089 committed Dec 18, 2020
1 parent 05687a2 commit f39fde3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/mma.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <mma_config.h>
#include <mma/mma.h>
#include <mma_config.h>

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
39 changes: 39 additions & 0 deletions src/string_list.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "string_list.h"

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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';
}
1 change: 1 addition & 0 deletions src/string_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion tests/mma.F90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f39fde3

Please sign in to comment.