1 //===-- MICmdArgValFile.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: MICmdArgValFile.cpp
13 // Overview: CMICmdArgValFile implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdArgValFile.h"
24 #include "MICmdArgContext.h"
26 //++ ------------------------------------------------------------------------------------
27 // Details: CMICmdArgValFile constructor.
33 CMICmdArgValFile::CMICmdArgValFile( void )
37 //++ ------------------------------------------------------------------------------------
38 // Details: CMICmdArgValFile constructor.
40 // Args: vrArgName - (R) Argument's name to search by.
41 // vbMandatory - (R) True = Yes must be present, false = optional argument.
42 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
46 CMICmdArgValFile::CMICmdArgValFile( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd )
47 : CMICmdArgValBaseTemplate( vrArgName, vbMandatory, vbHandleByCmd )
51 //++ ------------------------------------------------------------------------------------
52 // Details: CMICmdArgValFile destructor.
58 CMICmdArgValFile::~CMICmdArgValFile( void )
62 //++ ------------------------------------------------------------------------------------
63 // Details: Parse the command's argument options string and try to extract the value *this
64 // argument is looking for.
66 // Args: vwArgContext - (R) The command's argument options string.
67 // Return: MIstatus::success - Functional succeeded.
68 // MIstatus::failure - Functional failed.
71 bool CMICmdArgValFile::Validate( CMICmdArgContext & vwArgContext )
73 if( vwArgContext.IsEmpty() )
74 return MIstatus::success;
76 // The GDB/MI spec suggests there is only parameter
78 if( vwArgContext.GetNumberArgsPresent() == 1 )
80 const CMIUtilString & rFile( vwArgContext.GetArgsLeftToParse() );
81 if( IsFilePath( rFile ) )
85 m_argValue = rFile.Trim( '"' );
86 vwArgContext.RemoveArg( rFile );
87 return MIstatus::success;
90 return MIstatus::failure;
93 // In reality there are more than one option, if so the file option
94 // is the last one (don't handle that here - find the best looking one)
95 const CMIUtilString::VecString_t vecOptions( vwArgContext.GetArgs() );
96 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
97 while( it != vecOptions.end() )
99 const CMIUtilString & rTxt( *it );
100 if( IsFilePath( rTxt ) )
104 if( vwArgContext.RemoveArg( rTxt ) )
107 m_argValue = rTxt.Trim( '"' );
108 return MIstatus::success;
111 return MIstatus::success;
118 return MIstatus::failure;
121 //++ ------------------------------------------------------------------------------------
122 // Details: Given some text extract the file name path from it. If a space is found in
123 // path done return the path surrounded in quotes.
125 // Args: vrTxt - (R) The text to extract the file name path from.
126 // Return: CMIUtilString - File name and or path.
129 CMIUtilString CMICmdArgValFile::GetFileNamePath( const CMIUtilString & vrTxt ) const
131 CMIUtilString fileNamePath( vrTxt );
133 // Look for a space in the path
134 const MIchar cSpace = ' ';
135 const MIint nPos = fileNamePath.find( cSpace );
136 if( nPos != (MIint) std::string::npos )
137 fileNamePath = CMIUtilString::Format( "\"%s\"", fileNamePath.c_str() );
142 //++ ------------------------------------------------------------------------------------
143 // Details: Examine the string and determine if it is a valid file name path.
145 // Args: vrFileNamePath - (R) File's name and directory path.
146 // Return: bool - True = yes valid file path, false = no.
149 bool CMICmdArgValFile::IsFilePath( const CMIUtilString & vrFileNamePath ) const
151 if( vrFileNamePath.empty() )
154 const bool bHavePosSlash = (vrFileNamePath.find_first_of( "/" ) != std::string::npos);
155 const bool bHaveBckSlash = (vrFileNamePath.find_first_of( "\\" ) != std::string::npos);
157 // Look for --someLongOption
158 MIint nPos = vrFileNamePath.find_first_of( "--" );
159 const bool bLong = (nPos == 0);
163 // Look for -f type short parameters
164 nPos = vrFileNamePath.find_first_of( "-" );
165 const bool bShort = (nPos == 0);
169 // Look for i1 i2 i3....
170 nPos = vrFileNamePath.find_first_of( "i" );
171 const bool bFoundI1 = ((nPos == 0) && (::isdigit( vrFileNamePath[ 1 ] )) );
175 const bool bValidChars = IsValidChars( vrFileNamePath );
176 if( bValidChars || bHavePosSlash || bHaveBckSlash )
182 //++ ------------------------------------------------------------------------------------
183 // Details: Determine if the path contains valid characters for a file path. Letters can be
184 // either upper or lower case.
186 // Args: vrText - (R) The text data to examine.
187 // Return: bool - True = yes valid, false = one or more chars is valid.
190 bool CMICmdArgValFile::IsValidChars( const CMIUtilString & vrText ) const
192 const MIchar * pPtr = const_cast< MIchar * >( vrText.c_str() );
193 for( MIuint i = 0; i < vrText.length(); i++, pPtr++ )
195 const MIchar c = *pPtr;
196 if( ::isalnum( (int) c ) == 0 )
198 if( (c != '.') && (c != '-') && (c != '_') )