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