Mercurial > ecos-v2_0-branch
diff host/tools/Utils/common/Collections.cpp @ 82:6736c52df507 ecos-sw-2000-04-14
Merge from eCos master repository on 2000-04-14-13:35:46-BST
| author | jlarmour |
|---|---|
| date | Tue, 18 Apr 2000 21:51:55 +0000 |
| parents | 435cced73e2f |
| children | 9034ec3868e5 |
line wrap: on
line diff
--- a/host/tools/Utils/common/Collections.cpp +++ b/host/tools/Utils/common/Collections.cpp @@ -32,6 +32,16 @@ void String::Format (LPCTSTR const pszF va_end(args); } +String String::SFormat (LPCTSTR const pszFormat,...) +{ + String s; + va_list args; + va_start(args, pszFormat); + s.vFormat(pszFormat,args); + va_end(args); + return s; +} + void String::vFormat(LPCTSTR pszFormat, va_list marker) { for(int nLength=100;nLength;) { @@ -40,7 +50,7 @@ void String::vFormat(LPCTSTR pszFormat, if(-1==n){ nLength*=2; // NT behavior } else if (n<nLength){ - *this=buf; + string::operator=(buf); nLength=0; // trigger exit from loop } else { nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length @@ -52,7 +62,7 @@ void String::vFormat(LPCTSTR pszFormat, TCHAR * String::GetBuffer(unsigned int nLength) { assert(NULL==m_pszBuf); - nLength=MAX(nLength,GetLength()); // to accommodate _tcscpy below + nLength=MAX(nLength,size()); // to accommodate _tcscpy below m_pszBuf=new TCHAR[1+nLength]; m_nBufferLength=nLength; _tcscpy(m_pszBuf,c_str()); @@ -63,7 +73,7 @@ void String::ReleaseBuffer() { assert(m_pszBuf); m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten - *this=m_pszBuf; + string::operator=(m_pszBuf); delete [] m_pszBuf; m_pszBuf=0; } @@ -84,53 +94,78 @@ String String::CStrToUnicodeStr(const ch int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const { assert('\0'!=cSep); -#define IsSep(c) (cSep==_TCHAR(' ')?isspace(c):c==cSep) +#define IsSep(c) (cSep==_TCHAR(' ')?_istspace(c):c==cSep) LPCTSTR c=c_str(); ar.clear(); - for(;;){ - // Skip multiple separators - while(IsSep(*c))c++; - if(*c==_TCHAR('\0')){ - break; + while(*c){ + // Spaces are slightly different from other separators - we treat multiple instances as + // just one (a la sscanf) + if(_istspace(cSep)){ + while(IsSep(*c))c++; + } else if (ar.size()>0) { + c++; } - String strTok; - if(bObserveStrings){ - bool bInString=false; - do{ - if(*c==_TCHAR('\\') && c[1]){ - strTok+=c[1]; - c++; - } else if(*c==_TCHAR('"')){ - bInString ^= 1; - } else if (!bInString && IsSep(*c)) { - break; - } else { - strTok+=*c; - } - } while (*++c); - } else { - do { - if(IsSep(*c)) { - break; - } else { - strTok+=*c; - } - } while (*++c); + if(*c){ + String strTok; + if(bObserveStrings){ + bool bInString=false; + do{ + if(*c==_TCHAR('\\') && c[1]){ + strTok+=c[1]; + c++; + } else if(*c==_TCHAR('"')){ + bInString ^= 1; + } else if (!bInString && IsSep(*c)) { + break; + } else { + strTok+=*c; + } + } while (*++c); + } else { + do { + if(IsSep(*c)) { + break; + } else { + strTok+=*c; + } + } while (*++c); + } + ar.push_back(strTok); } - ar.push_back(strTok); } return ar.size(); } char * String::GetCString() const - { - char *psz=new char[1+GetLength()]; +{ + char *psz=new char[1+size()]; #ifdef _UNICODE - WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+GetLength(), NULL, NULL); + WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+size(), NULL, NULL); #else - strcpy(psz,c_str()); + strcpy(psz,c_str()); #endif - return psz; + return psz; +} + +void String::Replace(LPCTSTR psz1, LPCTSTR psz2, bool bObserveEscapes) +{ + for(unsigned int nOffset=0;nOffset<size();){ + LPCTSTR psz=c_str()+nOffset; + const TCHAR *pc=_tcsstr(psz,psz1); + if(pc){ + if(bObserveEscapes && pc>psz && _TCHAR('\\')==pc[-1]){ + // Substitution protected by escape + nOffset=(pc-psz)+_tcslen(psz1); + } else { + String strNew(psz,pc-psz); // before the substitution + strNew+=psz2; // substitution text + pc+=_tcslen(psz1); // past the substituted text + strNew+=pc; // after the substitution + string::operator=(strNew); + nOffset=(pc-psz)+_tcslen(psz2); + } + } else { + break; + } } - - +}
