Usually one exe command is followed by some parameters, such as c:/dir /a; our application can process those parameters, both in win app and console app.
Indicate that all paremeters are Separated by space ' ';
// win mode: void ParseCommandLineArgs() { CString strCmdLine = GetCommandLine(); if (!strCmdLine.IsEmpty()) { // unify the letters strCmdLine.MakeUpper(); int nPos = 0; do { nPos = strCmdLine.Find(TCHAR(' ')); if (nPos>0) { // remove the first parameter, it's usually the exe file path strCmdLine.Delete( 0, nPos+1); CString strCurrent = strCmdLine; int nNextPos = strCmdLine.Find(TCHAR(' ')); // store the current parameter in strCurrent if (nNextPos > 0) strCurrent = strCmdLine.Left( nNextPos ); // process the parameter } } while( nPos != -1); } }
console mode: (from open SSL Evp_text.c)
Indicate that all paremeters are Separated by space ':';
It’s also an easy script debugger.
// the delim is a stop character. // return the first part, // and terminate the string. static char *sstrsep(char **string, const char *delim) { char isdelim[256]; // stop character table char *token = *string;
if (**string == 0) return NULL;
memset(isdelim, 0, 256); isdelim[0] = 1; // ‘/0’ is a end of string
// initial stop character table while (*delim) { isdelim[(unsigned char)(*delim)] = 1; delim++; }
// begin check while (!isdelim[(unsigned char)(**string)]) { (*string)++; }
// end the string by ‘/0’ if (**string) { **string = 0; (*string)++; }
return token; } // remove the warn information static unsigned char *ustrsep(char **p,const char *sep) { return (unsigned char *)sstrsep(p,sep); }
int _tmain(int argc, _TCHAR* argv[]) { const char *szPara = argv[1]; for (int i = 0; i < argc; ++i) {
printf("para%d: %s /n",i, argv[i]); }
// some examples easy explement of test script //# CBC-AES128.Encrypt and CBC-AES128.Decrypt // AES-128-CBC:2B7E151628AED2A6ABF7158809CF4F3C:000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:7649ABAC8119B246CEE98E9B12E9197D
FILE *f=fopen(szPara,"r"); if(!f) return 0; // in a dead loop, we read every line of script for (;;) {
char line[4096]; char *p; char *cipher; unsigned char *iv,*key,*plaintext,*ciphertext; int encdec; // operation flag int kn,in,pn,cn;
if(!fgets((char *)line,sizeof line,f)) break; if(line[0] == '#' || line[0] == '/n') continue; p=line; cipher=sstrsep(&p,":"); key=ustrsep(&p,":"); iv=ustrsep(&p,":"); plaintext=ustrsep(&p,":"); ciphertext=ustrsep(&p,":"); if (p[-1] == '/n') { p[-1] = '/0'; encdec = -1; } else { encdec = atoi(sstrsep(&p,"/n")); } } fclose(f); return 0; } |