Skip to content

Instantly share code, notes, and snippets.

@ecere
Last active December 18, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecere/5772494 to your computer and use it in GitHub Desktop.
Save ecere/5772494 to your computer and use it in GitHub Desktop.
public int Tokenize(char * string, int maxTokens, char* tokens[], BackSlashEscaping esc)
{
#ifdef __WIN32__
//define windowsFileNameCharsNeedEscaping = " !%&'()+,;=[]^`{}~"; // "#$-.@_" are ok
const char * escChars = " !%&'()+,;=[]^`{}~\""; // windowsFileNameCharsNeedEscaping;
const char * escCharsQuoted = "\"";
#else
//define linuxFileNameCharsNeedEscaping = " !\"$&'()*:;<=>?[\\`{|"; // "#%+,-.@]^_}~" are ok
const char * escChars = " -!\"$&'()*:;<=>?[\\`{|"; // linuxFileNameCharsNeedEscaping;
const char * escCharsQuoted = "\"()$";
#endif
int count = 0;
bool quoted = false, escaped = false;
char * start = null, * output = string;
char ch;
for(; (ch = *string) && count<maxTokens; string++, output++)
{
bool wasEscaped = escaped;
if(output != string)
*output = ch;
if(start)
{
if(escaped)
{
escaped = false;
output--;
*output = ch;
}
else if(ch == '\"')
{
quoted ^= true;
output--;
}
else if(ch == ' ' && !quoted)
{
tokens[count++] = start;
*output = '\0';
start = null;
}
}
else if(ch != ' ')
{
if(ch == '\"')
{
quoted = true;
start = output+1;
}
else
start = output;
}
if(!wasEscaped && ch == '\\' && ( esc == true || (esc == forArgsPassing && strchr(quoted ? escCharsQuoted : escChars, *(string+1))) ))
escaped = true;
}
if(start && count < maxTokens)
{
tokens[count++] = start;
*output = '\0';
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment