Last active
December 18, 2015 10:59
-
-
Save ecere/5772494 to your computer and use it in GitHub Desktop.
This file contains 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
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