TSTRING Replace( const TSTRING& orignStr, const TSTRING& oldStr, const vector<TSTRING>&vc ) { size_t pos = 0; TSTRING tempStr = orignStr; TSTRING::size_type newStrLen; TSTRING::size_type oldStrLen = oldStr.length(); int i=0; while(true) { pos = tempStr.find(oldStr, pos); if (pos == TSTRING::npos) break; TSTRING s=vc.at(i); s = _T("\'")+s +_T("\'"); newStrLen= s.length(); tempStr.replace(pos, oldStrLen, s); pos += newStrLen; i++; } return tempStr; }
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; }