OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [Utils/] [common/] [Collections.cpp] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.            
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
 
23
// include winsock2.h early to eliminate fd_set warning
24
#ifdef __CYGWIN__
25
#include <winsock2.h>
26
#endif
27
 
28
#include "Collections.h"
29
 
30
void String::Format (LPCTSTR  const pszFormat,...)
31
{
32
  va_list args;
33
  va_start(args, pszFormat);
34
  vFormat(pszFormat,args);
35
  va_end(args);
36
}
37
 
38
String String::SFormat (LPCTSTR  const pszFormat,...)
39
{
40
  String s;
41
  va_list args;
42
  va_start(args, pszFormat);
43
  s.vFormat(pszFormat,args);
44
  va_end(args);
45
  return s;
46
}
47
 
48
void String::vFormat(LPCTSTR  pszFormat, va_list marker)
49
{
50
  for(int nLength=100;nLength;) {
51
    TCHAR *buf=new TCHAR[1+nLength];
52
    int n=_vsntprintf(buf, nLength, pszFormat, marker );
53
    if(-1==n){
54
      nLength*=2;  // NT behavior
55
    } else if (n<nLength){
56
      string::operator=(buf);
57
      nLength=0;   // trigger exit from loop
58
    } else {
59
      nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
60
    }
61
    delete [] buf;
62
  }
63
}
64
 
65
TCHAR * String::GetBuffer(unsigned int nLength)
66
{
67
  assert(NULL==m_pszBuf);
68
  nLength=MAX(nLength,size()); // to accommodate _tcscpy below
69
  m_pszBuf=new TCHAR[1+nLength];
70
  m_nBufferLength=nLength;
71
  _tcscpy(m_pszBuf,c_str());
72
  return m_pszBuf;
73
}
74
 
75
void String::ReleaseBuffer()
76
{
77
  assert(m_pszBuf);
78
  m_pszBuf[m_nBufferLength]=_TCHAR('\0'); // just in case the terminating null has been forgotten
79
  string::operator=(m_pszBuf);
80
  delete [] m_pszBuf;
81
  m_pszBuf=0;
82
}
83
 
84
String String::CStrToUnicodeStr(const char *psz)
85
{
86
  String str;
87
#ifdef _UNICODE
88
  int nLength=1+strlen(psz);
89
  MultiByteToWideChar(CP_ACP, 0, psz, -1, str.GetBuffer(nLength), nLength);
90
  str.ReleaseBuffer();
91
#else
92
  str=psz;
93
#endif
94
  return str;
95
}
96
 
97
int String::Chop(StringArray &ar,TCHAR cSep,bool bObserveStrings/*=TRUE*/) const
98
{
99
  assert('\0'!=cSep);
100
#define IsSep(c) (cSep==_TCHAR(' ')?_istspace(c):c==cSep)
101
  LPCTSTR c=c_str();
102
  ar.clear();
103
  while(*c){
104
    // Spaces are slightly different from other separators - we treat multiple instances as
105
    // just one (a la sscanf)
106
    if(_istspace(cSep)){
107
      while(IsSep(*c))c++;
108
    } else if (ar.size()>0) {
109
      c++;
110
    }
111
    if(*c){
112
      String strTok;
113
      if(bObserveStrings){
114
        bool bInString=false;
115
        do{
116
          if(*c==_TCHAR('\\') && c[1]){
117
            strTok+=c[1];
118
            c++;
119
          } else if(*c==_TCHAR('"')){
120
            bInString ^= 1;
121
          } else if (!bInString && IsSep(*c)) {
122
            break;
123
          } else {
124
            strTok+=*c;
125
          }
126
        } while (*++c);
127
      } else {
128
        do {
129
          if(IsSep(*c)) {
130
            break;
131
          } else {
132
            strTok+=*c;
133
          }
134
        } while (*++c);
135
      }
136
      ar.push_back(strTok);
137
    }
138
  }
139
  return ar.size();
140
}
141
 
142
char * String::GetCString() const
143
{
144
  char *psz=new char[1+size()];
145
#ifdef _UNICODE
146
  WideCharToMultiByte(CP_ACP, 0, c_str(), -1, psz, 1+size(), NULL, NULL);
147
#else
148
  strcpy(psz,c_str());
149
#endif
150
  return psz;
151
}
152
 
153
void String::Replace(LPCTSTR psz1, LPCTSTR psz2, bool bObserveEscapes)
154
{
155
  for(unsigned int nOffset=0;nOffset<size();){
156
    LPCTSTR psz=c_str()+nOffset;
157
    const TCHAR *pc=_tcsstr(psz,psz1);
158
    if(pc){
159
      if(bObserveEscapes && pc>psz && _TCHAR('\\')==pc[-1]){
160
        // Substitution protected by escape
161
        nOffset=(pc-psz)+_tcslen(psz1);
162
      } else {
163
        String strNew(psz,pc-psz); // before the substitution
164
        strNew+=psz2;              // substitution text
165
        pc+=_tcslen(psz1);         // past the substituted text
166
        strNew+=pc;                // after the substitution
167
        string::operator=(strNew);
168
        nOffset=(pc-psz)+_tcslen(psz2);
169
      }
170
    } else {
171
      break;
172
    }
173
  }
174
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.