2015-07-21 21:09:39 +08:00
|
|
|
//===-- MICmdArgContext.cpp -------------------------------------*- C++ -*-===//
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// In-house headers:
|
|
|
|
#include "MICmdArgContext.h"
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmdArgContext constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::CMICmdArgContext()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmdArgContext constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vrCmdLineArgsRaw - (R) The text description of the arguments options.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMICmdArgContext::CMICmdArgContext(const CMIUtilString &vrCmdLineArgsRaw)
|
|
|
|
: m_strCmdArgsAndOptions(vrCmdLineArgsRaw)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmdArgContext destructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::~CMICmdArgContext()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve the remainder of the command's argument options left to parse.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: CMIUtilString & - Argument options text.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
const CMIUtilString &
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::GetArgsLeftToParse() const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return m_strCmdArgsAndOptions;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Ask if this arguments string has any arguments.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: bool - True = Has one or more arguments present, false = no arguments.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::IsEmpty() const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return m_strCmdArgsAndOptions.empty();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2014-05-16 18:51:01 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Remove the argument from the options text and any space after the argument
|
|
|
|
// if applicable.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vArg - (R) The name of the argument.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMICmdArgContext::RemoveArg(const CMIUtilString &vArg)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (vArg.empty())
|
|
|
|
return MIstatus::success;
|
|
|
|
|
2015-07-03 23:40:44 +08:00
|
|
|
const size_t nLen = vArg.length();
|
|
|
|
const size_t nLenCntxt = m_strCmdArgsAndOptions.length();
|
2014-11-18 02:06:21 +08:00
|
|
|
if (nLen > nLenCntxt)
|
|
|
|
return MIstatus::failure;
|
|
|
|
|
2015-07-03 23:40:44 +08:00
|
|
|
size_t nExtraSpace = 0;
|
|
|
|
size_t nPos = m_strCmdArgsAndOptions.find(vArg);
|
2014-11-18 02:06:21 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2015-07-03 23:40:44 +08:00
|
|
|
if (nPos == std::string::npos)
|
2014-11-18 02:06:21 +08:00
|
|
|
return MIstatus::success;
|
|
|
|
|
|
|
|
bool bPass1 = false;
|
|
|
|
if (nPos != 0)
|
|
|
|
{
|
2015-07-03 23:30:38 +08:00
|
|
|
if (m_strCmdArgsAndOptions[nPos - 1] == ' ')
|
2014-11-18 02:06:21 +08:00
|
|
|
bPass1 = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bPass1 = true;
|
|
|
|
|
2015-07-03 23:40:44 +08:00
|
|
|
const size_t nEnd = nPos + nLen;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
|
|
|
if (bPass1)
|
|
|
|
{
|
|
|
|
bool bPass2 = false;
|
|
|
|
if (nEnd < nLenCntxt)
|
|
|
|
{
|
2015-07-03 23:30:38 +08:00
|
|
|
if (m_strCmdArgsAndOptions[nEnd] == ' ')
|
2014-11-18 02:06:21 +08:00
|
|
|
{
|
|
|
|
bPass2 = true;
|
|
|
|
nExtraSpace = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bPass2 = true;
|
|
|
|
|
|
|
|
if (bPass2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nPos = m_strCmdArgsAndOptions.find(vArg, nEnd);
|
|
|
|
}
|
|
|
|
|
2015-07-03 23:40:44 +08:00
|
|
|
const size_t nPosEnd = nLen + nExtraSpace;
|
2015-09-25 16:28:58 +08:00
|
|
|
m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "");
|
2014-11-18 02:06:21 +08:00
|
|
|
m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 00:35:50 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Remove the argument at the Nth word position along in the context string.
|
|
|
|
// Any space after the argument is removed if applicable. A search is not
|
|
|
|
// performed as there may be more than one vArg with the same 'name' in the
|
|
|
|
// context string.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vArg - (R) The name of the argument.
|
|
|
|
// nArgIndex - (R) The word count position to which to remove the vArg word.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-07-21 16:07:27 +08:00
|
|
|
CMICmdArgContext::RemoveArgAtPos(const CMIUtilString &vArg, size_t nArgIndex)
|
2014-06-25 00:35:50 +08:00
|
|
|
{
|
2015-07-21 16:07:27 +08:00
|
|
|
size_t nWordIndex = 0;
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilString strBuildContextUp;
|
|
|
|
const CMIUtilString::VecString_t vecWords(GetArgs());
|
|
|
|
const bool bSpaceRequired(GetNumberArgsPresent() > 2);
|
|
|
|
|
|
|
|
CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
|
|
|
|
const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
|
|
|
|
while (it != itEnd)
|
|
|
|
{
|
|
|
|
const CMIUtilString &rWord(*it);
|
|
|
|
if (nWordIndex++ != nArgIndex)
|
|
|
|
{
|
|
|
|
// Single words
|
|
|
|
strBuildContextUp += rWord;
|
|
|
|
if (bSpaceRequired)
|
2015-07-03 23:30:38 +08:00
|
|
|
strBuildContextUp += " ";
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If quoted loose quoted text
|
|
|
|
if (++it != itEnd)
|
|
|
|
{
|
|
|
|
CMIUtilString words = rWord;
|
|
|
|
while (vArg != words)
|
|
|
|
{
|
|
|
|
if (bSpaceRequired)
|
2015-07-03 23:30:38 +08:00
|
|
|
words += " ";
|
2014-11-18 02:06:21 +08:00
|
|
|
words += *it;
|
|
|
|
if (++it == itEnd)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (it != itEnd)
|
|
|
|
--it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next
|
|
|
|
if (it != itEnd)
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_strCmdArgsAndOptions = strBuildContextUp;
|
|
|
|
m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:51:01 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve number of arguments or options present in the command's option text.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
2015-07-21 16:07:27 +08:00
|
|
|
// Return: size_t - 0 to n arguments present.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-07-21 16:07:27 +08:00
|
|
|
size_t
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::GetNumberArgsPresent() const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilString::VecString_t vecOptions;
|
2015-07-03 23:30:38 +08:00
|
|
|
return m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve all the arguments or options remaining in *this context.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIUtilString::VecString_t - List of args remaining.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilString::VecString_t
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdArgContext::GetArgs() const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilString::VecString_t vecOptions;
|
2015-07-03 23:30:38 +08:00
|
|
|
m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
|
2014-11-18 02:06:21 +08:00
|
|
|
return vecOptions;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2014-05-16 18:51:01 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Copy assignment operator.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vOther - (R) The variable to copy from.
|
|
|
|
// Return: CMIUtilString & - this object.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMICmdArgContext &CMICmdArgContext::operator=(const CMICmdArgContext &vOther)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (this != &vOther)
|
|
|
|
{
|
|
|
|
m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|