source: CMT/v1r14p20031120/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  if (pattern[0] == 0) return;
378
379  int pattern_length = strlen (pattern);
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  if (pattern[0] == 0) return;
442
443  int pattern_length = strlen (pattern);
444
445  int replacement_length = strlen (replacement);
446  int delta = replacement_length - pattern_length;
447
448  int pos = 0;
449
450  while ((pos = find (pos, pattern)) != npos)
451    {
452      if (delta > 0)
453        {
454            // string will be enlarged
455          extend (delta);
456
457          char* src = &_data[_size];
458          char* dest = src + delta;
459          while (src > &_data[pos])
460            {
461              *dest = *src;
462              src--;
463              dest--;
464            }
465        }
466      else if (delta < 0)
467        {
468            // string will be shortened
469
470          char* src = &_data[pos + pattern_length];
471          char* dest = src + delta;
472          while (*src != 0)
473            {
474              *dest = *src;
475              src++;
476              dest++;
477            }
478          *dest = *src;
479        }
480
481      strncpy (&_data[pos], replacement, replacement_length);
482      pos += replacement_length;
483      _size += delta;
484    }
485}
486
487void cmt_string::replace_all (const cmt_string& pattern,
488                              const cmt_string& replacement)
489{
490  const char* p_text = pattern._data;
491  const char* r_text = replacement._data;
492  cmt_string& me = *this;
493
494  me.replace_all (p_text, r_text);
495}
496
497void cmt_string::trim ()
498{
499  if (size () == 0) return;
500
501  int i = 0;
502
503  i = strspn (_data, " \t");
504  if (i > 0) erase (0, i);
505
506  for (i = _size - 1; i >= 0; i--)
507    {
508      char c = _data[i];
509      if ((c == ' ') || (c == '\t')) continue;
510      erase (i + 1);
511      break;
512    }
513}
514
515cmt_string cmt_string::substr (int pos) const
516{
517  if ((_data == 0) ||
518      (pos < 0) ||
519      (pos >= _size))
520    {
521      return ((cmt_string) "");
522    }
523  else
524    {
525      return ((cmt_string) &_data[pos]);
526    }
527}
528
529cmt_string cmt_string::substr (int pos, int length) const
530{
531  if ((_data == 0) ||
532      (pos < 0) ||
533      (pos >= _size))
534    {
535      return ((cmt_string) "");
536    }
537  else
538    {
539      cmt_string result (&_data[pos]);
540      result.erase (length);
541      return (result);
542    }
543}
544
545void cmt_string::substr (int pos, cmt_string& dest) const
546{
547  if ((_data == 0) ||
548      (pos < 0) ||
549      (pos >= _size))
550    {
551      dest = "";
552    }
553  else
554    {
555      dest = (const char*) &_data[pos];
556    }
557}
558
559void cmt_string::substr (int pos, int length, cmt_string& dest) const
560{
561  if ((_data == 0) ||
562      (pos < 0) ||
563      (pos >= _size))
564    {
565      dest = "";
566    }
567  else
568    {
569      dest = (const char*) &_data[pos];
570      dest.erase (length);
571    }
572}
573
574bool cmt_string::operator < (const char* text) const
575{
576  if (text == 0) return (false);
577  if (_data == 0) return (false);
578
579  if (strcmp (_data, text) < 0) return (true);
580  return (false);
581}
582
583bool cmt_string::operator < (const cmt_string& other) const
584{
585  const char* text = other._data;
586  const cmt_string& me = *this;
587
588  return (me < text);
589}
590
591bool cmt_string::operator == (const char* text) const
592{
593  if (text == 0)
594    {
595      if (_data == 0) return (true);
596      if (_size == 0) return (true);
597      return (false);
598    }
599  if (_data == 0)
600    {
601      if (text == 0) return (true);
602      if (text[0] == 0) return (true);
603      return (false);
604    }
605
606  if (strcmp (_data, text) == 0) return (true);
607  return (false);
608}
609
610bool cmt_string::operator == (const cmt_string& other) const
611{
612  const char* text = other._data;
613  const cmt_string& me = *this;
614
615  return (me == text);
616}
617
618bool cmt_string::operator != (const char* text) const
619{
620  const cmt_string& me = *this;
621
622  if (!(me == text)) return (true);
623  return (false);
624}
625
626bool cmt_string::operator != (const cmt_string& other) const
627{
628  const char* text = other._data;
629  const cmt_string& me = *this;
630
631  return (me != text);
632}
633
634bool cmt_string::operator > (const char* text) const
635{
636  if (text == 0) return (false);
637  if (_data == 0) return (false);
638
639  if (strcmp (_data, text) > 0) return (true);
640  return (false);
641}
642
643bool cmt_string::operator > (const cmt_string& other) const
644{
645  const char* text = other._data;
646  const cmt_string& me = *this;
647
648  return (me > text);
649}
650
651void cmt_string::extend (int n)
652{
653  if (_data != 0) n += _size;
654  allocate (n);
655}
656
657void cmt_string::allocate (int n)
658{
659  if ((n + 1) > _allocated)
660    {
661      static const int quantum = 128;
662      int frames = ((n + 1)/quantum) + 1;
663      _allocated = frames * quantum;
664
665#ifdef CMT_USE_NEW_DELETE
666      char* new_data = new char [_allocated + 1];
667#else
668      char* new_data = (char*) malloc (_allocated + 1);
669#endif
670
671
672      if (_data != 0)
673        {
674          strcpy (new_data, _data);
675
676#ifdef CMT_USE_NEW_DELETE
677          delete[] _data;
678#else
679          free (_data);
680#endif
681
682          _data = new_data;
683        }
684      else
685        {
686          new_data[0] = 0;
687        }
688
689      _data = new_data;
690    }
691}
692
693ostream& operator << (ostream& o, const cmt_string& s)
694{
695  o << (const char*) s;
696  return (o);
697}
698
699cmt_string operator + (const char* text, const cmt_string& s)
700{
701  cmt_string result = text;
702  result += s;
703  return (result);
704}
705
706cmt_string operator + (char c, const cmt_string& s)
707{
708  cmt_string result = c;
709  result += s;
710  return (result);
711}
712
713bool cmt_string::read (const cmt_string& file_name)
714{
715  FILE* f = fopen (file_name.c_str (), "rb");
716  if (f != NULL)
717    {
718      fseek (f, 0L, SEEK_END);
719      int size = ftell (f);
720      fseek (f, 0L, SEEK_SET);
721
722      allocate (size + 1);
723
724      fread (&_data[0], size, 1, f);
725
726      _data[size] = 0;
727      _size = size;
728
729      fclose (f);
730
731      return (true);
732    }
733  else
734    {
735      cmt_string& me = *this;
736      me = "";
737
738      return (false);
739    }
740}
741
742bool cmt_string::write (const cmt_string& file_name) const
743{
744  FILE* f = fopen (file_name.c_str (), "wb");
745  if (f != NULL)
746    {
747      write (f);
748      fclose (f);
749      return (true);
750    }
751  else
752    {
753      return (false);
754    }
755}
756
757void cmt_string::write (FILE* f) const
758{
759  fwrite (&_data[0], size (), 1, f);
760}
761
762void cmt_string::write (ostream& output)
763{
764  output.write (&_data[0], size ());
765}
766
Note: See TracBrowser for help on using the repository browser.