kodi
NptStreams.h
1 /*****************************************************************
2 |
3 | Neptune - Byte Streams
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
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.
18 |
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.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_STREAMS_H_
33 #define _NPT_STREAMS_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptReferences.h"
40 #include "NptConstants.h"
41 #include "NptResults.h"
42 #include "NptDataBuffer.h"
43 #include "NptStrings.h"
44 
45 /*----------------------------------------------------------------------
46 | class references
47 +---------------------------------------------------------------------*/
48 class NPT_String;
49 
50 /*----------------------------------------------------------------------
51 | constants
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;
56 
57 /*----------------------------------------------------------------------
58 | NPT_InputStream
59 +---------------------------------------------------------------------*/
61 {
62  public:
63  // constructor and destructor
64  virtual ~NPT_InputStream() {};
65 
66  // methods
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;
78 
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);
85 };
86 
88 
89 /*----------------------------------------------------------------------
90 | NPT_OutputStream
91 +---------------------------------------------------------------------*/
93 {
94 public:
95  // constructor and destructor
96  virtual ~NPT_OutputStream() {};
97 
98  // methods
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; }
109 
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);
116 };
117 
119 
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);
128 
129 /*----------------------------------------------------------------------
130 | NPT_DelegatingInputStream
131 |
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
136 | methods.
137 +---------------------------------------------------------------------*/
139 {
140 public:
141  // NPT_InputStream methods
142  NPT_Result Seek(NPT_Position offset) override {
143  return InputSeek(offset);
144  }
145  NPT_Result Tell(NPT_Position& offset) override {
146  return InputTell(offset);
147  }
148 
149 private:
150  // methods
151  virtual NPT_Result InputSeek(NPT_Position offset) = 0;
152  virtual NPT_Result InputTell(NPT_Position& offset) = 0;
153 };
154 
155 /*----------------------------------------------------------------------
156 | NPT_DelegatingOutputStream
157 |
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
162 | Tell methods.
163 +---------------------------------------------------------------------*/
165 {
166 public:
167  // NPT_OutputStream methods
168  NPT_Result Seek(NPT_Position offset) override {
169  return OutputSeek(offset);
170  }
171  NPT_Result Tell(NPT_Position& offset) override {
172  return OutputTell(offset);
173  }
174 
175 private:
176  // methods
177  virtual NPT_Result OutputSeek(NPT_Position offset) = 0;
178  virtual NPT_Result OutputTell(NPT_Position& offset) = 0;
179 };
180 
181 /*----------------------------------------------------------------------
182 | NPT_MemoryStream
183 +---------------------------------------------------------------------*/
187 {
188 public:
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 {}
193 
194  // accessors
195  const NPT_DataBuffer& GetBuffer() const { return m_Buffer; }
196 
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();
203  return NPT_SUCCESS;
204  }
205  NPT_Result GetAvailable(NPT_LargeSize& available) override {
206  available = (NPT_LargeSize)m_Buffer.GetDataSize()-m_ReadOffset;
207  return NPT_SUCCESS;
208  }
209 
210  // NPT_OutputStream methods
211  NPT_Result Write(const void* buffer,
212  NPT_Size bytes_to_write,
213  NPT_Size* bytes_written = NULL) override;
214 
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();}
220 
221  // methods
222  NPT_Result SetDataSize(NPT_Size size);
223 
224 private:
225  // NPT_DelegatingInputStream methods
226  NPT_Result InputSeek(NPT_Position offset) override;
227  NPT_Result InputTell(NPT_Position& offset) override {
228  offset = m_ReadOffset;
229  return NPT_SUCCESS;
230  }
231 
232  // NPT_DelegatingOutputStream methods
233  NPT_Result OutputSeek(NPT_Position offset) override;
234  NPT_Result OutputTell(NPT_Position& offset) override {
235  offset = m_WriteOffset;
236  return NPT_SUCCESS;
237  }
238 
239 protected:
240  // members
241  NPT_DataBuffer m_Buffer;
242  NPT_Size m_ReadOffset;
243  NPT_Size m_WriteOffset;
244 };
245 
247 
248 /*----------------------------------------------------------------------
249 | NPT_StringOutputStream
250 +---------------------------------------------------------------------*/
252 {
253 public:
254  // methods
255  NPT_StringOutputStream(NPT_Size size = 4096);
257  ~NPT_StringOutputStream() override ;
258 
259  const NPT_String& GetString() const { return *m_String; }
260  NPT_Result Reset() { if (m_String) m_String->SetLength(0); return NPT_SUCCESS; }
261 
262  // NPT_OutputStream methods
263  NPT_Result Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written = NULL) override;
264 
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; }
267 
268 protected:
269  NPT_String* m_String;
270  bool m_StringIsOwned;
271 };
272 
274 
275 /*----------------------------------------------------------------------
276 | NPT_SubInputStream
277 +---------------------------------------------------------------------*/
279 {
280 public:
281  // constructor and destructor
283  NPT_Position start,
284  NPT_LargeSize size);
285 
286  // methods
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;
294 
295 private:
296  NPT_InputStreamReference m_Source;
297  NPT_Position m_Position;
298  NPT_Position m_Start;
299  NPT_LargeSize m_Size;
300 };
301 
302 /*----------------------------------------------------------------------
303 | NPT_NullOutputStream
304 +---------------------------------------------------------------------*/
306 {
307 public:
308  // methods
310  ~NPT_NullOutputStream() override {}
311 
312  // NPT_OutputStream methods
313  NPT_Result Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written = NULL) override;
314 
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; }
317 };
318 
320 
321 #endif // _NPT_STREAMS_H_
Definition: NptStreams.h:60
Definition: NptStreams.h:138
Definition: NptStreams.h:92
Definition: NptStreams.h:251
Definition: NptDataBuffer.h:44
Definition: NptStreams.h:278
Definition: NptStreams.h:184
Definition: NptStreams.h:164
Definition: NptStrings.h:57
Definition: NptStreams.h:305