1 //===-- MICmdArgContext.cpp -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "MICmdArgContext.h"
13 // Details: CMICmdArgContext constructor.
19 CMICmdArgContext::CMICmdArgContext() {}
22 // Details: CMICmdArgContext constructor.
24 // Args: vrCmdLineArgsRaw - (R) The text description of the arguments
29 CMICmdArgContext::CMICmdArgContext(const CMIUtilString &vrCmdLineArgsRaw)
30 : m_strCmdArgsAndOptions(vrCmdLineArgsRaw) {}
33 // Details: CMICmdArgContext destructor.
39 CMICmdArgContext::~CMICmdArgContext() {}
42 // Details: Retrieve the remainder of the command's argument options left to
46 // Return: CMIUtilString & - Argument options text.
49 const CMIUtilString &CMICmdArgContext::GetArgsLeftToParse() const {
50 return m_strCmdArgsAndOptions;
54 // Details: Ask if this arguments string has any arguments.
57 // Return: bool - True = Has one or more arguments present, false = no
61 bool CMICmdArgContext::IsEmpty() const {
62 return m_strCmdArgsAndOptions.empty();
66 // Details: Remove the argument from the options text and any space after the
70 // Args: vArg - (R) The name of the argument.
71 // Return: MIstatus::success - Functional succeeded.
72 // MIstatus::failure - Functional failed.
75 bool CMICmdArgContext::RemoveArg(const CMIUtilString &vArg) {
77 return MIstatus::success;
79 const size_t nLen = vArg.length();
80 const size_t nLenCntxt = m_strCmdArgsAndOptions.length();
82 return MIstatus::failure;
84 size_t nExtraSpace = 0;
85 size_t nPos = m_strCmdArgsAndOptions.find(vArg);
87 if (nPos == std::string::npos)
88 return MIstatus::success;
92 if (m_strCmdArgsAndOptions[nPos - 1] == ' ')
97 const size_t nEnd = nPos + nLen;
101 if (nEnd < nLenCntxt) {
102 if (m_strCmdArgsAndOptions[nEnd] == ' ') {
113 nPos = m_strCmdArgsAndOptions.find(vArg, nEnd);
116 const size_t nPosEnd = nLen + nExtraSpace;
117 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "");
118 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
120 return MIstatus::success;
124 // Details: Remove the argument at the Nth word position along in the context
126 // Any space after the argument is removed if applicable. A search is
128 // performed as there may be more than one vArg with the same 'name' in
132 // Args: vArg - (R) The name of the argument.
133 // nArgIndex - (R) The word count position to which to remove the
135 // Return: MIstatus::success - Functional succeeded.
136 // MIstatus::failure - Functional failed.
139 bool CMICmdArgContext::RemoveArgAtPos(const CMIUtilString &vArg,
141 size_t nWordIndex = 0;
142 CMIUtilString strBuildContextUp;
143 const CMIUtilString::VecString_t vecWords(GetArgs());
144 const bool bSpaceRequired(GetNumberArgsPresent() > 2);
146 CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
147 const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
148 while (it != itEnd) {
149 const CMIUtilString &rWord(*it);
150 if (nWordIndex++ != nArgIndex) {
152 strBuildContextUp += rWord;
154 strBuildContextUp += " ";
156 // If quoted loose quoted text
158 CMIUtilString words = rWord;
159 while (vArg != words) {
176 m_strCmdArgsAndOptions = strBuildContextUp;
177 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
179 return MIstatus::success;
183 // Details: Retrieve number of arguments or options present in the command's
187 // Return: size_t - 0 to n arguments present.
190 size_t CMICmdArgContext::GetNumberArgsPresent() const {
191 CMIUtilString::VecString_t vecOptions;
192 return m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
196 // Details: Retrieve all the arguments or options remaining in *this context.
199 // Return: MIUtilString::VecString_t - List of args remaining.
202 CMIUtilString::VecString_t CMICmdArgContext::GetArgs() const {
203 CMIUtilString::VecString_t vecOptions;
204 m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
209 // Details: Copy assignment operator.
211 // Args: vOther - (R) The variable to copy from.
212 // Return: CMIUtilString & - this object.
215 CMICmdArgContext &CMICmdArgContext::operator=(const CMICmdArgContext &vOther) {
216 if (this != &vOther) {
217 m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;