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