1 /*****************************************************************
3 | Neptune - Byte Streams
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 #ifndef _NPT_STREAMS_H_
33 #define _NPT_STREAMS_H_
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
39 #include "NptReferences.h"
40 #include "NptConstants.h"
41 #include "NptResults.h"
42 #include "NptDataBuffer.h"
43 #include "NptStrings.h"
45 /*----------------------------------------------------------------------
47 +---------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------
52 +---------------------------------------------------------------------*/
53 const int NPT_ERROR_READ_FAILED
= NPT_ERROR_BASE_IO
- 0;
54 const int NPT_ERROR_WRITE_FAILED
= NPT_ERROR_BASE_IO
- 1;
55 const int NPT_ERROR_EOS
= NPT_ERROR_BASE_IO
- 2;
57 /*----------------------------------------------------------------------
59 +---------------------------------------------------------------------*/
63 // constructor and destructor
64 virtual ~NPT_InputStream() {};
67 virtual NPT_Result
Load(NPT_DataBuffer
& buffer
, NPT_Size max_read
= 0);
68 virtual NPT_Result
Read(void* buffer
,
69 NPT_Size bytes_to_read
,
70 NPT_Size
* bytes_read
= NULL
) = 0;
71 virtual NPT_Result
ReadFully(void* buffer
,
72 NPT_Size bytes_to_read
);
73 virtual NPT_Result
Seek(NPT_Position offset
) = 0;
74 virtual NPT_Result
Skip(NPT_Size offset
);
75 virtual NPT_Result
Tell(NPT_Position
& offset
) = 0;
76 virtual NPT_Result
GetSize(NPT_LargeSize
& size
) = 0;
77 virtual NPT_Result
GetAvailable(NPT_LargeSize
& available
) = 0;
79 // data access methods
80 NPT_Result
ReadUI64(NPT_UInt64
& value
);
81 NPT_Result
ReadUI32(NPT_UInt32
& value
);
82 NPT_Result
ReadUI24(NPT_UInt32
& value
);
83 NPT_Result
ReadUI16(NPT_UInt16
& value
);
84 NPT_Result
ReadUI08(NPT_UInt8
& value
);
87 typedef NPT_Reference
<NPT_InputStream
> NPT_InputStreamReference
;
89 /*----------------------------------------------------------------------
91 +---------------------------------------------------------------------*/
92 class NPT_OutputStream
95 // constructor and destructor
96 virtual ~NPT_OutputStream() {};
99 virtual NPT_Result
Write(const void* buffer
,
100 NPT_Size bytes_to_write
,
101 NPT_Size
* bytes_written
= NULL
) = 0;
102 virtual NPT_Result
WriteFully(const void* buffer
,
103 NPT_Size bytes_to_write
);
104 virtual NPT_Result
WriteString(const char* string_buffer
);
105 virtual NPT_Result
WriteLine(const char* line_buffer
);
106 virtual NPT_Result
Seek(NPT_Position offset
) = 0;
107 virtual NPT_Result
Tell(NPT_Position
& offset
) = 0;
108 virtual NPT_Result
Flush() { return NPT_SUCCESS
; }
110 // data access methods
111 NPT_Result
WriteUI64(NPT_UInt64 value
);
112 NPT_Result
WriteUI32(NPT_UInt32 value
);
113 NPT_Result
WriteUI24(NPT_UInt32 value
);
114 NPT_Result
WriteUI16(NPT_UInt16 value
);
115 NPT_Result
WriteUI08(NPT_UInt8 value
);
118 typedef NPT_Reference
<NPT_OutputStream
> NPT_OutputStreamReference
;
120 /*----------------------------------------------------------------------
121 | NPT_StreamToStreamCopy
122 +---------------------------------------------------------------------*/
123 NPT_Result
NPT_StreamToStreamCopy(NPT_InputStream
& from
,
124 NPT_OutputStream
& to
,
125 NPT_Position offset
= 0,
126 NPT_LargeSize size
= 0, /* 0 means the entire stream */
127 NPT_LargeSize
* bytes_written
= NULL
);
129 /*----------------------------------------------------------------------
130 | NPT_DelegatingInputStream
132 | Use this class as a base class if you need to inherit both from
133 | NPT_InputStream and NPT_OutputStream which share the Seek and Tell
134 | method. In this case, you override the base-specific version of
135 | those methods, InputSeek, InputTell, instead of the Seek and Tell
137 +---------------------------------------------------------------------*/
138 class NPT_DelegatingInputStream
: public NPT_InputStream
141 // NPT_InputStream methods
142 NPT_Result
Seek(NPT_Position offset
) override
{
143 return InputSeek(offset
);
145 NPT_Result
Tell(NPT_Position
& offset
) override
{
146 return InputTell(offset
);
151 virtual NPT_Result
InputSeek(NPT_Position offset
) = 0;
152 virtual NPT_Result
InputTell(NPT_Position
& offset
) = 0;
155 /*----------------------------------------------------------------------
156 | NPT_DelegatingOutputStream
158 | Use this class as a base class if you need to inherit both from
159 | NPT_InputStream and NPT_OutputStream which share the Seek and Tell
160 | method. In this case, you override the base-specific version of
161 | those methods, OutputSeek and OutputTell, instead of the Seek and
163 +---------------------------------------------------------------------*/
164 class NPT_DelegatingOutputStream
: public NPT_OutputStream
167 // NPT_OutputStream methods
168 NPT_Result
Seek(NPT_Position offset
) override
{
169 return OutputSeek(offset
);
171 NPT_Result
Tell(NPT_Position
& offset
) override
{
172 return OutputTell(offset
);
177 virtual NPT_Result
OutputSeek(NPT_Position offset
) = 0;
178 virtual NPT_Result
OutputTell(NPT_Position
& offset
) = 0;
181 /*----------------------------------------------------------------------
183 +---------------------------------------------------------------------*/
184 class NPT_MemoryStream
:
185 public NPT_DelegatingInputStream
,
186 public NPT_DelegatingOutputStream
189 // constructor and destructor
190 NPT_MemoryStream(NPT_Size initial_capacity
= 0);
191 NPT_MemoryStream(const void* data
, NPT_Size size
);
192 ~NPT_MemoryStream() override
{}
195 const NPT_DataBuffer
& GetBuffer() const { return m_Buffer
; }
197 // NPT_InputStream methods
198 NPT_Result
Read(void* buffer
,
199 NPT_Size bytes_to_read
,
200 NPT_Size
* bytes_read
= NULL
) override
;
201 NPT_Result
GetSize(NPT_LargeSize
& size
) override
{
202 size
= m_Buffer
.GetDataSize();
205 NPT_Result
GetAvailable(NPT_LargeSize
& available
) override
{
206 available
= (NPT_LargeSize
)m_Buffer
.GetDataSize()-m_ReadOffset
;
210 // NPT_OutputStream methods
211 NPT_Result
Write(const void* buffer
,
212 NPT_Size bytes_to_write
,
213 NPT_Size
* bytes_written
= NULL
) override
;
215 // methods delegated to m_Buffer
216 const NPT_Byte
* GetData() const { return m_Buffer
.GetData(); }
217 NPT_Byte
* UseData() { return m_Buffer
.UseData(); }
218 NPT_Size
GetDataSize() const { return m_Buffer
.GetDataSize(); }
219 NPT_Size
GetBufferSize() const { return m_Buffer
.GetBufferSize();}
222 NPT_Result
SetDataSize(NPT_Size size
);
225 // NPT_DelegatingInputStream methods
226 NPT_Result
InputSeek(NPT_Position offset
) override
;
227 NPT_Result
InputTell(NPT_Position
& offset
) override
{
228 offset
= m_ReadOffset
;
232 // NPT_DelegatingOutputStream methods
233 NPT_Result
OutputSeek(NPT_Position offset
) override
;
234 NPT_Result
OutputTell(NPT_Position
& offset
) override
{
235 offset
= m_WriteOffset
;
241 NPT_DataBuffer m_Buffer
;
242 NPT_Size m_ReadOffset
;
243 NPT_Size m_WriteOffset
;
246 typedef NPT_Reference
<NPT_MemoryStream
> NPT_MemoryStreamReference
;
248 /*----------------------------------------------------------------------
249 | NPT_StringOutputStream
250 +---------------------------------------------------------------------*/
251 class NPT_StringOutputStream
: public NPT_OutputStream
255 NPT_StringOutputStream(NPT_Size size
= 4096);
256 NPT_StringOutputStream(NPT_String
* storage
);
257 ~NPT_StringOutputStream() override
;
259 const NPT_String
& GetString() const { return *m_String
; }
260 NPT_Result
Reset() { if (m_String
) m_String
->SetLength(0); return NPT_SUCCESS
; }
262 // NPT_OutputStream methods
263 NPT_Result
Write(const void* buffer
, NPT_Size bytes_to_write
, NPT_Size
* bytes_written
= NULL
) override
;
265 NPT_Result
Seek(NPT_Position
/*offset*/) override
{ return NPT_ERROR_NOT_SUPPORTED
; }
266 NPT_Result
Tell(NPT_Position
& offset
) override
{ offset
= m_String
->GetLength(); return NPT_SUCCESS
; }
269 NPT_String
* m_String
;
270 bool m_StringIsOwned
;
273 typedef NPT_Reference
<NPT_StringOutputStream
> NPT_StringOutputStreamReference
;
275 /*----------------------------------------------------------------------
277 +---------------------------------------------------------------------*/
278 class NPT_SubInputStream
: public NPT_InputStream
281 // constructor and destructor
282 NPT_SubInputStream(NPT_InputStreamReference
& source
,
287 NPT_Result
Read(void* buffer
,
288 NPT_Size bytes_to_read
,
289 NPT_Size
* bytes_read
= NULL
) override
;
290 NPT_Result
Seek(NPT_Position offset
) override
;
291 NPT_Result
Tell(NPT_Position
& offset
) override
;
292 NPT_Result
GetSize(NPT_LargeSize
& size
) override
;
293 NPT_Result
GetAvailable(NPT_LargeSize
& available
) override
;
296 NPT_InputStreamReference m_Source
;
297 NPT_Position m_Position
;
298 NPT_Position m_Start
;
299 NPT_LargeSize m_Size
;
302 /*----------------------------------------------------------------------
303 | NPT_NullOutputStream
304 +---------------------------------------------------------------------*/
305 class NPT_NullOutputStream
: public NPT_OutputStream
309 NPT_NullOutputStream() {}
310 ~NPT_NullOutputStream() override
{}
312 // NPT_OutputStream methods
313 NPT_Result
Write(const void* buffer
, NPT_Size bytes_to_write
, NPT_Size
* bytes_written
= NULL
) override
;
315 NPT_Result
Seek(NPT_Position
/*offset*/) override
{ return NPT_ERROR_NOT_SUPPORTED
; }
316 NPT_Result
Tell(NPT_Position
& /*offset*/) override
{ return NPT_ERROR_NOT_SUPPORTED
; }
319 typedef NPT_Reference
<NPT_NullOutputStream
> NPT_NullOutputStreamReference
;
321 #endif // _NPT_STREAMS_H_