MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
memorystream.cpp
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 _resourcememorystream_cpp
41 #define _resourcememorystream_cpp
42 
43 #include "Resource/memorystream.h"
44 #include "stringtool.h"
45 #include "exception.h"
46 
47 #include <cstring>
48 #include <algorithm>
49 
50 namespace Mezzanine
51 {
52  namespace Resource
53  {
54 #ifdef USENEWDATASTREAM
55  /// @todo Implement this
56 #else //USENEWDATASTREAM
57  MemoryStream::MemoryStream(const size_t& BufferSize, bool FreeOnClose, bool ReadOnly)
58  : DataStream( ReadOnly ? DataStream::SF_Read : static_cast<DataStream::StreamFlags>(DataStream::SF_Read | DataStream::SF_Write) ),
59  FreeBuffer(FreeOnClose)
60  {
61  Size = BufferSize;
62  BufferStart = new UInt8[BufferSize];
63  BufferPos = BufferStart;
64  BufferEnd = BufferStart + BufferSize;
65 
66  if(BufferEnd <= BufferStart)
67  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Using a zero or negative size buffer");
68  }
69 
70  MemoryStream::MemoryStream(void* Buffer, const size_t& BufferSize, bool FreeOnClose, bool ReadOnly)
71  : DataStream( ReadOnly ? DataStream::SF_Read : static_cast<DataStream::StreamFlags>(DataStream::SF_Read | DataStream::SF_Write) ),
72  FreeBuffer(FreeOnClose)
73  {
74  BufferStart = BufferPos = static_cast<UInt8*>(Buffer);
75  Size = BufferSize;
76  BufferEnd = BufferStart + BufferSize;
77 
78  if(BufferEnd <= BufferStart)
79  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Using a zero or negative size buffer");
80  }
81 
83  {
84  Close();
85  }
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Utility
89 
91  {
92  return BufferStart;
93  }
94 
96  {
97  return BufferPos;
98  }
99 
101  {
102  return BufferEnd;
103  }
104 
105  void MemoryStream::SetFreeOnClose(bool FreeOnClose)
106  {
107  FreeBuffer = FreeOnClose;
108  }
109 
110  ///////////////////////////////////////////////////////////////////////////////
111  // Stream Access and Manipulation
112 
113  size_t MemoryStream::Read(void* Buffer, const size_t& Count)
114  {
115  size_t RetCount = Count;
116  if(BufferPos + RetCount > BufferEnd)
117  RetCount = BufferEnd - BufferPos;
118  if(RetCount == 0)
119  return 0;
120 
121  //if(RetCount > Count);
122  // MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Cannot read passed end of stream");
123 
124  std::memcpy(Buffer,BufferPos,RetCount);
125  BufferPos += RetCount;
126  return RetCount;
127  }
128 
129  size_t MemoryStream::Write(const void* Buffer, const size_t& Count)
130  {
131  size_t Written = 0;
132  if(IsWriteable())
133  {
134  Written = Count;
135  if(BufferPos + Written > BufferEnd)
136  Written = BufferEnd - BufferPos;
137  if(Written == 0)
138  return 0;
139 
140  std::memcpy(BufferPos,Buffer,Written);
141  BufferPos += Written;
142  }
143  return Written;
144  }
145 
147  {
148  size_t NewPos = (size_t)( ( BufferPos - BufferStart ) + Count );
149  if( BufferStart + NewPos > BufferEnd )
150  NewPos = Size;
151 
152  BufferPos = BufferStart + NewPos;
153  }
154 
156  {
157  if( BufferStart + Position > BufferEnd || Position < 0 )
158  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Attempting to set position of stream to area outside the bounds of the buffer");
159 
160  BufferPos = BufferStart + Position;
161  }
162 
164  {
165  switch(Origin)
166  {
167  case SO_Beginning:
168  {
169  this->SetStreamPosition(Offset);
170  break;
171  }
172  case SO_Current:
173  {
174  if( GetStreamPosition() + Offset < 0 || GetStreamPosition() + Offset >= Size )
175  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Attempting to set position of stream to area outside the bounds of the buffer");
176 
177  BufferPos = BufferStart + (GetStreamPosition() + Offset);
178  break;
179  }
180  case SO_End:
181  {
182  if(Offset > 0 || Offset <= -Size)
183  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Attempting to set position of stream to area outside the bounds of the buffer");
184 
185  BufferPos = BufferStart + ((Size - 1) + Offset);
186  break;
187  }
188  }
189  }
190 
192  {
193  return BufferPos - BufferStart;
194  }
195 
196  bool MemoryStream::EoF() const
197  {
198  return BufferPos >= BufferEnd;
199  }
200 
202  {
203  if(FreeBuffer && BufferStart)
204  {
205  delete[] BufferStart;
206  BufferStart = NULL;
207  BufferPos = NULL;
208  BufferEnd = NULL;
209  }
210  }
211 
212  ///////////////////////////////////////////////////////////////////////////////
213  // Formatting Methods
214 
215  size_t MemoryStream::ReadLine(Char8* Buffer, size_t MaxCount, const String& Delim)
216  {
217  bool TrimCR = false;
218  if(Delim.find_first_of('\n') != String::npos)
219  {
220  TrimCR = true;
221  }
222 
223  size_t BytesRead = 0;
224  while(BytesRead < MaxCount && BufferPos < BufferEnd)
225  {
226  if(Delim.find(*BufferPos) != String::npos)
227  {
228  if(TrimCR && BytesRead && Buffer[BytesRead - 1] == '\r')
229  {
230  --BytesRead;
231  }
232 
233  ++BufferPos;
234  break;
235  }
236  Buffer[BytesRead++] = *BufferPos++;
237  }
238  Buffer[BytesRead] = '\0';
239  return BytesRead;
240  }
241 
242  size_t MemoryStream::SkipLine(const String& Delim)
243  {
244  size_t BytesSkipped = 0;
245  while(BufferPos < BufferEnd)
246  {
247  ++BytesSkipped;
248  if(Delim.find(*BufferPos++) != String::npos)
249  {
250  break;
251  }
252  }
253  return BytesSkipped;
254  }
255 #endif //USENEWDATASTREAM
256  }//Resource
257 }//Mezzanine
258 
259 #endif