Bump for 3.6-28
[LibreOffice.git] / hwpfilter / source / mzstring.cxx
blob090265d37afd870f6c75b8f6ffeb2b54abbd5f3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 /* PURPOSE
30 * supposed to be used instead of std::string
33 #include "mzstring.h"
35 #ifndef WIN32
36 #else
38 #if defined _MSC_VER
39 #pragma warning(push, 1)
40 #endif
41 # include <windows.h>
42 #if defined _MSC_VER
43 #pragma warning(pop)
44 #endif
45 #endif /* WIN32 */
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #ifndef WIN32
51 # define wsprintf sprintf
52 #endif
54 const int AllocSize = 8;
56 inline int get_alloc_size(int len)
58 return (len + AllocSize - 1) / AllocSize * AllocSize;
62 MzString::MzString()
64 Length = 0;
65 Allocated = 0;
66 Data = 0;
70 MzString::~MzString()
72 if (Data)
73 free(Data);
77 MzString &MzString::operator = (MzString &s)
79 int n = s.length();
80 if (allocate(n))
82 if (n > 0) memcpy(Data, s.Data, n);
83 Length = n;
85 return *this;
89 MzString &MzString::operator = (const char *s)
91 if (s == NULL)
92 s = "";
93 int n = strlen(s);
94 if (allocate(n))
96 if (n > 0) memcpy(Data, s, n);
97 Length = n;
99 return *this;
103 void MzString::append(const char *s, int slen)
105 if(!s || slen <= 0)
106 return;
108 int new_len = Length + slen;
109 if (resize(new_len))
111 memcpy(Data + Length, s, slen);
112 Length = new_len;
117 void MzString::append(MzString const &s)
119 if (s.Data)
120 append(s.Data, s.length());
124 void MzString::append(const char *s)
126 if (!s) return;
127 append(s, strlen(s));
131 int MzString::compare(const char *s)
133 if (!Data) return -1;
134 if (s==NULL) return 1;
136 Data[Length] = 0;
137 return strcmp(Data, s);
141 int MzString::find(char ch)
143 return find(ch,0);
147 int MzString::find(char ch, int pos)
149 for (int i = pos; i < Length; i++)
151 if (Data[i] == ch)
152 return i;
154 return -1;
158 int MzString::rfind(char ch)
160 return rfind(ch, Length - 1);
164 int MzString::rfind(char ch, int pos)
166 if (pos >= Length)
167 return -1;
169 while (pos >= 0)
171 if (Data[pos] == ch)
172 return pos;
173 pos--;
175 return -1;
179 // += operator
181 MzString &MzString::operator += (char ch)
183 append(&ch, 1);
184 return *this;
188 MzString &MzString::operator += (const char *str)
190 append(str);
191 return *this;
195 MzString &MzString::operator += (MzString const &s)
197 append(s);
198 return *this;
202 // << operator
203 MzString &MzString::operator << (const char *str)
205 append(str);
206 return *this;
210 MzString &MzString::operator << (char ch)
212 append(&ch, 1);
213 return *this;
217 MzString &MzString::operator << (int i)
219 char str[80];
221 wsprintf(str, "%d", i);
222 append(str);
223 return *this;
227 MzString &MzString::operator << (long l)
229 char str[80];
231 wsprintf(str, "%ld", l);
232 append(str);
233 return *this;
237 MzString &MzString::operator << (MzString const &s)
239 append(s);
240 return *this;
244 char MzString::operator [] (int n)
246 if (Data && 0 <= n && n < Length)
247 return Data[n];
249 return 0;
253 void MzString::replace(int pos, char ch)
255 if (Data && 0 <= pos && pos < Length)
256 Data[pos] = ch;
260 //------------------------------------------------------------------------
261 // Private Methods.
264 bool MzString::allocate(int len)
266 len++; // In case we want to add a null.
268 if (len < 0)
269 return false;
271 if (Data)
273 if (len < Allocated)
274 return true;
275 else
277 int n = get_alloc_size(len);
278 char *p = (char *)realloc(Data, n);
279 if (p)
281 Data = p;
282 Allocated = n;
283 return true;
287 else
289 // In case we want to add a null.
290 int n = get_alloc_size(len);
291 Data = (char *)malloc(n);
292 if (Data)
294 Allocated = n;
295 return true;
299 return false;
303 bool MzString::resize(int len)
305 return allocate(len);
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */