comparison 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
comparison
equal deleted inserted replaced
81:89fef2181d7d 82:6736c52df507
30 va_start(args, pszFormat); 30 va_start(args, pszFormat);
31 vFormat(pszFormat,args); 31 vFormat(pszFormat,args);
32 va_end(args); 32 va_end(args);
33 } 33 }
34 34
35 String String::SFormat (LPCTSTR const pszFormat,...)
36 {
37 String s;
38 va_list args;
39 va_start(args, pszFormat);
40 s.vFormat(pszFormat,args);
41 va_end(args);
42 return s;
43 }
44
35 void String::vFormat(LPCTSTR pszFormat, va_list marker) 45 void String::vFormat(LPCTSTR pszFormat, va_list marker)
36 { 46 {
37 for(int nLength=100;nLength;) { 47 for(int nLength=100;nLength;) {
38 TCHAR *buf=new TCHAR[1+nLength]; 48 TCHAR *buf=new TCHAR[1+nLength];
39 int n=_vsntprintf(buf, nLength, pszFormat, marker ); 49 int n=_vsntprintf(buf, nLength, pszFormat, marker );
40 if(-1==n){ 50 if(-1==n){
41 nLength*=2; // NT behavior 51 nLength*=2; // NT behavior
42 } else if (n<nLength){ 52 } else if (n<nLength){
43 *this=buf; 53 string::operator=(buf);
44 nLength=0; // trigger exit from loop 54 nLength=0; // trigger exit from loop
45 } else { 55 } else {
46 nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length 56 nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
47 } 57 }
48 delete [] buf; 58 delete [] buf;
50 } 60 }
51 61
52 TCHAR * String::GetBuffer(unsigned int nLength) 62 TCHAR * String::GetBuffer(unsigned int nLength)
53 { 63 {
54 assert(NULL==m_pszBuf); 64 assert(NULL==m_pszBuf);
55 nLength=MAX(nLength,GetLength()); // to accommodate _tcscpy below 65 nLength=MAX(nLength,size()); // to accommodate _tcscpy below
56 m_pszBuf=new TCHAR[1+nLength]; 66 m_pszBuf=new TCHAR[1+nLength];
57 m_nBufferLength=nLength; 67 m_nBufferLength=nLength;
58 _tcscpy(m_pszBuf,c_str()); 68 _tcscpy(m_pszBuf,c_str());
59 return m_pszBuf; 69 return m_pszBuf;
60 } 70 }
61 71
62 void String::ReleaseBuffer() 72 void String::ReleaseBuffer()
63 { 73 {
64 assert(m_pszBuf); 74 assert(m_pszBuf);
65 m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten 75 m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten
66 *this=m_pszBuf; 76 string::operator=(m_pszBuf);
67 delete [] m_pszBuf; 77 delete [] m_pszBuf;
68 m_pszBuf=0; 78 m_pszBuf=0;
69 } 79 }
70 80
71 String String::CStrToUnicodeStr(const char *psz) 81 String String::CStrToUnicodeStr(const char *psz)
82 } 92 }
83 93
84 int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const 94 int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const
85 { 95 {
86 assert('\0'!=cSep); 96 assert('\0'!=cSep);
87 #define IsSep(c) (cSep==_TCHAR(' ')?isspace(c):c==cSep) 97 #define IsSep(c) (cSep==_TCHAR(' ')?_istspace(c):c==cSep)
88 LPCTSTR c=c_str(); 98 LPCTSTR c=c_str();
89 ar.clear(); 99 ar.clear();
90 for(;;){ 100 while(*c){
91 // Skip multiple separators 101 // Spaces are slightly different from other separators - we treat multiple instances as
92 while(IsSep(*c))c++; 102 // just one (a la sscanf)
93 if(*c==_TCHAR('\0')){ 103 if(_istspace(cSep)){
94 break; 104 while(IsSep(*c))c++;
105 } else if (ar.size()>0) {
106 c++;
95 } 107 }
96 String strTok; 108 if(*c){
97 if(bObserveStrings){ 109 String strTok;
98 bool bInString=false; 110 if(bObserveStrings){
99 do{ 111 bool bInString=false;
100 if(*c==_TCHAR('\\') && c[1]){ 112 do{
101 strTok+=c[1]; 113 if(*c==_TCHAR('\\') && c[1]){
102 c++; 114 strTok+=c[1];
103 } else if(*c==_TCHAR('"')){ 115 c++;
104 bInString ^= 1; 116 } else if(*c==_TCHAR('"')){
105 } else if (!bInString && IsSep(*c)) { 117 bInString ^= 1;
106 break; 118 } else if (!bInString && IsSep(*c)) {
107 } else { 119 break;
108 strTok+=*c; 120 } else {
109 } 121 strTok+=*c;
110 } while (*++c); 122 }
111 } else { 123 } while (*++c);
112 do { 124 } else {
113 if(IsSep(*c)) { 125 do {
114 break; 126 if(IsSep(*c)) {
115 } else { 127 break;
116 strTok+=*c; 128 } else {
117 } 129 strTok+=*c;
118 } while (*++c); 130 }
131 } while (*++c);
132 }
133 ar.push_back(strTok);
119 } 134 }
120 ar.push_back(strTok);
121 } 135 }
122 return ar.size(); 136 return ar.size();
123 } 137 }
124 138
125 char * String::GetCString() const 139 char * String::GetCString() const
126 { 140 {
127 char *psz=new char[1+GetLength()]; 141 char *psz=new char[1+size()];
128 #ifdef _UNICODE 142 #ifdef _UNICODE
129 WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+GetLength(), NULL, NULL); 143 WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+size(), NULL, NULL);
130 #else 144 #else
131 strcpy(psz,c_str()); 145 strcpy(psz,c_str());
132 #endif 146 #endif
133 return psz; 147 return psz;
148 }
149
150 void String::Replace(LPCTSTR psz1, LPCTSTR psz2, bool bObserveEscapes)
151 {
152 for(unsigned int nOffset=0;nOffset<size();){
153 LPCTSTR psz=c_str()+nOffset;
154 const TCHAR *pc=_tcsstr(psz,psz1);
155 if(pc){
156 if(bObserveEscapes && pc>psz && _TCHAR('\\')==pc[-1]){
157 // Substitution protected by escape
158 nOffset=(pc-psz)+_tcslen(psz1);
159 } else {
160 String strNew(psz,pc-psz); // before the substitution
161 strNew+=psz2; // substitution text
162 pc+=_tcslen(psz1); // past the substituted text
163 strNew+=pc; // after the substitution
164 string::operator=(strNew);
165 nOffset=(pc-psz)+_tcslen(psz2);
166 }
167 } else {
168 break;
169 }
134 } 170 }
135 171 }
136