cppstring GTextUtils::MatchSubString(const cppstring & sFullName, const cppstring & sFormat, const cppstring & sSubName) { // MatchSubString determines whether sFullName matches sFormat (allowing sSubName to stand for a wildcard) // For example, if the format is "Run %n" and "%n" is the wildcard, then "Run 1" will match and return "1". cppstring sSubString; // Find the starting character of the wildcard string (assume 0 if wildcard isn't found) unsigned int nStartSub = sFormat.find(sSubName), nEndSub; if (nStartSub != cppstring::npos) { nEndSub = nStartSub + sSubName.length(); // Break the format into two chunks surrounding the wildcard cppstring sBeginFormat = sFormat.substr(0, nStartSub); cppstring sEndFormat = sFormat.substr(nEndSub, sFormat.length() - sSubName.length()); // Now match the beginning and ending formats and extract the wildcard string unsigned int nStartMatch = sFullName.find(sBeginFormat), nEndMatch; if (nStartMatch != cppstring::npos) { nStartMatch += sBeginFormat.length(); nEndMatch = sFullName.rfind(sEndFormat); if (nEndMatch != cppstring::npos) sSubString = sFullName.substr(nStartMatch, nEndMatch - nStartMatch); } } return sSubString; }
cppstring GTextUtils::StringReplace(cppstring sBase, // string to search const cppstring &sSearchFor, // substring to search for (to be replaced) const cppstring &sReplaceWith) // string to replace sSearchFor with { // RETURN sBase with all instances of sSearchFor replaced by sReplaceWith if (sBase.empty() || sSearchFor.empty()) return sBase; int nStart = 0; while (sBase.find(sSearchFor, nStart) != cppstring::npos) { nStart = sBase.find(sSearchFor, nStart); int nEnd = sSearchFor.length(); sBase.replace(nStart, nEnd, sReplaceWith); nStart += sReplaceWith.length(); } return sBase; }
int GPortRef::DecodeFromString(const cppstring & sInString) { int nResult = kResponse_Error; cppstring::size_type nSearchStartPos = 0; cppstring::size_type nCurrentPos = 0; cppstring::size_type nNextPos = 0; cppstring::size_type nTestPos = 0; // ensure that we have a GPortRef tag... nSearchStartPos = sInString.find(k_sPortRefCode, 0); if (nSearchStartPos != cppstring::npos) nSearchStartPos += k_sPortRefCode.length(); if (nSearchStartPos >= sInString.length()) nSearchStartPos = cppstring::npos; if (nSearchStartPos != cppstring::npos) nTestPos = sInString.find_last_of('>'); if (nTestPos != cppstring::npos) { cppstring sSubStr; // decode port-type nCurrentPos = sInString.find(k_sPortTypeCode, nSearchStartPos); nCurrentPos += k_sPortTypeCode.length(); if (nCurrentPos < sInString.length()) nCurrentPos = sInString.find('\"', nCurrentPos); else nCurrentPos = cppstring::npos; if (nCurrentPos != cppstring::npos) nNextPos = nCurrentPos + 1; if (nNextPos != cppstring::npos) nNextPos = sInString.find('\"', nNextPos); if ( nCurrentPos != cppstring::npos && nNextPos != cppstring::npos && nCurrentPos < nNextPos) sSubStr = sInString.substr(nCurrentPos + 1, (nNextPos - nCurrentPos - 1)); if (sSubStr.length() > 0) m_ePortType = (EPortType) GTextUtils::CPPStringToLong(sSubStr.c_str()); sSubStr = GSTD_S(""); // decode location nCurrentPos = sInString.find(k_sLocationCode, nSearchStartPos); nCurrentPos += k_sLocationCode.length(); if (nCurrentPos < sInString.length()) nCurrentPos = sInString.find('\"', nCurrentPos); else nCurrentPos = cppstring::npos; if (nCurrentPos != cppstring::npos) nNextPos = nCurrentPos + 1; if (nNextPos != cppstring::npos) nNextPos = sInString.find('\"', nNextPos); if ( nCurrentPos != cppstring::npos && nNextPos != cppstring::npos && nCurrentPos < nNextPos) sSubStr = sInString.substr(nCurrentPos + 1, (nNextPos - nCurrentPos - 1)); if (sSubStr.length() > 0) m_sLocation = sSubStr; sSubStr = GSTD_S(""); // decode display-name nCurrentPos = sInString.find(k_sDisplayNameCode, nSearchStartPos); nCurrentPos += k_sDisplayNameCode.length(); if (nCurrentPos < sInString.length()) nCurrentPos = sInString.find('\"', nCurrentPos); else nCurrentPos = cppstring::npos; if (nCurrentPos != cppstring::npos) nNextPos = nCurrentPos + 1; if (nNextPos != cppstring::npos) nNextPos = sInString.find('\"', nNextPos); if (nCurrentPos != cppstring::npos && nNextPos != cppstring::npos && nCurrentPos < nNextPos) sSubStr = sInString.substr(nCurrentPos + 1, (nNextPos - nCurrentPos - 1)); if (sSubStr.length() > 0) m_sDisplayName = sSubStr; sSubStr = GSTD_S(""); } return nResult; }