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 BUFFEREDSTREAM_H
27 #define BUFFEREDSTREAM_H
28 
29 #include "streambase.h"
30 #include "inputstreambuffer.h"
31 
32 #include <cassert>
33 #include <stdio.h>
34 
35 namespace jstreams {
36 
37 template <class T>
38 class BufferedInputStream : public StreamBase<T> {
39 private:
40     bool finishedWritingToBuffer;
41     InputStreamBuffer<T> buffer;
42 
43     void writeToBuffer(int32_t minsize);
44     int32_t read_(const T*& start, int32_t min, int32_t max);
45 protected:
46     /**
47      * This function must be implemented by the subclasses.
48      * It should write a maximum of @p space characters at the buffer
49      * position pointed to by @p start. If no more data is available due to
50      * end of file, -1 should be returned. If an error occurs, the status
51      * should be set to Error, an error message should be set and the function
52      * must return -1.
53      **/
54     virtual int32_t fillBuffer(T* start, int32_t space) = 0;
55     // this function might be useful if you want to reuse a bufferedstream
resetBuffer()56     void resetBuffer() {printf("implement 'resetBuffer'\n");}
57     BufferedInputStream<T>();
58 public:
59     int32_t read(const T*& start, int32_t min, int32_t max);
60     int64_t reset(int64_t);
61     virtual int64_t skip(int64_t ntoskip);
62 };
63 
64 template <class T>
BufferedInputStream()65 BufferedInputStream<T>::BufferedInputStream() {
66     finishedWritingToBuffer = false;
67 }
68 
69 template <class T>
70 void
writeToBuffer(int32_t ntoread)71 BufferedInputStream<T>::writeToBuffer(int32_t ntoread) {
72     int32_t missing = ntoread - buffer.avail;
73     int32_t nwritten = 0;
74     while (missing > 0 && nwritten >= 0) {
75         int32_t space;
76         space = buffer.makeSpace(missing);
77         T* start = buffer.readPos + buffer.avail;
78         nwritten = fillBuffer(start, space);
79         assert(StreamBase<T>::status != Eof);
80         if (nwritten > 0) {
81             buffer.avail += nwritten;
82             missing = ntoread - buffer.avail;
83         }
84     }
85     if (nwritten < 0) {
86         finishedWritingToBuffer = true;
87     }
88 }
89 template <class T>
90 int32_t
read(const T * & start,int32_t min,int32_t max)91 BufferedInputStream<T>::read(const T*& start, int32_t min, int32_t max) {
92     if (StreamBase<T>::status == Error) return -2;
93     if (StreamBase<T>::status == Eof) return -1;
94 
95     // do we need to read data into the buffer?
96     if (!finishedWritingToBuffer && min > buffer.avail) {
97         // do we have enough space in the buffer?
98         writeToBuffer(min);
99         if (StreamBase<T>::status == Error) return -2;
100     }
101 
102     int32_t nread = buffer.read(start, max);
103 
104     BufferedInputStream<T>::position += nread;
105     if (BufferedInputStream<T>::position > BufferedInputStream<T>::size
106         && BufferedInputStream<T>::size > 0) {
107         // error: we read more than was specified in size
108         // this is an error because all dependent code might have been labouring
109         // under a misapprehension
110         BufferedInputStream<T>::status = Error;
111         BufferedInputStream<T>::error = "Stream is longer than specified.";
112         nread = -2;
113     } else if (BufferedInputStream<T>::status == Ok && buffer.avail == 0
114             && finishedWritingToBuffer) {
115         BufferedInputStream<T>::status = Eof;
116         if (BufferedInputStream<T>::size == -1) {
117             BufferedInputStream<T>::size = BufferedInputStream<T>::position;
118         }
119         // save one call to read() by already returning -1 if no data is there
120         if (nread == 0) nread = -1;
121     }
122     return nread;
123 }
124 template <class T>
125 int64_t
reset(int64_t newpos)126 BufferedInputStream<T>::reset(int64_t newpos) {
127     if (StreamBase<T>::status == Error) return -2;
128     // check to see if we have this position
129     int64_t d = BufferedInputStream<T>::position - newpos;
130     if (buffer.readPos - d >= buffer.start && -d < buffer.avail) {
131         BufferedInputStream<T>::position -= d;
132         buffer.avail += (int32_t)d;
133         buffer.readPos -= d;
134         StreamBase<T>::status = Ok;
135     }
136     return StreamBase<T>::position;
137 }
138 template <class T>
139 int64_t
skip(int64_t ntoskip)140 BufferedInputStream<T>::skip(int64_t ntoskip) {
141     const T *begin;
142     int32_t nread;
143     int64_t skipped = 0;
144     while (ntoskip) {
145         int32_t step = (int32_t)((ntoskip > buffer.size) ?buffer.size :ntoskip);
146         nread = read(begin, 1, step);
147         if (nread <= 0) {
148             return skipped;
149         }
150         ntoskip -= nread;
151         skipped += nread;
152     }
153     return skipped;
154 }
155 }
156 
157 #endif
158