C Token Parsing
So I'm trying to implement a token parser that doesn't use any C library
functions like strtok() etc but I'm having a few issues with access
violations and after reading several similar questions on here still
haven't got it nailed down. Anyone willing to offer some pointers?
int main(int argc, char* argv[])
{
int maxTokens = 10;
char* tokens[10];
int i;
for(i = 0; i < maxTokens; i++)
{
tokens[i] = NULL;
}
char* str = "This,is,a,test,string";
int result = parseLine(str, ',', tokens, maxTokens);
printf("%d tokens were found!", result);
system("PAUSE");
return 0;
}
int parseLine(char* str, char delimeter, char* tokens[], int maxTokens)
{
char* srcStr = str;
int strlen = 0;
int tokenCount = 0;
if(srcStr[strlen] != delimeter && srcStr[strlen] != '\0')
{
tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
tokens[tokenCount] = &srcStr[strlen];
tokenCount++;
}
while(srcStr[strlen] != '\0')
{
if(srcStr[strlen] == delimeter)
{
tokens[tokenCount-1][strlen] = '\0';
if(srcStr[strlen+1] != '\0')
{
tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
tokens[tokenCount] = &srcStr[++strlen];
tokenCount++;
}
}
else
{
strlen++;
}
}
return tokenCount;
}
No comments:
Post a Comment