Last active
December 18, 2015 10:59
-
-
Save ecere/5772090 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 char * PassArg(char * output, const char * input) | |
{ | |
#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 | |
char *o = output, *i = input; | |
bool quoting = false; | |
char *l = input; | |
#ifdef __WIN32__ | |
while(*l && !strchr(escChars, *l)) l++; | |
if(*l) quoting = true; | |
#else | |
if(*i == '-') | |
{ | |
l++; | |
while(*l && !strchr(escChars, *l)) l++; | |
if(*l) quoting = true; | |
} | |
#endif | |
if(quoting) | |
*o++ = '\"'; | |
while(*i) | |
{ | |
if(strchr(quoting ? escCharsQuoted : escChars, *i)) | |
*o++ = '\\'; | |
*o++ = *i++; | |
} | |
if(quoting) | |
*o++ = '\"'; | |
*o = '\0'; | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment