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; }
std::string GTextUtils::ConvertWideStringToNarrow(const cppstring sWide) { #ifdef USE_WIDE_CHARS char * pMultiByteBuf; pMultiByteBuf = (char *)malloc( sWide.length()*2 ); size_t i = wcstombs( pMultiByteBuf, sWide.c_str(), sWide.length()*2 ); std::string sNarrow (pMultiByteBuf); delete pMultiByteBuf; #else std::string sNarrow(sWide); // since cppstring is already narrow there is nothing to do #endif return sNarrow; }
bool GTextUtils::StringIsAllDigits(const cppstring & sNumber) {// returns true if sNumber is all digits ... no decimals, etc bool bResult = true; cppstring::const_iterator iChar = sNumber.begin(); bResult = (sNumber.length() > 0); while(iChar != sNumber.end() && bResult) { if (!isdigit(*iChar)) { bResult = false; break; } iChar++; } return bResult; }
/* real GTextUtils::CPPStringToReal(const cppstring &sNumber) { real fResult; GTextUtils::IsStringRealNumber(sNumber, &fResult); // just return the result return fResult; } real GTextUtils::StringToReal(const narrowstring &sNumber) {// not as robust as CPPStringToReal but does not need to be real fValue; std::stringstream ss; ss << sNumber; ss >> fValue; return fValue; } */ int GTextUtils::CPPStringToLong(const cppstring &sInteger) { int nResult = 0L; if (GTextUtils::IsStringLong(sInteger)) nResult = GTextUtils::Gstrtol(sInteger.c_str(), NULL, 10); return nResult; }
void GUtils::Trace(cppstring msg, gchar * psFile, int nLine) { int nLen = 0; if (psFile != NULL) { #ifdef _UNICODE nLen = wcslen(psFile); #else nLen = strlen(psFile); #endif } if ((nLen > 0) && (nLine != -1) ) { // Only print File and Line if we have something.. cppsstream ss; ss << psFile << GSTD_S("(") << nLine << GSTD_S(") : ") << msg << kOSNewlineString; OSTrace(ss.str().c_str()); } else OSTrace(msg.c_str()); GSTD_LOG(msg); }
cppstring GTextUtils::ExtractBaseName(const cppstring & sFullName, int * p_nOutNumberSuffix) { // ExtractBaseName expects a string in the form of [base sequenece][space character][digits] // The base sequence is returned as a cppstring. // The digits, if present, are converted to an integer value and stored // on exit in the p_nOutNumberSuffix parameter if it is non-NULL. cppstring sBase; cppstring sNum; unsigned int nLastSpacePos = sFullName.find_last_of(' '); if (nLastSpacePos != cppstring::npos) { unsigned int ix; for (ix = 0; ix < nLastSpacePos; ix++) sBase += sFullName[ix]; for (ix = nLastSpacePos + 1; ix < sFullName.length(); ix++) sNum += sFullName[ix]; bool bGotNum = (sNum.length() > 0 && IsStringLong(sNum)); if (!bGotNum) sBase = sFullName; if (p_nOutNumberSuffix != NULL) { if (bGotNum) *p_nOutNumberSuffix = (int) GTextUtils::CPPStringToLong(sNum); else *p_nOutNumberSuffix = 0; } } else { sBase = sFullName; if (p_nOutNumberSuffix != NULL) *p_nOutNumberSuffix = 0; } return sBase; }
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; }