1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/Common/CommandLineParser.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,232 @@ 1.4 +// CommandLineParser.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "CommandLineParser.h" 1.9 + 1.10 +namespace NCommandLineParser { 1.11 + 1.12 +void SplitCommandLine(const UString &src, UString &dest1, UString &dest2) 1.13 +{ 1.14 + dest1.Empty(); 1.15 + dest2.Empty(); 1.16 + bool quoteMode = false; 1.17 + int i; 1.18 + for (i = 0; i < src.Length(); i++) 1.19 + { 1.20 + wchar_t c = src[i]; 1.21 + if (c == L'\"') 1.22 + quoteMode = !quoteMode; 1.23 + else if (c == L' ' && !quoteMode) 1.24 + { 1.25 + i++; 1.26 + break; 1.27 + } 1.28 + else 1.29 + dest1 += c; 1.30 + } 1.31 + dest2 = src.Mid(i); 1.32 +} 1.33 + 1.34 +void SplitCommandLine(const UString &s, UStringVector &parts) 1.35 +{ 1.36 + UString sTemp = s; 1.37 + sTemp.Trim(); 1.38 + parts.Clear(); 1.39 + while (true) 1.40 + { 1.41 + UString s1, s2; 1.42 + SplitCommandLine(sTemp, s1, s2); 1.43 + // s1.Trim(); 1.44 + // s2.Trim(); 1.45 + if (!s1.IsEmpty()) 1.46 + parts.Add(s1); 1.47 + if (s2.IsEmpty()) 1.48 + return; 1.49 + sTemp = s2; 1.50 + } 1.51 +} 1.52 + 1.53 + 1.54 +static const wchar_t kSwitchID1 = '-'; 1.55 +// static const wchar_t kSwitchID2 = '/'; 1.56 + 1.57 +static const wchar_t kSwitchMinus = '-'; 1.58 +static const wchar_t *kStopSwitchParsing = L"--"; 1.59 + 1.60 +static bool IsItSwitchChar(wchar_t c) 1.61 +{ 1.62 + return (c == kSwitchID1 /*|| c == kSwitchID2 */); 1.63 +} 1.64 + 1.65 +CParser::CParser(int numSwitches): 1.66 + _numSwitches(numSwitches) 1.67 +{ 1.68 + _switches = new CSwitchResult[_numSwitches]; 1.69 +} 1.70 + 1.71 +CParser::~CParser() 1.72 +{ 1.73 + delete []_switches; 1.74 +} 1.75 + 1.76 +void CParser::ParseStrings(const CSwitchForm *switchForms, 1.77 + const UStringVector &commandStrings) 1.78 +{ 1.79 + int numCommandStrings = commandStrings.Size(); 1.80 + bool stopSwitch = false; 1.81 + for (int i = 0; i < numCommandStrings; i++) 1.82 + { 1.83 + const UString &s = commandStrings[i]; 1.84 + if (stopSwitch) 1.85 + NonSwitchStrings.Add(s); 1.86 + else 1.87 + if (s == kStopSwitchParsing) 1.88 + stopSwitch = true; 1.89 + else 1.90 + if (!ParseString(s, switchForms)) 1.91 + NonSwitchStrings.Add(s); 1.92 + } 1.93 +} 1.94 + 1.95 +// if string contains switch then function updates switch structures 1.96 +// out: (string is a switch) 1.97 +bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms) 1.98 +{ 1.99 + int len = s.Length(); 1.100 + if (len == 0) 1.101 + return false; 1.102 + int pos = 0; 1.103 + if (!IsItSwitchChar(s[pos])) 1.104 + return false; 1.105 + while(pos < len) 1.106 + { 1.107 + if (IsItSwitchChar(s[pos])) 1.108 + pos++; 1.109 + const int kNoLen = -1; 1.110 + int matchedSwitchIndex = 0; // GCC Warning 1.111 + int maxLen = kNoLen; 1.112 + for(int switchIndex = 0; switchIndex < _numSwitches; switchIndex++) 1.113 + { 1.114 + int switchLen = MyStringLen(switchForms[switchIndex].IDString); 1.115 + if (switchLen <= maxLen || pos + switchLen > len) 1.116 + continue; 1.117 + 1.118 + UString temp = s + pos; 1.119 + temp = temp.Left(switchLen); 1.120 + if(temp.CompareNoCase(switchForms[switchIndex].IDString) == 0) 1.121 + // if(_strnicmp(switchForms[switchIndex].IDString, LPCSTR(s) + pos, switchLen) == 0) 1.122 + { 1.123 + matchedSwitchIndex = switchIndex; 1.124 + maxLen = switchLen; 1.125 + } 1.126 + } 1.127 + if (maxLen == kNoLen) 1.128 + throw "maxLen == kNoLen"; 1.129 + CSwitchResult &matchedSwitch = _switches[matchedSwitchIndex]; 1.130 + const CSwitchForm &switchForm = switchForms[matchedSwitchIndex]; 1.131 + if ((!switchForm.Multi) && matchedSwitch.ThereIs) 1.132 + throw "switch must be single"; 1.133 + matchedSwitch.ThereIs = true; 1.134 + pos += maxLen; 1.135 + int tailSize = len - pos; 1.136 + NSwitchType::EEnum type = switchForm.Type; 1.137 + switch(type) 1.138 + { 1.139 + case NSwitchType::kPostMinus: 1.140 + { 1.141 + if (tailSize == 0) 1.142 + matchedSwitch.WithMinus = false; 1.143 + else 1.144 + { 1.145 + matchedSwitch.WithMinus = (s[pos] == kSwitchMinus); 1.146 + if (matchedSwitch.WithMinus) 1.147 + pos++; 1.148 + } 1.149 + break; 1.150 + } 1.151 + case NSwitchType::kPostChar: 1.152 + { 1.153 + if (tailSize < switchForm.MinLen) 1.154 + throw "switch is not full"; 1.155 + UString set = switchForm.PostCharSet; 1.156 + const int kEmptyCharValue = -1; 1.157 + if (tailSize == 0) 1.158 + matchedSwitch.PostCharIndex = kEmptyCharValue; 1.159 + else 1.160 + { 1.161 + int index = set.Find(s[pos]); 1.162 + if (index < 0) 1.163 + matchedSwitch.PostCharIndex = kEmptyCharValue; 1.164 + else 1.165 + { 1.166 + matchedSwitch.PostCharIndex = index; 1.167 + pos++; 1.168 + } 1.169 + } 1.170 + break; 1.171 + } 1.172 + case NSwitchType::kLimitedPostString: 1.173 + case NSwitchType::kUnLimitedPostString: 1.174 + { 1.175 + int minLen = switchForm.MinLen; 1.176 + if (tailSize < minLen) 1.177 + throw "switch is not full"; 1.178 + if (type == NSwitchType::kUnLimitedPostString) 1.179 + { 1.180 + matchedSwitch.PostStrings.Add(s.Mid(pos)); 1.181 + return true; 1.182 + } 1.183 + int maxLen = switchForm.MaxLen; 1.184 + UString stringSwitch = s.Mid(pos, minLen); 1.185 + pos += minLen; 1.186 + for(int i = minLen; i < maxLen && pos < len; i++, pos++) 1.187 + { 1.188 + wchar_t c = s[pos]; 1.189 + if (IsItSwitchChar(c)) 1.190 + break; 1.191 + stringSwitch += c; 1.192 + } 1.193 + matchedSwitch.PostStrings.Add(stringSwitch); 1.194 + break; 1.195 + } 1.196 + case NSwitchType::kSimple: 1.197 + break; 1.198 + } 1.199 + } 1.200 + return true; 1.201 +} 1.202 + 1.203 +const CSwitchResult& CParser::operator[](size_t index) const 1.204 +{ 1.205 + return _switches[index]; 1.206 +} 1.207 + 1.208 +///////////////////////////////// 1.209 +// Command parsing procedures 1.210 + 1.211 +int ParseCommand(int numCommandForms, const CCommandForm *commandForms, 1.212 + const UString &commandString, UString &postString) 1.213 +{ 1.214 + for(int i = 0; i < numCommandForms; i++) 1.215 + { 1.216 + const UString id = commandForms[i].IDString; 1.217 + if (commandForms[i].PostStringMode) 1.218 + { 1.219 + if(commandString.Find(id) == 0) 1.220 + { 1.221 + postString = commandString.Mid(id.Length()); 1.222 + return i; 1.223 + } 1.224 + } 1.225 + else 1.226 + if (commandString == id) 1.227 + { 1.228 + postString.Empty(); 1.229 + return i; 1.230 + } 1.231 + } 1.232 + return -1; 1.233 +} 1.234 + 1.235 +}