Alphaios Blog

This is the official blog of Alphaios.net. When I feel like rambling I turn here.

6/24/08

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):

/* 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: , ,

6/11/08

News: Blog Merging with the News Updates.

If you recently used the usual http://alphaios.net link that I keep posting everywhere you may have noticed that you were automatically redirected to this blog. That is because, from now on, I want all visitors to the site to see the blog first. Why? Because I've officially abandon the very, very old static news page in favor of using the blog engine for posting updates.

For now the old news page can still be accessed at the same URL as before, but I will no longer be using it. I'll keep it up for a while so that older updates can still be accessed, at least until I can move those updates into the blog--or until they become so incredibly old that they no longer are relevant. (And that will be very soon...)

Also, the home page is still available as an "about" page from the house-shaped icon in the navigational bar.

Labels: , ,