Subversion Repositories spk

Rev

Rev 13 | Rev 34 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13 Rev 14
Line 14... Line 14...
14
String::String(void)						{ this->assign(""); }
14
String::String(void)						{ this->assign(""); }
15
String::String(const char *str)				{ this->assign(str); }
15
String::String(const char *str)				{ this->assign(str); }
16
String::String(const unsigned char *str)	{ this->assign((const char *)str); }
16
String::String(const unsigned char *str)	{ this->assign((const char *)str); }
17
String::String(const std::string &str)		{ this->assign(str);}
17
String::String(const std::string &str)		{ this->assign(str);}
18
String::String(const unsigned char c)		{ this->assign(1, c); }
18
String::String(const unsigned char c)		{ this->assign(1, c); }
-
 
19
String::String(const char c)				{ this->assign(1, c); }
19
String::String(const String &str)			{ this->assign(str); }
20
String::String(const String &str)			{ this->assign(str); }
20
String::String(long l)						{ this->fromLong(l); }
21
String::String(long l)						{ this->fromLong(l); }
21
String::String(unsigned long l)				{ this->fromLong((long)l); }
22
String::String(unsigned long l)				{ this->fromLong((long)l); }
22
String::String(float f)						{ this->fromFloat(f); }
23
String::String(float f)						{ this->fromFloat(f); }
23
String::String(double f)					{ this->fromDouble(f); }
24
String::String(double f)					{ this->fromDouble(f); }
Line 31... Line 32...
31
 
32
 
32
void String::fromLong(long l)
33
void String::fromLong(long l)
33
{
34
{
34
	std::stringstream strstream;
35
	std::stringstream strstream;
35
	strstream << l;
36
	strstream << l;
36
	strstream >> *this;
37
	*this = strstream.str();
37
}
38
}
38
 
39
 
39
void String::fromFloat(float f, int dp)
40
void String::fromFloat(float f, int dp)
40
{
41
{
41
	std::stringstream strstream;
42
	std::stringstream strstream;
42
	strstream.precision(dp);
43
	strstream.precision(dp);
43
	strstream << std::fixed << f;
44
	strstream << std::fixed << f;
44
	strstream >> *this;
45
	*this = strstream.str();
45
}
46
}
46
 
47
 
47
void String::fromFloat(float f)
48
void String::fromFloat(float f)
48
{
49
{
49
	std::stringstream strstream;
50
	std::stringstream strstream;
50
	strstream << f;
51
	strstream << f;
51
	strstream >> *this;
52
	*this = strstream.str();
52
}
53
}
53
void String::fromDouble(double f)
54
void String::fromDouble(double f)
54
{
55
{
55
	std::stringstream strstream;
56
	std::stringstream strstream;
56
	strstream << f;
57
	strstream << f;
57
	strstream >> *this;
58
	*this = strstream.str();
58
}
59
}
59
 
60
 
60
const String &String::format(const char *sFormat, ...)
61
const String &String::format(const char *sFormat, ...)
61
{
62
{
62
	char buffer[1024];
63
	char buffer[1024];
Line 123... Line 124...
123
}
124
}
124
const String &String::operator+= ( const long l )		
125
const String &String::operator+= ( const long l )		
125
{ 
126
{ 
126
	std::stringstream strm;
127
	std::stringstream strm;
127
	strm << (*this) << l;
128
	strm << (*this) << l;
128
	strm >> (*this);
129
	*this = strm.str();
129
	return (*this); 
130
	return (*this); 
130
}
131
}
131
const String &String::operator+= ( const float f )		
132
const String &String::operator+= ( const float f )		
132
{ 
133
{ 
133
	std::stringstream strm;
134
	std::stringstream strm;
134
	strm << (*this) << f;
135
	strm << (*this) << f;
135
	strm >> (*this);
136
	*this = strm.str();
136
	return (*this); 
137
	return (*this); 
137
}
138
}
138
const String &String::operator+= ( const double f )		
139
const String &String::operator+= ( const double f )		
139
{ 
140
{ 
140
	std::stringstream strm;
141
	std::stringstream strm;
141
	strm << (*this) << f;
142
	strm << (*this) << f;
142
	strm >> (*this);
143
	*this = strm.str();
143
	return (*this); 
144
	return (*this); 
144
}
145
}
145
 
146
 
146
 
147
 
147
String String::operator+ ( const char *str ) const			{ return String(*this) += str; }
148
String String::operator+ ( const char *str ) const			{ return String(*this) += str; }
Line 172... Line 173...
172
	}
173
	}
173
 
174
 
174
	if ( str.length() != this->length() ) return false;
175
	if ( str.length() != this->length() ) return false;
175
	for ( unsigned int i = 0; i < str.length(); i++ ) {
176
	for ( unsigned int i = 0; i < str.length(); i++ ) {
176
		if ( tolower(this->at(i)) != tolower(str[i]) ) {
177
		if ( tolower(this->at(i)) != tolower(str[i]) ) {
177
			return false;
178
			return false;
178
		}
179
		}
179
	}
180
	}
180
	return true;
181
	return true;
-
 
182
}
-
 
183
 
-
 
184
bool String::Compare(const unsigned char *str, bool bCaseSensative) const
-
 
185
{
-
 
186
	return this->Compare((const char *)str, bCaseSensative);
181
}
187
}
182
 
188
 
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
bool String::Compare(const char *str, bool bCaseSensative) const
189
{
190
{
190
	if ( bCaseSensative ) {
191
	if ( bCaseSensative ) {
191
		return (this->compare(str) == 0) ? true : false;
192
		return (this->compare(str) == 0) ? true : false;
192
	}
193
	}
193
 
194
 
Line 195... Line 196...
195
	for ( unsigned int i = 0; i < this->length(); i++ ) {
196
	for ( unsigned int i = 0; i < this->length(); i++ ) {
196
		if ( tolower(this->at(i)) != tolower(*(str + i)) ) {
197
		if ( tolower(this->at(i)) != tolower(*(str + i)) ) {
197
			return false;
198
			return false;
198
		}
199
		}
199
	}
200
	}
200
 
201
 
201
	return true;
202
	return true;
202
}
203
}
203
 
204
 
204
String String::token(const char *token, int tok) const
205
String String::token(const char *token, int tok) const
205
{
206
{
Line 207... Line 208...
207
}
208
}
208
 
209
 
209
String String::tokens(const char *token, int from, int to) const
210
String String::tokens(const char *token, int from, int to) const
210
{
211
{
211
	if ( this->empty() ) return "";
212
	if ( this->empty() ) return "";
212
 
213
 
213
	int max = this->countToken(token);
214
	int max = this->countToken(token);
214
 
215
 
215
	std::string newstr;
216
	std::string newstr;
216
 
217
 
217
	int whichtok = 1;
218
	int whichtok = 1;
Line 220... Line 221...
220
 
221
 
221
	if ( from < 0 )
222
	if ( from < 0 )
222
		from = max + (from + 1);
223
		from = max + (from + 1);
223
	if ( to < 0 )
224
	if ( to < 0 )
224
		to = max + (to + 1);
225
		to = max + (to + 1);
225
 
226
 
226
	while (std::string::npos != pos || std::string::npos != lastPos)
227
	while (std::string::npos != pos || std::string::npos != lastPos)
227
	{
228
	{
228
		if ( to == 0 )
229
		if ( to == 0 )
229
		{
230
		{
230
			if ( whichtok >= from )
231
			if ( whichtok >= from )
231
			{
232
			{
232
				if ( newstr != "" ) newstr = newstr + token;
233
				if ( newstr != "" ) newstr = newstr + token;
233
				newstr = newstr + this->substr(lastPos, pos - lastPos);
234
				newstr = newstr + this->substr(lastPos, pos - lastPos);
234
			}
235
			}
235
		}
236
		}
236
		else
237
		else
237
		{
238
		{
238
			if ( (whichtok >= from) && (whichtok <= to) )
239
			if ( (whichtok >= from) && (whichtok <= to) )
239
			{
240
			{
Line 270... Line 271...
270
	if ( this->empty() )
271
	if ( this->empty() )
271
		return 0;
272
		return 0;
272
 
273
 
273
	// finds the number of tokens in a string
274
	// finds the number of tokens in a string
274
	int found = 1;
275
	int found = 1;
275
 
276
 
276
	std::string tmpstr = *this;
277
	std::string tmpstr = *this;
277
 
278
 
278
	// ignore the initial spaces
279
	// ignore the initial spaces
279
	std::string::size_type pos = this->find_first_not_of(token, 0);
280
	std::string::size_type pos = this->find_first_not_of(token, 0);
280
 
281
 
281
	// remove end spaces
282
	// remove end spaces
282
	size_t i = tmpstr.size() - 1;
283
	size_t i = tmpstr.size() - 1;
Line 422... Line 423...
422
		}
423
		}
423
	}
424
	}
424
 
425
 
425
	if ( data[pos] == 0 ) return NULL;
426
	if ( data[pos] == 0 ) return NULL;
426
	return data + (pos + 1);
427
	return data + (pos + 1);
-
 
428
}
-
 
429
 
-
 
430
String String::replaceToken(const char *token, int from, const String &replace) const
-
 
431
{
-
 
432
	std::string newstr;
-
 
433
 
-
 
434
	int whichtok = 1;
-
 
435
	std::string::size_type lastPos = 0;
-
 
436
	std::string::size_type pos     = _token_nextPos(token, 0);
-
 
437
 
-
 
438
	while (std::string::npos != pos || std::string::npos != lastPos)
-
 
439
	{
-
 
440
		if ( whichtok == from )
-
 
441
		{
-
 
442
			if ( newstr != "" ) newstr = newstr + token;
-
 
443
			newstr = newstr + replace;
-
 
444
		}
-
 
445
		else
-
 
446
		{
-
 
447
			if ( newstr != "" ) newstr = newstr + token;
-
 
448
			newstr = newstr + this->substr(lastPos, pos - lastPos);
-
 
449
		}
-
 
450
 
-
 
451
		// Found a token, add it to the vector.
-
 
452
		whichtok++;
-
 
453
 
-
 
454
		if ( pos >= this->length() )
-
 
455
			break;
-
 
456
 
-
 
457
		// skip past token
-
 
458
		size_t i = 0;
-
 
459
		size_t max = strlen(token);
-
 
460
		lastPos = pos;
-
 
461
		while ( (i < max) && (token[i] == this->at(lastPos)) )
-
 
462
		{
-
 
463
			++i;
-
 
464
			++lastPos;
-
 
465
		}
-
 
466
 
-
 
467
		// get the next token
-
 
468
		pos = _token_nextPos(token, lastPos);
-
 
469
	}
-
 
470
	return newstr;
427
}
471
}
-
 
472
 
428
 
473
 
429
String *String::tokenise(const char *token, int *max) const
474
String *String::tokenise(const char *token, int *max) const
430
{
475
{
431
	if ( empty() ) {
476
	if ( empty() ) {
432
		*max = 0;
477
		*max = 0;
Line 522... Line 567...
522
		// check if it is a number, 0-9
567
		// check if it is a number, 0-9
523
		else if ( !this->isCharNumber(i) ) return false;
568
		else if ( !this->isCharNumber(i) ) return false;
524
	}
569
	}
525
	return true;
570
	return true;
526
}
571
}
-
 
572
 
-
 
573
const String &String::removeFirstSpace()
-
 
574
{
-
 
575
	std::string::size_type pos = this->find_first_not_of(" ", 0);
-
 
576
	if ( pos != std::string::npos ) {
-
 
577
		this->erase(0, pos);
-
 
578
	}
-
 
579
	return (*this);
-
 
580
}
-
 
581
 
527
}//NAMESPACE
582
}//NAMESPACE