1 //===-- MICmdArgContext.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 // In-house headers: 11 #include "MICmdArgContext.h" 12 13 //++ 14 //------------------------------------------------------------------------------------ 15 // Details: CMICmdArgContext constructor. 16 // Type: Method. 17 // Args: None. 18 // Return: None. 19 // Throws: None. 20 //-- CMICmdArgContext()21CMICmdArgContext::CMICmdArgContext() {} 22 23 //++ 24 //------------------------------------------------------------------------------------ 25 // Details: CMICmdArgContext constructor. 26 // Type: Method. 27 // Args: vrCmdLineArgsRaw - (R) The text description of the arguments 28 // options. 29 // Return: None. 30 // Throws: None. 31 //-- CMICmdArgContext(const CMIUtilString & vrCmdLineArgsRaw)32CMICmdArgContext::CMICmdArgContext(const CMIUtilString &vrCmdLineArgsRaw) 33 : m_strCmdArgsAndOptions(vrCmdLineArgsRaw) {} 34 35 //++ 36 //------------------------------------------------------------------------------------ 37 // Details: CMICmdArgContext destructor. 38 // Type: Method. 39 // Args: None. 40 // Return: None. 41 // Throws: None. 42 //-- ~CMICmdArgContext()43CMICmdArgContext::~CMICmdArgContext() {} 44 45 //++ 46 //------------------------------------------------------------------------------------ 47 // Details: Retrieve the remainder of the command's argument options left to 48 // parse. 49 // Type: Method. 50 // Args: None. 51 // Return: CMIUtilString & - Argument options text. 52 // Throws: None. 53 //-- GetArgsLeftToParse() const54const CMIUtilString &CMICmdArgContext::GetArgsLeftToParse() const { 55 return m_strCmdArgsAndOptions; 56 } 57 58 //++ 59 //------------------------------------------------------------------------------------ 60 // Details: Ask if this arguments string has any arguments. 61 // Type: Method. 62 // Args: None. 63 // Return: bool - True = Has one or more arguments present, false = no 64 // arguments. 65 // Throws: None. 66 //-- IsEmpty() const67bool CMICmdArgContext::IsEmpty() const { 68 return m_strCmdArgsAndOptions.empty(); 69 } 70 71 //++ 72 //------------------------------------------------------------------------------------ 73 // Details: Remove the argument from the options text and any space after the 74 // argument 75 // if applicable. 76 // Type: Method. 77 // Args: vArg - (R) The name of the argument. 78 // Return: MIstatus::success - Functional succeeded. 79 // MIstatus::failure - Functional failed. 80 // Throws: None. 81 //-- RemoveArg(const CMIUtilString & vArg)82bool CMICmdArgContext::RemoveArg(const CMIUtilString &vArg) { 83 if (vArg.empty()) 84 return MIstatus::success; 85 86 const size_t nLen = vArg.length(); 87 const size_t nLenCntxt = m_strCmdArgsAndOptions.length(); 88 if (nLen > nLenCntxt) 89 return MIstatus::failure; 90 91 size_t nExtraSpace = 0; 92 size_t nPos = m_strCmdArgsAndOptions.find(vArg); 93 while (1) { 94 if (nPos == std::string::npos) 95 return MIstatus::success; 96 97 bool bPass1 = false; 98 if (nPos != 0) { 99 if (m_strCmdArgsAndOptions[nPos - 1] == ' ') 100 bPass1 = true; 101 } else 102 bPass1 = true; 103 104 const size_t nEnd = nPos + nLen; 105 106 if (bPass1) { 107 bool bPass2 = false; 108 if (nEnd < nLenCntxt) { 109 if (m_strCmdArgsAndOptions[nEnd] == ' ') { 110 bPass2 = true; 111 nExtraSpace = 1; 112 } 113 } else 114 bPass2 = true; 115 116 if (bPass2) 117 break; 118 } 119 120 nPos = m_strCmdArgsAndOptions.find(vArg, nEnd); 121 } 122 123 const size_t nPosEnd = nLen + nExtraSpace; 124 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, ""); 125 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim(); 126 127 return MIstatus::success; 128 } 129 130 //++ 131 //------------------------------------------------------------------------------------ 132 // Details: Remove the argument at the Nth word position along in the context 133 // string. 134 // Any space after the argument is removed if applicable. A search is 135 // not 136 // performed as there may be more than one vArg with the same 'name' in 137 // the 138 // context string. 139 // Type: Method. 140 // Args: vArg - (R) The name of the argument. 141 // nArgIndex - (R) The word count position to which to remove the 142 // vArg word. 143 // Return: MIstatus::success - Functional succeeded. 144 // MIstatus::failure - Functional failed. 145 // Throws: None. 146 //-- RemoveArgAtPos(const CMIUtilString & vArg,size_t nArgIndex)147bool CMICmdArgContext::RemoveArgAtPos(const CMIUtilString &vArg, 148 size_t nArgIndex) { 149 size_t nWordIndex = 0; 150 CMIUtilString strBuildContextUp; 151 const CMIUtilString::VecString_t vecWords(GetArgs()); 152 const bool bSpaceRequired(GetNumberArgsPresent() > 2); 153 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) { 159 // Single words 160 strBuildContextUp += rWord; 161 if (bSpaceRequired) 162 strBuildContextUp += " "; 163 } else { 164 // If quoted loose quoted text 165 if (++it != itEnd) { 166 CMIUtilString words = rWord; 167 while (vArg != words) { 168 if (bSpaceRequired) 169 words += " "; 170 words += *it; 171 if (++it == itEnd) 172 break; 173 } 174 if (it != itEnd) 175 --it; 176 } 177 } 178 179 // Next 180 if (it != itEnd) 181 ++it; 182 } 183 184 m_strCmdArgsAndOptions = strBuildContextUp; 185 m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim(); 186 187 return MIstatus::success; 188 } 189 190 //++ 191 //------------------------------------------------------------------------------------ 192 // Details: Retrieve number of arguments or options present in the command's 193 // option text. 194 // Type: Method. 195 // Args: None. 196 // Return: size_t - 0 to n arguments present. 197 // Throws: None. 198 //-- GetNumberArgsPresent() const199size_t CMICmdArgContext::GetNumberArgsPresent() const { 200 CMIUtilString::VecString_t vecOptions; 201 return m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions); 202 } 203 204 //++ 205 //------------------------------------------------------------------------------------ 206 // Details: Retrieve all the arguments or options remaining in *this context. 207 // Type: Method. 208 // Args: None. 209 // Return: MIUtilString::VecString_t - List of args remaining. 210 // Throws: None. 211 //-- GetArgs() const212CMIUtilString::VecString_t CMICmdArgContext::GetArgs() const { 213 CMIUtilString::VecString_t vecOptions; 214 m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions); 215 return vecOptions; 216 } 217 218 //++ 219 //------------------------------------------------------------------------------------ 220 // Details: Copy assignment operator. 221 // Type: Method. 222 // Args: vOther - (R) The variable to copy from. 223 // Return: CMIUtilString & - this object. 224 // Throws: None. 225 //-- operator =(const CMICmdArgContext & vOther)226CMICmdArgContext &CMICmdArgContext::operator=(const CMICmdArgContext &vOther) { 227 if (this != &vOther) { 228 m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions; 229 } 230 231 return *this; 232 } 233