Advanced Strategic Command
ascstring.cpp
Go to the documentation of this file.
1 
2 #include "ascstring.h"
3 
37 {
38 // auto_ptr< charT > l_autopBuf ( new charT [ length () + sizeof ( charT ) ] );
39 // charT* l_pBuf = l_autopBuf.get();
40  charT* l_pBuf = new charT [ length () + sizeof ( charT ) ];
41 
42  ASCStringHelpers::_Strcpy ( l_pBuf, c_str () );
43  ASCStringHelpers::_Strlwr ( l_pBuf );
44  assign ( l_pBuf );
45 
46  delete[] l_pBuf;
47  return *this;
48 }
49 
56 {
57 // auto_ptr< charT > l_autopBuf ( new charT [ length () + sizeof ( charT ) ] );
58  charT* l_pBuf = new charT [ length () + sizeof ( charT ) ];
59 
60  ASCStringHelpers::_Strcpy ( l_pBuf, c_str () );
61  ASCStringHelpers::_Strupr ( l_pBuf );
62  assign ( l_pBuf );
63 
64  delete[] l_pBuf;
65  return *this;
66 }
67 
78 ASCString& ASCString::format ( const charT* pFormat, ... )
79 {
80  std::va_list arg_ptr;
81  va_start ( arg_ptr, pFormat );
82 
83  vaformat( pFormat, arg_ptr );
84 
85  va_end ( arg_ptr );
86 
87  return *this;
88 }
89 
90 
91 ASCString& ASCString::vaformat ( const charT* pFormat, va_list ap )
92 {
93  int l_iNbChar = 10000;
94  bool l_bIsDone = false;
95 
96  while ( l_bIsDone == false )
97  {
98  charT* l_pBuf = new charT [ l_iNbChar ];
99 
100  int l_iNbCharWritten = ASCStringHelpers::_Vsnprintf ( l_pBuf, l_iNbChar, pFormat, ap );
101 
102  if ( l_iNbCharWritten != -1 )
103  {
104  // ok, l_pBuf was large enough to hold the whole formated string
105  assign ( l_pBuf );
106  l_bIsDone = true;
107  }
108  else
109  {
110  // l_pBuf is not large enough to hold the whole formated string.
111  // Double the number of characters l_pBuf can hold and retry
112  // to format the string.
113  l_iNbChar *= 2;
114  }
115 
116  delete [] l_pBuf;
117  };
118  return *this;
119 }
120 
121 
138 {
139  ASCStringHelpers::_Printf ( c_str () );
140 }
141 
142 
146 bool ASCString::endswith( const ASCString& s ) const
147 {
148  size_type p = rfind( s );
149  if ( p != npos )
150  return p == length() - s.length();
151  else
152  return false;
153 }
154 
155 
156 
167 ASCString copytoLower ( const ASCString& String )
168 {
169  ASCString l_TempString ( String );
170  l_TempString.toLower ();
171 
172  return l_TempString;
173 }
174 
185 ASCString copytoUpper ( const ASCString& String )
186 {
187  ASCString l_TempString ( String );
188  l_TempString.toUpper ();
189 
190  return l_TempString;
191 }
192 
194 {
195  ASCString s;
196  s.format("%d",i);
197  return s;
198 }
199 
201 {
202  ASCString s;
203  s.format("%u",i);
204  return s;
205 }
206 
207 
208 #ifdef SIZE_T_not_identical_to_INT
209 ASCString ASCString::toString( size_t i )
210 {
211  ASCString s;
212  s.format("%d",i);
213  return s;
214 }
215 #endif
216 
217 
219 {
220  ASCString s;
221  s.format("%f",d);
222  return s;
223 }
224 
225 const ASCString operator+ ( const ASCString& s1, const ASCString& s2 )
226 {
227  ASCString s = s1;
228  s += s2;
229  return s;
230 }
231 
232 const ASCString operator+ ( const char* s1, const ASCString& s2 )
233 {
234  ASCString s = s1;
235  s += s2;
236  return s;
237 }
238 
239 ASCString& ASCString::replaceAll( const ASCString& old, const ASCString& newString)
240 {
241  ASCString::size_type it;
242  while ( (it = find(old)) != ASCString::npos)
243  replace( it, old.length(), newString);
244 
245  return *this;
246 }
247 
248 ASCString& ASCString::replaceAll_ci( const ASCString& old, const ASCString& newString)
249 {
250  ASCString::size_type it ;
251  ASCString old2 = copytoLower(old);
252  do {
253  ASCString tmp = copytoLower( *this );
254 
255  it = tmp.find( old2 );
256  if ( it != npos )
257  replace( it, old.length(), newString);
258 
259  } while ( it != npos);
260 
261  return *this;
262 }
263 
264 
static charT * _Strcpy(charT *pDest, const charT *pSrc)
ASCString & toLower()
Definition: ascstring.cpp:36
static charT * _Strlwr(charT *pS)
ASCStringHelpers::charT charT
Definition: ascstring.h:22
ASCString & toUpper()
Definition: ascstring.cpp:55
ASCString & vaformat(const charT *pFormat, va_list ap)
Definition: ascstring.cpp:91
static int _Printf(const charT *format,...)
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ASCString copytoLower(const ASCString &String)
Definition: ascstring.cpp:167
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
const ASCString operator+(const ASCString &s1, const ASCString &s2)
Definition: ascstring.cpp:225
static int _Vsnprintf(charT *buffer, size_t count, const charT *format, std::va_list argptr)
ASCString copytoUpper(const ASCString &String)
Definition: ascstring.cpp:185
static charT * _Strupr(charT *pS)
void printf()
Definition: ascstring.cpp:137
bool endswith(const ASCString &s) const
Checks if the last characters of string are equal to s.
Definition: ascstring.cpp:146
ASCString & replaceAll(const ASCString &old, const ASCString &newString)
replaces all occurances of old with newString
Definition: ascstring.cpp:239
ASCString & replaceAll_ci(const ASCString &old, const ASCString &newString)
replaces all occurances of old with newString ; case insensitive
Definition: ascstring.cpp:248