Last active
February 26, 2019 09:24
-
-
Save jshardy/f1cc173d27b7c00e56f84e8b15cc4ffd to your computer and use it in GitHub Desktop.
GetToken Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char **GetTokens(char *tokens, const char *delim) | |
{ | |
char *index = NULL; | |
char **cmdArgs = NULL; | |
int argCount = 0; | |
index = strtok(tokens, delim); | |
while(index) | |
{ | |
cmdArgs = (char**)realloc(cmdArgs, sizeof(char*) * (argCount + 1)); | |
cmdArgs[argCount] = strdup(index); | |
argCount++; | |
index = strtok(NULL, delim); | |
} | |
// Add the ending NULL so we can detect end of cmdArgs | |
if(argCount > 0) | |
{ | |
cmdArgs = (char**)realloc(cmdArgs, sizeof(char*) * (argCount + 1)); | |
cmdArgs[argCount] = NULL; | |
} | |
return cmdArgs; | |
} | |
void PurgeTokens(char **tokens) | |
{ | |
char **temp = tokens; | |
while(*temp) | |
free(*temp++); | |
free(tokens); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment