]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgContext.cpp
Upgrade our copy of clang, llvm and lldb to 3.5.0 release.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgContext.cpp
1 //===-- MICmnArgContext.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
10 //++
11 // File:                MICmnArgContext.cpp
12 //
13 // Overview:    CMICmdArgContext implementation.
14 //
15 // Environment: Compilers:      Visual C++ 12.
16 //                                                      gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //                              Libraries:      See MIReadmetxt. 
18 //
19 // Copyright:   None.
20 //--
21
22 // In-house headers:
23 #include "MICmdArgContext.h"
24
25 //++ ------------------------------------------------------------------------------------
26 // Details:     CMICmdArgContext constructor.
27 // Type:        Method.
28 // Args:        None.
29 // Return:      None.
30 // Throws:      None.
31 //--
32 CMICmdArgContext::CMICmdArgContext( void )
33 :       m_constCharSpace( ' ' )
34 ,       m_constStrSpace( " " )
35 {
36 }
37
38 //++ ------------------------------------------------------------------------------------
39 // Details:     CMICmdArgContext constructor.
40 // Type:        Method.
41 // Args:        vrCmdLineArgsRaw        - (R) The text description of the arguments options.
42 // Return:      None.
43 // Throws:      None.
44 //--
45 CMICmdArgContext::CMICmdArgContext( const CMIUtilString & vrCmdLineArgsRaw )
46 :       m_strCmdArgsAndOptions( vrCmdLineArgsRaw )
47 ,       m_constCharSpace( ' ' )
48 ,       m_constStrSpace( " " )
49 {
50 }
51
52 //++ ------------------------------------------------------------------------------------
53 // Details:     CMICmdArgContext destructor.
54 // Type:        Method.
55 // Args:        None.
56 // Return:      None.
57 // Throws:      None.
58 //--
59 CMICmdArgContext::~CMICmdArgContext( void )
60 {
61 }
62
63 //++ ------------------------------------------------------------------------------------
64 // Details:     Retrieve the remainder of the command's argument options left to parse.
65 // Type:        Method.
66 // Args:        None.
67 // Return:      CMIUtilString & - Argument options text.
68 // Throws:      None.
69 //--
70 const CMIUtilString & CMICmdArgContext::GetArgsLeftToParse( void ) const
71 {
72         return m_strCmdArgsAndOptions;
73 }
74
75 //++ ------------------------------------------------------------------------------------
76 // Details:     Ask if this arguments string has any arguments.
77 // Type:        Method.
78 // Args:        None.
79 // Return:      bool    - True = Has one or more arguments present, false = no arguments.
80 // Throws:      None.
81 //--
82 bool CMICmdArgContext::IsEmpty( void ) const
83 {
84         return m_strCmdArgsAndOptions.empty();
85 }
86         
87 //++ ------------------------------------------------------------------------------------
88 // Details:     Remove the argument from the options text and any space after the argument
89 //                      if applicable.
90 // Type:        Method.
91 // Args:        vArg    - (R) The name of the argument.
92 // Return:      MIstatus::success - Functional succeeded.
93 //                      MIstatus::failure - Functional failed.
94 // Throws:      None.
95 //--
96 bool CMICmdArgContext::RemoveArg( const CMIUtilString & vArg )
97 {
98         if( vArg.empty() )
99                 return MIstatus::success;
100
101         const MIuint nLen = vArg.length();
102         const MIuint nLenCntxt = m_strCmdArgsAndOptions.length();
103         if( nLen > nLenCntxt )
104                 return MIstatus::failure;
105
106         MIuint nExtraSpace = 0;
107         MIint nPos = m_strCmdArgsAndOptions.find( vArg );
108         while( 1 )
109         {
110                 if( nPos == (MIint) std::string::npos )
111                         return MIstatus::success;
112
113                 bool bPass1 = false;
114                 if( nPos != 0 )
115                 {
116                         if( m_strCmdArgsAndOptions[ nPos - 1 ] == m_constCharSpace )
117                                 bPass1 = true;
118                 }
119                 else
120                         bPass1 = true;
121
122                 const MIuint nEnd = nPos + nLen;
123                 
124                 if( bPass1 )
125                 {
126                         bool bPass2 = false;
127                         if( nEnd < nLenCntxt )
128                         {
129                                 if( m_strCmdArgsAndOptions[ nEnd ] == m_constCharSpace )
130                                 {
131                                         bPass2 = true;
132                                         nExtraSpace = 1;
133                                 }
134                         }
135                         else
136                                 bPass2 = true;
137
138                         if( bPass2 )
139                                 break;
140                 }
141
142                 nPos = m_strCmdArgsAndOptions.find( vArg, nEnd );
143         }
144         
145         const MIuint nPosEnd = nLen + nExtraSpace;
146         m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace( nPos, nPosEnd, "" ).c_str();
147         m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
148
149         return MIstatus::success;
150 }
151
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
156 //                      context string.
157 // Type:        Method.
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.
162 // Throws:      None.
163 //--
164 bool CMICmdArgContext::RemoveArgAtPos( const CMIUtilString & vArg, const MIuint nArgIndex )
165 {
166         MIuint nWordIndex = 0;
167         CMIUtilString strBuildContextUp;
168         const CMIUtilString::VecString_t vecWords( GetArgs() );
169         const bool bSpaceRequired( GetNumberArgsPresent() > 2 );
170
171         CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
172         const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
173         while( it != itEnd )
174         {
175                 const CMIUtilString & rWord( *it );
176                 if( nWordIndex++ != nArgIndex )
177                 {
178                         // Single words
179                         strBuildContextUp += rWord;
180                         if( bSpaceRequired )
181                                 strBuildContextUp += m_constStrSpace;
182                 }
183                 else
184                 {
185                         // If quoted loose quoted text
186                         if( ++it != itEnd ) 
187                         {
188                                 CMIUtilString words = rWord;
189                                 while( vArg != words )
190                                 {
191                                         if( bSpaceRequired )
192                                                 words += m_constStrSpace;
193                                         words += *it;
194                                         if( ++it == itEnd )
195                                                 break;
196                                 }
197                                 if( it != itEnd )
198                                         --it;
199                         }
200                 }
201                 
202                 // Next
203                 if( it != itEnd )
204                         ++it;
205         }
206         
207         m_strCmdArgsAndOptions = strBuildContextUp;
208         m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
209
210         return MIstatus::success;
211 }
212
213 //++ ------------------------------------------------------------------------------------
214 // Details:     Retrieve number of arguments or options present in the command's option text.
215 // Type:        Method.
216 // Args:        None.
217 // Return:      MIuint  - 0 to n arguments present.
218 // Throws:      None.
219 //--
220 MIuint CMICmdArgContext::GetNumberArgsPresent( void ) const
221 {
222         CMIUtilString::VecString_t vecOptions;
223         return m_strCmdArgsAndOptions.SplitConsiderQuotes( m_constStrSpace, vecOptions );
224 }
225
226 //++ ------------------------------------------------------------------------------------
227 // Details:     Retrieve all the arguments or options remaining in *this context.
228 // Type:        Method.
229 // Args:        None.
230 // Return:      MIUtilString::VecString_t       - List of args remaining.
231 // Throws:      None.
232 //--
233 CMIUtilString::VecString_t CMICmdArgContext::GetArgs( void ) const
234 {
235         CMIUtilString::VecString_t vecOptions;
236         m_strCmdArgsAndOptions.SplitConsiderQuotes( m_constStrSpace, vecOptions );
237         return vecOptions;
238 }
239         
240 //++ ------------------------------------------------------------------------------------
241 // Details:     Copy assignment operator.
242 // Type:        Method.
243 // Args:        vOther  - (R) The variable to copy from.
244 // Return:      CMIUtilString & - this object.
245 // Throws:      None.
246 //--
247 CMICmdArgContext & CMICmdArgContext::operator= ( const CMICmdArgContext & vOther )
248 {
249         if( this != &vOther )
250         {
251                 m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;
252         }       
253         
254         return *this;
255 }