blob: cdbcdb9f24140a3b656d08a2574a9130343cb640 [file] [log] [blame]
Deepak Panickal6f9c4682014-05-16 10:51:01 +00001//===-- MICmdArgValOptionLong.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Deepak Panickal6f9c4682014-05-16 10:51:01 +000010// In-house headers:
11#include "MICmdArgValOptionLong.h"
12#include "MICmdArgContext.h"
13
Kate Stoneb9c1b512016-09-06 20:57:50 +000014//++
15//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000016// Details: CMICmdArgValOptionLong constructor.
17// Type: Method.
18// Args: None.
19// Return: None.
20// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000021//--
Bruce Mitchenere2453af2015-08-04 10:24:20 +000022CMICmdArgValOptionLong::CMICmdArgValOptionLong()
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 : m_nExpectingNOptions(0), m_eExpectingOptionType(eArgValType_invalid) {}
Deepak Panickal6f9c4682014-05-16 10:51:01 +000024
Kate Stoneb9c1b512016-09-06 20:57:50 +000025//++
26//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000027// Details: CMICmdArgValOptionLong constructor.
28// Type: Method.
29// Args: vrArgName - (R) Argument's name to search by.
Kate Stoneb9c1b512016-09-06 20:57:50 +000030// vbMandatory - (R) True = Yes must be present, false = optional
31// argument.
32// vbHandleByCmd - (R) True = Command processes *this option, false =
33// not handled.
Zachary Turner1d6af022014-11-17 18:06:21 +000034// Return: None.
35// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000036//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000037CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName,
38 const bool vbMandatory,
39 const bool vbHandleByCmd)
40 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd),
41 m_nExpectingNOptions(0), m_eExpectingOptionType(eArgValType_invalid) {}
Deepak Panickal6f9c4682014-05-16 10:51:01 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043//++
44//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000045// Details: CMICmdArgValOptionLong constructor.
46// Type: Method.
47// Args: vrArgName - (R) Argument's name to search by.
Kate Stoneb9c1b512016-09-06 20:57:50 +000048// vbMandatory - (R) True = Yes must be present, false =
49// optional argument.
50// vbHandleByCmd - (R) True = Command processes *this option,
51// false = not handled.
52// veType - (R) The type of argument to look for and
53// create argument object of a certain type.
54// vnExpectingNOptions - (R) The number of options expected to read
55// following *this argument.
Zachary Turner1d6af022014-11-17 18:06:21 +000056// Return: None.
57// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000058//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000059CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName,
60 const bool vbMandatory,
61 const bool vbHandleByCmd,
62 const ArgValType_e veType,
63 const MIuint vnExpectingNOptions)
64 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd),
65 m_nExpectingNOptions(vnExpectingNOptions),
66 m_eExpectingOptionType(veType) {}
Deepak Panickal6f9c4682014-05-16 10:51:01 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068//++
69//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000070// Details: CMICmdArgValOptionLong destructor.
71// Type: Overridden.
72// Args: None.
73// Return: None.
74// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000075//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000076CMICmdArgValOptionLong::~CMICmdArgValOptionLong() {
77 // Tidy up
78 Destroy();
Deepak Panickal6f9c4682014-05-16 10:51:01 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081//++
82//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000083// Details: Tear down resources used by *this object.
84// Type: Method.
85// Args: None.
86// Return: None.
87// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000088//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000089void CMICmdArgValOptionLong::Destroy() {
90 // Tidy up
91 VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
92 while (it != m_vecArgsExpected.end()) {
93 CMICmdArgValBase *pOptionObj = *it;
94 delete pOptionObj;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 // Next
97 ++it;
98 }
99 m_vecArgsExpected.clear();
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102//++
103//------------------------------------------------------------------------------------
104// Details: Parse the command's argument options string and try to extract the
105// long
Zachary Turner1d6af022014-11-17 18:06:21 +0000106// argument *this argument type is looking for.
107// Type: Overridden.
108// Args: vwArgContext - (RW) The command's argument options string.
109// Return: MIstatus::success - Functional succeeded.
110// MIstatus::failure - Functional failed.
111// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000112//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113bool CMICmdArgValOptionLong::Validate(CMICmdArgContext &vwArgContext) {
114 if (vwArgContext.IsEmpty())
115 return m_bMandatory ? MIstatus::failure : MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (vwArgContext.GetNumberArgsPresent() == 1) {
118 const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
119 if (IsArgLongOption(rArg) && ArgNameMatch(rArg)) {
120 m_bFound = true;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (!vwArgContext.RemoveArg(rArg))
123 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 if (m_nExpectingNOptions == 0) {
126 m_bValid = true;
127 return MIstatus::success;
128 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 m_bIsMissingOptions = true;
131 return MIstatus::failure;
132 } else
133 return MIstatus::failure;
134 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 // More than one option...
137 MIuint nArgIndex = 0;
138 const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
139 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
140 while (it != vecOptions.end()) {
141 const CMIUtilString &rArg(*it);
142 if (IsArgOptionCorrect(rArg) && ArgNameMatch(rArg)) {
143 m_bFound = true;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 if (!vwArgContext.RemoveArg(rArg))
146 return MIstatus::failure;
Zachary Turner1d6af022014-11-17 18:06:21 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 if (m_nExpectingNOptions != 0) {
149 if (ExtractExpectedOptions(vwArgContext, nArgIndex)) {
150 m_bValid = true;
151 return MIstatus::success;
Zachary Turner1d6af022014-11-17 18:06:21 +0000152 }
153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 m_bIsMissingOptions = true;
155 return MIstatus::failure;
156 } else {
157 m_bValid = true;
158 return MIstatus::success;
159 }
Zachary Turner1d6af022014-11-17 18:06:21 +0000160 }
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 // Next
163 ++it;
164 ++nArgIndex;
165 }
166
167 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170//++
171//------------------------------------------------------------------------------------
172// Details: Parse the text following *this argument and extract the options the
173// values of
174// CMICmdArgValListBase::m_eArgType forming argument objects for each
175// of those
Zachary Turner1d6af022014-11-17 18:06:21 +0000176// options extracted.
177// Type: Method.
178// Args: vrwTxt - (RW) The command's argument options string.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179// nArgIndex - (R) The Nth arg position in argument context from
180// the left.
Zachary Turner1d6af022014-11-17 18:06:21 +0000181// Return: MIstatus::success - Functional succeeded.
182// MIstatus::failure - Functional failed.
183// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000184//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185bool CMICmdArgValOptionLong::ExtractExpectedOptions(CMICmdArgContext &vrwTxt,
186 const MIuint nArgIndex) {
187 CMIUtilString::VecString_t vecOptions;
188 MIuint nOptionsPresent = 0;
189 if ((m_eExpectingOptionType != eArgValType_StringQuoted) &&
190 (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
191 (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath))
192 nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split(" ", vecOptions);
193 else
194 nOptionsPresent =
195 vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes(" ", vecOptions);
196 if (nOptionsPresent == 0)
197 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 MIuint nArgIndexCnt = 0;
200 MIuint nTypeCnt = 0;
201 MIuint nTypeCnt2 = 0;
202 MIuint nFoundNOptionsCnt = 0;
203 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
204 while (it != vecOptions.end()) {
205 // Move to the Nth argument position from left before do validation/checking
206 if (nArgIndexCnt++ == nArgIndex) {
207 nTypeCnt++;
208 const CMIUtilString &rOption(*it);
209 if (IsExpectedCorrectType(rOption, m_eExpectingOptionType)) {
210 nTypeCnt2++;
211 CMICmdArgValBase *pOptionObj =
212 CreationObj(rOption, m_eExpectingOptionType);
213 if ((pOptionObj != nullptr) &&
214 vrwTxt.RemoveArgAtPos(rOption, nArgIndex)) {
215 nFoundNOptionsCnt++;
216 m_vecArgsExpected.push_back(pOptionObj);
Zachary Turner1d6af022014-11-17 18:06:21 +0000217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 // Is the sequence 'options' of same type broken. Expecting the same type
221 // until the
222 // next argument.
223 if (nTypeCnt != nTypeCnt2)
Zachary Turner1d6af022014-11-17 18:06:21 +0000224 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 if (nFoundNOptionsCnt == m_nExpectingNOptions)
227 return MIstatus::success;
228 }
229
230 // Next
231 ++it;
232 }
233 if (nFoundNOptionsCnt != m_nExpectingNOptions)
234 return MIstatus::failure;
235
236 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239//++
240//------------------------------------------------------------------------------------
241// Details: Examine the string and determine if it is a valid long type option
242// argument.
Zachary Turner1d6af022014-11-17 18:06:21 +0000243// Long type argument looks like --someLongOption.
244// Type: Method.
245// Args: vrTxt - (R) Some text.
246// Return: bool - True = yes valid arg, false = no.
247// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000248//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249bool CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const {
250 const bool bHavePosSlash = (vrTxt.find('/') != std::string::npos);
251 const bool bHaveBckSlash = (vrTxt.find('\\') != std::string::npos);
252 if (bHavePosSlash || bHaveBckSlash)
253 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 const size_t nPos = vrTxt.find("--");
256 if (nPos != 0)
257 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 if (vrTxt.length() < 3)
260 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 const CMIUtilString strArg = vrTxt.substr(2);
263 if (strArg.IsNumber())
264 return false;
Zachary Turner1d6af022014-11-17 18:06:21 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 return true;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269//++
270//------------------------------------------------------------------------------------
271// Details: Examine the string and determine if it is a valid long type option
272// argument.
Zachary Turner1d6af022014-11-17 18:06:21 +0000273// Long type argument looks like --someLongOption.
274// Type: Overideable.
275// Args: vrTxt - (R) Some text.
276// Return: bool - True = yes valid arg, false = no.
277// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000278//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279bool CMICmdArgValOptionLong::IsArgOptionCorrect(
280 const CMIUtilString &vrTxt) const {
281 return IsArgLongOption(vrTxt);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000282}
283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284//++
285//------------------------------------------------------------------------------------
286// Details: Does the argument name of the argument being parsed ATM match the
287// name of
Zachary Turner1d6af022014-11-17 18:06:21 +0000288// *this argument object.
289// Type: Method.
290// Args: vrTxt - (R) Some text.
291// Return: bool - True = yes arg name matched, false = no.
292// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000293//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294bool CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const {
295 const CMIUtilString strArg = vrTxt.substr(2);
296 return (strArg == GetName());
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000297}
298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299//++
300//------------------------------------------------------------------------------------
301// Details: Retrieve the list of CMICmdArgValBase derived option objects found
302// following
303// *this long option argument. For example "list-thread-groups [
304// --recurse 1 ]"
Zachary Turner1d6af022014-11-17 18:06:21 +0000305// where 1 is the list of expected option to follow.
306// Type: Method.
307// Args: None.
308// Return: CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
309// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000310//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000311const CMICmdArgValListBase::VecArgObjPtr_t &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312CMICmdArgValOptionLong::GetExpectedOptions() const {
313 return m_vecArgsExpected;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000314}