xref: /aoo41x/main/xml2cmp/source/support/sistr.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #include <sistr.hxx>
30 
31 // The following two header-files declare
32 //   standard ANSI-C++ functions. They may be replaced
33 //   by the corresponding header-file-names of the
34 //   actually used runtime library.
35 #include <string.h>  // strlen(), memcpy(), memset()
36 #include <ctype.h>   // tolower()
37 #include <limits.h>  // INT_MAX
38 
39 #if (_MSC_VER >=1400)
40 #pragma warning(disable:4365)
41 #endif
42 
43 const char NULCH = '\0';
44 const int  NO_POS = -1;
45 
46 
47 Simstr::Simstr(const char * str_)
48 {
49    if (str_ == 0)
50       {
51          len = 0;
52          sz = new char[1];
53          *sz = 0;
54       }
55    else
56       {
57          len = strlen(str_);
58          sz = new char[len+1];
59          memcpy(sz,str_,len+1);
60       }
61 }
62 
63 Simstr::Simstr( const char *   anybytes,
64 				int            firstBytesPos,
65 				int            nrOfBytes)
66 {
67    unsigned slen = strlen(anybytes);
68    if (anybytes == 0 || slen <= unsigned(firstBytesPos))
69 	  {
70 		 len = 0;
71 		 sz = new char[1];
72 		 *sz = 0;
73 	  }
74    else
75 	  {
76          int maxLen = slen - unsigned(firstBytesPos);
77          len =  maxLen < nrOfBytes
78                   ? maxLen
79                   : nrOfBytes;
80          sz = new char[len+1];
81          memcpy(sz,anybytes+firstBytesPos,len);
82          *(sz+len) = 0;
83       }
84 }
85 
86 
87 Simstr::Simstr(const Simstr & S)
88 {
89    len = S.len;
90    sz = new char[len+1];
91    memcpy(sz,S.sz,len+1);
92 }
93 
94 Simstr & Simstr::operator=(const Simstr & S)
95 {
96    if (sz == S.sz)
97       return *this;
98 
99    delete [] sz;
100 
101    len = S.len;
102    sz = new char[len+1];
103    memcpy(sz,S.sz,len+1);
104 
105    return *this;
106 }
107 
108 Simstr::~Simstr()
109 {
110    delete [] sz;
111 }
112 
113 Simstr
114 Simstr::operator+(const Simstr & S) const
115 {
116    Simstr ret = sz;
117    ret.push_back(S);
118    return ret;
119 }
120 
121 Simstr &
122 Simstr::operator+=(const Simstr & S)
123 {
124    push_back(S);
125    return *this;
126 }
127 
128 
129 // REL
130 
131 bool
132 Simstr::operator==(const Simstr & S) const
133 { return !strcmp(sz,S.sz) ? true : false; }
134 
135 bool
136 Simstr::operator!=(const Simstr & S) const
137 { return strcmp(sz,S.sz) ? true : false; }
138 
139 bool
140 Simstr::operator<(const Simstr & S) const
141 { return (strcmp(sz,S.sz) < 0) ? true : false; }
142 
143 bool
144 Simstr::operator>(const Simstr & S) const
145 { return (strcmp(sz,S.sz) > 0) ? true : false; }
146 
147 bool
148 Simstr::operator<=(const Simstr & S) const
149 { return (strcmp(sz,S.sz) <= 0) ? true : false; }
150 
151 bool
152 Simstr::operator>=(const Simstr & S) const
153 { return (strcmp(sz,S.sz) >= 0) ? true : false; }
154 
155 
156 
157 
158 // **************          LIST - Funktionen        *****************
159 
160 // Insert
161 
162 void
163 Simstr::push_front(char c)
164 {
165    char * result = new char[len+2];
166 
167    result[0] = c;
168    memcpy(result+1,sz,len+1);
169 
170    delete [] sz;
171    sz = result;
172    len++;
173 }
174 
175 void
176 Simstr::push_back(char c)
177 {
178    char * result = new char[len+2];
179 
180    memcpy(result,sz,len);
181    result[len] = c;
182    result[len+1] = 0;
183 
184    delete [] sz;
185    sz = result;
186    len++;
187 }
188 
189 void
190 Simstr::push_front(const Simstr & S)
191 {
192    char * result = new char[len+1+S.len];
193 
194    memcpy(result,S.sz,S.len);
195    memcpy(result+S.len,sz,len+1);
196 
197    delete [] sz;
198    sz = result;
199    len += S.len;
200 }
201 
202 void
203 Simstr::push_back(const Simstr & S)
204 {
205    char * result = new char[len+1+S.len];
206 
207    memcpy(result,sz,len);
208    memcpy(result+len,S.sz,S.len+1);
209 
210    delete [] sz;
211    sz = result;
212    len += S.len;
213 }
214 
215 
216 // Remove
217 
218 void
219 Simstr::remove(int  pos, int  anzahl)
220 {
221    if (pos >= len || pos < 0 || anzahl < 1)
222       return;
223 
224 	int anz = len - pos < anzahl ? len - pos : anzahl;
225 
226 	char * result = new char[len-anz+1];
227 
228 	memcpy(result,sz,pos);
229    memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
230 
231    delete [] sz;
232    sz = result;
233    len -= anz;
234 }
235 
236 void
237 Simstr::remove_trailing_blanks()
238 {
239 	int newlen = len-1;
240 	for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
241 
242 	if (newlen < len-1)
243     	remove ( newlen+1, len-newlen);
244 }
245 
246 // Find
247 
248 int
249 Simstr::pos_first(char c) const
250 {
251    int i = 0;
252    for (i = 0; i < len ? sz[i] != c : false; i++) ;
253    if (i >= len)
254       return NO_POS;
255    else
256       return i;
257 }
258 
259 int
260 Simstr::pos_last(char c) const
261 {
262    int i = 0;
263    for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
264    if (i < 0)
265       return NO_POS;
266    else
267       return i;
268 }
269 
270 bool
271 Simstr::is_no_text() const
272 {
273    if (!len)
274 	  return true;
275 
276    int i;
277    for (i = 0; sz[i] <= 32 && i < len; i++) ;
278    if (i < len)
279 		return false;
280 	return true;
281 }
282 
283 // Change
284 
285 void
286 Simstr::replace_all(char oldCh, char newCh)
287 {
288    for (int i=0; i < len; i++)
289       if (sz[i] == oldCh)
290          sz[i] = newCh;
291 }
292 
293 //   Simstr addition
294 Simstr
295 operator+(const char * str, const Simstr & S)
296 {
297    Simstr ret = S;
298    ret.push_front(str);
299    return ret;
300 }
301 
302 Simstr
303 operator+(const Simstr & S, const char * str)
304 {
305    Simstr ret = S;
306    ret.push_back(str);
307    return ret;
308 }
309 
310 Simstr
311 operator+(char c, const Simstr & S)
312 {
313    Simstr ret = S;
314    ret.push_front(c);
315    return ret;
316 }
317 
318 Simstr
319 operator+(const Simstr & S, char c)
320 {
321    Simstr ret = S;
322    ret.push_back(c);
323    return ret;
324 }
325 
326 
327 // Simstr-Vergleiche mit char *
328 bool
329 operator==(const Simstr & S, const char * str)
330 {
331    return strcmp(S,str) == 0;
332 }
333 
334 bool
335 operator!=(const Simstr & S, const char * str)
336 {
337    return strcmp(S,str) != 0;
338 }
339 
340 bool
341 operator<(const Simstr & S, const char * str)
342 {
343    return strcmp(S,str) < 0;
344 }
345 
346 bool
347 operator>(const Simstr & S, const char * str)
348 {
349    return strcmp(S,str) > 0;
350 }
351 
352 bool
353 operator<=(const Simstr & S, const char * str)
354 {
355    return strcmp(S,str) <= 0;
356 }
357 
358 bool
359 operator>=(const Simstr & S, const char * str)
360 {
361    return strcmp(S,str) >= 0;
362 }
363 
364 bool
365 operator==(const char * str, const Simstr & S)
366 {
367    return strcmp(str,S) == 0;
368 }
369 
370 bool
371 operator!=(const char * str, const Simstr & S)
372 {
373    return strcmp(str,S) != 0;
374 }
375 
376 bool
377 operator<(const char * str, const Simstr & S)
378 {
379    return strcmp(str,S) < 0;
380 }
381 
382 bool
383 operator>(const char * str, const Simstr & S)
384 {
385    return strcmp(str,S) > 0;
386 }
387 
388 bool
389 operator<=(const char * str, const Simstr & S)
390 {
391    return strcmp(str,S) <= 0;
392 }
393 
394 bool
395 operator>=(const char * str, const Simstr & S)
396 {
397    return strcmp(str,S) >= 0;
398 }
399 
400 
401