comparison host/tools/Utils/common/Collections.cpp @ 76:435cced73e2f ecos-v1_3_1-release

eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
author jlarmour
date Tue, 28 Mar 2000 14:10:45 +0000
parents
children 6736c52df507
comparison
equal deleted inserted replaced
75:41bf073c0c32 76:435cced73e2f
1 //####COPYRIGHTBEGIN####
2 //
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
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)
11 // any later version.
12 //
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
16 // more details.
17 //
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.
21 //
22 // ----------------------------------------------------------------------------
23 //
24 //####COPYRIGHTEND####
25 #include "Collections.h"
26
27 void String::Format (LPCTSTR const pszFormat,...)
28 {
29 va_list args;
30 va_start(args, pszFormat);
31 vFormat(pszFormat,args);
32 va_end(args);
33 }
34
35 void String::vFormat(LPCTSTR pszFormat, va_list marker)
36 {
37 for(int nLength=100;nLength;) {
38 TCHAR *buf=new TCHAR[1+nLength];
39 int n=_vsntprintf(buf, nLength, pszFormat, marker );
40 if(-1==n){
41 nLength*=2; // NT behavior
42 } else if (n<nLength){
43 *this=buf;
44 nLength=0; // trigger exit from loop
45 } else {
46 nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
47 }
48 delete [] buf;
49 }
50 }
51
52 TCHAR * String::GetBuffer(unsigned int nLength)
53 {
54 assert(NULL==m_pszBuf);
55 nLength=MAX(nLength,GetLength()); // to accommodate _tcscpy below
56 m_pszBuf=new TCHAR[1+nLength];
57 m_nBufferLength=nLength;
58 _tcscpy(m_pszBuf,c_str());
59 return m_pszBuf;
60 }
61
62 void String::ReleaseBuffer()
63 {
64 assert(m_pszBuf);
65 m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten
66 *this=m_pszBuf;
67 delete [] m_pszBuf;
68 m_pszBuf=0;
69 }
70
71 String String::CStrToUnicodeStr(const char *psz)
72 {
73 String str;
74 #ifdef _UNICODE
75 int nLength=1+strlen(psz);
76 MultiByteToWideChar(CP_ACP, 0, psz, -1, str.GetBuffer(nLength), nLength);
77 str.ReleaseBuffer();
78 #else
79 str=psz;
80 #endif
81 return str;
82 }
83
84 int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const
85 {
86 assert('\0'!=cSep);
87 #define IsSep(c) (cSep==_TCHAR(' ')?isspace(c):c==cSep)
88 LPCTSTR c=c_str();
89 ar.clear();
90 for(;;){
91 // Skip multiple separators
92 while(IsSep(*c))c++;
93 if(*c==_TCHAR('\0')){
94 break;
95 }
96 String strTok;
97 if(bObserveStrings){
98 bool bInString=false;
99 do{
100 if(*c==_TCHAR('\\') && c[1]){
101 strTok+=c[1];
102 c++;
103 } else if(*c==_TCHAR('"')){
104 bInString ^= 1;
105 } else if (!bInString && IsSep(*c)) {
106 break;
107 } else {
108 strTok+=*c;
109 }
110 } while (*++c);
111 } else {
112 do {
113 if(IsSep(*c)) {
114 break;
115 } else {
116 strTok+=*c;
117 }
118 } while (*++c);
119 }
120 ar.push_back(strTok);
121 }
122 return ar.size();
123 }
124
125 char * String::GetCString() const
126 {
127 char *psz=new char[1+GetLength()];
128 #ifdef _UNICODE
129 WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+GetLength(), NULL, NULL);
130 #else
131 strcpy(psz,c_str());
132 #endif
133 return psz;
134 }
135
136