[HIGHLIGHT="C"]void my_split(const char* tosplit, const char* delim, char** parsed, int* len)
{
char *local = strdup(tosplit);
char *tok = strtok(local, delim);
int i = 0;
while(tok) {
*parsed++ = strdup(tok);
i++;
tok = strtok(NULL, delim);
}
*len = i;
free(local);
}[/HIGHLIGHT]
Can anyone explain this to me? i know it parses with a delimeter
but im not quite sure how it works
I dont understand this parser code
Started by Salrandin, Oct 23 2007 08:24 AM
3 replies to this topic
#1
Posted 23 October 2007 - 08:24 AM
|
|
|
#2
Posted 23 October 2007 - 08:54 AM
Here you go, comments added :cool:
char *local = strdup(tosplit); // makes a copy of tosplit, and
// returns a pointer to the allocated copy
char *tok = strtok(local, delim); // split local into tokens using
// delim, store the first token in tok
int i = 0; // counter for token
while(tok) { // while token is not null
*parsed++ = strdup(tok); // makes a copy of token,
// returns a pointer to the allocated copy which is stored in
// parsed, parsed is then increment to point to the next
// pointer
i++; // increment the number of tokens parsed
tok = strtok(NULL, delim); // get next token
}
*len = i; // stores the number of tokens in len
free(local); // frees the memory taken up local
// when the function returns, *parsed will contain tokens created from tosplit
#3
Posted 23 October 2007 - 05:08 PM
thx, it takes in a string as a delimiter right?
On C/C++ I use MS Visual Studios 2005 Express (yuck)
On Java I use BlueJ (Rocks!)
On Java I use BlueJ (Rocks!)
#4
Posted 23 October 2007 - 08:56 PM
NP. Yes the function takes in both the string to tokenise, and a string which contains the delimiters.


Sign In
Create Account


Back to top









