1 //===-- MICmdArgValOptionLong.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MICmdArgValOptionLong.cpp
13 // Overview: CMICmdArgValOptionLong implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdArgValOptionLong.h"
24 #include "MICmdArgContext.h"
26 //++ ------------------------------------------------------------------------------------
27 // Details: CMICmdArgValOptionLong constructor.
33 CMICmdArgValOptionLong::CMICmdArgValOptionLong( void )
34 : m_nExpectingNOptions( 0 )
35 , m_eExpectingOptionType( eArgValType_invalid )
39 //++ ------------------------------------------------------------------------------------
40 // Details: CMICmdArgValOptionLong constructor.
42 // Args: vrArgName - (R) Argument's name to search by.
43 // vbMandatory - (R) True = Yes must be present, false = optional argument.
44 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
48 CMICmdArgValOptionLong::CMICmdArgValOptionLong( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd )
49 : CMICmdArgValListBase( vrArgName, vbMandatory, vbHandleByCmd )
50 , m_nExpectingNOptions( 0 )
51 , m_eExpectingOptionType( eArgValType_invalid )
55 //++ ------------------------------------------------------------------------------------
56 // Details: CMICmdArgValOptionLong constructor.
58 // Args: vrArgName - (R) Argument's name to search by.
59 // vbMandatory - (R) True = Yes must be present, false = optional argument.
60 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
61 // veType - (R) The type of argument to look for and create argument object of a certain type.
62 // vnExpectingNOptions - (R) The number of options expected to read following *this argument.
66 CMICmdArgValOptionLong::CMICmdArgValOptionLong( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd, const ArgValType_e veType, const MIuint vnExpectingNOptions )
67 : CMICmdArgValListBase( vrArgName, vbMandatory, vbHandleByCmd )
68 , m_nExpectingNOptions( vnExpectingNOptions )
69 , m_eExpectingOptionType( veType )
73 //++ ------------------------------------------------------------------------------------
74 // Details: CMICmdArgValOptionLong destructor.
80 CMICmdArgValOptionLong::~CMICmdArgValOptionLong( void )
86 //++ ------------------------------------------------------------------------------------
87 // Details: Tear down resources used by *this object.
93 void CMICmdArgValOptionLong::Destroy( void )
96 VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
97 while( it != m_vecArgsExpected.end() )
99 CMICmdArgValBase * pOptionObj = *it;
105 m_vecArgsExpected.clear();
108 //++ ------------------------------------------------------------------------------------
109 // Details: Parse the command's argument options string and try to extract the long
110 // argument *this argument type is looking for.
112 // Args: vwArgContext - (RW) The command's argument options string.
113 // Return: MIstatus::success - Functional succeeded.
114 // MIstatus::failure - Functional failed.
117 bool CMICmdArgValOptionLong::Validate( CMICmdArgContext & vwArgContext )
119 if( vwArgContext.IsEmpty() )
120 return MIstatus::success;
122 if( vwArgContext.GetNumberArgsPresent() == 1 )
124 const CMIUtilString & rArg( vwArgContext.GetArgsLeftToParse() );
125 if( IsArgLongOption( rArg ) && ArgNameMatch( rArg ) )
129 if( !vwArgContext.RemoveArg( rArg ) )
130 return MIstatus::failure;
132 if( m_nExpectingNOptions == 0 )
135 return MIstatus::success;
138 m_bIsMissingOptions = true;
139 return MIstatus::failure;
142 return MIstatus::failure;
145 // More than one option...
146 MIuint nArgIndex = 0;
147 const CMIUtilString::VecString_t vecOptions( vwArgContext.GetArgs() );
148 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
149 while( it != vecOptions.end() )
151 const CMIUtilString & rArg( *it );
152 if( IsArgOptionCorrect( rArg ) && ArgNameMatch( rArg ) )
156 if( !vwArgContext.RemoveArg( rArg ) )
157 return MIstatus::failure;
159 if( m_nExpectingNOptions != 0 )
161 if( ExtractExpectedOptions( vwArgContext, nArgIndex ) )
164 return MIstatus::success;
167 m_bIsMissingOptions = true;
168 return MIstatus::failure;
173 return MIstatus::success;
182 return MIstatus::failure;
185 //++ ------------------------------------------------------------------------------------
186 // Details: Parse the text following *this argument and extract the options the values of
187 // CMICmdArgValListBase::m_eArgType forming argument objects for each of those
188 // options extracted.
190 // Args: vrwTxt - (RW) The command's argument options string.
191 // nArgIndex - (R) The Nth arg position in argument context from the left.
192 // Return: MIstatus::success - Functional succeeded.
193 // MIstatus::failure - Functional failed.
196 bool CMICmdArgValOptionLong::ExtractExpectedOptions( CMICmdArgContext & vrwTxt, const MIuint nArgIndex )
198 CMIUtilString::VecString_t vecOptions;
199 MIuint nOptionsPresent = 0;
200 if( (m_eExpectingOptionType != eArgValType_StringQuoted) &&
201 (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
202 (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath) )
203 nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split( " ", vecOptions );
205 nOptionsPresent = vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes( " ", vecOptions );
206 if( nOptionsPresent == 0 )
207 return MIstatus::failure;
209 MIuint nArgIndexCnt = 0;
211 MIuint nTypeCnt2 = 0;
212 MIuint nFoundNOptionsCnt = 0;
213 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
214 while( it != vecOptions.end() )
216 // Move to the Nth argument position from left before do validation/checking
217 if( nArgIndexCnt++ == nArgIndex )
220 const CMIUtilString & rOption( *it );
221 if( IsExpectedCorrectType( rOption, m_eExpectingOptionType ) )
224 CMICmdArgValBase * pOptionObj = CreationObj( rOption, m_eExpectingOptionType );
225 if( (pOptionObj != nullptr) && vrwTxt.RemoveArgAtPos( rOption, nArgIndex ) )
228 m_vecArgsExpected.push_back( pOptionObj );
232 // Is the sequence 'options' of same type broken. Expecting the same type until the
234 if( nTypeCnt != nTypeCnt2 )
235 return MIstatus::failure;
237 if( nFoundNOptionsCnt == m_nExpectingNOptions )
238 return MIstatus::success;
244 if( nFoundNOptionsCnt != m_nExpectingNOptions )
245 return MIstatus::failure;
247 return MIstatus::success;
250 //++ ------------------------------------------------------------------------------------
251 // Details: Examine the string and determine if it is a valid long type option argument.
252 // Long type argument looks like --someLongOption.
254 // Args: vrTxt - (R) Some text.
255 // Return: bool - True = yes valid arg, false = no.
258 bool CMICmdArgValOptionLong::IsArgLongOption( const CMIUtilString & vrTxt ) const
260 const bool bHavePosSlash = (vrTxt.find_first_of( "/" ) != std::string::npos);
261 const bool bHaveBckSlash = (vrTxt.find_first_of( "\\" ) != std::string::npos);
262 if( bHavePosSlash || bHaveBckSlash )
265 const MIint nPos = vrTxt.find_first_of( "--" );
269 if( vrTxt.length() < 3 )
272 const CMIUtilString strArg = vrTxt.substr( 2 ).c_str();
273 if( strArg.IsNumber() )
279 //++ ------------------------------------------------------------------------------------
280 // Details: Examine the string and determine if it is a valid long type option argument.
281 // Long type argument looks like --someLongOption.
282 // Type: Overideable.
283 // Args: vrTxt - (R) Some text.
284 // Return: bool - True = yes valid arg, false = no.
287 bool CMICmdArgValOptionLong::IsArgOptionCorrect( const CMIUtilString & vrTxt ) const
289 return IsArgLongOption( vrTxt );
292 //++ ------------------------------------------------------------------------------------
293 // Details: Does the argument name of the argument being parsed ATM match the name of
294 // *this argument object.
296 // Args: vrTxt - (R) Some text.
297 // Return: bool - True = yes arg name matched, false = no.
300 bool CMICmdArgValOptionLong::ArgNameMatch( const CMIUtilString & vrTxt ) const
302 const CMIUtilString strArg = vrTxt.substr( 2 ).c_str();
303 return (strArg == GetName() );
306 //++ ------------------------------------------------------------------------------------
307 // Details: Retrieve the list of CMICmdArgValBase derived option objects found following
308 // *this long option argument. For example "list-thread-groups [ --recurse 1 ]"
309 // where 1 is the list of expected option to follow.
312 // Return: CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
315 const CMICmdArgValListBase::VecArgObjPtr_t & CMICmdArgValOptionLong::GetExpectedOptions( void ) const
317 return m_vecArgsExpected;