1 #ifdef INCS_NEED_DOT_H
2 #include <ctype.h>
3 #else
4 #include <ctype>
5 #endif
6 #include "cerror.h"
7 #include "cstring.h"
8 
CString()9 CString::CString()
10 {
11   buffer=0; length=alloc_len=0;
12 }
13 
CString(const CString & s)14 CString::CString(const CString &s)
15 {
16   if ((const char *)s!=0)
17   {
18     CheckPointer(buffer=new char[(length=alloc_len=s.Length())+1],
19                  "CString::copy constructor::buffer allocation");
20     strcpy (buffer,(const char *)s);
21   }
22   else
23   {
24     buffer=0; length=alloc_len=0;
25   }
26 }
27 
CString(const char * cp)28 CString::CString(const char *cp)
29 {
30   if (cp!=0)
31     {
32       CheckPointer(buffer=new char[(length=alloc_len=strlen(cp))+1],
33                    "CString::const char * constructor::buffer allocation");
34       strcpy(buffer,cp);
35     }
36   else
37     {
38       buffer=0;
39       length=alloc_len=0;
40     }
41 }
42 
~CString()43 CString::~CString()
44 {
45   if (buffer) delete [] buffer;
46   buffer=0; length=alloc_len=0;
47 }
48 
operator char*()49 CString::operator char*()
50 {
51   return buffer;
52 }
53 
operator const char*()54 CString::operator const char*()
55 {
56   return buffer;
57 }
58 
operator const char*() const59 CString::operator const char*() const
60 {
61   return buffer;
62 }
63 
substr(size_t i,size_t j) const64 CString CString::substr(size_t i,size_t j) const
65 {
66   char *cp,c;
67 
68   CheckUpperBound(i+1,length,"CString::substr()");
69   CheckUpperBound(j+1,length,"CString::substr()");
70   if (i>j)
71     {
72       int k;
73       k=j; j=i; i=k;
74     }
75   cp=buffer+i; c=buffer[j+1]; buffer[j+1]='\0';
76 
77   CString retval(cp);
78 
79   buffer[j+1]=c;
80 
81   return retval;
82 }
83 
84 
operator +=(const CString & s)85 CString& CString::operator +=(const CString &s)
86 {
87   char *cp=new char[(length=alloc_len=length+s.Length())+1];
88   CheckPointer(cp,"CString::operator +=::memory allocation");
89   *cp=0; if (buffer) strcpy(cp,buffer);
90   if ((const char *)s) strcat(cp,(const char *)s);
91   if (buffer) delete [] buffer;
92   buffer = cp;
93   return (*this);
94 }
95 
operator +=(const char & c)96 CString& CString::operator +=(const char &c)
97 {
98   char *cp;
99 
100   if (length>=alloc_len)
101     {
102       cp=new char[(alloc_len=length+64)];
103       *cp=0;
104       if (buffer)
105         {
106           strcpy(cp,buffer);
107           delete[] buffer;
108         }
109     }
110   else
111     {
112       cp=buffer;
113     }
114   cp[length]=c; cp[++length]='\0';
115   buffer=cp;
116 
117   return (*this);
118 }
119 
120 
operator =(const CString & s)121 CString& CString::operator =(const CString &s)
122 {
123   if (buffer) delete[] buffer;
124   if ((const char *)s!=0)
125     {
126       buffer=new char[(length=alloc_len=s.Length())+1];
127       CheckPointer(buffer,"CString::operator =::memory allocation");
128       strcpy(buffer,(const char*)s);
129     }
130   else
131     {
132       buffer=0;
133       length=alloc_len=0;
134     }
135   return (*this);
136 }
137 
138 
139 
140 /* Due to bad class design in my code, the following does not work
141    any more with GCC 2.8
142 
143 char& CString::operator[](const size_t index)
144 {
145   CheckUpperBound(index,length,"CString::operator[]");
146   return *(buffer+index);
147 }
148 
149 char& CString::operator[](const size_t index) const
150 {
151   CheckUpperBound(index,length,"CString::operator[]");
152   return *(buffer+index);
153 }
154 
155 It is replaced with the following:
156 */
157 
158 
charAt(const size_t index) const159 char& CString::charAt(const size_t index) const
160 {
161   CheckUpperBound(index,length,"CString::operator[]");
162   return *(buffer+index);
163 }
164 
setCharAt(const size_t index,const char & c)165 char& CString::setCharAt(const size_t index, const char& c)
166 {
167   CheckUpperBound(index,length,"CString::operator[]");
168   return *(buffer+index) = c;
169 }
170 
operator ==(const CString & s) const171 int CString::operator== (const CString &s) const
172 {
173   return ((*this)==(const char*)s);
174 }
175 
operator ==(const char * s) const176 int CString::operator== (const char *s) const
177 {
178   if (s==0||buffer==0)
179     return s==buffer;
180   if (strlen(s)==length)
181     return !strcmp((const char*)s,buffer);
182   else return 0;
183 }
184 
operator ==(char * s) const185 int CString::operator== (char *s) const
186 {
187   if (s==0||buffer==0)
188     return s==buffer;
189   if (strlen(s)==length)
190     return !strcmp((const char*)s,buffer);
191   else return 0;
192 }
193 
operator +(const CString & s1,const CString & s2)194 CString operator + (const CString &s1, const CString &s2)
195 {
196   CString tmp;
197 
198   tmp=s1;
199   tmp+=s2;
200   return tmp;
201 }
202 
operator <<(ostream & o,const CString & s)203 ostream& operator << (ostream& o, const CString&s)
204 {
205   o << (const char *)s;
206   return o;
207 }
208 
operator >>(istream & i,CString & s)209 istream& operator >> (istream& i, CString&s)
210 {
211   char c;
212   s="";
213 
214   while (i)
215     {
216       i.get(c);
217       if (!i)
218         break;
219       if (c=='\n'||c=='\r')
220         break;
221       else
222         s+=c;
223     }
224 
225   return i;
226 }
227 
Length() const228 size_t CString::Length() const
229 {
230   return length;
231 }
232 
upcase(const CString & src)233 CString upcase(const CString& src)
234 {
235   CString ret=src;
236   size_t i;
237 
238   for (i=0;i<src.Length();i++)
239     ret.setCharAt(i,toupper(ret.charAt(i)));
240 
241   return ret;
242 }
243