1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Jos van den Oever
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
/* This file is part of Strigi Desktop Search
9
* Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Library General Public
13
* License as published by the Free Software Foundation; either
14
* version 2 of the License, or (at your option) any later version.
16
* This library 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 GNU
19
* Library General Public License for more details.
21
* You should have received a copy of the GNU Library General Public License
22
* along with this library; see the file COPYING.LIB. If not, write to
23
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24
* Boston, MA 02110-1301, USA.
26
#ifndef SUBINPUTSTREAM_H
27
#define SUBINPUTSTREAM_H
29
#include "streambase.h"
34
class SubInputStream : public StreamBase<T> {
39
SubInputStream(StreamBase<T> *input, int64_t size=-1);
40
int32_t read(const T*& start, int32_t min, int32_t max);
41
int64_t reset(int64_t newpos);
42
int64_t skip(int64_t ntoskip);
45
SubInputStream<T>::SubInputStream(StreamBase<T> *i, int64_t length)
46
: offset(i->getPosition()), input(i) {
48
// printf("substream offset: %lli\n", offset);
49
StreamBase<T>::size = length;
53
int32_t SubInputStream<T>::read(const T*& start, int32_t min, int32_t max) {
54
if (StreamBase<T>::size != -1) {
55
const int64_t left = StreamBase<T>::size - StreamBase<T>::position;
59
// restrict the amount of data that can be read
60
if (max <= 0 || max > left) {
63
if (min > max) min = max;
64
if (left < min) min = (int32_t)left;
66
int32_t nread = input->read(start, min, max);
68
fprintf(stderr, "substream too short.\n");
69
StreamBase<T>::status = Error;
70
StreamBase<T>::error = input->getError();
71
} else if (nread < min) {
72
if (StreamBase<T>::size == -1) {
73
StreamBase<T>::status = Eof;
75
StreamBase<T>::position += nread;
76
StreamBase<T>::size = StreamBase<T>::position;
79
// fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! nread %i min %i max %i size %lli\n", nread, min, max, size);
80
// fprintf(stderr, "pos %lli parentpos %lli\n", position, input->getPosition());
81
// fprintf(stderr, "status: %i error: %s\n", input->getStatus(), input->getError());
82
// we expected data but didn't get enough so that's an error
83
StreamBase<T>::status = Error;
84
StreamBase<T>::error = "Premature end of stream\n";
88
StreamBase<T>::position += nread;
89
if (StreamBase<T>::position == StreamBase<T>::size) {
90
StreamBase<T>::status = Eof;
97
int64_t SubInputStream<T>::reset(int64_t newpos) {
98
// fprintf(stderr, "subreset pos: %lli newpos: %lli offset: %lli\n", position,
100
StreamBase<T>::position = input->reset(newpos + offset);
101
if (StreamBase<T>::position < offset) {
102
printf("###########\n");
103
StreamBase<T>::status = Error;
104
StreamBase<T>::error = input->getError();
106
StreamBase<T>::position -= offset;
107
StreamBase<T>::status = input->getStatus();
109
return StreamBase<T>::position;
113
int64_t SubInputStream<T>::skip(int64_t ntoskip) {
114
// printf("subskip pos: %lli ntoskip: %lli offset: %lli\n", position, ntoskip, offset);
115
if (StreamBase<T>::size == StreamBase<T>::position) {
116
StreamBase<T>::status = Eof;
119
if (StreamBase<T>::size != -1) {
120
const int64_t left = StreamBase<T>::size - StreamBase<T>::position;
121
// restrict the amount of data that can be skipped
122
if (ntoskip > left) {
126
int64_t skipped = input->skip(ntoskip);
127
if (input->getStatus() == Error) {
128
StreamBase<T>::status = Error;
129
StreamBase<T>::error = input->getError();
131
StreamBase<T>::position += skipped;
132
if (StreamBase<T>::position == StreamBase<T>::size) {
133
StreamBase<T>::status = Eof;
139
} //end namespace jstreams