source: CMT/v1r10p20011126/src/cmt_string.cxx @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

File size: 13.7 KB
Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include "cmt_std.h"
5#include "cmt_string.h"
6
7cmt_string::cmt_string ()
8{
9  _data = 0;
10  _allocated = 0;
11  _size = 0;
12}
13
14cmt_string::cmt_string (int n)
15{
16  _data = 0;
17  _allocated = 0;
18  _size = 0;
19  allocate (n + 1);
20}
21
22cmt_string::cmt_string (char c)
23{
24  _data = 0;
25  _allocated = 0;
26  _size = 0;
27
28  allocate (2);
29
30  _data[0] = c;
31  _data[1] = 0;
32  _size = 1;
33}
34
35cmt_string::cmt_string (const char* text)
36{
37  _data = 0;
38  _allocated = 0;
39  _size = 0;
40
41  if (text != 0)
42    {
43      _size = strlen (text);
44      allocate (_size + 1);
45      strcpy (_data, text);
46    }
47}
48
49cmt_string::cmt_string (const cmt_string& other)
50{
51  const char* text = other._data;
52
53  _data = 0;
54  _allocated = 0;
55  _size = 0;
56
57  if (text != 0)
58    {
59      _size = strlen (text);
60      allocate (_size + 1);
61      strcpy (_data, text);
62    }
63}
64
65cmt_string::~cmt_string ()
66{
67  if (_data != 0)
68    {
69#ifdef CMT_USE_NEW_DELETE
70      delete[] _data;
71#else
72      free (_data);
73#endif
74    }
75  _data = 0;
76  _allocated = 0;
77  _size = 0;
78}
79
80  //
81  // Operators
82  //
83cmt_string& cmt_string::operator = (char c)
84{
85  allocate (2);
86
87  _data[0] = c;
88  _data[1] = 0;
89
90  _size = 1;
91
92  return (*this);
93}
94
95cmt_string& cmt_string::operator = (const char* text)
96{
97  if (text == _data) return (*this);
98
99  if (text != 0)
100    {
101      _size = strlen (text);
102      allocate (_size + 1);
103      strcpy (_data, text);
104    }
105  else
106    {
107      _size = 0;
108
109      if (_data != 0)
110        {
111          _data[0] = 0;
112        }
113    }
114
115  return (*this);
116}
117
118cmt_string& cmt_string::operator = (const cmt_string& other)
119{
120  const char* text = other._data;
121  cmt_string& me = *this;
122  me = text;
123  return (me);
124}
125
126cmt_string::operator const char* () const
127{
128  if (_data == 0) return ("");
129  else return (_data);
130}
131
132const char* cmt_string::c_str () const
133{
134  if (_data == 0) return ("");
135  else return (_data);
136}
137
138/*
139char* cmt_string::c_str ()
140{
141  return (_data);
142}
143*/
144
145void cmt_string::operator += (char c)
146{
147  extend (2);
148
149  char temp[2] = { c, 0 };
150
151  strcat (&_data[_size], temp);
152  _size++;
153}
154
155void cmt_string::operator += (const char* text)
156{
157  if (text == 0) return;
158
159  int s = strlen (text);
160  extend (s + 1);
161
162  strcat (&_data[_size], text);
163  _size += s;
164}
165
166void cmt_string::operator += (const cmt_string& other)
167{
168  const char* text = other._data;
169  cmt_string& me = *this;
170
171  me += text;
172}
173
174cmt_string cmt_string::operator + (char c) const
175{
176  cmt_string result (_data);
177  result += c;
178
179  return (result);
180}
181
182cmt_string cmt_string::operator + (const char* text) const
183{
184  cmt_string result (_data);
185  result += text;
186
187  return (result);
188}
189
190cmt_string cmt_string::operator + (const cmt_string& other) const
191{
192  cmt_string result (_data);
193  result += other;
194
195  return (result);
196}
197
198char cmt_string::operator [] (int index) const
199{
200  if ((_data == 0) ||
201      (index < 0) ||
202      (index >= _size))
203    {
204      return (0);
205    }
206  else
207    {
208      return (_data[index]);
209    }
210}
211
212char& cmt_string::operator [] (int index)
213{
214  if ((_data == 0) ||
215      (index < 0) ||
216      (index >= _size))
217    {
218      static char temp;
219      return (temp);
220    }
221  else
222    {
223      return (_data[index]);
224    }
225}
226
227int cmt_string::size () const
228{
229  if (_data == 0) return (0);
230  return (_size);
231}
232
233int cmt_string::size ()
234{
235  if (_data == 0) return (0);
236  return (_size);
237}
238
239void cmt_string::resize (int n)
240{
241  allocate (n + 1);
242}
243
244int cmt_string::find (char c) const
245{
246  if (_data == 0) return (npos);
247
248  char* p = strchr (_data, c);
249  if (p == 0) return (npos);
250  return (p - _data);
251}
252
253int cmt_string::find (const char* text) const
254{
255  if (_data == 0) return (npos);
256  if (text == 0) return (npos);
257
258  char* p = strstr (_data, text);
259  if (p == 0) return (npos);
260  return (p - _data);
261}
262
263int cmt_string::find (const cmt_string& other) const
264{
265  const char* text = other._data;
266  return (find (text));
267}
268
269int cmt_string::find (int pos, char c) const
270{
271  if (_data == 0) return (npos);
272  if (pos < 0) return (npos);
273  if (pos >= _size) return (npos);
274
275  char* p = strchr (&_data[pos], c);
276  if (p == 0) return (npos);
277  return (p - _data);
278}
279
280int cmt_string::find (int pos, const char* text) const
281{
282  if (_data == 0) return (npos);
283  if (text == 0) return (npos);
284  if (pos < 0) return (npos);
285  if (pos >= _size) return (npos);
286
287  char* p = strstr (&_data[pos], text);
288  if (p == 0) return (npos);
289  return (p - _data);
290}
291
292int cmt_string::find (int pos, const cmt_string& other) const
293{
294  const char* text = other._data;
295  return (find (pos, text));
296}
297
298int cmt_string::find_last_of (char c) const
299{
300  if (_data == 0) return (npos);
301
302  char* p = strrchr (_data, c);
303  if (p == 0) return (npos);
304  return (p - _data);
305}
306
307int cmt_string::find_last_of (const char* text) const
308{
309  if (_data == 0) return (npos);
310  if (text == 0) return (npos);
311
312  char* ptr = _data;
313  char* last = 0;
314  char* p;
315  while ((p = strstr (ptr, text)) != 0)
316    {
317      last = p;
318      ptr = p + 1;
319    }
320  if (last == 0) return (npos);
321  return (last - _data);
322}
323
324int cmt_string::find_last_of (const cmt_string& other) const
325{
326  const char* text = other._data;
327  return (find_last_of (text));
328}
329
330void cmt_string::erase (int pos)
331{
332  if ((_data == 0) ||
333      (pos < 0) ||
334      (pos >= _size))
335    {
336      return;
337    }
338  else
339    {
340      _data[pos] = 0;
341      _size = pos;
342    }
343}
344
345void cmt_string::erase (int pos, int length)
346{
347  if ((_data == 0) ||
348      (pos < 0) ||
349      (pos >= _size))
350    {
351      return;
352    }
353  else
354    {
355      if ((pos + length) >= _size)
356        {
357          _data[pos] = 0;
358          _size = pos;
359        }
360      else
361        {
362          strcpy (&_data[pos], &_data[pos + length]);
363          _size -= length;
364        }
365    }
366}
367
368
369void cmt_string::replace (const char* pattern, const char* replacement)
370{
371  if (_data == 0) return;
372  if (_size == 0) return;
373  if (pattern == 0) return;
374
375  if (replacement == 0) replacement = "";
376
377  int pattern_length = strlen (pattern);
378
379  if (pattern_length == 0) return;
380
381  int replacement_length = strlen (replacement);
382  int delta = replacement_length - pattern_length;
383
384  int pos;
385
386  if ((pos = find (pattern)) != npos)
387    {
388      if (delta > 0)
389        {
390            // string will be enlarged
391          extend (delta);
392
393          char* src = &_data[_size];
394          char* dest = src + delta;
395          while (src > &_data[pos])
396            {
397              *dest = *src;
398              src--;
399              dest--;
400            }
401        }
402      else if (delta < 0)
403        {
404            // string will be shortened
405
406          char* src = &_data[pos + pattern_length];
407          char* dest = src + delta;
408          while (*src != 0)
409            {
410              *dest = *src;
411              src++;
412              dest++;
413            }
414          *dest = *src;
415        }
416
417      strncpy (&_data[pos], replacement, replacement_length);
418
419      _size += delta;
420    }
421}
422
423void cmt_string::replace (const cmt_string& pattern,
424                          const cmt_string& replacement)
425{
426  const char* p_text = pattern._data;
427  const char* r_text = replacement._data;
428  cmt_string& me = *this;
429
430  me.replace (p_text, r_text);
431}
432
433void cmt_string::replace_all (const char* pattern, const char* replacement)
434{
435  if (_data == 0) return;
436  if (_size == 0) return;
437  if (pattern == 0) return;
438
439  if (replacement == 0) replacement = "";
440
441  int pattern_length = strlen (pattern);
442  if (pattern_length == 0) return;
443
444  int replacement_length = strlen (replacement);
445  int delta = replacement_length - pattern_length;
446
447  int pos = 0;
448
449  while ((pos = find (pos, pattern)) != npos)
450    {
451      if (delta > 0)
452        {
453            // string will be enlarged
454          extend (delta);
455
456          char* src = &_data[_size];
457          char* dest = src + delta;
458          while (src > &_data[pos])
459            {
460              *dest = *src;
461              src--;
462              dest--;
463            }
464        }
465      else if (delta < 0)
466        {
467            // string will be shortened
468
469          char* src = &_data[pos + pattern_length];
470          char* dest = src + delta;
471          while (*src != 0)
472            {
473              *dest = *src;
474              src++;
475              dest++;
476            }
477          *dest = *src;
478        }
479
480      strncpy (&_data[pos], replacement, replacement_length);
481      pos += replacement_length;
482      _size += delta;
483    }
484}
485
486void cmt_string::replace_all (const cmt_string& pattern,
487                              const cmt_string& replacement)
488{
489  const char* p_text = pattern._data;
490  const char* r_text = replacement._data;
491  cmt_string& me = *this;
492
493  me.replace_all (p_text, r_text);
494}
495
496void cmt_string::trim ()
497{
498  if (size () == 0) return;
499
500  int i = 0;
501
502  i = strspn (_data, " \t");
503  if (i > 0) erase (0, i);
504
505  for (i = _size - 1; i >= 0; i--)
506    {
507      char c = _data[i];
508      if ((c == ' ') || (c == '\t')) continue;
509      erase (i + 1);
510      break;
511    }
512}
513
514cmt_string cmt_string::substr (int pos) const
515{
516  if ((_data == 0) ||
517      (pos < 0) ||
518      (pos >= _size))
519    {
520      return ((cmt_string) "");
521    }
522  else
523    {
524      return ((cmt_string) &_data[pos]);
525    }
526}
527
528cmt_string cmt_string::substr (int pos, int length) const
529{
530  if ((_data == 0) ||
531      (pos < 0) ||
532      (pos >= _size))
533    {
534      return ((cmt_string) "");
535    }
536  else
537    {
538      cmt_string result (&_data[pos]);
539      result.erase (length);
540      return (result);
541    }
542}
543
544void cmt_string::substr (int pos, cmt_string& dest) const
545{
546  if ((_data == 0) ||
547      (pos < 0) ||
548      (pos >= _size))
549    {
550      dest = "";
551    }
552  else
553    {
554      dest = (const char*) &_data[pos];
555    }
556}
557
558void cmt_string::substr (int pos, int length, cmt_string& dest) const
559{
560  if ((_data == 0) ||
561      (pos < 0) ||
562      (pos >= _size))
563    {
564      dest = "";
565    }
566  else
567    {
568      dest = (const char*) &_data[pos];
569      dest.erase (length);
570    }
571}
572
573bool cmt_string::operator < (const char* text) const
574{
575  if (text == 0) return (false);
576  if (_data == 0) return (false);
577
578  if (strcmp (_data, text) < 0) return (true);
579  return (false);
580}
581
582bool cmt_string::operator < (const cmt_string& other) const
583{
584  const char* text = other._data;
585  const cmt_string& me = *this;
586
587  return (me < text);
588}
589
590bool cmt_string::operator == (const char* text) const
591{
592  if (text == 0)
593    {
594      if (_data == 0) return (true);
595      if (_size == 0) return (true);
596      return (false);
597    }
598  if (_data == 0)
599    {
600      if (text == 0) return (true);
601      if (strlen (text) == 0) return (true);
602      return (false);
603    }
604
605  if (strcmp (_data, text) == 0) return (true);
606  return (false);
607}
608
609bool cmt_string::operator == (const cmt_string& other) const
610{
611  const char* text = other._data;
612  const cmt_string& me = *this;
613
614  return (me == text);
615}
616
617bool cmt_string::operator != (const char* text) const
618{
619  const cmt_string& me = *this;
620
621  if (!(me == text)) return (true);
622  return (false);
623}
624
625bool cmt_string::operator != (const cmt_string& other) const
626{
627  const char* text = other._data;
628  const cmt_string& me = *this;
629
630  return (me != text);
631}
632
633bool cmt_string::operator > (const char* text) const
634{
635  if (text == 0) return (false);
636  if (_data == 0) return (false);
637
638  if (strcmp (_data, text) > 0) return (true);
639  return (false);
640}
641
642bool cmt_string::operator > (const cmt_string& other) const
643{
644  const char* text = other._data;
645  const cmt_string& me = *this;
646
647  return (me > text);
648}
649
650void cmt_string::extend (int n)
651{
652  if (_data != 0) n += _size;
653  allocate (n);
654}
655
656void cmt_string::allocate (int n)
657{
658  if ((n + 1) > _allocated)
659    {
660      static const int quantum = 128;
661      int frames = ((n + 1)/quantum) + 1;
662      _allocated = frames * quantum;
663
664#ifdef CMT_USE_NEW_DELETE
665      char* new_data = new char [_allocated + 1];
666#else
667      char* new_data = (char*) malloc (_allocated + 1);
668#endif
669
670
671      if (_data != 0)
672        {
673          strcpy (new_data, _data);
674
675#ifdef CMT_USE_NEW_DELETE
676          delete[] _data;
677#else
678          free (_data);
679#endif
680
681          _data = new_data;
682        }
683      else
684        {
685          new_data[0] = 0;
686        }
687
688      _data = new_data;
689    }
690}
691
692ostream& operator << (ostream& o, const cmt_string& s)
693{
694  o << (const char*) s;
695  return (o);
696}
697
698cmt_string operator + (const char* text, const cmt_string& s)
699{
700  cmt_string result = text;
701  result += s;
702  return (result);
703}
704
705cmt_string operator + (char c, const cmt_string& s)
706{
707  cmt_string result = c;
708  result += s;
709  return (result);
710}
711
712bool cmt_string::read (const cmt_string& file_name)
713{
714  FILE* f = fopen (file_name.c_str (), "rb");
715  if (f != NULL)
716    {
717      fseek (f, 0L, SEEK_END);
718      int size = ftell (f);
719      fseek (f, 0L, SEEK_SET);
720
721      allocate (size + 1);
722
723      fread (&_data[0], size, 1, f);
724
725      _data[size] = 0;
726      _size = size;
727
728      fclose (f);
729
730      return (true);
731    }
732  else
733    {
734      cmt_string& me = *this;
735      me = "";
736
737      return (false);
738    }
739}
740
741bool cmt_string::write (const cmt_string& file_name) const
742{
743  FILE* f = fopen (file_name.c_str (), "wb");
744  if (f != NULL)
745    {
746      write (f);
747      fclose (f);
748      return (true);
749    }
750  else
751    {
752      return (false);
753    }
754}
755
756void cmt_string::write (FILE* f) const
757{
758  fwrite (&_data[0], size (), 1, f);
759}
760
761void cmt_string::write (ostream& output)
762{
763  output.write (&_data[0], size ());
764}
765
Note: See TracBrowser for help on using the repository browser.