Bump for 3.6-28
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blob20b368a770cb0b12d74f624e5cd3c5054b3bd90a
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 #include <stdio.h>
30 #include <errno.h>
31 // DVO: add zlib/ prefix
32 #ifdef SYSTEM_ZLIB
33 #include <zlib.h>
34 #else
35 #include <zlib/zlib.h>
36 #endif
37 #ifdef WIN32
38 # include <io.h>
39 #else
40 # include <unistd.h>
41 #endif
43 #include <osl/diagnose.h>
45 #include "hwplib.h"
46 #include "hgzip.h"
47 #include "hiodev.h"
48 #include "hwpfile.h"
49 #include "hstream.h"
51 const int BUFSIZE = 1024;
52 static uchar rBuf[BUFSIZE];
54 // HIODev abstract class
55 HIODev::HIODev()
57 init();
61 HIODev::~HIODev()
66 void HIODev::init()
68 compressed = false;
72 int HIODev::read1b(void *ptr, int nmemb)
74 uchar *p = (uchar *) ptr;
75 int ii;
77 if (state())
78 return -1;
79 for (ii = 0; ii < nmemb; ii++)
81 p[ii] = sal::static_int_cast<uchar>(read1b());
82 if (state())
83 break;
85 return ii;
89 int HIODev::read2b(void *ptr, int nmemb)
91 ushort *p = (ushort *) ptr;
92 int ii;
94 if (state())
95 return -1;
96 for (ii = 0; ii < nmemb; ii++)
98 p[ii] = sal::static_int_cast<uchar>(read2b());
99 if (state())
100 break;
102 return ii;
106 int HIODev::read4b(void *ptr, int nmemb)
108 ulong *p = (ulong *) ptr;
109 int ii;
111 if (state())
112 return -1;
113 for (ii = 0; ii < nmemb; ii++)
115 p[ii] = read4b();
116 if (state())
117 break;
119 return ii;
123 // hfileiodev class
124 HStreamIODev::HStreamIODev(HStream & stream):_stream(stream)
126 init();
130 HStreamIODev::~HStreamIODev()
132 close();
136 void HStreamIODev::init()
138 _gzfp = NULL;
139 compressed = false;
143 bool HStreamIODev::open()
145 if (!(_stream.available()))
146 return false;
147 return true;
151 void HStreamIODev::flush(void)
153 if (_gzfp)
154 gz_flush(_gzfp, Z_FINISH);
158 void HStreamIODev::close(void)
160 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
161 this->flush();
162 if (_gzfp)
163 gz_close(_gzfp); /* gz_close() calls stream_closeInput() */
164 else
165 _stream.closeInput();
166 _gzfp = NULL;
170 int HStreamIODev::state(void) const
172 return 0;
176 /* zlib °ü·Ã ºÎºÐ */
177 bool HStreamIODev::setCompressed(bool flag)
179 compressed = flag;
180 if (flag == true)
181 return 0 != (_gzfp = gz_open(_stream));
182 else if (_gzfp)
184 gz_flush(_gzfp, Z_FINISH);
185 gz_close(_gzfp);
186 _gzfp = 0;
188 return true;
192 // IO routines
194 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
196 int HStreamIODev::read1b()
198 int res = (compressed) ? GZREAD(rBuf, 1) : _stream.readBytes(rBuf, 1);
200 if (res <= 0)
201 return -1;
202 else
203 return (unsigned char) rBuf[0];
207 int HStreamIODev::read2b()
209 int res = (compressed) ? GZREAD(rBuf, 2) : _stream.readBytes(rBuf, 2);
211 if (res <= 0)
212 return -1;
213 else
214 return ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
218 long HStreamIODev::read4b()
220 int res = (compressed) ? GZREAD(rBuf, 4) : _stream.readBytes(rBuf, 4);
222 if (res <= 0)
223 return -1;
224 else
225 return ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
226 (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
230 int HStreamIODev::readBlock(void *ptr, int size)
232 int count =
233 (compressed) ? GZREAD(ptr, size) : _stream.readBytes((byte *) ptr,
235 size);
237 return count;
241 int HStreamIODev::skipBlock(int size)
243 if (compressed){
244 if( size <= BUFSIZE )
245 return GZREAD(rBuf, size);
246 else{
247 int remain = size;
248 while(remain){
249 if( remain > BUFSIZE )
250 remain -= GZREAD(rBuf, BUFSIZE);
251 else{
252 remain -= GZREAD(rBuf, remain);
253 break;
256 return size - remain;
259 return _stream.skipBytes(size);
263 HMemIODev::HMemIODev(char *s, int len)
265 init();
266 ptr = (uchar *) s;
267 length = len;
271 HMemIODev::~HMemIODev()
273 close();
277 void HMemIODev::init()
279 ptr = 0;
280 length = 0;
281 pos = 0;
285 bool HMemIODev::open()
287 return true;
291 void HMemIODev::flush(void)
296 void HMemIODev::close(void)
301 int HMemIODev::state(void) const
303 if (pos <= length)
304 return 0;
305 else
306 return -1;
310 bool HMemIODev::setCompressed(bool )
312 return false;
316 int HMemIODev::read1b()
318 if (pos <= length)
319 return ptr[pos++];
320 else
321 return 0;
325 int HMemIODev::read2b()
327 pos += 2;
328 if (pos <= length)
329 return ptr[pos - 1] << 8 | ptr[pos - 2];
330 else
331 return 0;
335 long HMemIODev::read4b()
337 pos += 4;
338 if (pos <= length)
339 return DWORD(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
340 ptr[pos - 3] << 8 | ptr[pos - 4]);
341 else
342 return 0;
346 int HMemIODev::readBlock(void *p, int size)
348 if (length < pos + size)
349 size = length - pos;
350 memcpy(p, ptr + pos, size);
351 pos += size;
352 return size;
356 int HMemIODev::skipBlock(int size)
358 if (length < pos + size)
359 return 0;
360 pos += size;
361 return size;
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */