Created
November 16, 2018 17:38
-
-
Save d3x0r/481696b14b2e826c17da474b918fa829 to your computer and use it in GitHub Desktop.
Parses a string into seprate arguments similar to those recieved by main( int argc, char **argv );
This file contains hidden or 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
#include <stdlib.h> | |
#include <string.h> | |
// strdup isn't 'standard'. | |
//extern char *strdup(char*); | |
/* | |
* usage: | |
* function f( char *argString ) { | |
* int argc; | |
* char **argv; | |
* ParseIntoArgs( argString, &argc, &argv ); | |
* } | |
* | |
*/ | |
void ParseIntoArgs( char *lpCmdLine, int *pArgc, char ***pArgv ) | |
{ | |
char *args = lpCmdLine; | |
char *p; | |
char **pp; | |
char **argv; // result variable, pp is a temp pointer | |
char quote = 0; | |
int escape = 0; | |
int count = 0; | |
int lastchar; | |
lastchar = ' '; // auto continue spaces... | |
p = args; | |
while( p && p[0] ) | |
{ | |
if( escape ) { | |
if( p[0] == '\"' || p[0] == '\'' ) { | |
escape = 0; | |
count++; | |
} | |
else { | |
escape = 0; | |
count += 2; | |
} | |
} | |
else if( p[0] == '\\' ) { | |
escape = 1; | |
count++; | |
} | |
else if( quote ) | |
{ | |
if( p[0] == quote ) | |
{ | |
count++; | |
quote = 0; | |
lastchar = ' '; | |
} | |
} | |
else | |
{ | |
if( p[0] == '\"' || p[0] == '\'' ) | |
quote = p[0]; | |
else | |
{ | |
if( lastchar != ' ' && p[0] == ' ' ) // and there's a space | |
{ | |
count++; | |
} | |
else if( lastchar == ' ' && p[0] != ' ' ) | |
{ | |
} | |
} | |
lastchar = p[0] ; | |
} | |
p++; | |
} | |
if( quote ) | |
count++; // complete this argument | |
else if( p != args ) | |
count++; | |
if( count ) | |
{ | |
char *start; | |
lastchar = ' '; // auto continue spaces... | |
pp = argv = (char**)malloc( sizeof( char* ) *( count + 2 ) ); | |
p = args; | |
quote = 0; | |
count = 0; | |
start = NULL; | |
while( p[0] ) | |
{ | |
if( escape ) { | |
escape = 0; | |
} | |
else if( p[0] == '\\' ) { | |
escape = 1; | |
} | |
else if( quote ) | |
{ | |
if( !escape ) { | |
if( !start ) | |
start = p; | |
if( p[0] == quote ) | |
{ | |
p[0] = 0; | |
pp[count++] = strdup( start ); | |
p[0] = quote; | |
quote = 0; | |
start = NULL; | |
lastchar = ' '; | |
} | |
} | |
} | |
else | |
{ | |
if( !escape ) { | |
if( p[0] == '\"' || p[0] == '\'' ) | |
quote = p[0]; | |
else | |
{ | |
if( lastchar != ' ' && p[0] == ' ' ) // and there's a space | |
{ | |
p[0] = 0; | |
pp[count++] = strdup( start ); | |
start = NULL; | |
p[0] = ' '; | |
} | |
else if( lastchar == ' ' && p[0] != ' ' ) | |
{ | |
if( !start ) | |
start = p; | |
} | |
} | |
lastchar = p[0] ; | |
} | |
} | |
p++; | |
} | |
if( start ) | |
pp[count++] = strdup( start ); | |
pp[count] = NULL; | |
if( pArgc ) | |
(*pArgc) = count; | |
if( pArgv ) | |
(*pArgv) = argv; | |
} | |
else | |
{ | |
if( pArgc ) | |
(*pArgc) = 0; | |
if( pArgv ) | |
{ | |
(*pArgv) = malloc( sizeof( char* ) * 1 ); | |
(*pArgv)[0] = NULL; | |
} | |
} | |
} | |
/* | |
* Test code | |
* | |
int main(void) { | |
// your code goes here | |
int argc; | |
char **argv; | |
char args[] = "Pass -a Some \"arguments with \" words"; | |
ParseIntoArgs( args, &argc, &argv ); | |
{ | |
int n = 0; | |
while( argv[n] ) { | |
puts( argv[n++] ); | |
} | |
} | |
return 0; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment