MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
datastream.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _resourcedatastream_h
41 #define _resourcedatastream_h
42 
43 #include "datatypes.h"
44 #include "countedptr.h"
45 
46 #include <iostream>
47 
48 /// @file
49 /// @brief Declaration of DataStream
50 /// @todo Investigate how required these stream implementations are
51 
52 //#define USENEWDATASTREAM
53 namespace Mezzanine
54 {
55  namespace Resource
56  {
57 #ifdef USENEWDATASTREAM
58  /// @typedef StreamPos
59  /// @brief Convenience define for the stream position datatype.
60  typedef std::streampos StreamPos;
61  /// @typedef StreamOff
62  /// @brief Convenience define for the stream offset datatype.
63  typedef std::streamoff StreamOff;
64  /// @typedef StreamSize
65  /// @brief Convenience define for the stream size datatype.
66  typedef std::streamsize StreamSize;
67  ///////////////////////////////////////////////////////////////////////////////
68  /// @brief
69  /// @details
70  ///////////////////////////////////////
71  class StreamBase
72  {
73  public:
74  /// @enum StreamFlags
75  /// @brief This enum describes the flags that control certain behaviors of a stream.
76  /// @details It is important to note that not all of these flags are used by all streams.
77  enum StreamFlags
78  {
79  SF_None = 0, ///< Error/no special initialization.
80  SF_Read = std::ios_base::in, ///< Permit read operations on the stream.
81  SF_Write = std::ios_base::out, ///< Permit write operations on the stream.
82  SF_Append = std::ios_base::app, ///< All write operations on the stream are done at the end of the stream.
83  SF_AtEnd = std::ios_base::ate, ///< Moves the starting position of the stream to the end upon initialization.
84  SF_Binary = std::ios_base::binary, ///< Tell the stream that the file in question is Binary.
85  SF_Truncate = std::ios_base::trunc, ///< Clear the contents of the file when opening. Note that this will also create the file if it's not found.
86  };
87 
88  /// @enum SeekOrigin
89  /// @brief An enum describing which position should be considered the origin for changing the current position in a stream.
90  enum SeekOrigin
91  {
92  SO_Beginning = std::ios_base::beg, ///< The beginning of the stream.
93  SO_Current = std::ios_base::cur, ///< The current position for read/write operations in the stream.
94  SO_End = std::ios_base::end ///< The end of the stream.
95  };
96  public:
97  /// @brief Class constructor.
98  StreamBase() { }
99  /// @brief Class destructor.
100  virtual ~StreamBase() { }
101 
102  /// @brief Gets the size of the stream.
103  /// @return Returns the size of this stream in bytes.
104  StreamSize GetSize() const = 0;
105  };//StreamBase
106 
107  ///////////////////////////////////////////////////////////////////////////////
108  /// @brief
109  /// @details
110  ///////////////////////////////////////
111  class IStream : virtual public StreamBase
112  {
113  public:
114  /// @brief Class constructor.
115  IStream() { }
116  /// @brief Class destructor.
117  virtual ~IStream() { }
118 
119  /// @brief Reads from the stream and copies that data to a buffer.
120  /// @param Buffer The buffer to be populated with the read data.
121  /// @param Size The number of bytes to read from the stream.
122  /// @return Returns the number of bytes successfully read.
123  virtual size_t Read(Char8* Buffer, StreamSize Size) = 0;
124 
125  /// @brief Sets the position of the read cursor explicitly.
126  /// @param Position The position to be set.
127  virtual void SetReadPosition(StreamPos Position) = 0;
128  /// @brief Sets the position of the read cursor.
129  /// @param Offset The number of bytes to move the read cursor back(if negative) or forward(if positive).
130  /// @param Origin The starting point to be considered for the offset.
131  virtual void SetReadPosition(StreamOff Offset, SeekOrigin Origin) = 0;
132  /// @brief Gets the current read position in this stream.
133  /// @return Returns a StreamPos representing the current read position.
134  virtual StreamPos GetReadPosition() = 0;
135  };//IStream
136 
137  ///////////////////////////////////////////////////////////////////////////////
138  /// @brief
139  /// @details
140  ///////////////////////////////////////
141  class OStream : virtual public StreamBase
142  {
143  public:
144  /// @brief Class constructor.
145  OStream() { }
146  /// @brief Class destructor.
147  virtual ~OStream() { }
148 
149  /// @brief Writes data to the stream.
150  /// @param Buffer The memory buffer to write to this stream.
151  /// @param Size The size of the buffer being passed in.
152  /// @return Returns the number of bytes successfully written.
153  virtual size_t Write(const Char8* Buffer, StreamSize Size) = 0;
154 
155  /// @brief Sets the position of the write cursor explicitly.
156  /// @param Position The position to be set.
157  virtual void SetWritePosition(StreamPos Position) = 0;
158  /// @brief Sets the position of the write cursor.
159  /// @param Offset The number of bytes to move the write cursor back(if negative) or forward(if positive).
160  /// @param Origin The starting point to be considered for the offset.
161  virtual void SetWritePosition(StreamOff Offset, SeekOrigin Origin) = 0;
162  /// @brief Gets the current write position in this stream.
163  /// @return Returns a StreamPos representing the current write position.
164  virtual StreamPos GetWritePosition() = 0;
165  };//OStream
166 
167  ///////////////////////////////////////////////////////////////////////////////
168  /// @brief
169  /// @details
170  ///////////////////////////////////////
171  class IOStream : public IStream, public OStream
172  {
173  public:
174  /// @brief Class constructor.
175  IOStream() { }
176  /// @brief Class destructor.
177  virtual ~IOStream() { }
178 
179  /// @brief Advances the position in the stream.
180  /// @param Count The number of bytes to skip/advance in the stream from the current position.
181  virtual void Advance(const StreamOff Count);
182  /// @brief Sets the position of the read and write cursors explicitly.
183  /// @param Position The position to be set.
184  virtual void SetStreamPosition(StreamPos Position);
185  /// @brief Sets the position of the read and write cursors.
186  /// @param Offset The number of bytes to move the cursors back(if negative) or forward(if positive).
187  /// @param Origin The starting point to be considered for the offset.
188  virtual void SetStreamPosition(StreamOff Offset, SeekOrigin Origin);
189  /// @brief Gets the current position in this stream.
190  /// @param Read Whether or not to get the Read position. If false this will get the write position instead.
191  /// @return Returns a StreamPos representing the current position specified from the beginning of the stream.
192  virtual StreamPos GetStreamPosition(bool Read = true);
193  };//IOStream
194 
195  /// @typedef DataStream
196  /// @brief Convenience define for compatibility.
197  typedef IOStream DataStream;
198 #else //USENEWDATASTREAM
199  /// @typedef StreamPos
200  /// @brief Convenience define for the stream position datatype.
202  /// @typedef StreamOff
203  /// @brief Convenience define for the stream offset datatype.
205  /// @typedef StreamSize
206  /// @brief Convenience define for the stream size datatype.
208 
209  ///////////////////////////////////////////////////////////////////////////////
210  /// @brief This represents a stream to a piece of data, usually a file.
211  /// @details This is a base class that can be overriden to read from a variey of sources including
212  /// data in memory, and archive files.
213  ///////////////////////////////////////
215  {
216  public:
217  /// @enum StreamFlags
218  /// @brief This enum describes the flags that control certain behaviors of a stream.
219  /// @details It is important to note that not all of these flags are used by all streams.
221  {
222  SF_None = 0, ///< Error/no special initialization.
223  SF_Read = 1, ///< Permit read operations on the stream.
224  SF_Write = 2, ///< Permit write operations on the stream.
225  SF_Append = 4, ///< All write operations on the stream are done at the end of the stream.
226  SF_AtEnd = 8, ///< Moves the starting position of the stream to the end upon initialization.
227  SF_Binary = 16, ///< Tell the stream that the file in question is Binary.
228  SF_Truncate = 32 ///< Clear the contents of the file when opening. Note that this will also create the file if it's not found.
229  };
230 
231  /// @enum SeekOrigin
232  /// @brief An enum describing which position should be considered the origin for changing the current position in a stream.
234  {
235  SO_Beginning = std::ios_base::beg, ///< The beginning of the stream.
236  SO_Current = std::ios_base::cur, ///< The current position for read/write operations in the stream.
237  SO_End = std::ios_base::end ///< The end of the stream.
238  };
239  protected:
240  /// @brief The type of access this stream has to the resource.
242  /// @brief The size of the stream.
244  public:
245  /// @brief Class constructor.
246  /// @param Flags The flags to use when initializing the stream.
247  DataStream(const UInt16 Flags = SF_Read);
248  /// @brief Class destructor.
249  virtual ~DataStream();
250 
251  ///////////////////////////////////////////////////////////////////////////////
252  // Utility
253 
254  /// @brief Gets the size of the stream.
255  /// @return Returns the size of this stream in bytes.
256  virtual StreamSize GetSize() const;
257  /// @brief Gets whether this stream can be read.
258  /// @return Returns true if this stream is in reading mode, false otherwise.
259  virtual bool IsReadable() const;
260  /// @brief Gets whether this stream can be written to.
261  /// @return Returns true if this stream is in writing mode, false otherwise.
262  virtual bool IsWriteable() const;
263 
264  ///////////////////////////////////////////////////////////////////////////////
265  // Stream Access and Manipulation
266 
267  /// @brief Reads data from the stream, copying to a buffer.
268  /// @param Buffer The buffer to place data from the stream.
269  /// @param Count The number of bytes to read from the stream.
270  /// @return Returns the number of bytes read.
271  virtual size_t Read(void* Buffer, const size_t& Count) = 0;
272  /// @brief Writes data from the stream, copying from the provided buffer.
273  /// @param Buffer The buffer of data to be written.
274  /// @param Count The number of bytes to write from the buffer to the stream.
275  /// @return Returns the number of bytes written.
276  virtual size_t Write(const void* Buffer, const size_t& Count) = 0;
277 
278  /// @brief Advances the position in the stream.
279  /// @param Count The number of bytes to skip/advance in the stream from the current position.
280  virtual void Advance(const StreamOff Count) = 0;
281  /// @brief Sets the position of the read and write cursors explicitly.
282  /// @param Position The position to be set.
283  virtual void SetStreamPosition(StreamPos Position) = 0;
284  /// @brief Sets the position of the read and write cursors.
285  /// @param Offset The number of bytes to move the cursors back(if negative) or forward(if positive).
286  /// @param Origin The starting point to be considered for the offset.
287  virtual void SetStreamPosition(StreamOff Offset, SeekOrigin Origin) = 0;
288  /// @brief Gets the current position in this stream.
289  /// @param Read Whether or not to get the Read position. If false this will get the write position instead.
290  /// @return Returns a StreamPos representing the current position specified from the beginning of the stream.
291  virtual StreamPos GetStreamPosition(bool Read = true) = 0;
292  /// @brief Gets whether or not the current position is at the end of the file/stream.
293  /// @return Returns true if the current position has reached the end of the stream, false otherwise.
294  virtual bool EoF() const = 0;
295  /// @brief Closes the stream to the resource.
296  virtual void Close() = 0;
297 
298  ///////////////////////////////////////////////////////////////////////////////
299  // Formatting Methods
300 
301  /// @brief Gets the contents of the stream as a string.
302  /// @return Returns a string with the contents of the stream.
303  virtual String GetAsString();
304  /// @brief Reads a single line from a string.
305  /// @param Buffer Pointer to the buffer to copy to.
306  /// @param MaxCount The maximum number of bytes to read. Usually you want this to be your buffer size.
307  /// @param Delim The character that marks the end of a line.
308  /// @return Returns the number of bytes actually read, not including the Delimiter.
309  virtual size_t ReadLine(Char8* Buffer, size_t MaxCount, const String& Delim = "\n");
310  /// @brief Gets the contents of the current line in the stream.
311  /// @param Trim Whether or not to trim whitespaces on both sides of the string.
312  /// @return Returns a string containing characters from the current position in the stream to the end of the line.
313  virtual String GetLine(bool Trim = true);
314  /// @brief Moves the current position to the start of the next line.
315  /// @param Delim The character that marks the end of a line.
316  /// @return Returns the number of bytes skipped.
317  virtual size_t SkipLine(const String& Delim = "\n");
318  };//DataStream
319 #endif //USENEWDATASTREAM
320  /// @typedef DataStreamPtr
321  /// @brief This is a convenience type for a data stream in a counted pointer.
323  }//Resource
324 }//Mezzanine
325 
326 #endif