1 //===-- MICmdArgValString.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: MICmdArgValString.cpp
13 // Overview: CMICmdArgValString implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdArgValString.h"
24 #include "MICmdArgContext.h"
26 //++ ------------------------------------------------------------------------------------
27 // Details: CMICmdArgValString constructor.
33 CMICmdArgValString::CMICmdArgValString( void )
34 : m_bHandleQuotedString( false )
35 , m_bAcceptNumbers( false )
36 , m_bHandleDirPaths( false )
37 , m_bHandleAnything( false )
41 //++ ------------------------------------------------------------------------------------
42 // Details: CMICmdArgValString constructor.
44 // Args: vbAnything - (R) True = Parse a string and accept anything, false = do not accept anything.
48 CMICmdArgValString::CMICmdArgValString( const bool vbAnything )
49 : m_bHandleQuotedString( false )
50 , m_bAcceptNumbers( false )
51 , m_bHandleDirPaths( false )
52 , m_bHandleAnything( vbAnything )
56 //++ ------------------------------------------------------------------------------------
57 // Details: CMICmdArgValString constructor.
59 // Args: vbHandleQuotes - (R) True = Parse a string surrounded by quotes spaces are not delimitors, false = only text up to next delimiting space character.
60 // vbAcceptNumbers - (R) True = Parse a string and accept as a number if number, false = numbers not recognised as string types.
61 // vbHandleDirPaths - (R) True = Parse a string and accept as a file path if a path, false = file paths are not recognised as string types.
65 CMICmdArgValString::CMICmdArgValString( const bool vbHandleQuotes, const bool vbAcceptNumbers, const bool vbHandleDirPaths )
66 : m_bHandleQuotedString( vbHandleQuotes )
67 , m_bAcceptNumbers( vbAcceptNumbers )
68 , m_bHandleDirPaths( vbHandleDirPaths )
69 , m_bHandleAnything( false )
73 //++ ------------------------------------------------------------------------------------
74 // Details: CMICmdArgValString constructor.
76 // Args: vrArgName - (R) Argument's name to search by.
77 // vbMandatory - (R) True = Yes must be present, false = optional argument.
78 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
79 // vbHandleQuotes - (R) True = Parse a string surrounded by quotes spaces are not delimitors, false = only text up to next delimiting space character. (Dflt = false)
80 // vbAcceptNumbers - (R) True = Parse a string and accept as a number if number, false = numbers not recognised as string types. (Dflt = false)
84 CMICmdArgValString::CMICmdArgValString( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd, const bool vbHandleQuotes /* = false */, const bool vbAcceptNumbers /* = false */ )
85 : CMICmdArgValBaseTemplate( vrArgName, vbMandatory, vbHandleByCmd )
86 , m_bHandleQuotedString( vbHandleQuotes )
87 , m_bAcceptNumbers( vbAcceptNumbers )
88 , m_bHandleDirPaths( false )
89 , m_bHandleAnything( false )
93 //++ ------------------------------------------------------------------------------------
94 // Details: CMICmdArgValString destructor.
100 CMICmdArgValString::~CMICmdArgValString( void )
104 //++ ------------------------------------------------------------------------------------
105 // Details: Parse the command's argument options string and try to extract the value *this
106 // argument is looking for.
108 // Args: vrwArgContext - (RW) The command's argument options string.
109 // Return: MIstatus::success - Functional succeeded.
110 // MIstatus::failure - Functional failed.
113 bool CMICmdArgValString::Validate( CMICmdArgContext & vrwArgContext )
115 if( vrwArgContext.IsEmpty() )
116 return MIstatus::success;
118 if( m_bHandleQuotedString )
119 return (ValidateQuotedText( vrwArgContext ) || ValidateQuotedTextEmbedded( vrwArgContext ) );
121 return ValidateSingleText( vrwArgContext );
124 //++ ------------------------------------------------------------------------------------
125 // Details: Parse the command's argument options string and try to extract only the next
126 // word delimited by the next space.
128 // Args: vrwArgContext - (RW) The command's argument options string.
129 // Return: MIstatus::success - Functional succeeded.
130 // MIstatus::failure - Functional failed.
133 bool CMICmdArgValString::ValidateSingleText( CMICmdArgContext & vrwArgContext )
135 if( vrwArgContext.GetNumberArgsPresent() == 1 )
137 const CMIUtilString & rArg( vrwArgContext.GetArgsLeftToParse() );
138 if( IsStringArg( rArg ) )
143 vrwArgContext.RemoveArg( rArg );
144 return MIstatus::success;
147 return MIstatus::failure;
150 // More than one option...
151 const CMIUtilString::VecString_t vecOptions( vrwArgContext.GetArgs() );
152 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
153 while( it != vecOptions.end() )
155 const CMIUtilString & rArg( *it );
156 if( IsStringArg( rArg ) )
160 if( vrwArgContext.RemoveArg( rArg ) )
164 return MIstatus::success;
167 return MIstatus::failure;
174 return MIstatus::failure;
177 //++ ------------------------------------------------------------------------------------
178 // Details: Parse the command's argument options string and try to extract all the words
179 // between quotes then delimited by the next space. Can fall through to
180 // ValidateSingleText() or ValidateQuotedQuotedTextEmbedded().
182 // Args: vrwArgContext - (RW) The command's argument options string.
183 // Return: MIstatus::success - Functional succeeded.
184 // MIstatus::failure - Functional failed.
187 bool CMICmdArgValString::ValidateQuotedText( CMICmdArgContext & vrwArgContext )
189 // CODETAG_QUOTEDTEXT_SIMILAR_CODE
190 CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
191 const MIchar cQuote = '"';
193 // Look for first quote of two
194 MIint nPos = strOptions.find( cQuote );
195 if( nPos == (MIint) std::string::npos )
196 return ValidateSingleText( vrwArgContext );
198 // Is one and only quote at end of the string
199 const MIint nLen = strOptions.length();
200 if( nPos == (MIint)(nLen - 1) )
201 return MIstatus::failure;
203 // Quote must be the first character in the string or be preceeded by a space
204 if( (nPos > 0) && (strOptions[ nPos - 1 ] != ' ') )
205 return MIstatus::failure;
207 // Need to find the other quote
208 const MIint nPos2 = strOptions.rfind( cQuote );
209 if( nPos2 == (MIint) std::string::npos )
210 return MIstatus::failure;
212 // Is there quotes surrounding string formatting embedded quotes
213 if( IsStringArgQuotedQuotedTextEmbedded( strOptions ) )
214 return ValidateQuotedQuotedTextEmbedded( vrwArgContext );
216 // Make sure not same back quote, need two quotes
218 return MIstatus::failure;
220 // Extract quoted text
221 const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPos2 - nPos + 1 ).c_str();
222 if( vrwArgContext.RemoveArg( strQuotedTxt ) )
226 m_argValue = strOptions.substr( nPos + 1, nPos2 - nPos - 1 ).c_str();
227 return MIstatus::success;
230 return MIstatus::failure;
233 //++ ------------------------------------------------------------------------------------
234 // Details: Parse the command's argument options string and try to extract all the words
235 // between quotes then delimited by the next space. If there any string format
236 // characters '\\' used to embed quotes these are ignored i.e. "\\\"%5d\\\""
237 // becomes "%5d". Can fall through to ValidateQuotedText().
239 // Args: vrwArgContext - (RW) The command's argument options string.
240 // Return: MIstatus::success - Functional succeeded.
241 // MIstatus::failure - Functional failed.
244 bool CMICmdArgValString::ValidateQuotedTextEmbedded( CMICmdArgContext & vrwArgContext )
246 // CODETAG_QUOTEDTEXT_SIMILAR_CODE
247 CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
248 const MIchar cBckSlash = '\\';
249 const MIint nPos = strOptions.find( cBckSlash );
250 if( nPos == (MIint) std::string::npos )
251 return ValidateQuotedText( vrwArgContext );
253 // Back slash must be the first character in the string or be preceeded by a space
255 const MIchar cSpace = ' ';
256 if( (nPos > 0) && (strOptions[ nPos - 1 ] != cSpace) )
257 return MIstatus::failure;
259 // Need to find the other back slash
260 const MIint nPos2 = strOptions.rfind( cBckSlash );
261 if( nPos2 == (MIint) std::string::npos )
262 return MIstatus::failure;
264 // Make sure not same back slash, need two slashs
266 return MIstatus::failure;
268 // Look for the two quotes
269 const MIint nLen = strOptions.length();
270 const MIchar cQuote = '"';
271 const MIint nPosQuote1 = nPos + 1;
272 const MIint nPosQuote2 = (nPos2 < nLen) ? nPos2 + 1 : nPos2;
273 if( (nPosQuote1 != nPosQuote2) &&
274 (strOptions[ nPosQuote1 ] != cQuote) && (strOptions[ nPosQuote2 ] != cQuote) )
275 return MIstatus::failure;
277 // Extract quoted text
278 const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPosQuote2 - nPos + 1 ).c_str();
279 if( vrwArgContext.RemoveArg( strQuotedTxt ) )
283 m_argValue = strQuotedTxt;
284 return MIstatus::success;
287 return MIstatus::failure;
290 //++ ------------------------------------------------------------------------------------
291 // Details: Parse the command's argument options string and try to extract all the words
292 // between quotes then delimited by the next space. If there any string format
293 // characters '\\' used to embed quotes these are ignored i.e. "\\\"%5d\\\""
296 // Args: vrwArgContext - (RW) The command's argument options string.
297 // Return: MIstatus::success - Functional succeeded.
298 // MIstatus::failure - Functional failed.
301 bool CMICmdArgValString::ValidateQuotedQuotedTextEmbedded( CMICmdArgContext & vrwArgContext )
303 // CODETAG_QUOTEDTEXT_SIMILAR_CODE
304 CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
305 const MIint nPos = strOptions.find( "\"\\\"" );
306 if( nPos == (MIint) std::string::npos )
307 return MIstatus::failure;
309 const MIint nPos2 = strOptions.rfind( "\\\"\"" );
310 if( nPos2 == (MIint) std::string::npos )
311 return MIstatus::failure;
313 const MIint nLen = strOptions.length();
314 if( (nLen > 5) && ((nPos + 2) == (nPos2 - 2)) )
315 return MIstatus::failure;
317 // Quote must be the first character in the string or be preceeded by a space
319 const MIchar cSpace = ' ';
320 if( (nPos > 0) && (strOptions[ nPos - 1 ] != cSpace) )
321 return MIstatus::failure;
323 // Extract quoted text
324 const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPos2 - nPos + 3 ).c_str();
325 if( vrwArgContext.RemoveArg( strQuotedTxt ) )
329 m_argValue = strQuotedTxt;
330 return MIstatus::success;
333 return MIstatus::failure;
336 //++ ------------------------------------------------------------------------------------
337 // Details: Examine the string and determine if it is a valid string type argument.
339 // Args: vrTxt - (R) Some text.
340 // Return: bool - True = yes valid arg, false = no.
343 bool CMICmdArgValString::IsStringArg( const CMIUtilString & vrTxt ) const
345 if( m_bHandleQuotedString )
346 return (IsStringArgQuotedText( vrTxt ) ||
347 IsStringArgQuotedTextEmbedded( vrTxt ) ||
348 IsStringArgQuotedQuotedTextEmbedded( vrTxt ) ||
349 IsStringArgSingleText( vrTxt ) ); // Still test for this as could just be one word still
351 return IsStringArgSingleText( vrTxt );
354 //++ ------------------------------------------------------------------------------------
355 // Details: Examine the string and determine if it is a valid string type argument or
356 // option value. If the string looks like a long option, short option, a thread
357 // group ID or just a number it is rejected as a string type value. There is an
358 // option to allow the string to accept a number as a string type.
360 // Args: vrTxt - (R) Some text.
361 // Return: bool - True = yes valid argument value, false = something else.
364 bool CMICmdArgValString::IsStringArgSingleText( const CMIUtilString & vrTxt ) const
366 // Accept anything as string word
367 if( m_bHandleAnything )
370 if( !m_bHandleDirPaths )
372 // Look for directory file paths, if found reject
373 const bool bHavePosSlash = (vrTxt.find_first_of( "/" ) != std::string::npos);
374 const bool bHaveBckSlash = (vrTxt.find_first_of( "\\" ) != std::string::npos);
375 if( bHavePosSlash || bHaveBckSlash )
379 // Look for --someLongOption, if found reject
380 if( 0 == vrTxt.find( "--" ) )
383 // Look for -f type short options, if found reject
384 if( (0 == vrTxt.find( "-" )) && (vrTxt.length() == 2) )
387 // Look for thread group i1 i2 i3...., if found reject
388 if( (vrTxt.find( "i" ) == 0) && ::isdigit( vrTxt[ 1 ]) )
391 // Look for numbers, if found reject
392 if( !m_bAcceptNumbers && vrTxt.IsNumber() )
398 //++ ------------------------------------------------------------------------------------
399 // Details: Examine the string and determine if it is a valid string type argument.
400 // Take into account quotes surrounding the text. Note this function falls
401 // through to IsStringArgSingleText() should the criteria match fail.
403 // Args: vrTxt - (R) Some text.
404 // Return: bool - True = yes valid arg, false = no.
407 bool CMICmdArgValString::IsStringArgQuotedText( const CMIUtilString & vrTxt ) const
409 // CODETAG_QUOTEDTEXT_SIMILAR_CODE
410 const MIchar cQuote = '"';
411 const MIint nPos = vrTxt.find( cQuote );
412 if( nPos == (MIint) std::string::npos )
415 // Is one and only quote at end of the string
416 if( nPos == (MIint)(vrTxt.length() - 1) )
419 // Quote must be the first character in the string or be preceeded by a space
420 // Also check for embedded string formating quote
421 const MIchar cBckSlash = '\\';
422 const MIchar cSpace = ' ';
423 if( (nPos > 1) && (vrTxt[ nPos - 1 ] == cBckSlash) && (vrTxt[ nPos - 2 ] != cSpace) )
427 if( (nPos > 0) && (vrTxt[ nPos - 1 ] != cSpace) )
430 // Need to find the other quote
431 const MIint nPos2 = vrTxt.rfind( cQuote );
432 if( nPos2 == (MIint) std::string::npos )
435 // Make sure not same quote, need two quotes
437 return MIstatus::failure;
442 //++ ------------------------------------------------------------------------------------
443 // Details: Examine the string and determine if it is a valid string type argument.
444 // Take into account quotes surrounding the text. Take into account string format
445 // embedded quotes surrounding the text i.e. "\\\"%5d\\\"". Note this function falls
446 // through to IsStringArgQuotedText() should the criteria match fail.
448 // Args: vrTxt - (R) Some text.
449 // Return: bool - True = yes valid arg, false = no.
452 bool CMICmdArgValString::IsStringArgQuotedTextEmbedded( const CMIUtilString & vrTxt ) const
454 // CODETAG_QUOTEDTEXT_SIMILAR_CODE
455 const MIchar cBckSlash = '\\';
456 const MIint nPos = vrTxt.find( cBckSlash );
457 if( nPos == (MIint) std::string::npos )
460 // Slash must be the first character in the string or be preceeded by a space
461 const MIchar cSpace = ' ';
462 if( (nPos > 0) && (vrTxt[ nPos - 1 ] != cSpace) )
465 // Need to find the other matching slash
466 const MIint nPos2 = vrTxt.rfind( cBckSlash );
467 if( nPos2 == (MIint) std::string::npos )
470 // Make sure not same back slash, need two slashs
472 return MIstatus::failure;
477 //++ ------------------------------------------------------------------------------------
478 // Details: Examine the string and determine if it is a valid string type argument.
479 // Take into account quotes surrounding the text. Take into account string format
480 // embedded quotes surrounding the text i.e. "\\\"%5d\\\"". Note this function falls
481 // through to IsStringArgQuotedTextEmbedded() should the criteria match fail.
483 // Args: vrTxt - (R) Some text.
484 // Return: bool - True = yes valid arg, false = no.
487 bool CMICmdArgValString::IsStringArgQuotedQuotedTextEmbedded( const CMIUtilString & vrTxt ) const
489 const MIint nPos = vrTxt.find( "\"\\\"" );
490 if( nPos == (MIint) std::string::npos )
493 const MIint nPos2 = vrTxt.rfind( "\\\"\"" );
494 if( nPos2 == (MIint) std::string::npos )
497 const MIint nLen = vrTxt.length();
498 if( (nLen > 5) && ((nPos + 2) == (nPos2 - 2)) )