Logo Search packages:      
Sourcecode: condor version File versions  Download package

bool MyString::replaceString ( const char *  pszToReplace,
const char *  pszReplaceWith,
int  iStartFromPos = 0 
)

Replaces a substring with another substring. It's okay for the new string to be empty, so you end up deleting a substring. Returns true iff it found any instances of the pszToReplace substring.

Definition at line 430 of file MyString.cpp.

References find().

Referenced by DaemonCore::Create_Process(), CronTab::expandParameter(), JobDisconnectedEvent::readEvent(), ParallelShadow::replaceNode(), MPIShadow::replaceNode(), and Job::ResolveVarsInterpolations().

{
      SimpleList<int> listMatchesFound;         
      
      int iToReplaceLen = strlen(pszToReplace);
      if (!iToReplaceLen) {
            return false;
      }
      
      int iWithLen = strlen(pszReplaceWith);
      while (iStartFromPos <= Len){
            iStartFromPos = find(pszToReplace, iStartFromPos);
            if (iStartFromPos == -1)
                  break;
            listMatchesFound.Append(iStartFromPos);
            iStartFromPos += iToReplaceLen;
      }
      if (!listMatchesFound.Number())
            return false;
      
      int iLenDifPerMatch = iWithLen - iToReplaceLen;
      int iNewLen = Len + iLenDifPerMatch * listMatchesFound.Number();
      char *pNewData = new char[iNewLen+1];
            
      int iItemStartInData;
      int iPosInNewData = 0;
      int iPreviousEnd = 0;
      listMatchesFound.Rewind();
      while(listMatchesFound.Next(iItemStartInData)) {
            memcpy(pNewData + iPosInNewData, 
                     Data + iPreviousEnd, 
                     iItemStartInData - iPreviousEnd);
            iPosInNewData += (iItemStartInData - iPreviousEnd);
            memcpy(pNewData + iPosInNewData, pszReplaceWith, iWithLen);
            iPosInNewData += iWithLen;
            iPreviousEnd = iItemStartInData + iToReplaceLen;
      }
      memcpy(pNewData + iPosInNewData, 
               Data + iPreviousEnd, 
               Len - iPreviousEnd + 1);
      delete [] Data;
      Data = pNewData;
      capacity = iNewLen;
      Len = iNewLen;
      
      return true;
}


Generated by  Doxygen 1.6.0   Back to index