blob: 3824fe19188a8203ed2ada6cd40758d3b9652176 [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) {
Ilia Ka3174852016-09-22 05:08:41 +0000187 CMIUtilString::VecString_t vecOptions = vrwTxt.GetArgs();
188 if (vecOptions.size() == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 MIuint nArgIndexCnt = 0;
192 MIuint nTypeCnt = 0;
193 MIuint nTypeCnt2 = 0;
194 MIuint nFoundNOptionsCnt = 0;
195 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
196 while (it != vecOptions.end()) {
197 // Move to the Nth argument position from left before do validation/checking
198 if (nArgIndexCnt++ == nArgIndex) {
199 nTypeCnt++;
200 const CMIUtilString &rOption(*it);
201 if (IsExpectedCorrectType(rOption, m_eExpectingOptionType)) {
202 nTypeCnt2++;
203 CMICmdArgValBase *pOptionObj =
204 CreationObj(rOption, m_eExpectingOptionType);
205 if ((pOptionObj != nullptr) &&
206 vrwTxt.RemoveArgAtPos(rOption, nArgIndex)) {
207 nFoundNOptionsCnt++;
208 m_vecArgsExpected.push_back(pOptionObj);
Zachary Turner1d6af022014-11-17 18:06:21 +0000209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 // Is the sequence 'options' of same type broken. Expecting the same type
213 // until the
214 // next argument.
215 if (nTypeCnt != nTypeCnt2)
Zachary Turner1d6af022014-11-17 18:06:21 +0000216 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (nFoundNOptionsCnt == m_nExpectingNOptions)
219 return MIstatus::success;
220 }
221
222 // Next
223 ++it;
224 }
225 if (nFoundNOptionsCnt != m_nExpectingNOptions)
226 return MIstatus::failure;
227
228 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231//++
232//------------------------------------------------------------------------------------
233// Details: Examine the string and determine if it is a valid long type option
234// argument.
Zachary Turner1d6af022014-11-17 18:06:21 +0000235// Long type argument looks like --someLongOption.
236// Type: Method.
237// Args: vrTxt - (R) Some text.
238// Return: bool - True = yes valid arg, false = no.
239// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000240//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241bool CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const {
242 const bool bHavePosSlash = (vrTxt.find('/') != std::string::npos);
243 const bool bHaveBckSlash = (vrTxt.find('\\') != std::string::npos);
244 if (bHavePosSlash || bHaveBckSlash)
245 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 const size_t nPos = vrTxt.find("--");
248 if (nPos != 0)
249 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (vrTxt.length() < 3)
252 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 const CMIUtilString strArg = vrTxt.substr(2);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000255 return !strArg.IsNumber();
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000256}
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258//++
259//------------------------------------------------------------------------------------
260// Details: Examine the string and determine if it is a valid long type option
261// argument.
Zachary Turner1d6af022014-11-17 18:06:21 +0000262// Long type argument looks like --someLongOption.
263// Type: Overideable.
264// Args: vrTxt - (R) Some text.
265// Return: bool - True = yes valid arg, false = no.
266// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000267//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268bool CMICmdArgValOptionLong::IsArgOptionCorrect(
269 const CMIUtilString &vrTxt) const {
270 return IsArgLongOption(vrTxt);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000271}
272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273//++
274//------------------------------------------------------------------------------------
275// Details: Does the argument name of the argument being parsed ATM match the
276// name of
Zachary Turner1d6af022014-11-17 18:06:21 +0000277// *this argument object.
278// Type: Method.
279// Args: vrTxt - (R) Some text.
280// Return: bool - True = yes arg name matched, false = no.
281// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000282//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283bool CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const {
284 const CMIUtilString strArg = vrTxt.substr(2);
285 return (strArg == GetName());
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000286}
287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288//++
289//------------------------------------------------------------------------------------
290// Details: Retrieve the list of CMICmdArgValBase derived option objects found
291// following
292// *this long option argument. For example "list-thread-groups [
293// --recurse 1 ]"
Zachary Turner1d6af022014-11-17 18:06:21 +0000294// where 1 is the list of expected option to follow.
295// Type: Method.
296// Args: None.
297// Return: CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
298// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000299//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000300const CMICmdArgValListBase::VecArgObjPtr_t &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301CMICmdArgValOptionLong::GetExpectedOptions() const {
302 return m_vecArgsExpected;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000303}