C - homebrew strtok
Posted: Sun May 03, 2009 11:18 pm
So this code came into existence cause avansc wanted to see how I'd do it and so he could use it. Clearly neither of us knew about strtok.
EDIT: So it turns out avansc likes this code more than strtok 
EDIT2: Ginto made it segfault(no surprise there) so added a little something to remove user(coder) stupidity.
Code: Select all
char *nextToken(char* data, char delimiter)
{
char *pos = strchr(data, (int)delimiter);
if(pos == NULL)
{
return NULL;
}
char *ret = (char *)malloc((pos-data+1)*sizeof(char));
strncpy(ret, data,(pos-data));
//just in case read and write addresses overlap (it shouldn't)
strcpy(data, pos+1);//here so strncpy doesn't fuck up
return ret;
};

EDIT2: Ginto made it segfault(no surprise there) so added a little something to remove user(coder) stupidity.