1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
29 #include <osl/diagnose.h>
35 #include "hstream.hxx"
37 const int BUFSIZE
= 1024;
38 static uchar rBuf
[BUFSIZE
];
40 // HIODev abstract class
58 int HIODev::read1b(void *ptr
, int nmemb
)
60 uchar
*p
= static_cast<uchar
*>(ptr
);
65 for (ii
= 0; ii
< nmemb
; ii
++)
75 int HIODev::read2b(void *ptr
, int nmemb
)
77 ushort
*p
= static_cast<ushort
*>(ptr
);
82 for (ii
= 0; ii
< nmemb
; ii
++)
92 int HIODev::read4b(void *ptr
, int nmemb
)
94 uint
*p
= static_cast<uint
*>(ptr
);
99 for (ii
= 0; ii
< nmemb
; ii
++)
111 HStreamIODev::HStreamIODev(HStream
* stream
):_stream(stream
)
117 HStreamIODev::~HStreamIODev()
123 void HStreamIODev::init()
130 bool HStreamIODev::open()
132 if (!(_stream
->available()))
138 void HStreamIODev::flush()
141 gz_flush(_gzfp
, Z_FINISH
);
145 void HStreamIODev::close()
155 int HStreamIODev::state() const
162 bool HStreamIODev::setCompressed(bool flag
)
166 return 0 != (_gzfp
= gz_open(*_stream
));
169 gz_flush(_gzfp
, Z_FINISH
);
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);
188 out
= (unsigned char)rBuf
[0];
192 bool HStreamIODev::read1b(char &out
)
201 bool HStreamIODev::read2b(unsigned short &out
)
203 int res
= (compressed
) ? GZREAD(rBuf
, 2) : _stream
->readBytes(rBuf
, 2);
208 out
= ((unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
212 bool HStreamIODev::read4b(unsigned int &out
)
214 int res
= (compressed
) ? GZREAD(rBuf
, 4) : _stream
->readBytes(rBuf
, 4);
219 out
= ((unsigned char) rBuf
[3] << 24 | (unsigned char) rBuf
[2] << 16 |
220 (unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
224 bool HStreamIODev::read4b(int &out
)
233 int HStreamIODev::readBlock(void *ptr
, int size
)
236 (compressed
) ? GZREAD(ptr
, size
) : _stream
->readBytes(static_cast<byte
*>(ptr
),
243 int HStreamIODev::skipBlock(int size
)
246 if( size
<= BUFSIZE
)
247 return GZREAD(rBuf
, size
);
251 if( remain
> BUFSIZE
)
252 remain
-= GZREAD(rBuf
, BUFSIZE
);
254 remain
-= GZREAD(rBuf
, remain
);
258 return size
- remain
;
261 return _stream
->skipBytes(size
);
265 HMemIODev::HMemIODev(char *s
, int len
)
268 ptr
= reinterpret_cast<uchar
*>(s
);
273 HMemIODev::~HMemIODev()
279 void HMemIODev::init()
287 bool HMemIODev::open()
293 void HMemIODev::flush()
298 void HMemIODev::close()
303 int HMemIODev::state() const
312 bool HMemIODev::setCompressed(bool )
317 bool HMemIODev::read1b(unsigned char &out
)
327 bool HMemIODev::read1b(char &out
)
336 bool HMemIODev::read2b(unsigned short &out
)
341 out
= ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
347 bool HMemIODev::read4b(unsigned int &out
)
352 out
= static_cast<unsigned int>(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
353 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
359 bool HMemIODev::read4b(int &out
)
368 int HMemIODev::readBlock(void *p
, int size
)
370 if (length
< pos
+ size
)
372 memcpy(p
, ptr
+ pos
, size
);
377 int HMemIODev::skipBlock(int size
)
379 if (length
< pos
+ size
)
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */