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
|
// CommandLineParser.cpp
#include "StdAfx.h"
#include "CommandLineParser.h"
static bool IsString1PrefixedByString2_NoCase(const wchar_t *u, const char *a)
{
for (;;)
{
char c = *a;
if (c == 0)
return true;
if ((unsigned char)MyCharLower_Ascii(c) != MyCharLower_Ascii(*u))
return false;
a++;
u++;
}
}
namespace NCommandLineParser {
#ifdef _WIN32
bool SplitCommandLine(const UString &src, UString &dest1, UString &dest2)
{
dest1.Empty();
dest2.Empty();
bool quoteMode = false;
unsigned i;
for (i = 0; i < src.Len(); i++)
{
wchar_t c = src[i];
if ((c == L' ' || c == L'\t') && !quoteMode)
{
dest2 = src.Ptr(i + 1);
return i != 0;
}
if (c == L'\"')
quoteMode = !quoteMode;
else
dest1 += c;
}
return i != 0;
}
void SplitCommandLine(const UString &s, UStringVector &parts)
{
UString sTemp = s;
sTemp.Trim();
parts.Clear();
for (;;)
{
UString s1, s2;
if (SplitCommandLine(sTemp, s1, s2))
parts.Add(s1);
if (s2.IsEmpty())
break;
sTemp = s2;
}
}
#endif
static const char *kStopSwitchParsing = "--";
static bool inline IsItSwitchChar(wchar_t c)
{
return (c == '-');
}
CParser::CParser(unsigned numSwitches):
_numSwitches(numSwitches),
_switches(0)
{
_switches = new CSwitchResult[numSwitches];
}
CParser::~CParser()
{
delete []_switches;
}
// if (s) contains switch then function updates switch structures
// out: true, if (s) is a switch
bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms)
{
if (s.IsEmpty() || !IsItSwitchChar(s[0]))
return false;
unsigned pos = 1;
unsigned switchIndex = 0;
int maxLen = -1;
for (unsigned i = 0; i < _numSwitches; i++)
{
const char *key = switchForms[i].Key;
unsigned switchLen = MyStringLen(key);
if ((int)switchLen <= maxLen || pos + switchLen > s.Len())
continue;
if (IsString1PrefixedByString2_NoCase((const wchar_t *)s + pos, key))
{
switchIndex = i;
maxLen = switchLen;
}
}
if (maxLen < 0)
{
ErrorMessage = "Unknown switch:";
return false;
}
pos += maxLen;
CSwitchResult &sw = _switches[switchIndex];
const CSwitchForm &form = switchForms[switchIndex];
if (!form.Multi && sw.ThereIs)
{
ErrorMessage = "Multiple instances for switch:";
return false;
}
sw.ThereIs = true;
int rem = s.Len() - pos;
if (rem < form.MinLen)
{
ErrorMessage = "Too short switch:";
return false;
}
sw.WithMinus = false;
sw.PostCharIndex = -1;
switch (form.Type)
{
case NSwitchType::kMinus:
if (rem == 1)
{
sw.WithMinus = (s[pos] == '-');
if (sw.WithMinus)
return true;
ErrorMessage = "Incorrect switch postfix:";
return false;
}
break;
case NSwitchType::kChar:
if (rem == 1)
{
wchar_t c = s[pos];
if (c <= 0x7F)
{
sw.PostCharIndex = FindCharPosInString(form.PostCharSet, (char)c);
if (sw.PostCharIndex >= 0)
return true;
}
ErrorMessage = "Incorrect switch postfix:";
return false;
}
break;
case NSwitchType::kString:
sw.PostStrings.Add((const wchar_t *)s + pos);
return true;
}
if (pos != s.Len())
{
ErrorMessage = "Too long switch:";
return false;
}
return true;
}
bool CParser::ParseStrings(const CSwitchForm *switchForms, const UStringVector &commandStrings)
{
ErrorLine.Empty();
bool stopSwitch = false;
FOR_VECTOR (i, commandStrings)
{
const UString &s = commandStrings[i];
if (!stopSwitch)
{
if (s.IsEqualTo(kStopSwitchParsing))
{
stopSwitch = true;
continue;
}
if (!s.IsEmpty() && IsItSwitchChar(s[0]))
{
if (ParseString(s, switchForms))
continue;
ErrorLine = s;
return false;
}
}
NonSwitchStrings.Add(s);
}
return true;
}
}
|