gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / PdbStream.cpp
blob0c4e3e6f768ad9e8af6f7d6f255488dc9339b892
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <string.h>
22 #include <ZLFile.h>
24 #include "PdbStream.h"
26 PdbStream::PdbStream(ZLFile &file) : myBase(file.inputStream()) {
27 myBuffer = 0;
30 PdbStream::~PdbStream() {
33 bool PdbStream::open() {
34 close();
35 if (myBase.isNull() || !myBase->open() || !myHeader.read(myBase)) {
36 return false;
39 myBase->seek(myHeader.Offsets[0], true);
41 myBufferLength = 0;
42 myBufferOffset = 0;
44 myOffset = 0;
46 return true;
49 size_t PdbStream::read(char *buffer, size_t maxSize) {
50 size_t realSize = 0;
51 while (realSize < maxSize) {
52 if (!fillBuffer()) {
53 break;
55 size_t size = std::min((size_t)(maxSize - realSize), (size_t)(myBufferLength - myBufferOffset));
56 if (size > 0) {
57 if (buffer != 0) {
58 memcpy(buffer + realSize, myBuffer + myBufferOffset, size);
60 realSize += size;
61 myBufferOffset += size;
65 myOffset += realSize;
66 return realSize;
69 void PdbStream::close() {
70 if (!myBase.isNull()) {
71 myBase->close();
73 if (myBuffer != 0) {
74 delete[] myBuffer;
75 myBuffer = 0;
79 void PdbStream::seek(int offset, bool absoluteOffset) {
80 if (absoluteOffset) {
81 offset -= this->offset();
83 if (offset > 0) {
84 read(0, offset);
85 } else if (offset < 0) {
86 offset += this->offset();
87 open();
88 if (offset >= 0) {
89 read(0, offset);
94 size_t PdbStream::offset() const {
95 return myOffset;
98 size_t PdbStream::sizeOfOpened() {
99 // TODO: implement
100 return 0;