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/5772090 to your computer and use it in GitHub Desktop.
Save ecere/5772090 to your computer and use it in GitHub Desktop.
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