1 // CommandLineParser.cpp
\r
5 #include "CommandLineParser.h"
\r
7 namespace NCommandLineParser {
\r
9 bool SplitCommandLine(const UString &src, UString &dest1, UString &dest2)
\r
13 bool quoteMode = false;
\r
15 for (i = 0; i < src.Length(); i++)
\r
18 if (c == L' ' && !quoteMode)
\r
20 dest2 = src.Mid(i + 1);
\r
24 quoteMode = !quoteMode;
\r
31 void SplitCommandLine(const UString &s, UStringVector &parts)
\r
39 if (SplitCommandLine(sTemp, s1, s2))
\r
48 static const wchar_t kSwitchID1 = '-';
\r
49 // static const wchar_t kSwitchID2 = '/';
\r
51 static const wchar_t kSwitchMinus = '-';
\r
52 static const wchar_t *kStopSwitchParsing = L"--";
\r
54 static bool IsItSwitchChar(wchar_t c)
\r
56 return (c == kSwitchID1 /*|| c == kSwitchID2 */);
\r
59 CParser::CParser(int numSwitches):
\r
60 _numSwitches(numSwitches)
\r
62 _switches = new CSwitchResult[_numSwitches];
\r
70 void CParser::ParseStrings(const CSwitchForm *switchForms,
\r
71 const UStringVector &commandStrings)
\r
73 int numCommandStrings = commandStrings.Size();
\r
74 bool stopSwitch = false;
\r
75 for (int i = 0; i < numCommandStrings; i++)
\r
77 const UString &s = commandStrings[i];
\r
79 NonSwitchStrings.Add(s);
\r
81 if (s == kStopSwitchParsing)
\r
84 if (!ParseString(s, switchForms))
\r
85 NonSwitchStrings.Add(s);
\r
89 // if string contains switch then function updates switch structures
\r
90 // out: (string is a switch)
\r
91 bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms)
\r
93 int len = s.Length();
\r
97 if (!IsItSwitchChar(s[pos]))
\r
101 if (IsItSwitchChar(s[pos]))
\r
103 const int kNoLen = -1;
\r
104 int matchedSwitchIndex = 0; // GCC Warning
\r
105 int maxLen = kNoLen;
\r
106 for (int switchIndex = 0; switchIndex < _numSwitches; switchIndex++)
\r
108 int switchLen = MyStringLen(switchForms[switchIndex].IDString);
\r
109 if (switchLen <= maxLen || pos + switchLen > len)
\r
112 UString temp = s + pos;
\r
113 temp = temp.Left(switchLen);
\r
114 if (temp.CompareNoCase(switchForms[switchIndex].IDString) == 0)
\r
115 // if (_strnicmp(switchForms[switchIndex].IDString, LPCSTR(s) + pos, switchLen) == 0)
\r
117 matchedSwitchIndex = switchIndex;
\r
118 maxLen = switchLen;
\r
121 if (maxLen == kNoLen)
\r
122 throw "maxLen == kNoLen";
\r
123 CSwitchResult &matchedSwitch = _switches[matchedSwitchIndex];
\r
124 const CSwitchForm &switchForm = switchForms[matchedSwitchIndex];
\r
125 if ((!switchForm.Multi) && matchedSwitch.ThereIs)
\r
126 throw "switch must be single";
\r
127 matchedSwitch.ThereIs = true;
\r
129 int tailSize = len - pos;
\r
130 NSwitchType::EEnum type = switchForm.Type;
\r
133 case NSwitchType::kPostMinus:
\r
136 matchedSwitch.WithMinus = false;
\r
139 matchedSwitch.WithMinus = (s[pos] == kSwitchMinus);
\r
140 if (matchedSwitch.WithMinus)
\r
145 case NSwitchType::kPostChar:
\r
147 if (tailSize < switchForm.MinLen)
\r
148 throw "switch is not full";
\r
149 UString set = switchForm.PostCharSet;
\r
150 const int kEmptyCharValue = -1;
\r
152 matchedSwitch.PostCharIndex = kEmptyCharValue;
\r
155 int index = set.Find(s[pos]);
\r
157 matchedSwitch.PostCharIndex = kEmptyCharValue;
\r
160 matchedSwitch.PostCharIndex = index;
\r
166 case NSwitchType::kLimitedPostString:
\r
167 case NSwitchType::kUnLimitedPostString:
\r
169 int minLen = switchForm.MinLen;
\r
170 if (tailSize < minLen)
\r
171 throw "switch is not full";
\r
172 if (type == NSwitchType::kUnLimitedPostString)
\r
174 matchedSwitch.PostStrings.Add(s.Mid(pos));
\r
177 int maxLen = switchForm.MaxLen;
\r
178 UString stringSwitch = s.Mid(pos, minLen);
\r
180 for (int i = minLen; i < maxLen && pos < len; i++, pos++)
\r
182 wchar_t c = s[pos];
\r
183 if (IsItSwitchChar(c))
\r
187 matchedSwitch.PostStrings.Add(stringSwitch);
\r
190 case NSwitchType::kSimple:
\r
197 const CSwitchResult& CParser::operator[](size_t index) const
\r
199 return _switches[index];
\r
202 /////////////////////////////////
\r
203 // Command parsing procedures
\r
205 int ParseCommand(int numCommandForms, const CCommandForm *commandForms,
\r
206 const UString &commandString, UString &postString)
\r
208 for (int i = 0; i < numCommandForms; i++)
\r
210 const UString id = commandForms[i].IDString;
\r
211 if (commandForms[i].PostStringMode)
\r
213 if (commandString.Find(id) == 0)
\r
215 postString = commandString.Mid(id.Length());
\r
220 if (commandString == id)
\r
222 postString.Empty();
\r