|
Lines 1-9
Link Here
|
| 1 |
/*------------------------------------------------------------------------------ |
1 |
/* This file is part of Strigi Desktop Search |
| 2 |
* Copyright (C) 2003-2006 Jos van den Oever |
2 |
* |
| 3 |
* |
3 |
* Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info> |
| 4 |
* Distributable under the terms of either the Apache License (Version 2.0) or |
4 |
* |
| 5 |
* the GNU Lesser General Public License, as specified in the COPYING file. |
5 |
* This library is free software; you can redistribute it and/or |
| 6 |
------------------------------------------------------------------------------*/ |
6 |
* modify it under the terms of the GNU Library General Public |
|
|
7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public License |
| 16 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 17 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301, USA. |
| 19 |
*/ |
| 7 |
#ifndef STREAMBASE_H |
20 |
#ifndef STREAMBASE_H |
| 8 |
#define STREAMBASE_H |
21 |
#define STREAMBASE_H |
| 9 |
|
22 |
|
|
Lines 17-29
Link Here
|
| 17 |
|
30 |
|
| 18 |
/** |
31 |
/** |
| 19 |
* @short Base class for stream read access to many different file types. |
32 |
* @short Base class for stream read access to many different file types. |
| 20 |
* |
33 |
* |
| 21 |
* This class is based on the interface java.io.InputStream. It allows |
34 |
* This class is based on the interface java.io.InputStream. It allows |
| 22 |
* for uniform access to streamed resources. |
35 |
* for uniform access to streamed resources. |
| 23 |
* The main difference with the java equivalent is a performance improvement. |
36 |
* The main difference with the java equivalent is a performance improvement. |
| 24 |
* When reading data, data is not copied into a buffer provided by the caller, |
37 |
* When reading data, data is not copied into a buffer provided by the caller, |
| 25 |
* but a pointer to the read data is provided. This makes this interface especially |
38 |
* but a pointer to the read data is provided. This makes this interface |
| 26 |
* useful for deriving from it and implementing filterers or transformers. |
39 |
* especially useful for deriving from it and implementing filterers or |
|
|
40 |
* transformers. |
| 27 |
*/ |
41 |
*/ |
| 28 |
// java mapping: long=int64, int=int32, byte=uint8_t |
42 |
// java mapping: long=int64, int=int32, byte=uint8_t |
| 29 |
template <class T> |
43 |
template <class T> |
|
Lines 37-65
Link Here
|
| 37 |
StreamBase() :size(-1), position(0), status(Ok){ } |
51 |
StreamBase() :size(-1), position(0), status(Ok){ } |
| 38 |
virtual ~StreamBase(){} |
52 |
virtual ~StreamBase(){} |
| 39 |
/** |
53 |
/** |
| 40 |
* Return a string representation of the last error that has occurred. |
54 |
* @brief Return a string representation of the last error. |
| 41 |
* If no error has occurred, an empty string is returned. |
55 |
* If no error has occurred, an empty string is returned. |
| 42 |
**/ |
56 |
**/ |
| 43 |
const char* getError() const { return error.c_str(); } |
57 |
const char* getError() const { return error.c_str(); } |
| 44 |
StreamStatus getStatus() const { return status; } |
58 |
StreamStatus getStatus() const { return status; } |
|
|
59 |
/** |
| 60 |
* @brief Get the current position in the stream. |
| 61 |
* The value obtained from this function can be used to reset the stream. |
| 62 |
**/ |
| 45 |
int64_t getPosition() const { return position; } |
63 |
int64_t getPosition() const { return position; } |
| 46 |
int64_t getSize() const { return size; } |
64 |
/** |
| 47 |
/** |
65 |
* @brief Return the size of the stream. |
| 48 |
* @brief Reads @p ntoread characters from the stream and sets \a start to |
66 |
* If the size of the stream is unknown, -1 |
| 49 |
* the first character that was read. |
67 |
* is returned. If the end of the stream has been reached the size is |
| 50 |
* |
68 |
* always known. |
| 51 |
* If @p ntoread is @c 0, then at least one character will be read. |
|
|
| 52 |
* |
| 53 |
* @param start Pointer passed by reference that will be set to point to |
| 54 |
* the retrieved array of characters. If the end of the stream |
| 55 |
* is encountered or an error occurs, the value of @p start |
| 56 |
* is undefined. |
| 57 |
* @return the number of characters that were read. If 0 is returned, the |
| 58 |
* end of the stream has been reached. If -1 is returned, an error |
| 59 |
* has occured. |
| 60 |
**/ |
69 |
**/ |
| 61 |
// virtual int32_t read(const T*& start) = 0; |
70 |
int64_t getSize() const { return size; } |
| 62 |
/** |
71 |
/** |
| 63 |
* @brief Reads characters from the stream and sets \a start to |
72 |
* @brief Reads characters from the stream and sets \a start to |
| 64 |
* the first character that was read. |
73 |
* the first character that was read. |
| 65 |
* |
74 |
* |
|
Lines 73-133
Link Here
|
| 73 |
* @p is @c 0 the stream reads at least 1 character. |
82 |
* @p is @c 0 the stream reads at least 1 character. |
| 74 |
* @return the number of characters that were read. If -1 is returned, the |
83 |
* @return the number of characters that were read. If -1 is returned, the |
| 75 |
* end of the stream has been reached. If -2 is returned, an error |
84 |
* end of the stream has been reached. If -2 is returned, an error |
| 76 |
* has occured. |
85 |
* has occurred. |
| 77 |
**/ |
86 |
**/ |
| 78 |
virtual int32_t read(const T*& start, int32_t min, int32_t max) = 0; |
87 |
virtual int32_t read(const T*& start, int32_t min, int32_t max) = 0; |
| 79 |
/** |
88 |
/** |
| 80 |
* Same as read(const T*& start, int32_t ntoread), but may read more. |
|
|
| 81 |
**/ |
| 82 |
// virtual int32_t readAtLeast(const T*& start, int32_t ntoread) = 0; |
| 83 |
/* the available value may be greater than the actual value if |
| 84 |
the encoding is a variable one (such as utf8 or unicode) */ |
| 85 |
/** |
| 86 |
* Skip @param ntoskip bytes. Unless an error occurs or the end of file is |
89 |
* Skip @param ntoskip bytes. Unless an error occurs or the end of file is |
| 87 |
* encountered, this amount of bytes is skipped. |
90 |
* encountered, this amount of bytes is skipped. |
| 88 |
* The optional @param skipped can be use to find out how many bites were skipped. |
91 |
* This function returns new position in the stream. |
| 89 |
* If the end of stream is reached, Eof is returned. |
|
|
| 90 |
* If an error occured, Error is returned. |
| 91 |
**/ |
92 |
**/ |
| 92 |
virtual int64_t skip(int64_t ntoskip); |
93 |
virtual int64_t skip(int64_t ntoskip); |
| 93 |
/** |
|
|
| 94 |
* \short Marks the current position in this input stream. |
| 95 |
* A subsequent call to the reset method repositions this stream at the |
| 96 |
* last marked position so that subsequent reads re-read the same bytes. |
| 97 |
* |
| 98 |
* The readlimit arguments tells this input stream to allow that many |
| 99 |
* bytes to be read before the mark position gets invalidated. |
| 100 |
* The stream somehow remembers all the bytes read after the call to mark |
| 101 |
* and stands ready to supply those same bytes again if and whenever the |
| 102 |
* method reset is called. However, the stream is not required to remember |
| 103 |
* any data at all if more than readlimit bytes are read from the stream |
| 104 |
* before reset is called. |
| 105 |
* |
| 106 |
* When calling the method mark more than once at the same position in the |
| 107 |
* stream, the call with the largest value for \p readlimit is defining. |
| 108 |
**/ |
| 109 |
virtual int64_t mark(int32_t readlimit) = 0; |
| 110 |
/** |
94 |
/** |
| 111 |
* \short Repositions this stream to given requested position. |
95 |
* @brief Repositions this stream to given requested position. |
| 112 |
* The general contract of reset is: |
96 |
* Reset is guaranteed to work after a successful call to read(), |
| 113 |
* - Reset is guaranteed to work after a successfull call to read(), |
97 |
* when the new position is in the range of the data returned by read(). |
| 114 |
* when new position is in the range of the data returned by read(). |
98 |
* This means that @p pos must lie between than the position |
| 115 |
* This means that @p pos must lie between than the position |
99 |
* corresponding to the @p start parameter (x) of the @r read function |
| 116 |
* corresponding to the @p start parameter (x) of the @r read function |
100 |
* and the position corresponding to the last position in the returned |
| 117 |
* and the position corresponding to the last position in the returned |
101 |
* buffer (x + @p nread). |
| 118 |
* buffer (x + @p nread). |
|
|
| 119 |
* if If the method mark has not been called since the stream was created, |
| 120 |
* or the number of bytes read from the stream since mark was last |
| 121 |
* called is larger than the argument to mark at that last call, then |
| 122 |
* Error is returned. |
| 123 |
* - Otherwise the stream is reset to a state such that all the bytes |
| 124 |
* read since the most recent call to mark (or since the start of the |
| 125 |
* file, if mark has not been called) will be resupplied to subsequent |
| 126 |
* callers of the read method, followed by any bytes that otherwise |
| 127 |
* would have been the next input data as of the time of the call to |
| 128 |
* reset. |
| 129 |
**/ |
102 |
**/ |
| 130 |
virtual int64_t reset(int64_t pos) = 0; |
103 |
virtual int64_t reset(int64_t pos) = 0; |
|
|
104 |
/** |
| 105 |
* deprecated function |
| 106 |
**/ |
| 107 |
int64_t mark(int32_t readlimit) { |
| 108 |
int64_t p = getPosition(); |
| 109 |
const T* ptr; |
| 110 |
read(ptr, readlimit, -1); |
| 111 |
return reset(p); |
| 112 |
} |
| 131 |
}; |
113 |
}; |
| 132 |
#define SKIPSTEP 1024 |
114 |
#define SKIPSTEP 1024 |
| 133 |
template <class T> |
115 |
template <class T> |