C String Handling
While attempting to write a utility for accessing the internal data of tracked modules I found that I required a C function that could break a string into an array of strings using a delimiter. I spent an hour or more just trying to figure out how to do it, and ended up with the following code (which you may use if you wish):
Shortly after writing this function I stumbled upon a little library called Bstrings (Better String Library) which is supposed to implement this feature, as well as solve a lot of common problems with strings in C.
I guess I'm kind of relieved and frustrated at the same time...
/* 2008-06-24 */
char **split_str(const char *str, const char *teststr) {
/* Splits a string into one or more substrings based on a test string.
* The substrings are returned as an array of strings, terminated with
* a NULL pointer.
* If the teststr is not found in the original string, an array with just
* that string is returned, as would be expected. Therefore, it might be a
* good idea to make sure the teststr is in the string before you call this
* function on it.
* If you pass an empty string as the str, you will get an empty array;
* i.e. an array with one object that is a NULL pointer.
* Note that the test str is not included in the results.
* Memory for the array is dynamically allocated using malloc, so remember
* to free it when you're done (unless you get a NULL pointer, of course).
*/
char **subs = NULL;
char const * breakstart = str;
char *breakend = NULL;
int splits = 0;
int startstrlen = 0;
int teststrlen = 0;
startstrlen = strlen(str); /* Length of original str */
teststrlen = strlen(teststr); /* Length of the test string */
/* Scan until break */
while (breakstart[0] != '\0') {
splits ++;
/* Continually add more space for the subs */
subs = realloc(subs,sizeof(char*)*splits);
/* Find the next break end */
breakend = strstr(breakstart,teststr);
/* If there was no occurence of the substr, put the break at the end */
if (!breakend)
breakend = (char*) str+strlen(str)+1;
/* Create a new block for the substr */
subs[splits-1] = malloc(sizeof(char*)*(breakend-breakstart)+1);
/* Copy the substring */
memcpy(subs[splits-1],breakstart,(breakend-breakstart));
/* Add the null byte */
subs[splits-1][breakend-breakstart] = '\0';
/* If there is more string to scan, continue scanning it */
if (breakend)
breakstart = breakend + teststrlen;
/* If there is no more to scan, don't scan anymore */
else
break;
}
/* Terminate the array */
subs = realloc(subs,sizeof(char*)*splits+1);
subs[splits] = NULL;
return subs;
}
Shortly after writing this function I stumbled upon a little library called Bstrings (Better String Library) which is supposed to implement this feature, as well as solve a lot of common problems with strings in C.
I guess I'm kind of relieved and frustrated at the same time...
Labels: c, programming, strings
