Subversion Repositories spk

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 cycrow 1
#include "String.h"
2
 
3
#include <sstream>
4
#include <stdarg.h>
5
 
6
namespace Utils {
7
 
8
String::String(void)						{ this->assign(""); }
9
String::String(const char *str)				{ this->assign(str); }
10
String::String(const unsigned char *str)	{ this->assign((const char *)str); }
11
String::String(const std::string &str)		{ this->assign(str);}
12
String::String(const unsigned char c)		{ this->assign(1, c); }
13
String::String(const String &str)			{ this->assign(str); }
14
String::String(long l)						{ this->fromLong(l); }
15
String::String(unsigned long l)				{ this->fromLong((long)l); }
16
String::String(float f)						{ this->fromFloat(f); }
17
String::String(double f)					{ this->fromDouble(f); }
18
 
19
String::~String(void)
20
{
21
}
22
 
23
/////////////////////////////////////////////////////////////////
24
// Conversion functions
25
 
26
void String::fromLong(long l)
27
{
28
	std::stringstream strstream;
29
	strstream << l;
30
	strstream >> *this;
31
}
32
 
33
void String::fromFloat(float f, int dp)
34
{
35
	std::stringstream strstream;
36
	strstream.precision(dp);
37
	strstream << std::fixed << f;
38
	strstream >> *this;
39
}
40
 
41
void String::fromFloat(float f)
42
{
43
	std::stringstream strstream;
44
	strstream << f;
45
	strstream >> *this;
46
}
47
void String::fromDouble(double f)
48
{
49
	std::stringstream strstream;
50
	strstream << f;
51
	strstream >> *this;
52
}
53
 
54
const String &String::format(const char *sFormat, ...)
55
{
56
	char buffer[1024];
57
	va_list args;
58
	va_start (args, sFormat);
59
	vsprintf (buffer, sFormat, args);
60
	va_end (args);
61
 
62
	this->assign(buffer);
63
	return (*this);
64
}
65
 
66
String String::fromFormat(const char *sFormat, ...)
67
{
68
	char buffer[1024];
69
	va_list args;
70
	va_start (args, sFormat);
71
	vsprintf (buffer, sFormat, args);
72
	va_end (args);
73
 
74
	return String(buffer);
75
}
76
 
77
long String::toLong() const
78
{
79
	return atoi(this->c_str());
80
}
81
double String::toDouble() const
82
{
83
	return atof(this->c_str());
84
}
85
float String::toFloat() const
86
{
87
	return (float)atof(this->c_str());
88
}
89
 
90
 
91
const String &String::operator= ( const char *str )				{ this->assign(str); return (*this); }
92
const String &String::operator= ( const unsigned char *str )	{ this->assign((const char *)str); return (*this); }
93
const String &String::operator= ( const String &str )			{ this->assign((std::string &)str); return (*this); }
94
const String &String::operator= ( const std::string &str )		{ this->assign(str); return (*this); }
95
const String &String::operator= ( unsigned char c )				{ this->assign(1, c); return (*this); }
96
const String &String::operator= ( char c )						{ this->assign(1, c); return (*this); }
97
const String &String::operator= ( long l )						{ this->fromLong(l); return (*this); }
98
const String &String::operator= ( unsigned long l )				{ this->fromLong(l); return (*this); }
99
const String &String::operator= ( float f )						{ this->fromFloat(f); return (*this); }
100
const String &String::operator= ( double f )					{ this->fromDouble(f); return (*this); }
101
 
102
const unsigned char String::operator[] ( int num ) const			{ return this->at(num); }
103
unsigned char &String::operator[] ( int num )						{ return (unsigned char &)this->at(num); }
104
 
105
const String &String::operator+= ( const char *str )			{ this->append(str); return (*this); }
106
const String &String::operator+= ( const unsigned char *str ) 	{ this->append((const char *)str); return (*this); }
107
const String &String::operator+= ( const std::string &str )		{ this->append(str); return (*this); }
108
const String &String::operator+= ( const String &str )		{ this->append(str); return (*this); }
109
const String &String::operator+= ( const char c )				{ this->append(1, c); return (*this); }
110
const String &String::operator+= ( const unsigned char c )		{ this->append(1, c); return (*this); }
111
const String &String::operator+= ( const unsigned long l )		
112
{ 
113
	std::stringstream strm;
114
	strm << (*this) << l;
115
	strm >> (*this);
116
	return (*this); 
117
}
118
const String &String::operator+= ( const long l )		
119
{ 
120
	std::stringstream strm;
121
	strm << (*this) << l;
122
	strm >> (*this);
123
	return (*this); 
124
}
125
const String &String::operator+= ( const float f )		
126
{ 
127
	std::stringstream strm;
128
	strm << (*this) << f;
129
	strm >> (*this);
130
	return (*this); 
131
}
132
const String &String::operator+= ( const double f )		
133
{ 
134
	std::stringstream strm;
135
	strm << (*this) << f;
136
	strm >> (*this);
137
	return (*this); 
138
}
139
 
140
 
141
String String::operator+ ( const char *str ) const			{ return String(*this) += str; }
142
String String::operator+ ( const unsigned char *str ) const	{ return String(*this) += str; }
143
String String::operator+ ( const std::string &str ) const	{ return String(*this) += str; }
144
String String::operator+ ( const String &str ) const		{ return String(*this) += str; }
145
String String::operator+ ( const char c ) const				{ return String(*this) += c; }
146
String String::operator+ ( const unsigned char c ) const	{ return String(*this) += c; }
147
String String::operator+ ( const long l ) const				{ return String(*this) += l; }
148
String String::operator+ ( const unsigned long l ) const	{ return String(*this) += l; }
149
String String::operator+ ( const float f ) const			{ return String(*this) += f; }
150
String String::operator+ ( const double f ) const			{ return String(*this) += f; }
151
 
152
bool String::operator== ( const char *str ) const				{ return (this->compare(str)) ? false : true; }
153
bool String::operator== ( const unsigned char *str ) const	{ return (this->compare((const char *)str)) ? false : true; }
154
bool String::operator== ( const std::string &str ) const		{ return (this->compare(str)) ? false : true; }
155
bool String::operator== ( const String &str ) const			{ return (this->compare(str)) ? false : true; }
156
 
157
bool String::operator!= ( const char *str ) const				{ return (this->compare(str)) ? true : false; }
158
bool String::operator!= ( const unsigned char *str ) const	{ return (this->compare((const char *)str)) ? true : false; }
159
bool String::operator!= ( const std::string &str ) const		{ return (this->compare(str)) ? true : false; }
160
bool String::operator!= ( const String &str ) const			{ return (this->compare(str)) ? true : false; }
161
 
162
String operator+(const char *str1, const String &str2)
163
{
164
	return String(str1) + str2;
165
}
166
 
167
bool String::Compare(const String &str, bool bCaseSensative) const
168
{
169
	if ( bCaseSensative ) {
170
		return (this->compare(str) == 0) ? true : false;
171
	}
172
 
173
	if ( str.length() != this->length() ) return false;
174
	for ( unsigned int i = 0; i < str.length(); i++ ) {
175
		if ( tolower(this->at(i)) != tolower(str[i]) ) {
176
			return false;
177
		}
178
	}
179
	return true;
180
}
181
 
182
bool String::Compare(const unsigned char *str, bool bCaseSensative) const
183
{
184
	return this->Compare((const char *)str, bCaseSensative);
185
}
186
 
187
bool String::Compare(const char *str, bool bCaseSensative) const
188
{
189
	if ( bCaseSensative ) {
190
		return (this->compare(str) == 0) ? true : false;
191
	}
192
 
193
	if ( strlen(str) != this->length() ) return false;
194
	for ( unsigned int i = 0; i < this->length(); i++ ) {
195
		if ( tolower(this->at(i)) != tolower(*(str + i)) ) {
196
			return false;
197
		}
198
	}
199
 
200
	return true;
201
}
202
 
203
String String::token(const char *token, int tok) const
204
{
205
	return this->tokens(token, tok, tok);
206
}
207
 
208
String String::tokens(const char *token, int from, int to) const
209
{
210
	if ( this->empty() ) return "";
211
 
212
	int max = this->countToken(token);
213
 
214
	std::string newstr;
215
 
216
	int whichtok = 1;
217
	std::string::size_type lastPos = 0;
218
	std::string::size_type pos     = _token_nextPos(token, 0);
219
 
220
	if ( from < 0 )
221
		from = max + (from + 1);
222
	if ( to < 0 )
223
		to = max + (to + 1);
224
 
225
	while (std::string::npos != pos || std::string::npos != lastPos)
226
	{
227
		if ( to == 0 )
228
		{
229
			if ( whichtok >= from )
230
			{
231
				if ( newstr != "" ) newstr = newstr + token;
232
				newstr = newstr + this->substr(lastPos, pos - lastPos);
233
			}
234
		}
235
		else
236
		{
237
			if ( (whichtok >= from) && (whichtok <= to) )
238
			{
239
				if ( newstr != "" ) newstr = newstr + token;
240
				newstr = newstr + this->substr(lastPos, pos - lastPos);
241
			}
242
			if ( whichtok > to )
243
				break;
244
		}
245
		// Found a token, add it to the vector.
246
		whichtok++;
247
 
248
		if ( pos >= this->length() )
249
			break;
250
 
251
		// skip past token
252
		size_t i = 0;
253
		size_t max = strlen(token);
254
		lastPos = pos;
255
		while ( (i < max) && (token[i] == this->at(lastPos)) )
256
		{
257
			++i;
258
			++lastPos;
259
		}
260
 
261
		// get the next token
262
		pos = _token_nextPos(token, lastPos);
263
	}
264
	return newstr;
265
}
266
 
267
int String::countToken(const char *token) const
268
{
269
	if ( this->empty() )
270
		return 0;
271
 
272
	// finds the number of tokens in a string
273
	int found = 1;
274
 
275
	std::string tmpstr = *this;
276
 
277
	// ignore the initial spaces
278
	std::string::size_type pos = this->find_first_not_of(token, 0);
279
 
280
	// remove end spaces
281
	size_t i = tmpstr.size() - 1;
282
	while ( tmpstr[i] == ' ' && i > 0 )
283
		i--;
284
	if ( i <= 0 ) return 0;
285
	tmpstr.erase ( i + 1, tmpstr.size() - i );
286
 
287
	// count tokens
288
	pos = tmpstr.find (token,pos + 1);
289
	while ( pos < std::string::npos )
290
	{
291
		while (tmpstr[pos] == ' ')
292
			pos++;
293
		found++;
294
		pos = tmpstr.find (token,pos + 1);
295
	}
296
 
297
	// return number of tokens
298
	return found;
299
}
300
 
301
std::string::size_type String::_token_nextPos(const char *token, std::string::size_type curPos) const
302
{
303
	bool found = false;
304
	std::string::size_type startPos = 0;
305
	size_t max = strlen(token);
306
	while ( !found ) {
307
		found = true;
308
		startPos = this->find_first_of(token[0], curPos);
309
		if ( startPos == std::string::npos ) {
310
			startPos = this->length();
311
			break;
312
		}
313
 
314
		std::string::size_type pos = startPos;
315
		size_t i = 1;
316
		while ( i < max )
317
		{
318
			++pos;
319
			if ( this->at(pos) != token[i] )
320
			{
321
				found = false;
322
				break;
323
			}
324
			++i;
325
		}
326
		curPos = pos;
327
	}
328
	return startPos;
329
}
330
 
331
String String::findReplace(const String &find, const String &replace ) const
332
{
333
	std::string newstr = *this;
334
	std::string::size_type pos = newstr.find(find, 0);
335
	while ( pos < std::string::npos ) {
336
		newstr.replace(pos, find.length(), replace);
337
		pos = newstr.find(find, pos + replace.length());
338
	}
339
 
340
	return newstr;
341
}
342
 
343
bool String::isin(const String &str, bool bCaseSensative) const
344
{
345
	unsigned int check = 0;
346
	for ( unsigned int i = 0; i < this->length(); i++ )
347
	{
348
		bool bCheck	= (bCaseSensative) ? (this->at(i) == str[check]) : (tolower(this->at(i)) == tolower(str[check]));
349
		if ( bCheck )
350
			++check;
351
		else
352
			check = 0;
353
 
354
		if ( check >= str.length() )
355
			return true;
356
	}
357
	return false;
358
}
359
 
360
bool String::isin(char c, bool bCaseSensative) const
361
{
362
	bool f = false;
363
	for ( unsigned int i = 0; i < this->length(); i++ )
364
	{
365
		bool bCheck = (bCaseSensative) ? (this->at(i) == c) : (tolower(this->at(i)) == tolower(c));
366
		if ( bCheck ) {
367
			f = true;
368
			break;
369
		}
370
	}
371
	return f;
372
}
373
 
374
String String::left(long num) const
375
{
376
	if ( num < 0 )
377
		num = (long)this->length() + num;
378
	if ( num > (long)this->length() )
379
		num = (long)this->length();
380
 
381
	return this->substr(0, num);
382
}
383
 
384
String String::right(int num) const
385
{
386
	if ( num > 0 ) num -= (int)this->length();
387
	if ( num < -(int)this->length() ) num = -(int)this->length();
388
 
389
	return this->substr(-num);
390
}
391
 
392
char *String::readToEndOfLine(char *data)
393
{
394
	return (char *)this->readToEndOfLine((unsigned char *)data);
395
}
396
unsigned char *String::readToEndOfLine(unsigned char *data)
397
{
398
	int pos = 0;
399
 
400
	// do until we get a line break
401
	while ( (data[pos] != 13) && (data[pos] != 0) && (data[pos] != '\n') ) {
402
		++pos;
403
	}
404
 
405
	if ( !pos ) {
406
		this->clear();
407
	}
408
	else {
409
		if ( pos > 2000 ) {
410
			char *d = new char[pos + 1];
411
			memcpy(d, data, pos);
412
			d[pos] = 0;
413
			*this = d;
414
			delete d;
415
		}
416
		else {
417
			char d[2000];
418
			memcpy(d, data, pos);
419
			d[pos] = 0;
420
			*this = d;
421
		}
422
	}
423
 
424
	if ( data[pos] == 0 ) return NULL;
425
	return data + (pos + 1);
426
}
427
 
10 cycrow 428
String *String::tokenise(const char *token, int *max) const
429
{
430
	if ( empty() ) {
431
		*max = 0;
432
		return NULL;
433
	}
8 cycrow 434
 
10 cycrow 435
	*max = this->countToken(token);
8 cycrow 436
 
10 cycrow 437
	if ( (*max) <= 0 ) {
438
		*max = 0;
439
		return NULL;
440
	}
441
 
442
	String *newstr = (*max == 1) ? new String : new String[*max];
443
 
444
	int whichtok = 0;
445
	// get first token
446
	std::string::size_type lastPos = 0;
447
	std::string::size_type pos     = this->_token_nextPos(token, 0);
448
 
449
	while (std::string::npos != pos || std::string::npos != lastPos)
450
	{
451
		// set token
452
		if ( *max == 1 ) *newstr = this->substr(lastPos, pos - lastPos);
453
		else			  newstr[whichtok] = this->substr(lastPos, pos - lastPos);
454
 
455
		// move to next token
456
		whichtok++;
457
		if ( whichtok >= *max )
458
			break;
459
 
460
		if ( pos >= this->length() )
461
			break;
462
 
463
		// skip past token
464
		size_t i = 0;
465
		size_t max = strlen(token);
466
		lastPos = pos;
467
		while ( (i < max) && (token[i] == this->at(lastPos)) ) {
468
			++i;
469
			++lastPos;
470
		}
471
 
472
		// get the next token
473
		pos = _token_nextPos(token, lastPos);
474
	}
475
 
476
	return newstr;
477
}
478
 
479
const String &String::remove(char c)
480
{
481
	std::string::size_type pos = this->find_first_of(c, 0);
482
	while(pos != std::string::npos) {
483
		this->erase(pos, 1);
484
		pos = this->find_first_of(c, 0);
485
	}
486
 
487
	return (*this);
488
}
489
 
490
String String::removeChar(char c) const
491
{
492
	String newStr = *this;
493
 
494
	std::string::size_type pos = newStr.find_first_of(c, 0);
495
	while(pos != std::string::npos) {
496
		newStr.erase(pos, 1);
497
		pos = newStr.find_first_of(c, 0);
498
	}
499
 
500
	return newStr;
501
}
502
 
8 cycrow 503
}//NAMESPACE