added parseCmdLine module

git-svn-id: http://apu.dw.local/svnusers/JoshHoltrop/pppc/trunk@45 8131a0b2-b21c-1c47-bd6a-f003126495bd
This commit is contained in:
joshholtrop 2009-08-03 19:29:21 +00:00
parent 94ea8c4aec
commit cfb6dd739b
4 changed files with 127 additions and 1 deletions

View File

@ -1,6 +1,6 @@
TARGET := pppc.exe TARGET := pppc.exe
OBJS := pppc.o session_notification.o resources.o pport.o OBJS := pppc.o session_notification.o resources.o pport.o parseCmdLine.o
CXXFLAGS := -O2 -Wall CXXFLAGS := -O2 -Wall
LDFLAGS := -mwindows LDFLAGS := -mwindows

111
parseCmdLine.cc Normal file
View File

@ -0,0 +1,111 @@
#include "parseCmdLine.h"
#include <iostream>
using namespace std;
static std::string trim(const std::string & orig)
{
const char ws[] = " \t\r\n\v\f";
size_t first = orig.find_first_not_of(ws);
size_t last = orig.find_last_not_of(ws);
if (first == string::npos || last == string::npos)
{
return "";
}
return string(orig, first, last - first + 1);
}
std::vector<std::string> parseCmdLine(char * cmdline)
{
enum { WSSKIP, GATHERING, SQUOTE, DQUOTE } state = WSSKIP;
vector<string> args;
string arg;
for (; *cmdline; cmdline++)
{
char c = *cmdline;
switch (state)
{
case WSSKIP:
if (!isspace(c))
{
if (c == '\'')
{
state = SQUOTE;
arg = "";
}
else if (c == '"')
{
state = DQUOTE;
arg = "";
}
else
{
state = GATHERING;
arg = c;
}
}
break;
case GATHERING:
if (c == '\'')
{
state = SQUOTE;
}
else if (c == '"')
{
state = DQUOTE;
}
else if (isspace(c))
{
args.push_back(arg);
state = WSSKIP;
}
else
{
arg += c;
}
break;
case SQUOTE:
if (c == '\'')
{
state = GATHERING;
}
else
{
arg += c;
}
break;
case DQUOTE:
if (c == '"')
{
state = GATHERING;
}
else
{
arg += c;
}
break;
}
}
switch (state)
{
case WSSKIP:
break;
case GATHERING:
args.push_back(arg);
break;
case SQUOTE:
cerr << "Warning: unmatched '\\'' character!" << endl;
break;
case DQUOTE:
cerr << "Warning: unmatched '\"' character!" << endl;
break;
}
for (int args_len = args.size(), i = 0; i < args_len; i++)
{
args[i] = trim(args[i]);
}
return args;
}

10
parseCmdLine.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef PARSECMDLINE_H
#define PARSECMDLINE_H
#include <vector>
#include <string>
std::vector<std::string> parseCmdLine(char * cmdline);
#endif

View File

@ -2,9 +2,12 @@
#include <windows.h> #include <windows.h>
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
#include <vector>
#include <string>
#include "session_notification.h" #include "session_notification.h"
#include "pport.h" #include "pport.h"
#include "resources.h" #include "resources.h"
#include "parseCmdLine.h"
using namespace std; using namespace std;
#define APP_NAME "Parallel Port Power Controller" #define APP_NAME "Parallel Port Power Controller"
@ -21,6 +24,8 @@ int WinMain(
LPSTR lpCmdLine, LPSTR lpCmdLine,
int nCmdShow) int nCmdShow)
{ {
vector<string> args = parseCmdLine(lpCmdLine);
m_hInstance = hInstance; m_hInstance = hInstance;
/* Load the application icon */ /* Load the application icon */