1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Jos van den Oever
3 *
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
8 *
9 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
10 *
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.
15 *
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.
20 *
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.
25 */
26 #ifndef SUBINPUTSTREAM_H
27 #define SUBINPUTSTREAM_H
28
29 #include "streambase.h"
30
31 namespace jstreams {
32
33 template<class T>
34 class SubInputStream : public StreamBase<T> {
35 private:
36 const int64_t offset;
37 StreamBase<T> *input;
38 public:
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);
43 };
44 template<class T>
SubInputStream(StreamBase<T> * i,int64_t length)45 SubInputStream<T>::SubInputStream(StreamBase<T> *i, int64_t length)
46 : offset(i->getPosition()), input(i) {
47 assert(length >= -1);
48 // printf("substream offset: %lli\n", offset);
49 StreamBase<T>::size = length;
50 }
51
52 template<class T>
read(const T * & start,int32_t min,int32_t max)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;
56 if (left == 0) {
57 return -1;
58 }
59 // restrict the amount of data that can be read
60 if (max <= 0 || max > left) {
61 max = (int32_t)left;
62 }
63 if (min > max) min = max;
64 if (left < min) min = (int32_t)left;
65 }
66 int32_t nread = input->read(start, min, max);
67 if (nread < -1) {
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;
74 if (nread > 0) {
75 StreamBase<T>::position += nread;
76 StreamBase<T>::size = StreamBase<T>::position;
77 }
78 } else {
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";
85 nread = -2;
86 }
87 } else {
88 StreamBase<T>::position += nread;
89 if (StreamBase<T>::position == StreamBase<T>::size) {
90 StreamBase<T>::status = Eof;
91 }
92 }
93 return nread;
94 }
95
96 template<class T>
reset(int64_t newpos)97 int64_t SubInputStream<T>::reset(int64_t newpos) {
98 // fprintf(stderr, "subreset pos: %lli newpos: %lli offset: %lli\n", position,
99 // newpos, offset);
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();
105 } else {
106 StreamBase<T>::position -= offset;
107 StreamBase<T>::status = input->getStatus();
108 }
109 return StreamBase<T>::position;
110 }
111
112 template<class T>
skip(int64_t ntoskip)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;
117 return -1;
118 }
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) {
123 ntoskip = left;
124 }
125 }
126 int64_t skipped = input->skip(ntoskip);
127 if (input->getStatus() == Error) {
128 StreamBase<T>::status = Error;
129 StreamBase<T>::error = input->getError();
130 } else {
131 StreamBase<T>::position += skipped;
132 if (StreamBase<T>::position == StreamBase<T>::size) {
133 StreamBase<T>::status = Eof;
134 }
135 }
136 return skipped;
137 }
138
139 } //end namespace jstreams
140
141 #endif
142