Iobuffer.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 /* Input / OutputBuffer classes:
34  * A complete and concise solution for binary data with data streams.
35  *
36 
37  */
38 
39 #ifndef H_IOBUFFER_INCLUDED
40 #define H_IOBUFFER_INCLUDED
41 
42 #include <string>
43 #include <cstring>
44 #include <vector>
45 
46 
47 
48 namespace Dynacoe {
62 class InputBuffer {
63 
64 
65  public:
66 
67  InputBuffer();
68  ~InputBuffer();
69 
72  void OpenBuffer(const std::vector<uint8_t> & buffer);
73 
77  void Open(const std::string & file);
78 
81  std::string ReadString(unsigned int nBytes);
82 
87  std::vector<uint8_t> ReadBytes(uint32_t numBytes);
88 
94  template<typename T>
95  T Read();
96 
102  template<typename T>
103  void Read(T &);
104 
111  void GoToByte(int nBytes);
112 
113 
116  int Size();
117 
120  int BytesLeft();
121 
124  bool ReachedEnd();
125  private:
126  uint8_t * buffer;
127  int bufferPos;
128  long size;
129  void * readN(int num);
130  void readN(void *, uint32_t);
131  uint8_t * immediateBuffer;
132 
133 };
134 
149  public:
150 
151 
152  OutputBuffer();
153  ~OutputBuffer();
154 
157  bool CommitToFile(std::string fileName, bool append);
158 
164  template<typename T>
165  void Write(const T &);
166  void WriteBytes(const std::vector<uint8_t> & data);
167  void WriteString(std::string str);
169 
172  void GoToByte(int n);
173 
176  std::vector<uint8_t> GetData();
177 
180  int Size();
181 
184  void Clear();
185 
186  private:
187  uint8_t * buffer;
188  int bufferPos;
189  long size;
190  long operationalSize;
191 
192  void resize();
193 
194 
195 
196 
197 };
198 }
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 template <typename T>
209 void Dynacoe::OutputBuffer::Write(const T & obj) {
210  uint8_t * block = (uint8_t *) &obj;
211  std::vector<uint8_t> data(block, block+sizeof(T));
212  WriteBytes(data);
213 }
214 
215 
216 template <typename T>
218  T object;
219  std::vector<uint8_t> data = ReadBytes(sizeof(T));
220  if (!data.size()) return T();
221  memcpy(&object, &data[0], sizeof(object));
222  return object;
223 }
224 
225 
226 template <typename T>
228  readN(&o, sizeof(o));
229 }
230 #endif
std::vector< uint8_t > ReadBytes(uint32_t numBytes)
Returns the next n bytes.
int Size()
returns the size of the buffer in bytes.
int Size()
Returns the size of the queued data in bytes.
void GoToByte(int nBytes)
Set the buffer position byte.
Definition: AssetID.h:37
T Read()
Reads the next sizeof bytes adn returns a T object with its state set to the read contents...
Definition: Iobuffer.h:217
Binary file reading utility.
Definition: Iobuffer.h:62
int BytesLeft()
Returns the number of bytes left in the buffer.
void Open(const std::string &file)
Opens a file to read from.
std::vector< uint8_t > GetData()
Returns a copy of the queued data.
void OpenBuffer(const std::vector< uint8_t > &buffer)
Opens a raw buffer to read from.
bool ReachedEnd()
Returns whether or not all data has been read.
Binary file writing utility.
Definition: Iobuffer.h:148
void Clear()
Resets the queued data buffer.
void GoToByte(int n)
Set the buffer position to the specifified byte.
std::string ReadString(unsigned int nBytes)
Returns the next nBytes as a string.
bool CommitToFile(std::string fileName, bool append)
Stores the written data to the specified file.