//++ //------------------------------------------------------------------------------------ // Details: Find and replace all matches of a sub string with another string. It // does not // alter *this string. // Type: Method. // Args: vFind - (R) The string to look for. // vReplaceWith - (R) The string to replace the vFind match. // Return: CMIUtilString - New version of the string. // Throws: None. //-- CMIUtilString CMIUtilString::FindAndReplace(const CMIUtilString &vFind, const CMIUtilString &vReplaceWith) const { if (vFind.empty() || this->empty()) return *this; size_t nPos = find(vFind); if (nPos == std::string::npos) return *this; CMIUtilString strNew(*this); while (nPos != std::string::npos) { strNew.replace(nPos, vFind.length(), vReplaceWith); nPos += vReplaceWith.length(); nPos = strNew.find(vFind, nPos); } return strNew; }
//++ ------------------------------------------------------------------------------------ // 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. //-- bool CMICmdArgContext::RemoveArg( const CMIUtilString & vArg ) { if( vArg.empty() ) return MIstatus::success; const MIuint nLen = vArg.length(); const MIuint nLenCntxt = m_strCmdArgsAndOptions.length(); if( nLen > nLenCntxt ) return MIstatus::failure; MIuint nExtraSpace = 0; MIint nPos = m_strCmdArgsAndOptions.find( vArg ); while( 1 ) { if( nPos == (MIint) std::string::npos ) return MIstatus::success; bool bPass1 = false; if( nPos != 0 ) { if( m_strCmdArgsAndOptions[ nPos - 1 ] == m_constCharSpace ) bPass1 = true; } else bPass1 = true; const MIuint nEnd = nPos + nLen; if( bPass1 ) { bool bPass2 = false; if( nEnd < nLenCntxt ) { if( m_strCmdArgsAndOptions[ nEnd ] == m_constCharSpace ) { bPass2 = true; nExtraSpace = 1; } } else bPass2 = true; if( bPass2 ) break; } nPos = m_strCmdArgsAndOptions.find( vArg, nEnd ); } const MIuint nPosEnd = nLen + nExtraSpace; m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace( nPos, nPosEnd, "" ).c_str(); m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim(); return MIstatus::success; }
//++ ------------------------------------------------------------------------------------ // Details: Retrieve system local current time. Format is HH:MM:SS 24 hour clock. // Type: Method. // Args: None. // Return: CMIUtilString - Text description. // Throws: None. //-- CMIUtilString CMIUtilDateTimeStd::GetTime(void) { std::time(&m_rawTime); const std::tm *pTi = std::localtime(&m_rawTime); const CMIUtilString seconds(CMIUtilString::Format("%d", pTi->tm_sec)); const CMIUtilString zero((seconds.length() == 1) ? "0" : ""); const CMIUtilString strTime(CMIUtilString::Format("%d:%d:%s%s", pTi->tm_hour, pTi->tm_min, zero.c_str(), seconds.c_str())); return strTime; }
CMIUtilString CMIUtilString::ConvertToPrintableASCII(const char16_t vChar16, bool bEscapeQuotes) { if (vChar16 == (char16_t)(char)vChar16) { // Convert char16_t to char (if possible) CMIUtilString str = ConvertCharValueToPrintableASCII((char)vChar16, bEscapeQuotes); if (str.length() > 0) return str; } return Format("\\u%02" PRIx8 "%02" PRIx8, (vChar16 >> 8) & 0xff, vChar16 & 0xff); }
//++ ------------------------------------------------------------------------------------ // Details: Does the command name entered match the criteria for a MI command format. // Is a recogised command present? The command name is entered into the // command meta data structure whether correct or not for reporting or later // command execution purposes. Command options is present are also put into the // command meta data structure. // Type: Method. // Args: vTextLine - (R) Command information structure. // Return: bool - True = yes command name present, false = command not recognised. // Throws: None. //-- bool CMICmdInterpreter::MiHasCmd(const CMIUtilString &vTextLine) { MIint nPos = 0; if (m_miCmdData.bMIOldStyle) { char cChar = vTextLine[0]; MIuint i = 0; while (::isdigit(cChar) != 0) { cChar = vTextLine[++i]; } nPos = --i; } else { nPos = vTextLine.find("-", 0); } bool bFoundCmd = false; const MIint nLen = vTextLine.length(); const MIint nPos2 = vTextLine.find(" ", nPos); if (nPos2 != (MIint)std::string::npos) { if (nPos2 == nLen) return false; const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nPos2 - nPos - 1).c_str()); if (cmd.empty()) return false; m_miCmdData.strMiCmd = cmd; if (nPos2 < nLen) m_miCmdData.strMiCmdOption = CMIUtilString(vTextLine.substr(nPos2 + 1, nLen - nPos2 - 1).c_str()); bFoundCmd = true; } else { const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nLen - nPos - 1).c_str()); if (cmd.empty()) return false; m_miCmdData.strMiCmd = cmd; bFoundCmd = true; } if (bFoundCmd) m_miCmdData.strMiCmdAll = vTextLine; return bFoundCmd; }
//++ // Details: Find first occurrence in *this string which doesn't match the // pattern. // Type: Method. // Args: vrPattern - (R) The pattern to search for. // vnPos - Position of the first character in the string to be // considered in the search. (Dflt = 0) // Return: size_t - The position of the first character that doesn't match. // Throws: None. //-- size_t CMIUtilString::FindFirstNot(const CMIUtilString &vrPattern, size_t vnPos /* = 0 */) const { const size_t nLen(length()); const size_t nPatternLen(vrPattern.length()); size_t nPatternPos(vnPos); do { const bool bMatchPattern(compare(nPatternPos, nPatternLen, vrPattern) == 0); if (!bMatchPattern) return nPatternPos; nPatternPos += nPatternLen; } while (nPatternPos < nLen); return std::string::npos; }
//++ // Details: Examine the string and determine if it is a valid long type option // argument. // Long type argument looks like --someLongOption. // Type: Method. // Args: vrTxt - (R) Some text. // Return: bool - True = yes valid arg, false = no. // Throws: None. //-- bool CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const { const bool bHavePosSlash = (vrTxt.find('/') != std::string::npos); const bool bHaveBckSlash = (vrTxt.find('\\') != std::string::npos); if (bHavePosSlash || bHaveBckSlash) return false; const size_t nPos = vrTxt.find("--"); if (nPos != 0) return false; if (vrTxt.length() < 3) return false; const CMIUtilString strArg = vrTxt.substr(2); return !strArg.IsNumber(); }
//++ ------------------------------------------------------------------------------------ // Details: Determine if the path contains valid characters for a file path. Letters can be // either upper or lower case. // Type: Method. // Args: vrText - (R) The text data to examine. // Return: bool - True = yes valid, false = one or more chars is valid. // Throws: None. //-- bool CMICmdArgValFile::IsValidChars(const CMIUtilString &vrText) const { const MIchar *pPtr = const_cast<MIchar *>(vrText.c_str()); for (MIuint i = 0; i < vrText.length(); i++, pPtr++) { const MIchar c = *pPtr; if (::isalnum((int)c) == 0) { if ((c != '.') && (c != '-') && (c != '_')) return false; } } return true; }
//++ ------------------------------------------------------------------------------------ // Details: Examine the string and determine if it is a valid string type argument. // Take into account quotes surrounding the text. Take into account string format // embedded quotes surrounding the text i.e. "\\\"%5d\\\"". Note this function falls // through to IsStringArgQuotedTextEmbedded() should the criteria match fail. // Type: Method. // Args: vrTxt - (R) Some text. // Return: bool - True = yes valid arg, false = no. // Throws: None. //-- bool CMICmdArgValString::IsStringArgQuotedQuotedTextEmbedded( const CMIUtilString & vrTxt ) const { const MIint nPos = vrTxt.find( "\"\\\"" ); if( nPos == (MIint) std::string::npos ) return false; const MIint nPos2 = vrTxt.rfind( "\\\"\"" ); if( nPos2 == (MIint) std::string::npos ) return false; const MIint nLen = vrTxt.length(); if( (nLen > 5) && ((nPos + 2) == (nPos2 - 2)) ) return false; return true; }
//++ ------------------------------------------------------------------------------------ // Details: Determine if the path contains valid characters for a file path. Letters can be // either upper or lower case. // Type: Method. // Args: vrText - (R) The text data to examine. // Return: bool - True = yes valid, false = one or more chars is valid. // Throws: None. //-- bool CMICmdArgValFile::IsValidChars(const CMIUtilString &vrText) const { static CMIUtilString s_strSpecialCharacters(".'\"`@#$%^&*()_+-={}[]| "); const char *pPtr = vrText.c_str(); for (MIuint i = 0; i < vrText.length(); i++, pPtr++) { const char c = *pPtr; if (::isalnum((int)c) == 0) { if (s_strSpecialCharacters.find(c) == CMIUtilString::npos) return false; } } return true; }
//++ ------------------------------------------------------------------------------------ // Details: Parse the command's argument options string and try to extract all the words // between quotes then delimited by the next space. If there any string format // characters '\\' used to embed quotes these are ignored i.e. "\\\"%5d\\\"" // becomes "%5d". Can fall through to ValidateQuotedText(). // Type: Method. // Args: vrwArgContext - (RW) The command's argument options string. // Return: MIstatus::success - Functional succeeded. // MIstatus::failure - Functional failed. // Throws: None. //-- bool CMICmdArgValString::ValidateQuotedTextEmbedded( CMICmdArgContext & vrwArgContext ) { // CODETAG_QUOTEDTEXT_SIMILAR_CODE CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse(); const MIchar cBckSlash = '\\'; const MIint nPos = strOptions.find( cBckSlash ); if( nPos == (MIint) std::string::npos ) return ValidateQuotedText( vrwArgContext ); // Back slash must be the first character in the string or be preceeded by a space // or '\\' const MIchar cSpace = ' '; if( (nPos > 0) && (strOptions[ nPos - 1 ] != cSpace) ) return MIstatus::failure; // Need to find the other back slash const MIint nPos2 = strOptions.rfind( cBckSlash ); if( nPos2 == (MIint) std::string::npos ) return MIstatus::failure; // Make sure not same back slash, need two slashs if( nPos == nPos2 ) return MIstatus::failure; // Look for the two quotes const MIint nLen = strOptions.length(); const MIchar cQuote = '"'; const MIint nPosQuote1 = nPos + 1; const MIint nPosQuote2 = (nPos2 < nLen) ? nPos2 + 1 : nPos2; if( (nPosQuote1 != nPosQuote2) && (strOptions[ nPosQuote1 ] != cQuote) && (strOptions[ nPosQuote2 ] != cQuote) ) return MIstatus::failure; // Extract quoted text const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPosQuote2 - nPos + 1 ).c_str(); if( vrwArgContext.RemoveArg( strQuotedTxt ) ) { m_bFound = true; m_bValid = true; m_argValue = strQuotedTxt; return MIstatus::success; } return MIstatus::failure; }
//++ ------------------------------------------------------------------------------------ // Details: Parse the command's argument options string and try to extract all the words // between quotes then delimited by the next space. Can fall through to // ValidateSingleText() or ValidateQuotedQuotedTextEmbedded(). // Type: Method. // Args: vrwArgContext - (RW) The command's argument options string. // Return: MIstatus::success - Functional succeeded. // MIstatus::failure - Functional failed. // Throws: None. //-- bool CMICmdArgValString::ValidateQuotedText( CMICmdArgContext & vrwArgContext ) { // CODETAG_QUOTEDTEXT_SIMILAR_CODE CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse(); const MIchar cQuote = '"'; // Look for first quote of two MIint nPos = strOptions.find( cQuote ); if( nPos == (MIint) std::string::npos ) return ValidateSingleText( vrwArgContext ); // Is one and only quote at end of the string const MIint nLen = strOptions.length(); if( nPos == (MIint)(nLen - 1) ) return MIstatus::failure; // Quote must be the first character in the string or be preceeded by a space if( (nPos > 0) && (strOptions[ nPos - 1 ] != ' ') ) return MIstatus::failure; // Need to find the other quote const MIint nPos2 = strOptions.rfind( cQuote ); if( nPos2 == (MIint) std::string::npos ) return MIstatus::failure; // Is there quotes surrounding string formatting embedded quotes if( IsStringArgQuotedQuotedTextEmbedded( strOptions ) ) return ValidateQuotedQuotedTextEmbedded( vrwArgContext ); // Make sure not same back quote, need two quotes if( nPos == nPos2 ) return MIstatus::failure; // Extract quoted text const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPos2 - nPos + 1 ).c_str(); if( vrwArgContext.RemoveArg( strQuotedTxt ) ) { m_bFound = true; m_bValid = true; m_argValue = strOptions.substr( nPos + 1, nPos2 - nPos - 1 ).c_str(); return MIstatus::success; } return MIstatus::failure; }
//++ ------------------------------------------------------------------------------------ // Details: Examine the string and determine if it is a valid long type option argument. // Long type argument looks like --someLongOption. // Type: Method. // Args: vrTxt - (R) Some text. // Return: bool - True = yes valid arg, false = no. // Throws: None. //-- bool CMICmdArgValOptionLong::IsArgLongOption( const CMIUtilString & vrTxt ) const { const bool bHavePosSlash = (vrTxt.find_first_of( "/" ) != std::string::npos); const bool bHaveBckSlash = (vrTxt.find_first_of( "\\" ) != std::string::npos); if( bHavePosSlash || bHaveBckSlash ) return false; const MIint nPos = vrTxt.find_first_of( "--" ); if( nPos != 0 ) return false; if( vrTxt.length() < 3 ) return false; const CMIUtilString strArg = vrTxt.substr( 2 ).c_str(); if( strArg.IsNumber() ) return false; return true; }
//++ ------------------------------------------------------------------------------------ // Details: Examine the string and determine if it is a valid string type argument. // Type: Method. // Args: vrTxt - (R) Some text. // Return: bool - True = yes valid arg, false = no. // Throws: None. //-- bool CMICmdArgValString::IsStringArgQuotedText( const CMIUtilString & vrTxt ) const { // CODETAG_QUOTEDTEXT_SIMILAR_CODE const MIchar cQuote = '"'; const MIint nPos = vrTxt.find( cQuote ); if( nPos == (MIint) std::string::npos ) return IsStringArgSingleText( vrTxt ); // Is one and only quote at end of the string if( nPos == (MIint)(vrTxt.length() - 1) ) return false; // Quote must be the first character in the string or be preceeded by a space if( (nPos > 0) && (vrTxt[ nPos - 1 ] != ' ' ) ) return false; // Need to find the other quote const MIint nPos2 = vrTxt.find( cQuote, nPos + 1 ); if( nPos2 == (MIint) std::string::npos ) return false; return true; }