Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / hwpfilter / source / hiodev.cxx
blobbb367b8cdfcfc848150715bdaf56945bc0b09eaf
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>
22 // DVO: add zlib/ prefix
23 #ifdef SYSTEM_ZLIB
24 #include <zlib.h>
25 #else
26 #include <zlib/zlib.h>
27 #endif
28 #ifdef WIN32
29 # include <io.h>
30 #else
31 # include <unistd.h>
32 #endif
34 #include <osl/diagnose.h>
36 #include "hwplib.h"
37 #include "hgzip.h"
38 #include "hiodev.h"
39 #include "hwpfile.h"
40 #include "hstream.h"
42 const int BUFSIZE = 1024;
43 static uchar rBuf[BUFSIZE];
45 // HIODev abstract class
46 HIODev::HIODev()
48 init();
52 HIODev::~HIODev()
57 void HIODev::init()
59 compressed = false;
63 int HIODev::read1b(void *ptr, int nmemb)
65 uchar *p = (uchar *) ptr;
66 int ii;
68 if (state())
69 return -1;
70 for (ii = 0; ii < nmemb; ii++)
72 p[ii] = sal::static_int_cast<uchar>(read1b());
73 if (state())
74 break;
76 return ii;
80 int HIODev::read2b(void *ptr, int nmemb)
82 ushort *p = (ushort *) ptr;
83 int ii;
85 if (state())
86 return -1;
87 for (ii = 0; ii < nmemb; ii++)
89 p[ii] = sal::static_int_cast<uchar>(read2b());
90 if (state())
91 break;
93 return ii;
97 int HIODev::read4b(void *ptr, int nmemb)
99 ulong *p = (ulong *) ptr;
100 int ii;
102 if (state())
103 return -1;
104 for (ii = 0; ii < nmemb; ii++)
106 p[ii] = read4b();
107 if (state())
108 break;
110 return ii;
114 // hfileiodev class
115 HStreamIODev::HStreamIODev(HStream & stream):_stream(stream)
117 init();
121 HStreamIODev::~HStreamIODev()
123 close();
127 void HStreamIODev::init()
129 _gzfp = NULL;
130 compressed = false;
134 bool HStreamIODev::open()
136 if (!(_stream.available()))
137 return false;
138 return true;
142 void HStreamIODev::flush(void)
144 if (_gzfp)
145 gz_flush(_gzfp, Z_FINISH);
149 void HStreamIODev::close(void)
151 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
152 this->flush();
153 if (_gzfp)
154 gz_close(_gzfp); /* gz_close() calls stream_closeInput() */
155 else
156 _stream.closeInput();
157 _gzfp = NULL;
161 int HStreamIODev::state(void) const
163 return 0;
167 /* zlib °ü·Ã ºÎºÐ */
168 bool HStreamIODev::setCompressed(bool flag)
170 compressed = flag;
171 if (flag == true)
172 return 0 != (_gzfp = gz_open(_stream));
173 else if (_gzfp)
175 gz_flush(_gzfp, Z_FINISH);
176 gz_close(_gzfp);
177 _gzfp = 0;
179 return true;
183 // IO routines
185 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
187 int HStreamIODev::read1b()
189 int res = (compressed) ? GZREAD(rBuf, 1) : _stream.readBytes(rBuf, 1);
191 if (res <= 0)
192 return -1;
193 else
194 return (unsigned char) rBuf[0];
198 int HStreamIODev::read2b()
200 int res = (compressed) ? GZREAD(rBuf, 2) : _stream.readBytes(rBuf, 2);
202 if (res <= 0)
203 return -1;
204 else
205 return ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
209 long HStreamIODev::read4b()
211 int res = (compressed) ? GZREAD(rBuf, 4) : _stream.readBytes(rBuf, 4);
213 if (res <= 0)
214 return -1;
215 else
216 return ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
217 (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
221 int HStreamIODev::readBlock(void *ptr, int size)
223 int count =
224 (compressed) ? GZREAD(ptr, size) : _stream.readBytes((byte *) ptr,
226 size);
228 return count;
232 int HStreamIODev::skipBlock(int size)
234 if (compressed){
235 if( size <= BUFSIZE )
236 return GZREAD(rBuf, size);
237 else{
238 int remain = size;
239 while(remain){
240 if( remain > BUFSIZE )
241 remain -= GZREAD(rBuf, BUFSIZE);
242 else{
243 remain -= GZREAD(rBuf, remain);
244 break;
247 return size - remain;
250 return _stream.skipBytes(size);
254 HMemIODev::HMemIODev(char *s, int len)
256 init();
257 ptr = (uchar *) s;
258 length = len;
262 HMemIODev::~HMemIODev()
264 close();
268 void HMemIODev::init()
270 ptr = 0;
271 length = 0;
272 pos = 0;
276 bool HMemIODev::open()
278 return true;
282 void HMemIODev::flush(void)
287 void HMemIODev::close(void)
292 int HMemIODev::state(void) const
294 if (pos <= length)
295 return 0;
296 else
297 return -1;
301 bool HMemIODev::setCompressed(bool )
303 return false;
307 int HMemIODev::read1b()
309 if (pos <= length)
310 return ptr[pos++];
311 else
312 return 0;
316 int HMemIODev::read2b()
318 pos += 2;
319 if (pos <= length)
320 return ptr[pos - 1] << 8 | ptr[pos - 2];
321 else
322 return 0;
326 long HMemIODev::read4b()
328 pos += 4;
329 if (pos <= length)
330 return DWORD(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
331 ptr[pos - 3] << 8 | ptr[pos - 4]);
332 else
333 return 0;
337 int HMemIODev::readBlock(void *p, int size)
339 if (length < pos + size)
340 size = length - pos;
341 memcpy(p, ptr + pos, size);
342 pos += size;
343 return size;
347 int HMemIODev::skipBlock(int size)
349 if (length < pos + size)
350 return 0;
351 pos += size;
352 return size;
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */