/////////////////////////////////////////////////////////////////////////////// // DisplayStringToFCOName /////////////////////////////////////////////////////////////////////////////// bool cFSNameTranslator::DisplayStringToFCOName( const TSTRING& strC, cFCOName& name ) const { TSTRING str = strC; const TCHAR dq = _T('\"'); // do a little error checking. must have at least '""' if( str.size() < 1 ) return false; if( dq != str[0] ) return false; if( dq != str[str.size() - 1] ) return false; // get rid of beginning and trailing quote str = str.substr( 1, str.size() - 2 ); // // undo funky wide char encoding // cDisplayEncoder e( cDisplayEncoder::ROUNDTRIP ); if( ! e.Decode( str ) ) return false; // give to cFCOName name = str; return true; }
////////////////////////////////////////////////////////////////////////////////// // Function name : util_RemoveLastPathElement // Description : // effectively pops off a path element from the end, except for the root dir, where it does nothing // it removes any slashes before and after the element // ///root//foo/ -> leaves "///root" ("foo" is strElem) // ///root -> leaves "" ("root" is strElem) // // -> leaves "" ("" is strElem) // // Return type : void // Argument : TSTRING& strPath // Argument : TSTRING& strElem ///////////////////////////////////////////////////////////////////////////////// void util_RemoveLastPathElement(TSTRING& strPath, TSTRING& strElem) { // remove all trailing separators util_RemoveTrailingSeps(strPath); // find the last separator TSTRING::size_type lastSep = strPath.rfind(TW_SLASH); // if separator was found, take all chars after it if (lastSep != TSTRING::npos) { strElem = strPath.substr(lastSep + 1); strPath.resize(lastSep + 1); } else // no seps in name, take whole string { // last element strElem = strPath; strPath.erase(); } // remove all trailing separators util_RemoveTrailingSeps(strPath); }
//////////////////////////////////////////////////////////////////////////////////// // Function name : util_GetNextPathElement // Description : // starting from the left side of the path string, returns the index'th path element // returns true if the element exists, false if there aren't <index + 1> many elements // // index is ZERO BASED // // 2rd element of ABC/DEF/GH -> GH // 1st element of //ABC/DEF/GH -> DEF // // Return type : bool : got path element? ( i.e. was there index path elements? ) // Argument : const TSTRING& strPathC // Argument : TSTRING& strElem // Argument : int index ///////////////////////////////////////////////////////////////////////////////// bool util_GetNextPathElement(const TSTRING& strPathC, TSTRING& strElem, int index) { // don't do anything if root or empty if (strPathC.empty() || iFSServices::GetInstance()->IsRoot(strPathC)) return false; TSTRING strPath = strPathC; // writable local version bool fMoreSeps = true; TSTRING::size_type nextSep, nextNonSep; nextSep = nextNonSep = (TSTRING::size_type)-1; for (int i = 0; i <= index && fMoreSeps; i++) { // go past leading separators nextNonSep = strPath.find_first_not_of(TW_SLASH, nextSep + 1); if (nextNonSep != TSTRING::npos) { // find index'th slash (start of index'th element) nextSep = strPath.find(TW_SLASH, nextNonSep); // if we're at the end and we haven't found the index'th element // left, then tell the caller that there aren't that many elemnts if (nextSep == TSTRING::npos && i < index) fMoreSeps = false; } else fMoreSeps = false; } // get the element and remove it from the path if (fMoreSeps) strElem = strPath.substr(nextNonSep, nextSep - nextNonSep); return (fMoreSeps); }
bool cGenreParseInfo::DoVarSubstitution( TSTRING &rval ) //throw( eParserHelper ) { cDebug d("cConfigFile::DoVarSubst()"); d.TraceDebug("ORIG: %s\n", rval.c_str()); // walk through string // look for $( // find matching ) // pick out subset // look up in symbol table // substitute subset // continue iterating // step through string // iterate to (slen-1), because we are looking for a two-character sentinel "$(" bool fEscaping = false; for (TSTRING::size_type i = 0; i < rval.size(); i++) { TCHAR c = rval[i]; // is it the "$(" sentinel? (an escaped '$' is not a variable) if (c == '$' && ! fEscaping ) { c = rval[i+1]; if (c == '(') { // ooh, wow! it's a variable! find the end bool found = false; TSTRING::size_type j; for (j = i+1; j < rval.size(); j++) { if (rval[j] == ')') { found = true; break; } } // did we find it? if (!found) { // TODO: throw error return false; } // otherwise, cut out the variable name TSTRING::size_type begin = i + 2; TSTRING::size_type size = j - i - 2; TSTRING varname; varname = rval.substr(begin, size); d.TraceDebug("symbol = %s\n", varname.c_str()); // look up in symbol table TSTRING varvalue; if ( ! LookupVariable( varname, varvalue ) ) throw eParserUseUndefVar( varname ); // replace varname with varvalue rval.replace(begin-2, size+3, varvalue); d.TraceDebug("POST: %s\n", rval.c_str()); // no no no // we should exit function, and get called again // update counters // we should bump the cursor over by the length of the // varvalue (minus one, to compensate for post-increment of index) i += varvalue.size() - 1; goto nextchar; } } else if (c == '\\') { fEscaping = ! fEscaping; } else { fEscaping = false; } nextchar: ; } d.TraceDebug("DONE: %s\n", rval.c_str()); // switch around return true; }
HRESULT SLog::splitFullPath(TSTRING szFullPath) { HRESULT retValue = S_OK; TSTRING szSlash = TSTRING(_T("/\\")); TSTRING szDot = TSTRING(_T(".")); int iLastSlash, iLastDot; bool bBadPath = false; // Quick sanity check... if (szFullPath.empty()) { retValue = ERROR_BAD_ARGUMENTS; goto EXIT; } // First, make sure we actually have strings... if (NULL == m_szLogFilePath) m_szLogFilePath = new TSTRING(); if (NULL == m_szLogFileName) m_szLogFileName = new TSTRING(); if (NULL == m_szLogFileExt) m_szLogFileExt = new TSTRING(); // Make sure they're clear (remember, we may not have created them) m_szLogFilePath->clear(); m_szLogFileName->clear(); m_szLogFileExt->clear(); // To peel apart the string, we need to find the last slash character iLastSlash = szFullPath.find_last_of(szSlash.c_str(), TNPOS); // Now, this could go either way; either we have no slash, in which case // this DOESN'T have a path, or we do and there's a path... if (iLastSlash == TNPOS) { // We didn't get a path... bBadPath = true; } else { // Get the path, if there is one... m_szLogFilePath->append(szFullPath.substr(0, (iLastSlash + 1))); // Does that path actually exist? WIN32_FILE_ATTRIBUTE_DATA dirData; if (!GetFileAttributesEx(m_szLogFilePath->c_str(), GetFileExInfoStandard, &dirData)) { // We hit an error! DWORD dw; dw = GetLastError(); retValue = HRESULT_FROM_WIN32(dw); bBadPath = true; } else if ((dirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) { // The path isn't to a directory! retValue = ERROR_BAD_PATHNAME; bBadPath = true; } } if (bBadPath) { // We either got no path or a bad path, so let's // set it to the Current Directory... // First, get the size of buffer we're going to need int iDirSize = GetCurrentDirectory(0, NULL); // Next, declare the buffer and use it to get the current directory m_szLogFilePath->clear(); TCHAR* szDirBuffer = new TCHAR[iDirSize]; GetCurrentDirectory(iDirSize, szDirBuffer); m_szLogFilePath->append(szDirBuffer); m_szLogFilePath->append(_T("\\")); // Have to add the trailing slash } // To peel apart the extension, we need to find the last dot character iLastDot = szFullPath.find_last_of(szDot.c_str(), TNPOS); // We may or may not have a dot; no dot, no extension if (iLastDot == TNPOS) { iLastDot = szFullPath.length(); m_szLogFileExt->append(DEFAULT_EXT); } else { m_szLogFileExt->append(szFullPath.substr(iLastDot)); } // With all that out of the way, we can get our file name m_szLogFileName->append(szFullPath.substr((iLastSlash + 1), ( iLastDot - iLastSlash - 1 ))); EXIT: return retValue; }