merge the formfield patch from ooo-build
[ooovba.git] / xml2cmp / source / support / sistr.cxx
blob880b94086c3b97fa1977abfe517b1aeb5ddab67e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sistr.cxx,v $
10 * $Revision: 1.7.10.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <sistr.hxx>
34 // The following two header-files declare
35 // standard ANSI-C++ functions. They may be replaced
36 // by the corresponding header-file-names of the
37 // actually used runtime library.
38 #include <string.h> // strlen(), memcpy(), memset()
39 #include <ctype.h> // tolower()
40 #include <limits.h> // INT_MAX
42 #if (_MSC_VER >=1400)
43 #pragma warning(disable:4365)
44 #endif
46 const char NULCH = '\0';
47 const int NO_POS = -1;
50 Simstr::Simstr(const char * str_)
52 if (str_ == 0)
54 len = 0;
55 sz = new char[1];
56 *sz = 0;
58 else
60 len = strlen(str_);
61 sz = new char[len+1];
62 memcpy(sz,str_,len+1);
66 Simstr::Simstr( const char * anybytes,
67 int firstBytesPos,
68 int nrOfBytes)
70 unsigned slen = strlen(anybytes);
71 if (anybytes == 0 || slen <= unsigned(firstBytesPos))
73 len = 0;
74 sz = new char[1];
75 *sz = 0;
77 else
79 int maxLen = slen - unsigned(firstBytesPos);
80 len = maxLen < nrOfBytes
81 ? maxLen
82 : nrOfBytes;
83 sz = new char[len+1];
84 memcpy(sz,anybytes+firstBytesPos,len);
85 *(sz+len) = 0;
90 Simstr::Simstr(const Simstr & S)
92 len = S.len;
93 sz = new char[len+1];
94 memcpy(sz,S.sz,len+1);
97 Simstr & Simstr::operator=(const Simstr & S)
99 if (sz == S.sz)
100 return *this;
102 delete [] sz;
104 len = S.len;
105 sz = new char[len+1];
106 memcpy(sz,S.sz,len+1);
108 return *this;
111 Simstr::~Simstr()
113 delete [] sz;
116 Simstr
117 Simstr::operator+(const Simstr & S) const
119 Simstr ret = sz;
120 ret.push_back(S);
121 return ret;
124 Simstr &
125 Simstr::operator+=(const Simstr & S)
127 push_back(S);
128 return *this;
132 // REL
134 bool
135 Simstr::operator==(const Simstr & S) const
136 { return !strcmp(sz,S.sz) ? true : false; }
138 bool
139 Simstr::operator!=(const Simstr & S) const
140 { return strcmp(sz,S.sz) ? true : false; }
142 bool
143 Simstr::operator<(const Simstr & S) const
144 { return (strcmp(sz,S.sz) < 0) ? true : false; }
146 bool
147 Simstr::operator>(const Simstr & S) const
148 { return (strcmp(sz,S.sz) > 0) ? true : false; }
150 bool
151 Simstr::operator<=(const Simstr & S) const
152 { return (strcmp(sz,S.sz) <= 0) ? true : false; }
154 bool
155 Simstr::operator>=(const Simstr & S) const
156 { return (strcmp(sz,S.sz) >= 0) ? true : false; }
161 // ************** LIST - Funktionen *****************
163 // Insert
165 void
166 Simstr::push_front(char c)
168 char * result = new char[len+2];
170 result[0] = c;
171 memcpy(result+1,sz,len+1);
173 delete [] sz;
174 sz = result;
175 len++;
178 void
179 Simstr::push_back(char c)
181 char * result = new char[len+2];
183 memcpy(result,sz,len);
184 result[len] = c;
185 result[len+1] = 0;
187 delete [] sz;
188 sz = result;
189 len++;
192 void
193 Simstr::push_front(const Simstr & S)
195 char * result = new char[len+1+S.len];
197 memcpy(result,S.sz,S.len);
198 memcpy(result+S.len,sz,len+1);
200 delete [] sz;
201 sz = result;
202 len += S.len;
205 void
206 Simstr::push_back(const Simstr & S)
208 char * result = new char[len+1+S.len];
210 memcpy(result,sz,len);
211 memcpy(result+len,S.sz,S.len+1);
213 delete [] sz;
214 sz = result;
215 len += S.len;
219 // Remove
221 void
222 Simstr::remove(int pos, int anzahl)
224 if (pos >= len || pos < 0 || anzahl < 1)
225 return;
227 int anz = len - pos < anzahl ? len - pos : anzahl;
229 char * result = new char[len-anz+1];
231 memcpy(result,sz,pos);
232 memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
234 delete [] sz;
235 sz = result;
236 len -= anz;
239 void
240 Simstr::remove_trailing_blanks()
242 int newlen = len-1;
243 for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
245 if (newlen < len-1)
246 remove ( newlen+1, len-newlen);
249 // Find
252 Simstr::pos_first(char c) const
254 int i = 0;
255 for (i = 0; i < len ? sz[i] != c : false; i++) ;
256 if (i >= len)
257 return NO_POS;
258 else
259 return i;
263 Simstr::pos_last(char c) const
265 int i = 0;
266 for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
267 if (i < 0)
268 return NO_POS;
269 else
270 return i;
273 bool
274 Simstr::is_no_text() const
276 if (!len)
277 return true;
279 int i;
280 for (i = 0; sz[i] <= 32 && i < len; i++) ;
281 if (i < len)
282 return false;
283 return true;
286 // Change
288 void
289 Simstr::replace_all(char oldCh, char newCh)
291 for (int i=0; i < len; i++)
292 if (sz[i] == oldCh)
293 sz[i] = newCh;
296 // Simstr addition
297 Simstr
298 operator+(const char * str, const Simstr & S)
300 Simstr ret = S;
301 ret.push_front(str);
302 return ret;
305 Simstr
306 operator+(const Simstr & S, const char * str)
308 Simstr ret = S;
309 ret.push_back(str);
310 return ret;
313 Simstr
314 operator+(char c, const Simstr & S)
316 Simstr ret = S;
317 ret.push_front(c);
318 return ret;
321 Simstr
322 operator+(const Simstr & S, char c)
324 Simstr ret = S;
325 ret.push_back(c);
326 return ret;
330 // Simstr-Vergleiche mit char *
331 bool
332 operator==(const Simstr & S, const char * str)
334 return strcmp(S,str) == 0;
337 bool
338 operator!=(const Simstr & S, const char * str)
340 return strcmp(S,str) != 0;
343 bool
344 operator<(const Simstr & S, const char * str)
346 return strcmp(S,str) < 0;
349 bool
350 operator>(const Simstr & S, const char * str)
352 return strcmp(S,str) > 0;
355 bool
356 operator<=(const Simstr & S, const char * str)
358 return strcmp(S,str) <= 0;
361 bool
362 operator>=(const Simstr & S, const char * str)
364 return strcmp(S,str) >= 0;
367 bool
368 operator==(const char * str, const Simstr & S)
370 return strcmp(str,S) == 0;
373 bool
374 operator!=(const char * str, const Simstr & S)
376 return strcmp(str,S) != 0;
379 bool
380 operator<(const char * str, const Simstr & S)
382 return strcmp(str,S) < 0;
385 bool
386 operator>(const char * str, const Simstr & S)
388 return strcmp(str,S) > 0;
391 bool
392 operator<=(const char * str, const Simstr & S)
394 return strcmp(str,S) <= 0;
397 bool
398 operator>=(const char * str, const Simstr & S)
400 return strcmp(str,S) >= 0;