1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
26 // include winsock2.h early to eliminate fd_set warning
31 #include "Collections.h"
33 void String::Format (LPCTSTR const pszFormat,...)
36 va_start(args, pszFormat);
37 vFormat(pszFormat,args);
41 String String::SFormat (LPCTSTR const pszFormat,...)
45 va_start(args, pszFormat);
46 s.vFormat(pszFormat,args);
51 void String::vFormat(LPCTSTR pszFormat, va_list marker)
53 for(int nLength=100;nLength;) {
54 TCHAR *buf=new TCHAR[1+nLength];
55 int n=_vsntprintf(buf, nLength, pszFormat, marker );
57 nLength*=2; // NT behavior
58 } else if (n<nLength){
59 string::operator=(buf);
60 nLength=0; // trigger exit from loop
62 nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
68 TCHAR * String::GetBuffer(unsigned int nLength)
70 assert(NULL==m_pszBuf);
71 nLength=MAX(nLength,size()); // to accommodate _tcscpy below
72 m_pszBuf=new TCHAR[1+nLength];
73 m_nBufferLength=nLength;
74 _tcscpy(m_pszBuf,c_str());
78 void String::ReleaseBuffer()
81 m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten
82 string::operator=(m_pszBuf);
87 String String::CStrToUnicodeStr(const char *psz)
91 int nLength=1+strlen(psz);
92 MultiByteToWideChar(CP_ACP, 0, psz, -1, str.GetBuffer(nLength), nLength);
100 int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const
103 #define IsSep(c) (cSep==_TCHAR(' ')?_istspace(c):c==cSep)
107 // Spaces are slightly different from other separators - we treat multiple instances as
108 // just one (a la sscanf)
111 } else if (ar.size()>0) {
117 bool bInString=false;
119 if(*c==_TCHAR('\\') && c[1]){
122 } else if(*c==_TCHAR('"')){
124 } else if (!bInString && IsSep(*c)) {
139 ar.push_back(strTok);
145 char * String::GetCString() const
147 char *psz=new char[1+size()];
149 WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+size(), NULL, NULL);
156 void String::Replace(LPCTSTR psz1, LPCTSTR psz2, bool bObserveEscapes)
158 for(unsigned int nOffset=0;nOffset<size();){
159 LPCTSTR psz=c_str()+nOffset;
160 const TCHAR *pc=_tcsstr(psz,psz1);
162 if(bObserveEscapes && pc>psz && _TCHAR('\\')==pc[-1]){
163 // Substitution protected by escape
164 nOffset=(pc-psz)+_tcslen(psz1);
166 String strNew(psz,pc-psz); // before the substitution
167 strNew+=psz2; // substitution text
168 pc+=_tcslen(psz1); // past the substituted text
169 strNew+=pc; // after the substitution
170 string::operator=(strNew);
171 nOffset=(pc-psz)+_tcslen(psz2);