~ubuntu-branches/ubuntu/quantal/poco/quantal

« back to all changes in this revision

Viewing changes to Zip/include/Poco/Zip/PartialStream.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2008-11-15 11:39:15 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081115113915-7kauhm2c3m2i7oid
Tags: 1.3.3p1-2
* Fixed FTBFS with GCC 4.4 due to missing #include (Closes: #505619)
* Renamed 20_gcc43-missing-include.dpatch to 20_gcc44-missing-include.dpatch
* Downgraded dependencies on -dbg packages (Closes: #504342)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// PartialStream.h
 
3
//
 
4
// $Id: //poco/1.3/Zip/include/Poco/Zip/PartialStream.h#3 $
 
5
//
 
6
// Library: Zip
 
7
// Package: Zip
 
8
// Module:  PartialStream
 
9
//
 
10
// Definition of the PartialStream class.
 
11
//
 
12
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
15
// Permission is hereby granted, free of charge, to any person or organization
 
16
// obtaining a copy of the software and accompanying documentation covered by
 
17
// this license (the "Software") to use, reproduce, display, distribute,
 
18
// execute, and transmit the Software, and to prepare derivative works of the
 
19
// Software, and to permit third-parties to whom the Software is furnished to
 
20
// do so, all subject to the following:
 
21
// 
 
22
// The copyright notices in the Software and this entire statement, including
 
23
// the above license grant, this restriction and the following disclaimer,
 
24
// must be included in all copies of the Software, in whole or in part, and
 
25
// all derivative works of the Software, unless such copies or derivative
 
26
// works are solely in the form of machine-executable object code generated by
 
27
// a source language processor.
 
28
// 
 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
35
// DEALINGS IN THE SOFTWARE.
 
36
//
 
37
 
 
38
 
 
39
#ifndef Zip_PartialStream_INCLUDED
 
40
#define Zip_PartialStream_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Zip/Zip.h"
 
44
#include "Poco/BufferedStreamBuf.h"
 
45
#include "Poco/Buffer.h"
 
46
#include <istream>
 
47
#include <ostream>
 
48
 
 
49
 
 
50
namespace Poco {
 
51
namespace Zip {
 
52
 
 
53
 
 
54
class Zip_API PartialStreamBuf: public Poco::BufferedStreamBuf
 
55
        /// A PartialStreamBuf is a class that limits one view on an inputstream to a selected view range
 
56
{
 
57
public:
 
58
        PartialStreamBuf(std::istream& in, std::ios::pos_type start, std::ios::pos_type end, const std::string& prefix, const std::string& postfix, bool initStream);
 
59
                /// Creates the PartialStream. 
 
60
                /// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
 
61
                /// to position start
 
62
 
 
63
        PartialStreamBuf(std::ostream& out, std::size_t start, std::size_t end, bool initStream);
 
64
                /// Creates the PartialStream. 
 
65
                /// If initStream is true the status of the stream will be cleared on the first access.
 
66
                /// start and end acts as offset values for the written content. A start value greater than zero,
 
67
                /// means that the first bytes are not written but discarded instead,
 
68
                /// an end value not equal to zero means that the last end bytes are not written!
 
69
                /// Examples:
 
70
                ///     start = 3; end = 1
 
71
                ///     write("hello", 5) -> "l"
 
72
 
 
73
        ~PartialStreamBuf();
 
74
                /// Destroys the PartialStream.
 
75
 
 
76
        void close();
 
77
                /// Flushes a writing streambuf
 
78
 
 
79
        Poco::UInt64 bytesWritten() const;
 
80
 
 
81
protected:
 
82
        int readFromDevice(char* buffer, std::streamsize length);
 
83
 
 
84
        int writeToDevice(const char* buffer, std::streamsize length);
 
85
 
 
86
private:
 
87
        enum 
 
88
        {
 
89
                STREAM_BUFFER_SIZE  = 1024
 
90
        };
 
91
 
 
92
        bool           _initialized;
 
93
        std::ios::pos_type  _start;
 
94
        Poco::UInt64   _numBytes;
 
95
        Poco::UInt64   _bytesWritten;
 
96
        std::istream*  _pIstr;
 
97
        std::ostream*  _pOstr;
 
98
        std::string    _prefix;
 
99
        std::string    _postfix;
 
100
        std::size_t    _ignoreStart;
 
101
        Poco::Buffer<char> _buffer;
 
102
        Poco::UInt32   _bufferOffset;
 
103
};
 
104
 
 
105
 
 
106
inline Poco::UInt64 PartialStreamBuf::bytesWritten() const
 
107
{
 
108
        return _bytesWritten;
 
109
}
 
110
 
 
111
 
 
112
class Zip_API PartialIOS: public virtual std::ios
 
113
        /// The base class for PartialInputStream and PartialOutputStream.
 
114
        ///
 
115
        /// This class is needed to ensure the correct initialization
 
116
        /// order of the stream buffer and base classes.
 
117
{
 
118
public:
 
119
        PartialIOS(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, const std::string& prefix, const std::string& postfix, bool initStream);
 
120
                /// Creates the basic stream and connects it
 
121
                /// to the given input stream.
 
122
                /// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
 
123
                /// to position start
 
124
 
 
125
        PartialIOS(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream);
 
126
                /// Creates the basic stream and connects it
 
127
                /// to the given output stream.
 
128
                /// If initStream is true the status of the stream will be cleared on the first access.
 
129
                /// start and end acts as offset values for the written content. A start value greater than zero,
 
130
                /// means that the first bytes are not written but discarded instead,
 
131
                /// an end value not equal to zero means that the last end bytes are not written!
 
132
                /// Examples:
 
133
                ///     start = 3; end = 1
 
134
                ///     write("hello", 5) -> "l"
 
135
 
 
136
        ~PartialIOS();
 
137
                /// Destroys the stream.
 
138
 
 
139
        PartialStreamBuf* rdbuf();
 
140
                /// Returns a pointer to the underlying streambuf.
 
141
 
 
142
protected:
 
143
        PartialStreamBuf _buf;
 
144
};
 
145
 
 
146
 
 
147
class Zip_API PartialInputStream: public PartialIOS, public std::istream
 
148
        /// This stream copies all characters read through it
 
149
        /// to one or multiple output streams.
 
150
{
 
151
public:
 
152
        PartialInputStream(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, bool initStream = true, const std::string& prefix = std::string(), const std::string& postfix = std::string());
 
153
                /// Creates the PartialInputStream and connects it
 
154
                /// to the given input stream. Bytes read are guaranteed to be in the range [start, end-1]
 
155
                /// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
 
156
                /// to position start
 
157
 
 
158
        ~PartialInputStream();
 
159
                /// Destroys the PartialInputStream.
 
160
};
 
161
 
 
162
 
 
163
class Zip_API PartialOutputStream: public PartialIOS, public std::ostream
 
164
        /// This stream copies all characters written to it
 
165
        /// to one or multiple output streams.
 
166
{
 
167
public:
 
168
        PartialOutputStream(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream = true);
 
169
                /// Creates the PartialOutputStream and connects it
 
170
                /// to the given output stream. Bytes written are guaranteed to be in the range [start, realEnd - end].
 
171
                /// If initStream is true the status of the stream will be cleared on the first access.
 
172
                /// start and end acts as offset values for the written content. A start value greater than zero,
 
173
                /// means that the first bytes are not written but discarded instead,
 
174
                /// an end value not equal to zero means that the last end bytes are not written!
 
175
                /// Examples:
 
176
                ///     start = 3; end = 1
 
177
                ///     write("hello", 5) -> "l"
 
178
                ///     
 
179
                ///     start = 3; end = 0
 
180
                ///     write("hello", 5) -> "lo"
 
181
 
 
182
        ~PartialOutputStream();
 
183
                /// Destroys the PartialOutputStream.
 
184
 
 
185
        void close();
 
186
                /// must be called for the stream to properly terminate it
 
187
 
 
188
        Poco::UInt64 bytesWritten() const;
 
189
                /// Returns the number of bytes actually forwarded to the inner ostream
 
190
};
 
191
 
 
192
 
 
193
inline void PartialOutputStream::close()
 
194
{
 
195
        flush();
 
196
        _buf.close();
 
197
}
 
198
 
 
199
 
 
200
inline Poco::UInt64 PartialOutputStream::bytesWritten() const
 
201
{
 
202
        return _buf.bytesWritten();
 
203
}
 
204
 
 
205
 
 
206
} } // namespace Poco::Zip
 
207
 
 
208
 
 
209
#endif // Zip_PartialStream_INCLUDED