diff --git a/src/grib_accessor_class_bufr_elements_table.c b/src/grib_accessor_class_bufr_elements_table.c index 032b860cd..2849f283e 100644 --- a/src/grib_accessor_class_bufr_elements_table.c +++ b/src/grib_accessor_class_bufr_elements_table.c @@ -249,7 +249,7 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err) dictionary=grib_trie_new(c); while(fgets(line,sizeof(line)-1,f)) { - list=string_split(line,'|'); + list=string_split(line, "|"); grib_trie_insert(dictionary,list[0],list); } @@ -260,7 +260,7 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err) if (!f) {*err=GRIB_IO_PROBLEM; dictionary=NULL; goto the_end;} while(fgets(line,sizeof(line)-1,f)) { - list=string_split(line,'|'); + list=string_split(line, "|"); grib_trie_insert(dictionary,list[0],list); } diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index 40004f4a3..6176b8a94 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -1421,7 +1421,7 @@ int is_index_file(const char *filename); char get_dir_separator_char(void); const char *extract_filename(const char *filepath); char *codes_getenv(const char *name); -char** string_split(char* inputString, const char delimiterChar); +char** string_split(char* inputString, const char* delimiter); /* bufr_util.c */ int compute_bufr_key_rank(grib_handle* h, grib_string_list* keys, const char* key); diff --git a/src/grib_util.c b/src/grib_util.c index e17831c23..fdde23834 100644 --- a/src/grib_util.c +++ b/src/grib_util.c @@ -1682,19 +1682,22 @@ char* codes_getenv(const char* name) return result; } + /* Returns an array of strings the last of which is NULL */ -char** string_split(char* inputString, const char delimiterChar) +char** string_split(char* inputString, const char* delimiter) { char** result = NULL; char* p = inputString; char* lastDelimiter = NULL; char* aToken = NULL; - char delimiterString[2] = {0, 0}; size_t numTokens = 0; size_t strLength = 0; size_t index = 0; + char delimiterChar = 0; - Assert(inputString); + DebugAssert(inputString); + DebugAssert( delimiter && (strlen(delimiter)==1) ); + delimiterChar = delimiter[0]; while (*p) { const char ctmp = *p; if (ctmp == delimiterChar) { @@ -1712,14 +1715,12 @@ char** string_split(char* inputString, const char delimiterChar) result = (char**)malloc(numTokens * sizeof(char*)); Assert(result); - delimiterString[0] = delimiterChar; - delimiterString[1] = '\0'; /* Start tokenizing */ - aToken = strtok(inputString, delimiterString); + aToken = strtok(inputString, delimiter); while (aToken) { Assert(index < numTokens); *(result + index++) = strdup(aToken); - aToken = strtok(NULL, delimiterString); + aToken = strtok(NULL, delimiter); } Assert(index == numTokens - 1); *(result + index) = '\0'; diff --git a/tests/unit_tests.c b/tests/unit_tests.c index fdb46ad99..49c682e7d 100644 --- a/tests/unit_tests.c +++ b/tests/unit_tests.c @@ -1371,7 +1371,7 @@ void test_string_splitting() int i=0; char input[80] = "Born|To|Be|Wild"; char** list=0; - list = string_split(input, '|'); + list = string_split(input, "|"); for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ assert(i == 4); if ( strcmp(list[0], "Born")!=0 ) assert(0); @@ -1380,14 +1380,14 @@ void test_string_splitting() if ( strcmp(list[3], "Wild")!=0 ) assert(0); strcpy(input, "12345|a gap|"); - list = string_split(input, '|'); + list = string_split(input, "|"); for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ assert(i == 2); if ( strcmp(list[0], "12345")!=0 ) assert(0); if ( strcmp(list[1], "a gap")!=0 ) assert(0); strcpy(input, "Steppenwolf"); - list = string_split(input, '|'); + list = string_split(input, ","); for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ assert(i == 1); if ( strcmp(list[0], "Steppenwolf")!=0 ) assert(0);