File: clanstring.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (598 lines) | stat: -rw-r--r-- 10,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
	$Id: clanstring.h,v 1.30 2001/11/02 02:24:08 plasmoid Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	See http://www.clanlib.org
	------------------------------------------------------------------------
*/

//! clanCore="System"
//! header=core.h

#ifndef header_clanstring
#define header_clanstring

#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <stdio.h>

#ifndef WIN32
#include <cctype>
#endif

#ifdef __BORLANDC__
#include <ctype.h>
#endif

#ifdef __MWERKS__
#include <extras.h>
#endif

#ifdef __MACOS__
#include <cctype>
#define NEED_FCVT
#endif


#ifdef NEED_FCVT
extern char *fcvt (double, int, int *, int *);
#endif

#include "../Resources/resourcetype_string.h"
#include "../Resources/resource.h"
#include "../Resources/resource_manager.h"
#include "error.h"

using namespace std;

//: Clan String Class
class CL_String
{
public:
//! Construction:
	//: String Constructor
	CL_String(const std::string &res_id, CL_ResourceManager *resources)
	{
		CL_Resource res = resources->get_resource(res_id);
		res.load();
		CL_ResourceData_String *data =
			(CL_ResourceData_String *) res.get_data("string");
		str = data->value;
		res.unload();
	}

	//: String Constructor
	CL_String(const std::string &res_id, CL_ResourceManager *resources, const std::string &def_value)
	{
		try
		{
			CL_Resource res = resources->get_resource(res_id);
			res.load();
			CL_ResourceData_String *data =
				(CL_ResourceData_String *) res.get_data("string");
			str = data->value;
			res.unload();
		}
		catch (CL_Error err)
		{
			str = def_value;
		}
	}

	//: String Constructor
	CL_String()
	{
	}
	
	//: String Constructor
	CL_String(const std::string &_str)
	{
		this->str = append_string(NULL, _str.c_str());
	}

	//: String Constructor
	CL_String(const int value)
	{
		str=int_to_string(NULL, value);
	}

	//: String Constructor
	CL_String(const float float_value)
	{
		str=float_to_string(NULL, float_value);
	}

	//: String Constructor
	CL_String(const char *text)
	{
		str=text;
	}
	
	//: String Constructor
	CL_String(const CL_String &clone)
	{
		str=clone.str;
	}

	//: String Constructor
	CL_String(const CL_String &text1, const CL_String &text2)
	{
		str=text1.str;
		str+=text2.str;
	}

	//: String Constructor
	CL_String(const char *text1, const char *text2)
	{
		str=text1;
		str+=text2;
	}

	//: String Constructor
	CL_String(const char *text, int length)
	{
		str.append(text, length);
	}
//! Operators:

	//: String Operator
	CL_String operator+ (const CL_String &other_string)
	{
		return CL_String(str, other_string.str);
	}

	//: String Operator
	CL_String operator+ (const char *other_text)
	{
		return CL_String(str, other_text);
	}

	//: String Operator
	CL_String operator+ (const int number)
	{
		return CL_String(int_to_string(str.c_str(), number));
	}

	//: String Operator
	CL_String operator+ (const float number)
	{
		return CL_String(float_to_string(str.c_str(), number));
	}

	//: String Operator
	CL_String &operator+= (const CL_String &other_string)
	{
		str += other_string.str;
		return *this;
	}

	//: String Operator
	CL_String &operator+= (const char *other_text)
	{
		str += other_text;
		return *this;
	}

	//: String Operator
	CL_String &operator+= (const int number)
	{
		str = int_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator+= (const float number)
	{
		str = float_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator+= (double float_number)
	{
		return operator+= ((float) float_number);
	}

	//: String Operator
	CL_String &operator= (const CL_String &other_string)
	{
		str=other_string.str;
		return *this;
	}

	//: String Operator
	CL_String &operator= (const char *other_text)
	{
		str = other_text;
		return *this;
	}

	//: String Operator
	CL_String &operator= (const int number)
	{
		str=int_to_string(NULL, number);
		return *this;
	}

	//: String Operator
	CL_String &operator= (const float float_number)
	{
		str=float_to_string(NULL, float_number);
		return *this;
	}

	//: String Operator
	CL_String &operator= (double float_number)
	{
		return operator= ((float) float_number);
	}

	//: String Operator
	bool operator== (const CL_String &other_string)
	{
		return str == other_string.str;
	}

	//: String Operator
	bool operator== (const char *other_string)
	{
		return str == other_string;
	}
	
	//: String Operator
	bool operator!= (const CL_String &other_string)
	{
		return !operator==(other_string);
	}

	//: String Operator
	bool operator!= (const char *other_text)
	{
		return !operator==(other_text);
	}

	//: String Operator
	operator const char* ()
	{
		return str.c_str();
	}
	
	//: String Operator
	operator std::string ()
	{
		return str;
	}

	//: String Operator
	CL_String &operator<< (const CL_String &other_string)
	{
		str += other_string.str;
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const char *other_string)
	{
		str += other_string;
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const unsigned char *other_string)
	{
		str += (const char *) other_string;
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const unsigned short number)
	{
		str=int_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const short number)
	{
		str=int_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const unsigned int number)
	{
		str=int_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const int number)
	{
		str=int_to_string(str.c_str(), number);
		return *this;
	}

	//: String Operator
	CL_String &operator<< (const float float_number)
	{
		str=float_to_string(str.c_str(), float_number);
		return *this;
	}

	//: String Operator
	CL_String &operator<< (double float_number)
	{
		return operator<< ((float) float_number);
	}

//! Attributes:
	//: Get String
	char *get_string()
	{
		return (char *) str.c_str();
	}

	//: Get Length
	int get_length()
	{
		return str.length();
	}

	//: Get As int
	int get_as_int()
	{
		return atoi(str.c_str());
	}

	//: Get As Float
	float get_as_float()
	{
		return atof(str.c_str());
	}

	//: Get Word
	CL_String get_word(int word_no)
	{
		int len=get_length();
		int cur_word=-1;
		int word_start=0;

		int cur_state=0;
		for (int i=0; i<len; i++)
		{
			if (cur_state == 0)
			{
				if (str[i]!=32)
				{
					if (str[i]==34)
					{
						word_start = i+1;
						cur_state = 2;
					}
					else
					{
						word_start = i;
						cur_state = 1;
					}
					cur_word++;
				}
			}
			else if (cur_state == 1)
			{
				if (str[i]==32)
				{
					if (cur_word == word_no) return mid(word_start, i-word_start);
					cur_state = 0;
				}
			}
			else if (cur_state == 2)
			{
				if (str[i]==34)
				{
					if (cur_word == word_no) return mid(word_start, i-word_start);
					cur_state = 0;
				}
			}
		}

		if (cur_word == word_no) return mid(word_start);

		return CL_String("");
	}

	//: Mid
	CL_String mid(int start, int len=-1)
	{
		if (len==-1) len=get_length()-start;
		return CL_String(std::string(str, start, len));
	}

	//: Left
	CL_String left(int len)
	{
		int strlength = get_length();
		if (strlength < len) return *this;
		return CL_String(std::string(str, 0, len));
	}

	//: Right
	CL_String right(int len)
	{
		int strlength = get_length();
		if (strlength < len) return *this;
		return CL_String(std::string(str, strlength-len, len));
	}

	//: Find
	int find(int character, int start_pos=0)
	{
		const char *ptr = str.c_str();
		char *pos = (char*)strchr(ptr+start_pos, character);
		if (pos == NULL) return -1;
		return int(pos-ptr);
	}

	//: Find Last
	int find_last(char character) const
	{
		const char *temp=str.c_str();
		int start_pos=str.length();
		for(int i=start_pos;i;i--)
			if(*(temp+i)==character)
				return i;
		return -1;
	}

	//: Case Compare
	bool case_compare(const char *other)
	{
		if (other==NULL) return false;

#ifdef WIN32
#ifdef __BORLANDC__
		if (stricmp(str.c_str(), other)==0) return true;
#else
		if (_stricmp(str.c_str(), other)==0) return true;
#endif
#else
		if (strcasecmp(str.c_str(), other)==0) return true;
#endif
		return false;
	}

//! Operations:
	//: To Lower
	void to_lower()
	{
		int len = str.length();
		for (int i=0; i<len; i++)
		{
			str[i] = tolower(str[i]);
		}
	}

	//: To Upper
	void to_upper()
	{
		int len = str.length();
		for (int i=0; i<len; i++)
		{
			str[i] = toupper(str[i]);
		}
	}

private:
	std::string str;

	std::string int_to_string(const char *prefix, const int number)
	{
		char buf[20];
		sprintf(buf, "%d", number);

		int len=0;
		if (prefix!=NULL) len=strlen(prefix);

		char *text=new char[len+strlen(buf)+1];
		if (prefix!=NULL)
		{
			strcpy(text, prefix);
			strcat(text, buf);
		}
		else
		{
			strcpy(text, buf);
		}

		std::string res(text);
		delete[] text;
		return res;
	}
	
	std::string float_to_string(const char *prefix, const float _float_number)
	{
		char buf[25];
		buf[0]=0;

		int decimal, sign;
		int precision=4;
		char temp;

		float float_number = _float_number;

		const char *_float_buffer = fcvt(
			float_number,
			precision,
			&decimal,
			&sign);
			
		char *float_buffer = new char[strlen(_float_buffer)+1];
		strcpy(float_buffer, _float_buffer);

		if (sign) strcpy(buf, "-");
		if (decimal<=0)
		{
			strcat(buf, "0.");
			strcat(buf, float_buffer);
		}
	    else
		{
			temp=float_buffer[decimal];
			float_buffer[decimal]=0;
			strcat(buf, float_buffer);
			float_buffer[decimal]=temp;

			strcat(buf, ".");
			strcat(buf, &float_buffer[decimal]);
		}
    
		int len=0;
		if (prefix!=NULL) len=strlen(prefix);

		char *text=new char[len+strlen(buf)+1];
		if (prefix!=NULL)
		{
			strcpy(text, prefix);
			strcat(text, buf);
		}
		else strcpy(text, buf);

		std::string res(text);
		delete[] text;
		return res;
	}

	std::string append_string(const char *prefix, const char *postfix)
	{
		int len=0;
		if (prefix!=NULL) len=strlen(prefix);
		if (postfix!=NULL) len+=strlen(postfix);
		char *text=new char[len+1];
		if (prefix!=NULL)
		{
			strcpy(text, prefix);
			if (postfix!=NULL) strcat(text, postfix);
		}
		else if (postfix!=NULL)
		{
			strcpy(text, postfix);
		}

		std::string res(text);
		delete[] text;
		return res;
	}

};

#endif