fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / mzstring.cxx
blob5fdb802824f9492bd433edc631b1c398d23a5c84
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /* PURPOSE
21 * supposed to be used instead of std::string
24 #include "mzstring.h"
26 #ifndef WIN32
27 #else
29 #if defined _MSC_VER
30 #pragma warning(push, 1)
31 #endif
32 # include <windows.h>
33 #if defined _MSC_VER
34 #pragma warning(pop)
35 #endif
36 #endif /* WIN32 */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #ifndef WIN32
42 # define wsprintf sprintf
43 #endif
45 const int AllocSize = 8;
47 inline int get_alloc_size(int len)
49 return (len + AllocSize - 1) / AllocSize * AllocSize;
53 MzString::MzString()
55 Length = 0;
56 Allocated = 0;
57 Data = 0;
61 MzString::~MzString()
63 if (Data)
64 free(Data);
68 MzString &MzString::operator = (MzString &s)
70 int n = s.length();
71 if (allocate(n))
73 if (n > 0) memcpy(Data, s.Data, n);
74 Length = n;
76 return *this;
80 MzString &MzString::operator = (const char *s)
82 if (s == NULL)
83 s = "";
84 int n = strlen(s);
85 if (allocate(n))
87 if (n > 0) memcpy(Data, s, n);
88 Length = n;
90 return *this;
94 void MzString::append(const char *s, int slen)
96 if(!s || slen <= 0)
97 return;
99 int new_len = Length + slen;
100 if (resize(new_len))
102 memcpy(Data + Length, s, slen);
103 Length = new_len;
108 void MzString::append(MzString const &s)
110 if (s.Data)
111 append(s.Data, s.length());
115 void MzString::append(const char *s)
117 if (!s) return;
118 append(s, strlen(s));
122 int MzString::compare(const char *s)
124 if (!Data) return -1;
125 if (s==NULL) return 1;
127 Data[Length] = 0;
128 return strcmp(Data, s);
132 int MzString::find(char ch)
134 return find(ch,0);
138 int MzString::find(char ch, int pos)
140 for (int i = pos; i < Length; i++)
142 if (Data[i] == ch)
143 return i;
145 return -1;
149 int MzString::rfind(char ch)
151 return rfind(ch, Length - 1);
155 int MzString::rfind(char ch, int pos)
157 if (pos >= Length)
158 return -1;
160 while (pos >= 0)
162 if (Data[pos] == ch)
163 return pos;
164 pos--;
166 return -1;
170 // += operator
172 MzString &MzString::operator += (char ch)
174 append(&ch, 1);
175 return *this;
179 MzString &MzString::operator += (const char *str)
181 append(str);
182 return *this;
186 MzString &MzString::operator += (MzString const &s)
188 append(s);
189 return *this;
193 // << operator
194 MzString &MzString::operator << (const char *str)
196 append(str);
197 return *this;
201 MzString &MzString::operator << (char ch)
203 append(&ch, 1);
204 return *this;
208 MzString &MzString::operator << (int i)
210 char str[80];
212 wsprintf(str, "%d", i);
213 append(str);
214 return *this;
218 MzString &MzString::operator << (long l)
220 char str[80];
222 wsprintf(str, "%ld", l);
223 append(str);
224 return *this;
228 MzString &MzString::operator << (MzString const &s)
230 append(s);
231 return *this;
235 char MzString::operator [] (int n)
237 if (Data && 0 <= n && n < Length)
238 return Data[n];
240 return 0;
244 void MzString::replace(int pos, char ch)
246 if (Data && 0 <= pos && pos < Length)
247 Data[pos] = ch;
252 // Private Methods.
255 bool MzString::allocate(int len)
257 len++; // In case we want to add a null.
259 if (len < 0)
260 return false;
262 if (Data)
264 if (len < Allocated)
265 return true;
266 else
268 int n = get_alloc_size(len);
269 char *p = static_cast<char *>(realloc(Data, n));
270 if (p)
272 Data = p;
273 Allocated = n;
274 return true;
278 else
280 // In case we want to add a null.
281 int n = get_alloc_size(len);
282 Data = static_cast<char *>(malloc(n));
283 if (Data)
285 Allocated = n;
286 return true;
290 return false;
294 bool MzString::resize(int len)
296 return allocate(len);
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */