Skip to content

Instantly share code, notes, and snippets.

@jshardy
Last active February 26, 2019 09:24
Show Gist options
  • Save jshardy/f1cc173d27b7c00e56f84e8b15cc4ffd to your computer and use it in GitHub Desktop.
Save jshardy/f1cc173d27b7c00e56f84e8b15cc4ffd to your computer and use it in GitHub Desktop.
GetToken Array
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