fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blob604c45cc97b5810fd6f6155c73d417b4fdaf71c6
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 #include <stdio.h>
21 #include <errno.h>
23 #ifdef WIN32
24 # include <io.h>
25 #else
26 # include <unistd.h>
27 #endif
29 #include <osl/diagnose.h>
31 #include "hwplib.h"
32 #include "hgzip.h"
33 #include "hiodev.h"
34 #include "hwpfile.h"
35 #include "hstream.hxx"
37 const int BUFSIZE = 1024;
38 static uchar rBuf[BUFSIZE];
40 // HIODev abstract class
41 HIODev::HIODev()
43 init();
47 HIODev::~HIODev()
52 void HIODev::init()
54 compressed = false;
58 int HIODev::read1b(void *ptr, int nmemb)
60 uchar *p = static_cast<uchar *>(ptr);
61 int ii;
63 if (state())
64 return -1;
65 for (ii = 0; ii < nmemb; ii++)
67 if (!read1b(p[ii]))
68 break;
69 if (state())
70 break;
72 return ii;
75 int HIODev::read2b(void *ptr, int nmemb)
77 ushort *p = static_cast<ushort *>(ptr);
78 int ii;
80 if (state())
81 return -1;
82 for (ii = 0; ii < nmemb; ii++)
84 if (!read2b(p[ii]))
85 break;
86 if (state())
87 break;
89 return ii;
92 int HIODev::read4b(void *ptr, int nmemb)
94 uint *p = static_cast<uint *>(ptr);
95 int ii;
97 if (state())
98 return -1;
99 for (ii = 0; ii < nmemb; ii++)
101 if (!read4b(p[ii]))
102 break;
103 if (state())
104 break;
106 return ii;
110 // hfileiodev class
111 HStreamIODev::HStreamIODev(HStream * stream):_stream(stream)
113 init();
117 HStreamIODev::~HStreamIODev()
119 close();
123 void HStreamIODev::init()
125 _gzfp = NULL;
126 compressed = false;
130 bool HStreamIODev::open()
132 if (!(_stream->available()))
133 return false;
134 return true;
138 void HStreamIODev::flush()
140 if (_gzfp)
141 gz_flush(_gzfp, Z_FINISH);
145 void HStreamIODev::close()
147 /* 플러시한 후 닫는다. */
148 this->flush();
149 if (_gzfp)
150 gz_close(_gzfp);
151 _gzfp = NULL;
155 int HStreamIODev::state() const
157 return 0;
161 /* zlib 관련 부분 */
162 bool HStreamIODev::setCompressed(bool flag)
164 compressed = flag;
165 if (flag)
166 return 0 != (_gzfp = gz_open(*_stream));
167 else if (_gzfp)
169 gz_flush(_gzfp, Z_FINISH);
170 gz_close(_gzfp);
171 _gzfp = 0;
173 return true;
177 // IO routines
179 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
181 bool HStreamIODev::read1b(unsigned char &out)
183 int res = (compressed) ? GZREAD(rBuf, 1) : _stream->readBytes(rBuf, 1);
185 if (res < 1)
186 return false;
188 out = (unsigned char)rBuf[0];
189 return true;
192 bool HStreamIODev::read1b(char &out)
194 unsigned char tmp8;
195 if (!read1b(tmp8))
196 return false;
197 out = tmp8;
198 return true;
201 bool HStreamIODev::read2b(unsigned short &out)
203 int res = (compressed) ? GZREAD(rBuf, 2) : _stream->readBytes(rBuf, 2);
205 if (res < 2)
206 return false;
208 out = ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
209 return true;
212 bool HStreamIODev::read4b(unsigned int &out)
214 int res = (compressed) ? GZREAD(rBuf, 4) : _stream->readBytes(rBuf, 4);
216 if (res < 4)
217 return false;
219 out = ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
220 (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
221 return true;
224 bool HStreamIODev::read4b(int &out)
226 unsigned int tmp32;
227 if (!read4b(tmp32))
228 return false;
229 out = tmp32;
230 return true;
233 int HStreamIODev::readBlock(void *ptr, int size)
235 int count =
236 (compressed) ? GZREAD(ptr, size) : _stream->readBytes(static_cast<byte *>(ptr),
238 size);
240 return count;
243 int HStreamIODev::skipBlock(int size)
245 if (compressed){
246 if( size <= BUFSIZE )
247 return GZREAD(rBuf, size);
248 else{
249 int remain = size;
250 while(remain){
251 if( remain > BUFSIZE )
252 remain -= GZREAD(rBuf, BUFSIZE);
253 else{
254 remain -= GZREAD(rBuf, remain);
255 break;
258 return size - remain;
261 return _stream->skipBytes(size);
265 HMemIODev::HMemIODev(char *s, int len)
267 init();
268 ptr = reinterpret_cast<uchar *>(s);
269 length = len;
273 HMemIODev::~HMemIODev()
275 close();
279 void HMemIODev::init()
281 ptr = 0;
282 length = 0;
283 pos = 0;
287 bool HMemIODev::open()
289 return true;
293 void HMemIODev::flush()
298 void HMemIODev::close()
303 int HMemIODev::state() const
305 if (pos <= length)
306 return 0;
307 else
308 return -1;
312 bool HMemIODev::setCompressed(bool )
314 return false;
317 bool HMemIODev::read1b(unsigned char &out)
319 if (pos <= length)
321 out = ptr[pos++];
322 return true;
324 return false;
327 bool HMemIODev::read1b(char &out)
329 unsigned char tmp8;
330 if (!read1b(tmp8))
331 return false;
332 out = tmp8;
333 return true;
336 bool HMemIODev::read2b(unsigned short &out)
338 pos += 2;
339 if (pos <= length)
341 out = ptr[pos - 1] << 8 | ptr[pos - 2];
342 return true;
344 return false;
347 bool HMemIODev::read4b(unsigned int &out)
349 pos += 4;
350 if (pos <= length)
352 out = static_cast<unsigned int>(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
353 ptr[pos - 3] << 8 | ptr[pos - 4]);
354 return true;
356 return false;
359 bool HMemIODev::read4b(int &out)
361 unsigned int tmp32;
362 if (!read4b(tmp32))
363 return false;
364 out = tmp32;
365 return true;
368 int HMemIODev::readBlock(void *p, int size)
370 if (length < pos + size)
371 size = length - pos;
372 memcpy(p, ptr + pos, size);
373 pos += size;
374 return size;
377 int HMemIODev::skipBlock(int size)
379 if (length < pos + size)
380 return 0;
381 pos += size;
382 return size;
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */