xref: /aoo41x/main/xml2cmp/source/support/sistr.cxx (revision ab595ff6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #include <sistr.hxx>
26 
27 // The following two header-files declare
28 //   standard ANSI-C++ functions. They may be replaced
29 //   by the corresponding header-file-names of the
30 //   actually used runtime library.
31 #include <string.h>  // strlen(), memcpy(), memset()
32 #include <ctype.h>   // tolower()
33 #include <limits.h>  // INT_MAX
34 
35 #if (_MSC_VER >=1400)
36 #pragma warning(disable:4365)
37 #endif
38 
39 const char NULCH = '\0';
40 const int  NO_POS = -1;
41 
42 
Simstr(const char * str_)43 Simstr::Simstr(const char * str_)
44 {
45    if (str_ == 0)
46       {
47          len = 0;
48          sz = new char[1];
49          *sz = 0;
50       }
51    else
52       {
53          len = strlen(str_);
54          sz = new char[len+1];
55          memcpy(sz,str_,len+1);
56       }
57 }
58 
Simstr(const char * anybytes,int firstBytesPos,int nrOfBytes)59 Simstr::Simstr( const char *   anybytes,
60 				int            firstBytesPos,
61 				int            nrOfBytes)
62 {
63    unsigned slen = strlen(anybytes);
64    if (anybytes == 0 || slen <= unsigned(firstBytesPos))
65 	  {
66 		 len = 0;
67 		 sz = new char[1];
68 		 *sz = 0;
69 	  }
70    else
71 	  {
72          int maxLen = slen - unsigned(firstBytesPos);
73          len =  maxLen < nrOfBytes
74                   ? maxLen
75                   : nrOfBytes;
76          sz = new char[len+1];
77          memcpy(sz,anybytes+firstBytesPos,len);
78          *(sz+len) = 0;
79       }
80 }
81 
82 
Simstr(const Simstr & S)83 Simstr::Simstr(const Simstr & S)
84 {
85    len = S.len;
86    sz = new char[len+1];
87    memcpy(sz,S.sz,len+1);
88 }
89 
operator =(const Simstr & S)90 Simstr & Simstr::operator=(const Simstr & S)
91 {
92    if (sz == S.sz)
93       return *this;
94 
95    delete [] sz;
96 
97    len = S.len;
98    sz = new char[len+1];
99    memcpy(sz,S.sz,len+1);
100 
101    return *this;
102 }
103 
~Simstr()104 Simstr::~Simstr()
105 {
106    delete [] sz;
107 }
108 
109 Simstr
operator +(const Simstr & S) const110 Simstr::operator+(const Simstr & S) const
111 {
112    Simstr ret = sz;
113    ret.push_back(S);
114    return ret;
115 }
116 
117 Simstr &
operator +=(const Simstr & S)118 Simstr::operator+=(const Simstr & S)
119 {
120    push_back(S);
121    return *this;
122 }
123 
124 
125 // REL
126 
127 bool
operator ==(const Simstr & S) const128 Simstr::operator==(const Simstr & S) const
129 { return !strcmp(sz,S.sz) ? true : false; }
130 
131 bool
operator !=(const Simstr & S) const132 Simstr::operator!=(const Simstr & S) const
133 { return strcmp(sz,S.sz) ? true : false; }
134 
135 bool
operator <(const Simstr & S) const136 Simstr::operator<(const Simstr & S) const
137 { return (strcmp(sz,S.sz) < 0) ? true : false; }
138 
139 bool
operator >(const Simstr & S) const140 Simstr::operator>(const Simstr & S) const
141 { return (strcmp(sz,S.sz) > 0) ? true : false; }
142 
143 bool
operator <=(const Simstr & S) const144 Simstr::operator<=(const Simstr & S) const
145 { return (strcmp(sz,S.sz) <= 0) ? true : false; }
146 
147 bool
operator >=(const Simstr & S) const148 Simstr::operator>=(const Simstr & S) const
149 { return (strcmp(sz,S.sz) >= 0) ? true : false; }
150 
151 
152 
153 
154 // **************          LIST - Funktionen        *****************
155 
156 // Insert
157 
158 void
push_front(char c)159 Simstr::push_front(char c)
160 {
161    char * result = new char[len+2];
162 
163    result[0] = c;
164    memcpy(result+1,sz,len+1);
165 
166    delete [] sz;
167    sz = result;
168    len++;
169 }
170 
171 void
push_back(char c)172 Simstr::push_back(char c)
173 {
174    char * result = new char[len+2];
175 
176    memcpy(result,sz,len);
177    result[len] = c;
178    result[len+1] = 0;
179 
180    delete [] sz;
181    sz = result;
182    len++;
183 }
184 
185 void
push_front(const Simstr & S)186 Simstr::push_front(const Simstr & S)
187 {
188    char * result = new char[len+1+S.len];
189 
190    memcpy(result,S.sz,S.len);
191    memcpy(result+S.len,sz,len+1);
192 
193    delete [] sz;
194    sz = result;
195    len += S.len;
196 }
197 
198 void
push_back(const Simstr & S)199 Simstr::push_back(const Simstr & S)
200 {
201    char * result = new char[len+1+S.len];
202 
203    memcpy(result,sz,len);
204    memcpy(result+len,S.sz,S.len+1);
205 
206    delete [] sz;
207    sz = result;
208    len += S.len;
209 }
210 
211 
212 // Remove
213 
214 void
remove(int pos,int anzahl)215 Simstr::remove(int  pos, int  anzahl)
216 {
217    if (pos >= len || pos < 0 || anzahl < 1)
218       return;
219 
220 	int anz = len - pos < anzahl ? len - pos : anzahl;
221 
222 	char * result = new char[len-anz+1];
223 
224 	memcpy(result,sz,pos);
225    memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
226 
227    delete [] sz;
228    sz = result;
229    len -= anz;
230 }
231 
232 void
remove_trailing_blanks()233 Simstr::remove_trailing_blanks()
234 {
235 	int newlen = len-1;
236 	for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
237 
238 	if (newlen < len-1)
239     	remove ( newlen+1, len-newlen);
240 }
241 
242 // Find
243 
244 int
pos_first(char c) const245 Simstr::pos_first(char c) const
246 {
247    int i = 0;
248    for (i = 0; i < len ? sz[i] != c : false; i++) ;
249    if (i >= len)
250       return NO_POS;
251    else
252       return i;
253 }
254 
255 int
pos_last(char c) const256 Simstr::pos_last(char c) const
257 {
258    int i = 0;
259    for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
260    if (i < 0)
261       return NO_POS;
262    else
263       return i;
264 }
265 
266 bool
is_no_text() const267 Simstr::is_no_text() const
268 {
269    if (!len)
270 	  return true;
271 
272    int i;
273    for (i = 0; sz[i] <= 32 && i < len; i++) ;
274    if (i < len)
275 		return false;
276 	return true;
277 }
278 
279 // Change
280 
281 void
replace_all(char oldCh,char newCh)282 Simstr::replace_all(char oldCh, char newCh)
283 {
284    for (int i=0; i < len; i++)
285       if (sz[i] == oldCh)
286          sz[i] = newCh;
287 }
288 
289 //   Simstr addition
290 Simstr
operator +(const char * str,const Simstr & S)291 operator+(const char * str, const Simstr & S)
292 {
293    Simstr ret = S;
294    ret.push_front(str);
295    return ret;
296 }
297 
298 Simstr
operator +(const Simstr & S,const char * str)299 operator+(const Simstr & S, const char * str)
300 {
301    Simstr ret = S;
302    ret.push_back(str);
303    return ret;
304 }
305 
306 Simstr
operator +(char c,const Simstr & S)307 operator+(char c, const Simstr & S)
308 {
309    Simstr ret = S;
310    ret.push_front(c);
311    return ret;
312 }
313 
314 Simstr
operator +(const Simstr & S,char c)315 operator+(const Simstr & S, char c)
316 {
317    Simstr ret = S;
318    ret.push_back(c);
319    return ret;
320 }
321 
322 
323 // Simstr-Vergleiche mit char *
324 bool
operator ==(const Simstr & S,const char * str)325 operator==(const Simstr & S, const char * str)
326 {
327    return strcmp(S,str) == 0;
328 }
329 
330 bool
operator !=(const Simstr & S,const char * str)331 operator!=(const Simstr & S, const char * str)
332 {
333    return strcmp(S,str) != 0;
334 }
335 
336 bool
operator <(const Simstr & S,const char * str)337 operator<(const Simstr & S, const char * str)
338 {
339    return strcmp(S,str) < 0;
340 }
341 
342 bool
operator >(const Simstr & S,const char * str)343 operator>(const Simstr & S, const char * str)
344 {
345    return strcmp(S,str) > 0;
346 }
347 
348 bool
operator <=(const Simstr & S,const char * str)349 operator<=(const Simstr & S, const char * str)
350 {
351    return strcmp(S,str) <= 0;
352 }
353 
354 bool
operator >=(const Simstr & S,const char * str)355 operator>=(const Simstr & S, const char * str)
356 {
357    return strcmp(S,str) >= 0;
358 }
359 
360 bool
operator ==(const char * str,const Simstr & S)361 operator==(const char * str, const Simstr & S)
362 {
363    return strcmp(str,S) == 0;
364 }
365 
366 bool
operator !=(const char * str,const Simstr & S)367 operator!=(const char * str, const Simstr & S)
368 {
369    return strcmp(str,S) != 0;
370 }
371 
372 bool
operator <(const char * str,const Simstr & S)373 operator<(const char * str, const Simstr & S)
374 {
375    return strcmp(str,S) < 0;
376 }
377 
378 bool
operator >(const char * str,const Simstr & S)379 operator>(const char * str, const Simstr & S)
380 {
381    return strcmp(str,S) > 0;
382 }
383 
384 bool
operator <=(const char * str,const Simstr & S)385 operator<=(const char * str, const Simstr & S)
386 {
387    return strcmp(str,S) <= 0;
388 }
389 
390 bool
operator >=(const char * str,const Simstr & S)391 operator>=(const char * str, const Simstr & S)
392 {
393    return strcmp(str,S) >= 0;
394 }
395 
396 
397