1 //===-- MICmnArgContext.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: MICmnArgContext.cpp
13 // Overview: CMICmdArgContext implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdArgContext.h"
25 //++ ------------------------------------------------------------------------------------
26 // Details: CMICmdArgContext constructor.
32 CMICmdArgContext::CMICmdArgContext( void )
33 : m_constCharSpace( ' ' )
34 , m_constStrSpace( " " )
38 //++ ------------------------------------------------------------------------------------
39 // Details: CMICmdArgContext constructor.
41 // Args: vrCmdLineArgsRaw - (R) The text description of the arguments options.
45 CMICmdArgContext::CMICmdArgContext( const CMIUtilString & vrCmdLineArgsRaw )
46 : m_strCmdArgsAndOptions( vrCmdLineArgsRaw )
47 , m_constCharSpace( ' ' )
48 , m_constStrSpace( " " )
52 //++ ------------------------------------------------------------------------------------
53 // Details: CMICmdArgContext destructor.
59 CMICmdArgContext::~CMICmdArgContext( void )
63 //++ ------------------------------------------------------------------------------------
64 // Details: Retrieve the remainder of the command's argument options left to parse.
67 // Return: CMIUtilString & - Argument options text.
70 const CMIUtilString & CMICmdArgContext::GetArgsLeftToParse( void ) const
72 return m_strCmdArgsAndOptions;
75 //++ ------------------------------------------------------------------------------------
76 // Details: Ask if this arguments string has any arguments.
79 // Return: bool - True = Has one or more arguments present, false = no arguments.
82 bool CMICmdArgContext::IsEmpty( void ) const
84 return m_strCmdArgsAndOptions.empty();
87 //++ ------------------------------------------------------------------------------------
88 // Details: Remove the argument from the options text and any space after the argument
91 // Args: vArg - (R) The name of the argument.
92 // Return: MIstatus::success - Functional succeeded.
93 // MIstatus::failure - Functional failed.
96 bool CMICmdArgContext::RemoveArg( const CMIUtilString & vArg )
99 return MIstatus::success;
101 const MIuint nLen = vArg.length();
102 const MIuint nLenCntxt = m_strCmdArgsAndOptions.length();
103 if( nLen > nLenCntxt )
104 return MIstatus::failure;
106 MIuint nExtraSpace = 0;
107 MIint nPos = m_strCmdArgsAndOptions.find( vArg );
110 if( nPos == (MIint) std::string::npos )
111 return MIstatus::success;
116 if( m_strCmdArgsAndOptions[ nPos - 1 ] == m_constCharSpace )
122 const MIuint nEnd = nPos + nLen;
127 if( nEnd < nLenCntxt )
129 if( m_strCmdArgsAndOptions[ nEnd ] == m_constCharSpace )
142 nPos = m_strCmdArgsAndOptions.find( vArg, nEnd );
145 const MIuint nPosEnd = nLen + nExtraSpace;
146 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace( nPos, nPosEnd, "" ).c_str();
147 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
149 return MIstatus::success;
152 //++ ------------------------------------------------------------------------------------
153 // Details: Remove the argument at the Nth word position along in the context string.
154 // Any space after the argument is removed if applicable. A search is not
155 // performed as there may be more than one vArg with the same 'name' in the
158 // Args: vArg - (R) The name of the argument.
159 // nArgIndex - (R) The word count position to which to remove the vArg word.
160 // Return: MIstatus::success - Functional succeeded.
161 // MIstatus::failure - Functional failed.
164 bool CMICmdArgContext::RemoveArgAtPos( const CMIUtilString & vArg, const MIuint nArgIndex )
166 MIuint nWordIndex = 0;
167 CMIUtilString strBuildContextUp;
168 const CMIUtilString::VecString_t vecWords( GetArgs() );
169 const bool bSpaceRequired( GetNumberArgsPresent() > 2 );
171 CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
172 const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
175 const CMIUtilString & rWord( *it );
176 if( nWordIndex++ != nArgIndex )
179 strBuildContextUp += rWord;
181 strBuildContextUp += m_constStrSpace;
185 // If quoted loose quoted text
188 CMIUtilString words = rWord;
189 while( vArg != words )
192 words += m_constStrSpace;
207 m_strCmdArgsAndOptions = strBuildContextUp;
208 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
210 return MIstatus::success;
213 //++ ------------------------------------------------------------------------------------
214 // Details: Retrieve number of arguments or options present in the command's option text.
217 // Return: MIuint - 0 to n arguments present.
220 MIuint CMICmdArgContext::GetNumberArgsPresent( void ) const
222 CMIUtilString::VecString_t vecOptions;
223 return m_strCmdArgsAndOptions.SplitConsiderQuotes( m_constStrSpace, vecOptions );
226 //++ ------------------------------------------------------------------------------------
227 // Details: Retrieve all the arguments or options remaining in *this context.
230 // Return: MIUtilString::VecString_t - List of args remaining.
233 CMIUtilString::VecString_t CMICmdArgContext::GetArgs( void ) const
235 CMIUtilString::VecString_t vecOptions;
236 m_strCmdArgsAndOptions.SplitConsiderQuotes( m_constStrSpace, vecOptions );
240 //++ ------------------------------------------------------------------------------------
241 // Details: Copy assignment operator.
243 // Args: vOther - (R) The variable to copy from.
244 // Return: CMIUtilString & - this object.
247 CMICmdArgContext & CMICmdArgContext::operator= ( const CMICmdArgContext & vOther )
249 if( this != &vOther )
251 m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;