1 
2 #include "MString.H"
3 
4 String Left(const String &, int);
5 String Mid(String, int, int);
6 String Right(const String &, int);
7 
8 // �����ȥ饯��
String(int n)9 String::String(int n)
10 {
11 	length = n ;
12 
13 	if (n == 0)
14 	{
15 		str = NULL ;
16 		Clear() ;
17 	}
18 	else
19 	{
20 		allocated = shouldAlloc(length) ;
21 		str = new char[allocated] ;
22 		memset(str, ' ', length) ;
23 		str[length] = '\0' ;
24 	}
25 }
26 
String(const char * c)27 String::String(const char *c)
28 {
29 //      cout << "String::String from const char * OK!!!!!!!" << endl ;
30 
31     length = strlen(c) ;
32 	if (length == 0)
33 	{
34 		str = NULL ;
35 		Clear() ;
36 		return ;
37 	}
38 
39 /*
40 	if (length < MIN_ALLOC_UNIT)
41 		allocated = MIN_ALLOC_UNIT ;
42 	else
43 		allocated = (length + 1) * 2 ;
44 */
45 	allocated = shouldAlloc(length) ;
46     str = new char[allocated] ;
47 
48 	// �ǽ餫��̥뤬ͭ������Τǡ��̥�������ɲ�
49     memcpy(str, c, length) ;
50     str[length] = '\0' ; // �Ǹ�˥̥�ʸ���ղ�
51 }
52 
String(const char * c,int n)53 String::String(const char *c, int n)
54 {
55 //      cout << "String::String from const *char, int OK!!!!!!!" << endl ;
56 
57 	int slen = strlen(c) ;
58 
59     length = n ;
60 	allocated = shouldAlloc(length) ;
61     str = new char[allocated] ;
62 
63     if (slen < length)
64     {
65         // ���ԡ���������û�����Ϥ��ޤ����������
66         memcpy(str, c, slen) ;
67 		memset(&(str[slen]) , ' ', length - slen) ;
68     }
69     else
70     {
71         // ���ԡ���������Ĺ����Ʊ������ñ���ɬ��ʬ���ԡ�
72         memcpy(str, c, length) ;
73     }
74 
75 	// �Ǹ�˥̥�ʸ���ɲ�
76 	str[length] = '\0' ;
77 }
78 
String(const String & s)79 String::String(const String& s)
80 {
81 //    cout << "String::String from String OK!!!!!!!" << endl ;
82 //	if (this == &s) return ;
83 
84     length = s.length ;
85 	allocated = s.allocated ;
86 
87     str = new char[allocated] ;
88     memcpy(str, s.str, length) ;
89     str[length] = '\0' ; // �Ǹ�˥̥�ʸ���ղ�
90 
91 //    *this = String(s.str, s.length) ;
92 }
93 
String(const String & s,int n)94 String::String(const String& s, int n)
95 {
96 //    cout << "String::String from String, int  OK!!!!!!!" << endl ;
97     length = n ;
98 	allocated = shouldAlloc(length) ;
99     str = new char[allocated] ;
100 
101     if (s.length < length)
102     {
103         // ���ԡ���������û�����Ϥ��ޤ����������
104         memcpy(str, s.str, s.length) ;
105 		memset(&(str[s.length]), ' ', length - s.length) ;
106     }
107     else
108     {
109         // ���ԡ���������Ĺ����Ʊ������ñ���ɬ��ʬ���ԡ�
110         memcpy(str, s.str, length) ;
111     }
112 
113 	// �Ǹ�˥̥�ʸ���ɲ�
114 	str[length] = '\0' ;
115 
116 //    *this = String(s.str, n) ;
117 }
118 
String(char c)119 String::String(char c)
120 {
121     str = NULL ;
122     if (c == '\0')
123 	{
124 		str = NULL ;
125 		Clear() ;
126 	}
127     else
128     {
129 		length = 1 ;
130 		allocated = MIN_ALLOC_UNIT ;
131 		str = new char[allocated] ;
132 		str[0] = c ; str[1] = '\0' ;
133     }
134 }
135 
136 
137 // ʸ�����Ĺ�����Ѳ���;�ä���ʬ�� ' '��
Length(int n)138 String& String::Length(int n)
139 {
140 	if (length == n)
141 		return *this ;
142 	else if (length > n)
143 	{
144 		// û���ʤä��Ȥ�
145 		Realloc(n) ;
146 		length = n ;
147 		return *this ;
148 	}
149 	else
150 	{
151 		// Ĺ���ʤä��Ȥ��Τߡ�������ʬ�˶�����å�
152 		Realloc(n) ;
153 		memset(&(str[length]), ' ', n - length) ;
154 //		str[n] = '\0' ;
155 		length = n ;
156 		return *this ;
157 	}
158 
159 //    return (*this = String(str, n)) ;
160 }
161 
162 
163 // n ʸ���ܤΣ�ʸ��
Character(int n) const164 char String::Character(int n) const
165 {
166 	if (n < 1 || n > length) return '\0' ;
167 	return str[n - 1] ;
168 }
169 
170 // n ʸ���ܤ�ʸ�����ѹ�
SetAt(int n,char c)171 String& String::SetAt(int n, char c)
172 {
173 	if (n > 0 && n <= length)
174 		str[n - 1] = c ;
175 	return *this ;
176 }
177 
178 
ReplaceString(char s,char d)179 String& String::ReplaceString(char s, char d)
180 {
181 	for (int n = 1 ; n <= length ; n ++)
182 	{
183 		if (str[n - 1] == s)
184 			str[n - 1] = d ;
185 	}
186 
187 	return *this ;
188 }
189 
ReplaceString(const String & s,const String & d)190 String& String::ReplaceString(const String& s, const String& d)
191 {
192 	if (this == &s || this == &d) return *this ;
193 
194 	int pos = 1 ;
195 	while (TRUE)
196 	{
197 		int start = Search(s) ;
198 
199 		// 2000.06.08
200 		if (start < pos || !start)
201 			break ;
202 
203 		int rlen = length - (start + s.length - 1) ;
204 		String right ;
205 		if (rlen > 0)
206 			right = ::Right(*this, rlen) ;
207 
208 		if (start == 1)
209 		{
210 			*this = d + right ;
211 		}
212 		else
213 		{
214 			Length(start - 1) ;
215 			*this += d + right ;
216 		}
217 
218 		pos = start + d.Length() ;
219 	}
220 
221 	return *this ;
222 }
223 
224 
225 // Lower, Upper
Lower()226 String& String::Lower()
227 {
228 	for (int i = 0 ; i < length ; i ++)
229 	{
230 		if (isupper(str[i]))
231 			str[i] = tolower(str[i]) ;
232 	}
233 
234 	return *this ;
235 }
236 
Upper()237 String& String::Upper()
238 {
239 	for (int i = 0 ; i < length ; i ++)
240 	{
241 		if (islower(str[i]))
242 			str[i] = toupper(str[i]) ;
243 	}
244 
245 	return *this ;
246 }
247 
248 
249 // �ե����ޥå�
Format(const char * format,...)250 String& String::Format(const char* format, ...)
251 {
252 	char buf[MAX_LINE_BUF] ;
253 
254 	va_list args ;
255 	va_start(args, format) ;
256 //	int r = vsprintf(buf, format, args) ;
257 	vsprintf(buf, format, args) ;
258 	va_end(args) ;
259 
260 	// ʸ�����å�
261 	*this = buf ;
262 	return *this ;
263 }
264 
265 // �ե����ޥå�
AddFormat(const char * format,...)266 String& String::AddFormat(const char* format, ...)
267 {
268 	char buf[MAX_LINE_BUF] ;
269 
270 	va_list args ;
271 	va_start(args, format) ;
272 //	int r = vsprintf(buf, format, args) ;
273 	vsprintf(buf, format, args) ;
274 	va_end(args) ;
275 
276 	// ʸ�����å�
277 	*this += buf ;
278 	return *this ;
279 }
280 
281 
282 // �ץ饤�١���
ReadAllLines(FILE * fp)283 String& String::ReadAllLines(FILE *fp)
284 {
285 	while (TRUE)
286 	{
287 		char buf[MAX_LINE_BUF] ;
288 		char *s ;
289 
290 		s = fgets(buf, MAX_LINE_BUF, fp) ;
291 		if (!s)
292 			break ;
293 
294 		*this += s ;
295 	}
296 	return *this ;
297 }
298 
299 
300 // �ƥ����ȥե������ɤ߹���
ReadFile(const String & fileName)301 String& String::ReadFile(const String& fileName)
302 {
303 	FILE *fp ;
304 	fp = fopen(fileName, "r") ;
305 
306 	if (!fp)
307 		*this = "" ;
308 	else
309 	{
310 		*this = ReadAllLines(fp) ;
311 		fclose(fp) ;
312 	}
313 
314 	return *this ;
315 }
316 
317 
318 // �ȡ��������
319 // �֤��줿�ȡ�����ʺǽ��ʸ����ˤϺ�������
320 // String("") ���֤����ޤǷ����֤�����
Token(const String & delimiters,const String & comment)321 String String::Token(const String& delimiters, const String& comment)
322 {
323 	String	token ;
324 
325 	// ���Υȡ�����Υ������Ȱ��֤���
326 	int start ;
327 	for (start = 1 ; start <= Length() ; start ++)
328 	{
329 		// �����ȹԥ����å�
330 		if (::Mid(*this, start, comment.Length()) == comment)
331 		{
332 			// �����Ȥʤ�ԤκǸ�ޤ����Ф�
333 			String right = ::Mid(*this, start, Length()) ;
334 			int next = right.Search('\n') ;
335 
336 			if (next)
337 				start = next ;
338 			else	// ���Ԥ��ʤ��Ȥ������Ȥ�ʸ����κǸ�
339 				start = Length() ;
340 
341 			continue ;
342 		}
343 
344 		int flag = TRUE ;
345 		for (int c = 1 ; c <= delimiters.Length() ; c ++)
346 		{
347 			if (Character(start) == delimiters.Character(c))
348 			{
349 				flag = FALSE ;
350 				break ;
351 			}
352 		}
353 
354 		// �ǥ�ߥ��Τɤ�Ȥ�ޥå����ʤ����Ϸ���
355 		if (flag)
356 			break ;
357 	}
358 
359 	Mid(start, Length()) ;
360 
361 	// ���Υȡ�����ν����ΰ��֤���
362 	int length ;
363 	for (length = 1 ; length <= Length() ; length ++)
364 	{
365 		// �����ȥ����å�������ǥ����ȤˤʤäƤ�����ϥ����Ȥ�ľ���ޤǡ�
366 		if (::Mid(*this, length, comment.Length()) == comment)
367 			break ;
368 
369 		// �ǥ�ߥ��Τɤ줫���ĤǤ�ޥå�����Ф���ľ���ޤ�
370 		int flag = FALSE ;
371 		for (int c = 1 ; c <= delimiters.Length() ; c ++)
372 		{
373 			if (Character(length) == delimiters.Character(c))
374 			{
375 				flag = TRUE ;
376 				break ;
377 			}
378 		}
379 
380 		if (flag)
381 			break ;
382 	}
383 
384 	length -- ;
385 	token = ::Left(*this, length) ;
386 	Mid(length + 1, Length()) ;
387 
388 	return token ;
389 }
390 
391 
392 // m ʸ���ܤ��� n ʸ��
Mid(int m,int n)393 String& String::Mid(int m, int n)
394 {
395 	// ���ԡ���Ĺ���Ȱ��֤�Ŭ����
396     if (m < 1) m = 1 ;
397     if (m > length || n < 0) return (this->Clear()) ;
398 	if (m + n > length + 1) n = length + 1 - m ;
399 
400 	// ��ʣ�����ǽ�������뤿��
401     memmove(str, &(str[m - 1]), n) ;
402 
403 	// ̵�̤�����к��
404 	Realloc(n) ;
405 	length = n ;
406 	str[length] = '\0' ;
407 
408 	return *this ;
409 
410 /*
411     if (m < 1) m = 1 ;
412     if (m > length || n < 0) return (this->Clear()) ;
413     if (m + n > length + 1) n = length + 1 - m ;
414 
415     String  w(n) ;
416 //    for (int i = 0 ; i < n ; i ++) w.str[i] = this->str[m + i - 1] ;
417     memcpy(w.str, &(this->str[m - 1]), n) ;
418 
419     *this = w ;
420     return *this ;
421 */
422 }
Mid(String s,int m,int n)423 String Mid(String s, int m, int n)
424 {
425     return (s.Mid(m, n)) ;
426 }
427 
428 // ������ n ʸ��
Left(int n)429 String& String::Left(int n)
430 {
431     return (this->Mid(1, n)) ;
432 }
Left(const String & s,int n)433 String Left(const String& s, int n)
434 {
435     return (Mid(s, 1, n)) ;
436 }
437 
438 // ������ n ʸ��
Right(int n)439 String& String::Right(int n)
440 {
441     if (n > length) n = length ;
442     this->Mid(length + 1 - n, n) ;
443 	return *this ;
444 }
Right(const String & s,int n)445 String Right(const String& s, int n)
446 {
447     String  w = s ;
448     return (w.Right(n)) ;
449 }
450 
451 // ���ꤵ�줿�ե�����̾�Υǥ��쥯�ȥ���ʬ���֤�
452 // �����鸡������"/" �⤷���� "\" �μ����ޤǤ��֤�
FileDirectory() const453 String String::FileDirectory() const
454 {
455 #ifdef WIN32
456 	char dpath = '\\' ;
457 #else
458 	char dpath = '/' ;
459 #endif
460 
461 	String dir ;
462 
463 	int locate = length - 1 ;
464 	while (locate > 0 && str[locate - 1] != dpath) locate -- ;
465 	if (locate <= 0)
466 		dir = "" ;
467 	else
468 		dir = ::Left(*this, locate - 1) ;
469 
470 	return dir ;
471 }
472 
473 // ���ꤵ�줿�ե�����̾�Υե�����͡�����ʬ���֤�
474 // �����鸡������"/" �⤷���� "\" �θ�Τߤ��֤�
FileNamePart() const475 String String::FileNamePart() const
476 {
477 #ifdef WIN32
478 	char dpath = '\\' ;
479 #else
480 	char dpath = '/' ;
481 #endif
482 
483 	String dir ;
484 
485 	int locate = length - 1 ;
486 	while (locate > 0 && str[locate - 1] != dpath) locate -- ;
487 	if (locate <= 0)
488 		dir = str ;
489 	else
490 		dir = ::Right(*this, length - locate) ;
491 
492 	return dir ;
493 }
494 
495 // ʸ���������ʺǽ�˰��פ��������֤���
Search(const String & key) const496 int String::Search(const String& key) const
497 {
498     if ((!key.length) || (!length)) return FALSE ; // ʸ����Ĺ�� 0 �ʤ� FALSE
499 
500     // ʸ�����Ĺ�����饭����������Ȥ���ޤǥ�����
501     for (int i = 0 ; i < length - (key.length - 1) ; i ++)
502     {
503 		char tmp = key.str[0] ;
504 		if (tmp == str[i])
505 		{
506 		    // ��������ʸ���ʤ餳�λ����dz���
507 		    if (key.length == 1)
508 				return (i + 1) ;
509 		    // ��������ʸ���ʾ�ξ��Ϥ���˥�����
510 		    else
511 		    {
512 			int j ;
513 			for (j = 1 ; j < key.length ; j ++)
514 			    if (str[i + j] != key.str[j]) break ;
515 
516 			// ������ʸ�������������֤��Ƥ���г���
517 			if (j == key.length)
518 			    return (i + 1) ;
519 			}
520 		}
521 	}
522 
523     return FALSE ;
524 }
Search(const char * key) const525 int String::Search(const char *key) const
526 {
527     return Search(String(key)) ;
528 }
Search(const char key) const529 int String::Search(const char key) const
530 {
531     return Search(String(key)) ;
532 }
533 
534 
535 // ���ڥ졼����/�ˤĤޤ�ϡ�������ʸ����������������פ���������֤�
operator /(const String & key) const536 int String::operator /(const String& key) const
537 {
538     if ((!key.length) || (!length)) return FALSE ; // ʸ����Ĺ�� 0 �ʤ� FALSE
539 
540     int     n = 0 ;
541 
542     // ʸ�����Ĺ�����饭����������Ȥ���ޤǥ�����
543     for (int i = 0 ; i < length - (key.length - 1) ; i ++)
544     {
545 		char tmp = key.str[0] ;
546 		if (tmp == str[i])
547 		{
548 		    // ��������ʸ���ʤ餳�λ����dz���
549 		    if (key.length == 1)
550 				n ++ ;
551 			// ��������ʸ���ʾ�ξ��Ϥ���˥�����
552 		    else
553 		    {
554 				int j ;
555 				for (j = 1 ; j < key.length ; j ++)
556 				    if (str[i + j] != key.str[j]) break ;
557 
558 				// ������ʸ�������������֤��Ƥ���г���
559 				if (j == key.length)
560 			    n ++ ;
561 		    }
562 		}
563     }
564 
565     return n ;
566 }
operator /(const char * key) const567 int String::operator /(const char *key) const
568 {
569     return *this / String(key) ;
570 }
operator /(const char key) const571 int String::operator /(const char key) const
572 {
573     return *this / String(key) ;
574 }
575 
576 // �������� String ��
IntToString(int i)577 String IntToString(int i)
578 {
579     String  s ;
580     if (i < 0)
581     {
582 		s = '-' ;
583 		i = -i ;
584     }
585 
586     for (int flg = OFF, d = MAX_DIV_DEF ; d > 1 ; d /= 10)
587     {
588 		if (d > i)
589 		{
590 		    if (flg) s += '0' ;
591 		    continue ;
592 		}
593 		flg = ON ;
594 		s += char(i / d + '0') ;
595 		i %= d ;
596     }
597     return (s + char(i + '0')) ;
598 }
599 
600 // ���ڥ졼����=�������黻��
operator =(const String & s)601 String& String::operator =(const String& s)
602 {
603 /*
604     delete [] str ;
605     length = s.length ;
606     str = new char[length + 1] ;
607 
608 
609 //    for (int i = 0 ; i < length ; i ++) str[i] = s.str[i] ;
610 //    str[length] = '\0' ; // �Ǹ�˥̥�ʸ���ղ�
611 
612     memcpy(str, s.str, length + 1) ;
613     return *this ;
614 */
615 	if (this == &s) return *this ;
616 
617 	Copy(s) ;
618 	return *this ;
619 }
operator =(const char * c)620 String& String::operator =(const char *c)
621 {
622 	int clen = strlen(c) ;
623 	Realloc(clen) ;
624 	length = clen ;
625 	memcpy(str, c, length + 1);
626 	return *this ;
627 
628 //    return (*this = String(c)) ;
629 }
operator =(const char c)630 String& String::operator =(const char c)
631 {
632 	Realloc(1, FALSE) ;
633 	length = 1 ;
634 	str[0] = c ;
635 	str[1] = '\0' ;
636 	return *this ;
637 
638 //	return (*this = String(c)) ;
639 }
640 
641 
642 // ʸ����ӡʺǽ�� n ʸ���� s ��Ʊ���ǡ�n ���夬 s ��Ʊ�������ڤ���
643 // String str ;
644 // str.Equ("abcde",3) �Τ褦�� ("abcde", 3) ����Ӥ���ȡ�
645 // str = "abc", str = "abcd", str = "abcde" �� TRUE
646 // str �� "abcde"��5 ʸ���ˤ��Ĺ������ FALSE
647 // n <= 0 �ޤ��� ��ά����ȡ�n = 1 ��Ʊ��
Equ(const String s,int n)648 int String::Equ(const String s, int n)
649 {
650     if (n <= 0) n = 1 ;
651 
652     if (length > s.length || length < n) return FALSE ;
653     String w = s ;
654     w.Length(length) ;
655     return (*this == w) ;
656 }
657 
658 // n ʸ���羮��ӡ�strncmp()��Ʊ��������n ���ά����ȡ�strcmp() ��Ʊ����
Cmp(const String s,int n)659 int String::Cmp(const String s, int n)
660 {
661     if (n < 0) return strcmp(str, s.str) ;
662     else       return strncmp(str, s.str, n) ;
663 }
664 
665 /*
666 // ���ڥ졼����==, !=����ӱ黻��
667 int operator ==(const String& s1, const String& s2)
668 {
669     if (!strcmp(s1.str, s2.str)) return TRUE ;
670     else                         return FALSE ;
671 }
672 int operator ==(const char *c, const String& s2)
673 {
674     if (!strcmp(c, s2.str)) return TRUE ;
675     else                    return FALSE ;
676 }
677 int operator !=(const String& s1, const String& s2)
678 {
679     return !(s1 == s2) ;
680 }
681 int operator !=(const char *c, const String& s2)
682 {
683     return !(c == s2) ;
684 }
685 */
686 
687 
688 // ���ڥ졼����==, !=����ӱ黻��
689 
690 // String Ʊ�Τ����
operator ==(const String & s2) const691 int String::operator ==(const String& s2) const
692 {
693     if (!strcmp(str, s2.str)) return TRUE ;
694     else                      return FALSE ;
695 }
operator !=(const String & s2) const696 int String::operator !=(const String& s2) const
697 {
698     if (strcmp(str, s2.str)) return TRUE ;
699     else                     return FALSE ;
700 //	{
701 //    if (strcmp(str, s2.str)) return TRUE ;
702 //    else                     return FALSE ;
703 //	}
704 }
705 
706 // char * �Ȥ����
operator ==(const char * c) const707 int String::operator ==(const char *c) const
708 {
709     if (!strcmp(str, c)) return TRUE ;
710     else                 return FALSE ;
711 }
operator !=(const char * c) const712 int String::operator !=(const char *c) const
713 {
714     if (strcmp(str, c)) return TRUE ;
715     else                return FALSE ;
716 }
717 
718 // char �Ȥ����
operator ==(const char c) const719 int String::operator ==(const char c) const
720 {
721     if (length == 1 && str[0] == c) return TRUE ;
722     else                            return FALSE ;
723 }
operator !=(const char c) const724 int String::operator !=(const char c) const
725 {
726     return !(*this == c) ;
727 }
728 
729 // ���դ� char * �Ȥ����
operator ==(const char * c,const String & s2)730 int operator ==(const char *c, const String& s2)
731 {
732     if (!strcmp(c, s2.str)) return TRUE ;
733     else                    return FALSE ;
734 }
operator !=(const char * c,const String & s2)735 int operator !=(const char *c, const String& s2)
736 {
737     if (strcmp(c, s2.str)) return TRUE ;
738     else                   return FALSE ;
739 }
740 
741 // ���դ� char �Ȥ����
operator ==(const char c,const String & s2)742 int operator ==(const char c, const String& s2)
743 {
744     return (s2 == c) ;
745 }
operator !=(const char c,const String & s2)746 int operator !=(const char c, const String& s2)
747 {
748     return (s2 != c) ;
749 }
750 
751 
752 /*
753 // ���ڥ졼����>, <, >=, <=���羮��ӱ黻��
754 int operator >(const String& s1, const String& s2)
755 {
756     if (strcmp(s1.str, s2.str) > 0) return TRUE ;
757     else                            return FALSE ;
758 }
759 int operator >(const char *c, const String& s2)
760 {
761     if (strcmp(c, s2.str) > 0) return TRUE ;
762     else                       return FALSE ;
763 }
764 int operator <(const String& s1, const String& s2)
765 {
766     if (strcmp(s1.str, s2.str) < 0) return TRUE ;
767     else                            return FALSE ;
768 }
769 int operator <(const char *c, const String& s2)
770 {
771     if (strcmp(c, s2.str) < 0) return TRUE ;
772     else                            return FALSE ;
773 }
774 int operator >=(const String& s1, const String& s2)
775 {
776     return (s1 > s2 || s1 == s2) ;
777 }
778 int operator >=(const char *c, const String& s2)
779 {
780     return (c > s2 || c == s2) ;
781 }
782 int operator <=(const String& s1, const String& s2)
783 {
784     return (s1 < s2 || s1 == s2) ;
785 }
786 int operator <=(const char *c, const String& s2)
787 {
788     return (c < s2 || c == s2) ;
789 }
790 */
791 
792 // ���ڥ졼����>, <, >=, <=���羮��ӱ黻��
793 
794 // String Ʊ�Τ����
operator >(const String & s2) const795 int String::operator >(const String& s2) const
796 {
797     if (strcmp(str, s2.str) > 0) return TRUE ;
798     else                         return FALSE ;
799 }
operator <(const String & s2) const800 int String::operator <(const String& s2) const
801 {
802     if (strcmp(str, s2.str) < 0) return TRUE ;
803     else                         return FALSE ;
804 }
operator >=(const String & s2) const805 int String::operator >=(const String& s2) const
806 {
807     if (strcmp(str, s2.str) >= 0) return TRUE ;
808     else                          return FALSE ;
809 }
operator <=(const String & s2) const810 int String::operator <=(const String& s2) const
811 {
812     if (strcmp(str, s2.str) <= 0) return TRUE ;
813     else                          return FALSE ;
814 }
815 
816 // char * �Ȥ����
operator >(const char * c) const817 int String::operator >(const char *c) const
818 {
819     if (strcmp(str, c) > 0) return TRUE ;
820     else                    return FALSE ;
821 }
operator <(const char * c) const822 int String::operator <(const char *c) const
823 {
824     if (strcmp(str, c) < 0) return TRUE ;
825     else                    return FALSE ;
826 }
operator >=(const char * c) const827 int String::operator >=(const char * c) const
828 {
829     if (strcmp(str, c) >= 0) return TRUE ;
830     else                     return FALSE ;
831 }
operator <=(const char * c) const832 int String::operator <=(const char *c) const
833 {
834     if (strcmp(str, c) <= 0) return TRUE ;
835     else                     return FALSE ;
836 }
837 
838 // char �Ȥ����
operator >(const char c) const839 int String::operator >(const char c) const
840 {
841     return (*this > String(c)) ;
842 }
operator <(const char c) const843 int String::operator <(const char c) const
844 {
845     return (*this < String(c)) ;
846 }
operator >=(const char c) const847 int String::operator >=(const char c) const
848 {
849     return (*this >= String(c)) ;
850 }
operator <=(const char c) const851 int String::operator <=(const char c) const
852 {
853     return (*this <= String(c)) ;
854 }
855 
856 // ���դ� char* �Ȥ����
operator >(const char * c,const String & s2)857 int operator >(const char *c, const String& s2)
858 {
859     if (strcmp(c, s2.str) > 0) return TRUE ;
860     else                       return FALSE ;
861 }
operator <(const char * c,const String & s2)862 int operator <(const char *c, const String& s2)
863 {
864     if (strcmp(c, s2.str) < 0) return TRUE ;
865     else                       return FALSE ;
866 }
operator >=(const char * c,const String & s2)867 int operator >=(const char *c, const String& s2)
868 {
869     if (strcmp(c, s2.str) >= 0) return TRUE ;
870     else                        return FALSE ;
871 }
operator <=(const char * c,const String & s2)872 int operator <=(const char *c, const String& s2)
873 {
874     if (strcmp(c, s2.str) <= 0) return TRUE ;
875     else                        return FALSE ;
876 }
877 
878 // ���դ� char �Ȥ����
operator >(const char c,const String & s2)879 int operator >(const char c, const String& s2)
880 {
881     return (String(c) > s2) ;
882 }
operator <(const char c,const String & s2)883 int operator <(const char c, const String& s2)
884 {
885     return (String(c) < s2) ;
886 }
operator >=(const char c,const String & s2)887 int operator >=(const char c, const String& s2)
888 {
889     return (String(c) >= s2) ;
890 }
operator <=(const char c,const String & s2)891 int operator <=(const char c, const String& s2)
892 {
893     return (String(c) <= s2) ;
894 }
895 
896 
897 // ���ڥ졼����()�������黻��
operator ()(const String & s)898 String& String::operator ()(const String& s)
899 {
900     return (*this = s) ;
901 }
operator ()(const char * c)902 String& String::operator ()(const char *c)
903 {
904     return (*this = c) ;
905 }
operator ()(const char c)906 String& String::operator ()(const char c)
907 {
908     return (*this = c) ;
909 }
910 
911 
912 /*
913 // ���ڥ졼����+, +=��
914 String operator +(const String& d, const String& s)
915 {
916     String  w(d.str, d.length + s.length) ;
917     for (int i = 0 ; i < s.length ; i ++) w.str[i + d.length] = s.str[i] ;
918     return w ;
919 }
920 String operator +(char d, const String& s)
921 {
922     String wd = String(d), w(wd, s.length + 1) ;
923     for (int i = 0 ; i < s.length ; i ++) w.str[i + 1] = s.str[i] ;
924     return w ;
925 }
926 String operator +(const String& d, char s)
927 {
928     String ws(s), w(d.str, d.length + 1) ;
929     for (int i = 0 ; i < ws.length ; i ++) w.str[i + d.length] = ws.str[i] ;
930     return w ;
931 }
932 String& String::operator +=(const char c)
933 {
934     return (*this = *this + c) ;
935 }
936 String& String::operator +=(const String& s)
937 {
938     return (*this = *this + s) ;
939 }
940 */
941 
942 // ���ڥ졼����+, +=��
943 
944 // String Ʊ�Τ�Ϣ��
operator +(const String & s) const945 String String::operator +(const String& s) const
946 {
947     String  w(str, length + s.length) ;
948 
949 //    for (int i = 0 ; i < s.length ; i ++) w.str[i + length] = s.str[i] ;
950     memcpy(&(w.str[length]), s.str, s.length) ;
951 
952     return w ;
953 }
954 // ���դ� char * �Ȥ�Ϣ��
operator +(const char * s) const955 String String::operator +(const char *s) const
956 {
957     int slen = strlen(s) ;
958     String  w(str, length + slen) ;
959 
960 //    for (int i = 0 ; i < slen ; i ++) w.str[i + length] = s[i] ;
961     memcpy(&(w.str[length]), s, slen) ;
962 
963     return w ;
964 }
965 // ���դ� char �Ȥ�Ϣ��
operator +(const char s) const966 String String::operator +(const char s) const
967 {
968     String w(str, length + 1) ;
969     w.str[w.length - 1] = s ;
970     return w ;
971 }
972 
973 // ���դ� char * �Ȥ�Ϣ��
operator +(const char * d,const String & s)974 String operator +(const char *d, const String& s)
975 {
976     int     dlen = strlen(d) ;
977     String  w(d, dlen + s.length) ;
978     for (int i = 0 ; i < s.length ; i ++) w.str[i + dlen] = s.str[i] ;
979     return w ;
980 }
981 
982 // ���դ� char �Ȥ�Ϣ��
operator +(const char d,const String & s)983 String operator +(const char d, const String& s)
984 {
985     String wd(d), w(wd, s.length + 1) ;
986     for (int i = 0 ; i < s.length ; i ++) w.str[i + 1] = s.str[i] ;
987     return w ;
988 }
989 
990 // Ϣ������
operator +=(const String & s)991 String& String::operator +=(const String& s)
992 {
993 	if (this == &s)
994 	{
995 		*this += String(s) ;
996 		return *this ;
997 	}
998 
999 	Realloc(length + s.length) ;
1000 	memcpy(&(str[length]), s.str, s.length + 1) ;
1001 	length += s.length ;
1002 
1003 	return *this ;
1004 /*
1005     int len = length ;
1006     Length(len + s.length) ;
1007     for (int i = 0 ; i < s.length ; i ++)
1008       str[i + len] = s.str[i] ;
1009 
1010     return (*this) ;
1011 */
1012 }
operator +=(const char * s)1013 String& String::operator +=(const char *s)
1014 {
1015 	int slen = strlen(s) ;
1016 	Realloc(length + slen) ;
1017 	memcpy(&(str[length]), s, slen + 1) ;
1018 	length += slen ;
1019 
1020 	return *this ;
1021 
1022 //    return (*this = *this + s) ;
1023 }
operator +=(const char c)1024 String& String::operator +=(const char c)
1025 {
1026 	Realloc(length + 1) ;
1027 	length ++ ;
1028 	str[length - 1] = c ;
1029 	str[length] = '\0' ;
1030 
1031 	return *this ;
1032 /*
1033     Length(length + 1) ;
1034     str[length - 1] = c ;
1035     return (*this) ;
1036 */
1037 }
1038 
1039 
1040 // ���ڥ졼����*, *=��
operator *(int n)1041 String String::operator *(int n)
1042 {
1043 	String s(length * n) ;
1044 	for (int i = 0 ; i < n ; i ++)
1045 		memcpy(&(str[length * i]), str, length) ;
1046 	length *= n ;
1047 	return s ;
1048 /*
1049     String  s ;
1050     for (int i = 0 ; i < n ; i ++) s += *this ;
1051     return s ;
1052 */
1053 }
operator *=(int n)1054 String& String::operator *=(int n)
1055 {
1056 	Realloc(length * n) ;
1057 	for (int i = 1 ; i < n ; i ++)
1058 		memcpy(&(str[length * i]), str, length) ;
1059 	length *= n ;
1060 	return *this ;
1061 
1062 //    return (*this = *this * n) ;
1063 }
1064 
1065 // �����ϥ��ڥ졼��
operator <<(ostream & os,const String & s)1066 ostream& operator <<(ostream& os, const String& s)
1067 {
1068     os << s.str ;
1069     return os ;
1070 }
operator >>(istream & is,String & s)1071 istream& operator >>(istream& is, String& s)
1072 {
1073     is >> s.str ;
1074     return is ;
1075 }
1076 
1077 
1078 // ��ĥ
1079 
1080 // ʸ������ڤ�ͤ�ʱ�¦�������ޤ��ϲ��Ԥޤ���TAB��ʤ���
Cut()1081 String& String::Cut()
1082 {
1083     int  i ;
1084     for (i = length ; i >= 1 ; i --)
1085     {
1086 //	if (Character(i) != ' '  &&
1087 //	    Character(i) != '\n' &&
1088 //	    Character(i) != '\t') break ;
1089 	if (!isspace(Character(i))) break ;
1090     }
1091     Length(i) ; // ʸ�����Ĺ�����ڤ�ͤ��
1092 
1093     return *this ;
1094 }
1095 
1096 
1097 // ʸ������ڤ�ͤ�ʱ�¦�������ޤ��ϲ��Ԥޤ���TAB��ʤ���
Cut(String s)1098 String& Cut(String s)
1099 {
1100     return s.Cut() ;
1101 }
1102 
1103 // ʸ������ڤ�ͤ�ʱ�¦�������ޤ��ϲ��Ԥޤ���TAB��ʤ���
Cut(const char * s)1104 String& Cut(const char *s)
1105 {
1106     String tmp(s) ;
1107     return tmp.Cut() ;
1108 }
1109 
1110 
1111 
1112 
1113 
1114 
1115 #ifdef OVERRIDE_NEW_OPERATOR
1116 
1117 
1118 //��������������������������������������������������������������������������
1119 //      ����Ρ��ɹ�¤��
1120 //��������������������������������������������������������������������������
1121 typedef struct memnode_tag
1122 {
1123         struct memnode_tag      *pNext;
1124         unsigned long                        dwSize;
1125         int                                     pMem[1];
1126 } MEMNODE;
1127 
1128 // ��¤�ΤΥإå���ʬ�Υ�����
1129 
1130 #define MEMNODE_HDRSIZE (sizeof(MEMNODE*) + sizeof(unsigned long))
1131 
1132 
1133 
1134 //��������������������������������������������������������������������������
1135 //      �ϥå���ơ��֥��Ϣ
1136 //��������������������������������������������������������������������������
1137 // �ϥå���ơ��֥���礭���ʣ����߾�Ǥ��뤳�Ȥ�ɬ�ܡ�
1138 //#define HASHTABLE_SIZE  0x1000
1139 #define HASHTABLE_SIZE  0x1000
1140 
1141 // �ϥå���ơ��֥�
1142 static MEMNODE memtbl[HASHTABLE_SIZE];
1143 
1144 // �ݥ�������ϥå��奭������륤��饤��ؿ�
GetHashKey(void * p)1145 static inline unsigned long GetHashKey( void* p )
1146 {
1147         return (int)p & (HASHTABLE_SIZE - 1);
1148 }
1149 
1150 
1151 
1152 //��������������������������������������������������������������������������
1153 //      �ǡ�������
1154 //��������������������������������������������������������������������������
1155 // ���������׵����ʼ��Դޤ��
1156 static int nAllocCtr = 0;
1157 
1158 // �ե꡼�׵����ʼ��Դޤ��
1159 static int nFreeCtr = 0;
1160 
1161 // �������ȼ��Բ��
1162 static int nAllocFail = 0;
1163 
1164 // �ե꡼���Բ��
1165 static int nFreeFail = 0;
1166 
1167 // NULL �ե꡼���
1168 static int nFreeNull = 0;
1169 
1170 // ���ߤΥ�������̡�ñ������¡�
1171 static int nAllocAmt = 0;
1172 
1173 // ������Υ��������
1174 static int nAllocAmtMax = 0;
1175 
1176 // ����Ū�����å���λ
1177 static int isChecked = FALSE ;
1178 
1179 
1180 // ̤��������ơ��֥�
1181 #define MAX_UNFREE_TABLE 10000
1182 static int nLeaks ;
1183 static MEMNODE *unfree[MAX_UNFREE_TABLE] ;
1184 
1185 
1186 //��������������������������������������������������������������������������
1187 //      �����С��饤�ɤ���new�黻��
1188 //��������������������������������������������������������������������������
operator new(size_t n)1189 void* :: operator new( size_t n )
1190 {
1191         MEMNODE*        pNewNode;
1192         MEMNODE*        pNode;
1193         unsigned long           nKey;
1194 
1195 		if (!nAllocCtr)
1196 		{
1197 			for (int i = 0 ; i < HASHTABLE_SIZE ; i ++)
1198 				memtbl[i].pNext = NULL ;
1199 		}
1200 
1201         nAllocCtr++;
1202 
1203         // �Ȥꤢ�����ΰ�����
1204         pNewNode = (MEMNODE*)malloc( n + sizeof(MEMNODE) );
1205         if ( pNewNode == NULL )
1206         {
1207                 nAllocFail++;
1208                 return NULL;
1209         }
1210 
1211 
1212 /*
1213 		else if (//((int)pNewNode) == (0xD82B40) ||
1214 				 //((int)pNewNode) == (0xD82DE0) ||
1215 //				 ((int)pNewNode) == (0xD83830) ||
1216 				 ((int)pNewNode) == (0xD837B0))
1217 		{
1218 			pNewNode = pNewNode ;
1219 //			inline int logprintf(const char* format, ...) ;
1220 //			logprintf("pNewNode:%lX\n", pNewNode) ;
1221 		}
1222 
1223 //			inline int logprintf(const char* format, ...) ;
1224 //			logprintf("pNewNode:%lX  pNewNode->pMem:%lX\n", (int)pNewNode, (int)pNewNode->pMem) ;
1225 */
1226 
1227         pNewNode->pNext = NULL;
1228         pNewNode->dwSize = n;
1229 
1230         // ��������̤ν���
1231         nAllocAmt += n;
1232         if ( nAllocAmt > nAllocAmtMax ) nAllocAmtMax = nAllocAmt;
1233 
1234         // �ϥå���ơ��֥����Ƭ�Υ���ȥ������
1235         nKey    = GetHashKey(pNewNode);
1236         pNode   = &memtbl[nKey];
1237 
1238         // Ϣ��κǸ���������
1239         while ( pNode->pNext != NULL )
1240         {
1241                 pNode = pNode->pNext;
1242         }
1243 
1244         // Ϣ��κǸ����� pNewNode ���ɲ�
1245         pNode->pNext = pNewNode;
1246 
1247         // pMem�����ݤ��줿�ΰ�Ȥʤ�
1248         return (void*)pNewNode->pMem;
1249 }
1250 
1251 
1252 
1253 //��������������������������������������������������������������������������
1254 //      �����С��饤�ɤ���delete�黻��
1255 //��������������������������������������������������������������������������
operator delete(void * p)1256 void :: operator delete( void* p )
1257 {
1258         MEMNODE*        pDelNode;
1259         MEMNODE*        pNode;
1260         int                     nKey;
1261 
1262         nFreeCtr++;
1263 
1264 		if (!p)
1265 		{
1266 			nFreeFail ++ ;
1267 			nFreeNull ++ ;
1268 			return ;
1269 		}
1270 
1271         // �ݥ���p���Ρ��ɤΥݥ������Ф���
1272         pDelNode = (MEMNODE*)((int)p - MEMNODE_HDRSIZE);
1273 
1274 		if (isChecked)
1275 		{
1276 			for (int i = 0 ; i < nLeaks ; i ++)
1277 			{
1278 				if (unfree[i] == pDelNode)
1279 				{
1280 					unfree[i] = NULL ;
1281 					free(pDelNode) ;
1282 				}
1283 			}
1284 
1285 			FILE *fp = fopen("leakcheck.log", "w") ;
1286 			if (fp)
1287 			{
1288 				for (int i = 0 ; i < nLeaks ; i ++)
1289 				{
1290 					if (unfree[i])
1291 						fprintf(fp, "leak: address: 0x%lX\n", (int)unfree[i]) ;
1292 				}
1293 //				fprintf(fp, "already checked!  address: %lX  delete request: %ld\n", ((int)p - MEMNODE_HDRSIZE), nFreeCtr) ;
1294 				fprintf(fp, "delete request: %ld\n", nFreeCtr) ;
1295 				fclose(fp) ;
1296 			}
1297 			return ;
1298 		}
1299 
1300 		if ((int)pDelNode == 0x00d729d0)
1301 		{
1302 			pDelNode = pDelNode ;
1303 		}
1304 
1305         // pDelNode���ϥå��奭�������
1306         nKey = GetHashKey(pDelNode);
1307 
1308         // �ϥå���ơ��֥��ꡤpDelNode�ΰ�����ΥΡ��ɤ�����
1309         pNode = &memtbl[nKey];
1310         while ( pNode->pNext != pDelNode && pNode->pNext != NULL )
1311         {
1312                 pNode = pNode->pNext;
1313         }
1314 
1315         // �Ρ��ɤ����Ĥ���ʤ��ä����ν����ʥ��顼��
1316         if ( pNode->pNext == NULL )
1317         {
1318                 nFreeFail++;
1319                 return;
1320         }
1321 
1322         // Ϣ��ηҤ�ľ��
1323         pNode->pNext = pDelNode->pNext;
1324 
1325         // �ΰ�γ���
1326         nAllocAmt -= pDelNode->dwSize;
1327         free(pDelNode);
1328 }
1329 
1330 
1331 
1332 //��������������������������������������������������������������������������
1333 //      ���׽���
1334 //��������������������������������������������������������������������������
MemDebugFinal()1335 void MemDebugFinal()
1336 {
1337         char    pStrBuf[256];
1338         int             i;
1339 		int     leakBytes = 0 ;
1340 
1341 		isChecked = TRUE ;
1342 		for (i = 0 ; i < MAX_UNFREE_TABLE ; i ++)
1343 			unfree[i] = NULL ;
1344 
1345 
1346 //		inline int logprintf(const char* format, ...) ;
1347 
1348 		nLeaks = 0 ;
1349 
1350         // �ϥå���ơ��֥����������̤�������ΰ褬���ä���ٹ��ɽ����
1351         for ( i=0 ; i<HASHTABLE_SIZE ; i++ )
1352         {
1353                 MEMNODE* pNode = memtbl[i].pNext;
1354 
1355                 while ( pNode )
1356                 {
1357                         MEMNODE* pNext = pNode->pNext;
1358 //                        sprintf( pStrBuf, ">> Memory leak found!!  key: %d  address:%lX  pNext:%lX  size:%d bytes\r\n", i, (int)pNode, (int)pNode->pNext, (int)pNode->dwSize) ;
1359                         sprintf( pStrBuf, ">> Memory leak found!!  address:%lX  %d bytes\r\n", (int)pNode, (int)pNode->dwSize) ;
1360 //                        logprintf( pStrBuf );
1361 						leakBytes += pNode->dwSize ;
1362 //                        free( pNode );
1363 
1364 						unfree[nLeaks] = pNode ;
1365 						nLeaks ++ ;
1366 
1367                         pNode = pNext;
1368                 }
1369         }
1370 
1371 /*
1372         // ���׷�̤����
1373         sprintf( pStrBuf, ">> new    request : %ld times\r\n", nAllocCtr );
1374         logprintf( pStrBuf );
1375 
1376         sprintf( pStrBuf, ">> delete request : %ld times\r\n", nFreeCtr );
1377         logprintf( pStrBuf );
1378 
1379         sprintf( pStrBuf, ">> new    failure : %ld times\r\n", nAllocFail );
1380         logprintf( pStrBuf );
1381 
1382         sprintf( pStrBuf, ">> delete failure : %ld times\r\n", nFreeFail );
1383         logprintf( pStrBuf );
1384 
1385         sprintf( pStrBuf, ">> delete NULL    : %ld times\r\n", nFreeNull );
1386         logprintf( pStrBuf );
1387 
1388         sprintf( pStrBuf, ">> allocation max : %ld bytes\r\n", nAllocAmtMax );
1389         logprintf( pStrBuf );
1390 
1391         sprintf( pStrBuf, ">> total %d bytes leak\r\n", leakBytes );
1392         logprintf( pStrBuf );
1393 */
1394 }
1395 
1396 
1397 #endif	// #ifdef OVERRIDE_NEW_OPERATOR
1398