-
Notifications
You must be signed in to change notification settings - Fork 176
/
CmdLineParser.cpp
214 lines (190 loc) · 5.29 KB
/
CmdLineParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// CmdLineParser.cpp: implementation of the CCmdLineParser class.
//
// Copyright (c) Pavel Antonov, 2002
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CmdLineParser.h"
const TCHAR CCmdLineParser::m_sDelimeters[] = _T("-/"); //GLOK
const TCHAR CCmdLineParser::m_sQuotes[] = _T("\""); // Can be _T("\"\'"), for instance //GLOK
const TCHAR CCmdLineParser::m_sValueSep[] = _T(" :="); // Space MUST be in set //GLOK
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCmdLineParser::CCmdLineParser(LPCTSTR sCmdLine, bool bCaseSensitive)
: m_bCaseSensitive(bCaseSensitive)
{
if(sCmdLine) {
Parse(sCmdLine);
}
}
CCmdLineParser::~CCmdLineParser()
{
m_ValsMap.clear();
}
bool CCmdLineParser::Parse(LPCTSTR sCmdLine) {
if(!sCmdLine) return false;
m_sCmdLine = sCmdLine;
m_ValsMap.clear();
const CCmdLineParser_String sEmpty;
int nArgs = 0;
LPCTSTR sCurrent = sCmdLine;
while(true)
{
// /Key:"arg"
if(_tcslen(sCurrent) == 0)
break; // No data left
LPCTSTR sArg = _tcspbrk(sCurrent, m_sDelimeters);
if(!sArg)
break; // No delimiters found
sArg = _tcsinc(sArg);
// Key:"arg"
if(_tcslen(sArg) == 0)
break; // String ends with delimeter
LPCTSTR sVal = _tcspbrk(sArg, m_sValueSep);
if(sVal == NULL)
{ //Key ends command line
CCmdLineParser_String csKey(sArg);
if(!m_bCaseSensitive)
csKey.MakeLower();
m_ValsMap.insert(CValsMap::value_type(csKey, sEmpty));
break;
}
//if here, we're on one of:
//white space
//real separator
bool bNoVal = false;
if(sVal[0] == ' ')
{
//the next non-white space char is either a value or next delimiter
LPCWSTR scanForward = sVal;
while(_istspace(*scanForward))
scanForward++;
if((NULL != _tcschr(m_sDelimeters, *scanForward)) || (*scanForward == L'\0'))
{
//on a delimiter (or end of string), so sVal is not going to be a value (ie this arg has no value)
bNoVal = true;
}
else
{
//sVal is a real value
}
}
if(bNoVal)
{ // Key with no value or cmdline ends with /Key:
CCmdLineParser_String csKey(sArg, (int)(sVal - sArg));
if(!csKey.IsEmpty())
{ // Prevent /: case
if(!m_bCaseSensitive)
{
csKey.MakeLower();
}
m_ValsMap.insert(CValsMap::value_type(csKey, sEmpty));
}
sCurrent = _tcsinc(sVal);
continue;
}
else
{ // Key with value
CCmdLineParser_String csKey(sArg, (int)(sVal - sArg));
if(!m_bCaseSensitive)
{
csKey.MakeLower();
}
sVal = _tcsinc(sVal);
// "arg"
LPCTSTR sQuote = _tcspbrk(sVal, m_sQuotes), sEndQuote(NULL);
if(sQuote == sVal)
{ // Quoted String
sQuote = _tcsinc(sVal);
sEndQuote = _tcspbrk(sQuote, m_sQuotes);
}
else
{
sQuote = sVal;
sEndQuote = _tcschr(sQuote, _T(' '));
}
if(sEndQuote == NULL)
{ // No end quotes or terminating space, take rest of string
CCmdLineParser_String csVal(sQuote);
if(!csKey.IsEmpty())
{ // Prevent /:val case
m_ValsMap.insert(CValsMap::value_type(csKey, csVal));
}
break;
}
else
{ // End quote or space present
if(!csKey.IsEmpty())
{ // Prevent /:"val" case
CCmdLineParser_String csVal(sQuote, (int)(sEndQuote - sQuote));
m_ValsMap.insert(CValsMap::value_type(csKey, csVal));
}
sCurrent = _tcsinc(sEndQuote);
continue;
}
}
}
return (nArgs > 0);
}
CCmdLineParser::CValsMap::const_iterator CCmdLineParser::findKey(LPCTSTR sKey) const {
CCmdLineParser_String s(sKey);
if(!m_bCaseSensitive) {
s.MakeLower();
}
return m_ValsMap.find(s);
}
void CCmdLineParser::SetVal(LPCTSTR sKey, LPCTSTR val)
{
CCmdLineParser_String key = sKey;
if(!m_bCaseSensitive)
key.MakeLower();
m_ValsMap.insert(CValsMap::value_type(key, val));
}
// TRUE if "Key" present in command line
bool CCmdLineParser::HasKey(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
return true;
}
// Is "key" present in command line and have some value
bool CCmdLineParser::HasVal(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
if(it->second.IsEmpty()) return false;
return true;
}
// Returns value if value was found or NULL otherwise
LPCTSTR CCmdLineParser::GetVal(LPCTSTR sKey) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
return LPCTSTR(it->second);
}
// Returns true if value was found
bool CCmdLineParser::GetVal(LPCTSTR sKey, CCmdLineParser_String& sValue) const {
CValsMap::const_iterator it = findKey(sKey);
if(it == m_ValsMap.end()) return false;
sValue = it->second;
return true;
}
CCmdLineParser::POSITION CCmdLineParser::getFirst() const {
return m_ValsMap.begin();
}
CCmdLineParser::POSITION CCmdLineParser::getNext(POSITION& pos, CCmdLineParser_String& sKey, CCmdLineParser_String& sValue) const {
if(isLast(pos)) {
sKey.Empty();
return pos;
} else {
sKey = pos->first;
sValue = pos->second;
pos ++;
return pos;
}
}
// just helper ;)
bool CCmdLineParser::isLast(POSITION& pos) const {
return (pos == m_ValsMap.end());
}