Mercurial > projects > hoofbaby
comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptStreams.h @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
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 +---------------------------------------------------------------------*/ | |
60 class NPT_InputStream | |
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 virtual NPT_Result Close() { return NPT_SUCCESS; } | |
79 | |
80 // data access methods | |
81 NPT_Result ReadUI64(NPT_UInt64& value); | |
82 NPT_Result ReadUI32(NPT_UInt32& value); | |
83 NPT_Result ReadUI24(NPT_UInt32& value); | |
84 NPT_Result ReadUI16(NPT_UInt16& value); | |
85 NPT_Result ReadUI08(NPT_UInt8& value); | |
86 }; | |
87 | |
88 typedef NPT_Reference<NPT_InputStream> NPT_InputStreamReference; | |
89 | |
90 /*---------------------------------------------------------------------- | |
91 | NPT_OutputStream | |
92 +---------------------------------------------------------------------*/ | |
93 class NPT_OutputStream | |
94 { | |
95 public: | |
96 // constructor and destructor | |
97 virtual ~NPT_OutputStream() {}; | |
98 | |
99 // methods | |
100 virtual NPT_Result Write(const void* buffer, | |
101 NPT_Size bytes_to_write, | |
102 NPT_Size* bytes_written = NULL) = 0; | |
103 virtual NPT_Result WriteFully(const void* buffer, | |
104 NPT_Size bytes_to_write); | |
105 virtual NPT_Result WriteString(const char* string_buffer); | |
106 virtual NPT_Result WriteLine(const char* line_buffer); | |
107 virtual NPT_Result Seek(NPT_Position offset) = 0; | |
108 virtual NPT_Result Tell(NPT_Position& offset) = 0; | |
109 virtual NPT_Result Flush() { return NPT_SUCCESS; } | |
110 | |
111 // data access methods | |
112 NPT_Result WriteUI64(NPT_UInt64 value); | |
113 NPT_Result WriteUI32(NPT_UInt32 value); | |
114 NPT_Result WriteUI24(NPT_UInt32 value); | |
115 NPT_Result WriteUI16(NPT_UInt16 value); | |
116 NPT_Result WriteUI08(NPT_UInt8 value); | |
117 }; | |
118 | |
119 typedef NPT_Reference<NPT_OutputStream> NPT_OutputStreamReference; | |
120 | |
121 /*---------------------------------------------------------------------- | |
122 | NPT_StreamToStreamCopy | |
123 +---------------------------------------------------------------------*/ | |
124 NPT_Result NPT_StreamToStreamCopy(NPT_InputStream& from, | |
125 NPT_OutputStream& to, | |
126 NPT_Position offset = 0, | |
127 NPT_LargeSize size = 0 /* 0 means the entire stream */); | |
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 +---------------------------------------------------------------------*/ | |
138 class NPT_DelegatingInputStream : public NPT_InputStream | |
139 { | |
140 public: | |
141 // NPT_InputStream methods | |
142 NPT_Result Seek(NPT_Position offset) { | |
143 return InputSeek(offset); | |
144 } | |
145 NPT_Result Tell(NPT_Position& offset) { | |
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 +---------------------------------------------------------------------*/ | |
164 class NPT_DelegatingOutputStream : public NPT_OutputStream | |
165 { | |
166 public: | |
167 // NPT_OutputStream methods | |
168 NPT_Result Seek(NPT_Position offset) { | |
169 return OutputSeek(offset); | |
170 } | |
171 NPT_Result Tell(NPT_Position& offset) { | |
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 +---------------------------------------------------------------------*/ | |
184 class NPT_MemoryStream : | |
185 public NPT_DelegatingInputStream, | |
186 public NPT_DelegatingOutputStream | |
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 virtual ~NPT_MemoryStream() {} | |
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); | |
201 NPT_Result GetSize(NPT_LargeSize& size) { | |
202 size = m_Buffer.GetDataSize(); | |
203 return NPT_SUCCESS; | |
204 } | |
205 NPT_Result GetAvailable(NPT_LargeSize& available) { | |
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); | |
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 SetSize(NPT_Size size); | |
223 | |
224 private: | |
225 // NPT_DelegatingInputStream methods | |
226 NPT_Result InputSeek(NPT_Position offset); | |
227 NPT_Result InputTell(NPT_Position& offset) { | |
228 offset = m_ReadOffset; | |
229 return NPT_SUCCESS; | |
230 } | |
231 | |
232 // NPT_DelegatingOutputStream methods | |
233 NPT_Result OutputSeek(NPT_Position offset); | |
234 NPT_Result OutputTell(NPT_Position& offset) { | |
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 | |
246 typedef NPT_Reference<NPT_MemoryStream> NPT_MemoryStreamReference; | |
247 | |
248 /*---------------------------------------------------------------------- | |
249 | NPT_StringOutputStream | |
250 +---------------------------------------------------------------------*/ | |
251 class NPT_StringOutputStream : public NPT_OutputStream | |
252 { | |
253 public: | |
254 // methods | |
255 NPT_StringOutputStream(NPT_Size size = 4096); | |
256 NPT_StringOutputStream(NPT_String* storage); | |
257 virtual ~NPT_StringOutputStream() ; | |
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); | |
264 | |
265 NPT_Result Seek(NPT_Position /*offset*/) { return NPT_ERROR_NOT_SUPPORTED; } | |
266 NPT_Result Tell(NPT_Position& offset) { offset = m_String->GetLength(); return NPT_SUCCESS; } | |
267 | |
268 protected: | |
269 NPT_String* m_String; | |
270 bool m_StringIsOwned; | |
271 }; | |
272 | |
273 typedef NPT_Reference<NPT_StringOutputStream> NPT_StringOutputStreamReference; | |
274 | |
275 #endif // _NPT_STREAMS_H_ |