update dev300-m58
[ooovba.git] / hwpfilter / source / mzstring.cpp
blob0cddc060a8e387ef776dd133c466b4b7d45cc7d1
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: mzstring.cpp,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
31 /* NAME $Id: mzstring.cpp,v 1.5 2008-06-04 10:02:20 vg Exp $
32 * PURPOSE
33 * supposed to be used instead of std::string
34 * NOTES
36 * HISTORY
37 * frog - Oct 8, 1998: Created.
40 #ifdef __GNUG__
41 #pragma implementation "mzstring.h"
42 #endif
44 //#include "stdafx.h" //
45 #include "mzstring.h"
47 #ifndef WIN32
48 #else
50 #if defined _MSC_VER
51 #pragma warning(push, 1)
52 #endif
53 # include <windows.h>
54 #if defined _MSC_VER
55 #pragma warning(pop)
56 #endif
57 //# include <crtdbg.h>
58 #endif /* WIN32 */
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
63 #ifndef WIN32
64 # define wsprintf sprintf
65 #endif
67 const int AllocSize = 8;
69 inline int get_alloc_size(int len)
71 return (len + AllocSize - 1) / AllocSize * AllocSize;
75 MzString::MzString()
77 Length = 0;
78 Allocated = 0;
79 Data = 0;
83 MzString::~MzString()
85 if (Data)
86 free(Data);
90 void MzString::operator = (MzString &s)
92 int n = s.length();
93 if (allocate(n))
95 if (n > 0) memcpy(Data, s.Data, n);
96 Length = n;
101 void MzString::operator = (const char *s)
103 if (s == NULL)
104 s = "";
105 int n = strlen(s);
106 if (allocate(n))
108 if (n > 0) memcpy(Data, s, n);
109 Length = n;
114 void MzString::append(const char *s, int slen)
116 if(!s || slen <= 0)
117 return;
119 int new_len = Length + slen;
120 if (resize(new_len))
122 memcpy(Data + Length, s, slen);
123 Length = new_len;
128 void MzString::append(MzString const &s)
130 if (s.Data)
131 append(s.Data, s.length());
135 void MzString::append(const char *s)
137 if (!s) return;
138 append(s, strlen(s));
142 int MzString::compare(const char *s)
144 if (!Data) return -1;
145 if (s==NULL) return 1;
147 Data[Length] = 0;
148 return strcmp(Data, s);
152 int MzString::find(char ch)
154 return find(ch,0);
158 int MzString::find(char ch, int pos)
160 for (int i = pos; i < Length; i++)
162 if (Data[i] == ch)
163 return i;
165 return -1;
169 int MzString::rfind(char ch)
171 return rfind(ch, Length - 1);
175 int MzString::rfind(char ch, int pos)
177 if (pos >= Length)
178 return -1;
180 while (pos >= 0)
182 if (Data[pos] == ch)
183 return pos;
184 pos--;
186 return -1;
190 // += operator
192 MzString &MzString::operator += (char ch)
194 append(&ch, 1);
195 return *this;
199 MzString &MzString::operator += (const char *str)
201 append(str);
202 return *this;
206 MzString &MzString::operator += (MzString const &s)
208 append(s);
209 return *this;
213 // << operator
214 MzString &MzString::operator << (const char *str)
216 append(str);
217 return *this;
221 MzString &MzString::operator << (char ch)
223 append(&ch, 1);
224 return *this;
228 MzString &MzString::operator << (int i)
230 char str[80];
232 wsprintf(str, "%d", i);
233 append(str);
234 return *this;
238 MzString &MzString::operator << (long l)
240 char str[80];
242 wsprintf(str, "%ld", l);
243 append(str);
244 return *this;
248 MzString &MzString::operator << (MzString const &s)
250 append(s);
251 return *this;
255 char MzString::operator [] (int n)
257 if (Data && 0 <= n && n < Length)
258 return Data[n];
260 return 0;
264 void MzString::replace(int pos, char ch)
266 if (Data && 0 <= pos && pos < Length)
267 Data[pos] = ch;
271 //------------------------------------------------------------------------
272 // Private Methods.
275 bool MzString::allocate(int len)
277 len++; // In case we want to add a null.
279 if (len < 0)
280 return false;
282 if (Data)
284 if (len < Allocated)
285 return true;
286 else
288 int n = get_alloc_size(len);
289 char *p = (char *)realloc(Data, n);
290 if (p)
292 Data = p;
293 Allocated = n;
294 return true;
298 else
300 // In case we want to add a null.
301 int n = get_alloc_size(len);
302 Data = (char *)malloc(n);
303 if (Data)
305 Allocated = n;
306 return true;
310 return false;
314 bool MzString::resize(int len)
316 return allocate(len);