1 //===-- MICmdArgContext.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 #include "MICmdArgContext.h"
14 //------------------------------------------------------------------------------------
15 // Details: CMICmdArgContext constructor.
21 CMICmdArgContext::CMICmdArgContext() {}
24 //------------------------------------------------------------------------------------
25 // Details: CMICmdArgContext constructor.
27 // Args: vrCmdLineArgsRaw - (R) The text description of the arguments
32 CMICmdArgContext::CMICmdArgContext(const CMIUtilString &vrCmdLineArgsRaw)
33 : m_strCmdArgsAndOptions(vrCmdLineArgsRaw) {}
36 //------------------------------------------------------------------------------------
37 // Details: CMICmdArgContext destructor.
43 CMICmdArgContext::~CMICmdArgContext() {}
46 //------------------------------------------------------------------------------------
47 // Details: Retrieve the remainder of the command's argument options left to
51 // Return: CMIUtilString & - Argument options text.
54 const CMIUtilString &CMICmdArgContext::GetArgsLeftToParse() const {
55 return m_strCmdArgsAndOptions;
59 //------------------------------------------------------------------------------------
60 // Details: Ask if this arguments string has any arguments.
63 // Return: bool - True = Has one or more arguments present, false = no
67 bool CMICmdArgContext::IsEmpty() const {
68 return m_strCmdArgsAndOptions.empty();
72 //------------------------------------------------------------------------------------
73 // Details: Remove the argument from the options text and any space after the
77 // Args: vArg - (R) The name of the argument.
78 // Return: MIstatus::success - Functional succeeded.
79 // MIstatus::failure - Functional failed.
82 bool CMICmdArgContext::RemoveArg(const CMIUtilString &vArg) {
84 return MIstatus::success;
86 const size_t nLen = vArg.length();
87 const size_t nLenCntxt = m_strCmdArgsAndOptions.length();
89 return MIstatus::failure;
91 size_t nExtraSpace = 0;
92 size_t nPos = m_strCmdArgsAndOptions.find(vArg);
94 if (nPos == std::string::npos)
95 return MIstatus::success;
99 if (m_strCmdArgsAndOptions[nPos - 1] == ' ')
104 const size_t nEnd = nPos + nLen;
108 if (nEnd < nLenCntxt) {
109 if (m_strCmdArgsAndOptions[nEnd] == ' ') {
120 nPos = m_strCmdArgsAndOptions.find(vArg, nEnd);
123 const size_t nPosEnd = nLen + nExtraSpace;
124 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "");
125 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
127 return MIstatus::success;
131 //------------------------------------------------------------------------------------
132 // Details: Remove the argument at the Nth word position along in the context
134 // Any space after the argument is removed if applicable. A search is
136 // performed as there may be more than one vArg with the same 'name' in
140 // Args: vArg - (R) The name of the argument.
141 // nArgIndex - (R) The word count position to which to remove the
143 // Return: MIstatus::success - Functional succeeded.
144 // MIstatus::failure - Functional failed.
147 bool CMICmdArgContext::RemoveArgAtPos(const CMIUtilString &vArg,
149 size_t nWordIndex = 0;
150 CMIUtilString strBuildContextUp;
151 const CMIUtilString::VecString_t vecWords(GetArgs());
152 const bool bSpaceRequired(GetNumberArgsPresent() > 2);
154 CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
155 const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
156 while (it != itEnd) {
157 const CMIUtilString &rWord(*it);
158 if (nWordIndex++ != nArgIndex) {
160 strBuildContextUp += rWord;
162 strBuildContextUp += " ";
164 // If quoted loose quoted text
166 CMIUtilString words = rWord;
167 while (vArg != words) {
184 m_strCmdArgsAndOptions = strBuildContextUp;
185 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
187 return MIstatus::success;
191 //------------------------------------------------------------------------------------
192 // Details: Retrieve number of arguments or options present in the command's
196 // Return: size_t - 0 to n arguments present.
199 size_t CMICmdArgContext::GetNumberArgsPresent() const {
200 CMIUtilString::VecString_t vecOptions;
201 return m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
205 //------------------------------------------------------------------------------------
206 // Details: Retrieve all the arguments or options remaining in *this context.
209 // Return: MIUtilString::VecString_t - List of args remaining.
212 CMIUtilString::VecString_t CMICmdArgContext::GetArgs() const {
213 CMIUtilString::VecString_t vecOptions;
214 m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
219 //------------------------------------------------------------------------------------
220 // Details: Copy assignment operator.
222 // Args: vOther - (R) The variable to copy from.
223 // Return: CMIUtilString & - this object.
226 CMICmdArgContext &CMICmdArgContext::operator=(const CMICmdArgContext &vOther) {
227 if (this != &vOther) {
228 m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;