update dev300-m57
[ooovba.git] / hwpfilter / source / hiodev.cpp
blob312bf5cfc04d5861a5e7b4c53e7fa10c3be573b8
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: hiodev.cpp,v $
10 * $Revision: 1.6 $
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 /* $Id: hiodev.cpp,v 1.6 2008-04-10 12:05:38 rt Exp $ */
33 #ifdef __GNUG__
34 #pragma implementation "hiodev.h"
35 #endif
37 #include <stdio.h>
38 #include <errno.h>
39 // DVO: add zlib/ prefix
40 #ifdef SYSTEM_ZLIB
41 #include <zlib.h>
42 #else
43 #include <zlib/zlib.h>
44 #endif
45 #ifdef WIN32
46 # include <io.h>
47 #else
48 # include <unistd.h>
49 #endif
51 #include "hwplib.h"
52 #include "hgzip.h"
53 #include "hiodev.h"
54 #include "hwpfile.h"
55 #include "hstream.h"
57 const int BUFSIZE = 1024;
58 static uchar rBuf[BUFSIZE];
60 // HIODev abstract class
61 HIODev::HIODev()
63 init();
67 HIODev::~HIODev()
72 void HIODev::init()
74 compressed = false;
78 int HIODev::read1b(void *ptr, int nmemb)
80 uchar *p = (uchar *) ptr;
81 int ii;
83 if (state())
84 return -1;
85 for (ii = 0; ii < nmemb; ii++)
87 p[ii] = sal::static_int_cast<uchar>(read1b());
88 if (state())
89 break;
91 return ii;
95 int HIODev::read2b(void *ptr, int nmemb)
97 ushort *p = (ushort *) ptr;
98 int ii;
100 if (state())
101 return -1;
102 for (ii = 0; ii < nmemb; ii++)
104 p[ii] = sal::static_int_cast<uchar>(read2b());
105 if (state())
106 break;
108 return ii;
112 int HIODev::read4b(void *ptr, int nmemb)
114 ulong *p = (ulong *) ptr;
115 int ii;
117 if (state())
118 return -1;
119 for (ii = 0; ii < nmemb; ii++)
121 p[ii] = read4b();
122 if (state())
123 break;
125 return ii;
129 // hfileiodev class
130 HStreamIODev::HStreamIODev(HStream & stream):_stream(stream)
132 init();
136 HStreamIODev::~HStreamIODev()
138 close();
142 void HStreamIODev::init()
144 _gzfp = NULL;
145 compressed = false;
149 bool HStreamIODev::open()
151 if (!(_stream.available()))
152 return false;
153 return true;
157 void HStreamIODev::flush(void)
159 if (_gzfp)
160 gz_flush(_gzfp, Z_FINISH);
164 void HStreamIODev::close(void)
166 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
167 this->flush();
168 if (_gzfp)
169 gz_close(_gzfp); /* gz_close() calls stream_closeInput() */
170 else
171 _stream.closeInput();
172 _gzfp = NULL;
176 int HStreamIODev::state(void) const
178 return 0;
182 /* zlib °ü·Ã ºÎºÐ */
183 bool HStreamIODev::setCompressed(bool flag)
185 compressed = flag;
186 if (flag == true)
187 return 0 != (_gzfp = gz_open(_stream));
188 else if (_gzfp)
190 gz_flush(_gzfp, Z_FINISH);
191 gz_close(_gzfp);
192 _gzfp = 0;
194 return true;
198 // IO routines
200 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
202 int HStreamIODev::read1b()
204 int res = (compressed) ? GZREAD(rBuf, 1) : _stream.readBytes(rBuf, 1);
206 if (res <= 0)
207 return -1;
208 else
209 return (unsigned char) rBuf[0];
213 int HStreamIODev::read2b()
215 int res = (compressed) ? GZREAD(rBuf, 2) : _stream.readBytes(rBuf, 2);
217 if (res <= 0)
218 return -1;
219 else
220 return ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
224 long HStreamIODev::read4b()
226 int res = (compressed) ? GZREAD(rBuf, 4) : _stream.readBytes(rBuf, 4);
228 if (res <= 0)
229 return -1;
230 else
231 return ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
232 (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
236 int HStreamIODev::readBlock(void *ptr, int size)
238 int count =
239 (compressed) ? GZREAD(ptr, size) : _stream.readBytes((byte *) ptr,
241 size);
243 return count;
247 int HStreamIODev::skipBlock(int size)
249 if (compressed){
250 if( size <= BUFSIZE )
251 return GZREAD(rBuf, size);
252 else{
253 int remain = size;
254 while(remain){
255 if( remain > BUFSIZE )
256 remain -= GZREAD(rBuf, BUFSIZE);
257 else{
258 remain -= GZREAD(rBuf, remain);
259 break;
262 return size - remain;
265 return _stream.skipBytes(size);
269 HMemIODev::HMemIODev(char *s, int len)
271 init();
272 ptr = (uchar *) s;
273 length = len;
277 HMemIODev::~HMemIODev()
279 close();
283 void HMemIODev::init()
285 ptr = 0;
286 length = 0;
287 pos = 0;
291 bool HMemIODev::open()
293 return true;
297 void HMemIODev::flush(void)
302 void HMemIODev::close(void)
307 int HMemIODev::state(void) const
309 if (pos <= length)
310 return 0;
311 else
312 return -1;
316 bool HMemIODev::setCompressed(bool )
318 return false;
322 int HMemIODev::read1b()
324 if (pos <= length)
325 return ptr[pos++];
326 else
327 return 0;
331 int HMemIODev::read2b()
333 pos += 2;
334 if (pos <= length)
335 return ptr[pos - 1] << 8 | ptr[pos - 2];
336 else
337 return 0;
341 long HMemIODev::read4b()
343 pos += 4;
344 if (pos <= length)
345 return DWORD(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
346 ptr[pos - 3] << 8 | ptr[pos - 4]);
347 else
348 return 0;
352 int HMemIODev::readBlock(void *p, int size)
354 if (length < pos + size)
355 size = length - pos;
356 memcpy(p, ptr + pos, size);
357 pos += size;
358 return size;
362 int HMemIODev::skipBlock(int size)
364 if (length < pos + size)
365 return 0;
366 pos += size;
367 return size;