JugiMap Framework
jmStreams.h
1 #ifndef JUGIMAP_STREAMS_H
2 #define JUGIMAP_STREAMS_H
3 
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 
8 #include "jmCommon.h"
9 
10 
11 
12 namespace jugimap{
13 
14 
15 
16 class SourceGraphics;
17 class Map;
18 class GraphicFile;
19 class GraphicItem;
20 class SourceSprite;
21 class VectorShape;
22 class FrameAnimation;
23 class SpriteLayer;
24 class VectorLayer;
25 class ComposedSprite;
26 class ObjectFactory;
27 
28 
29 
30 
34 {
35 public:
36 
38  virtual ~BinaryStreamReader() {}
39 
41  virtual bool IsOpen() = 0;
42 
44  virtual int Pos() = 0;
45 
47  virtual void Seek(int _pos) = 0;
48 
50  virtual int Size() = 0;
51 
53  virtual void Close() = 0;
54 
56  virtual unsigned char ReadByte() = 0;
57 
59  virtual int ReadInt() = 0;
60 
62  virtual float ReadFloat() = 0;
63 
65  virtual std::string ReadString() = 0;
66 
67 };
68 
69 
70 
74 {
75 public:
76 
80  StdBinaryFileStreamReader(const std::string &fileName);
81 
82 
83  bool IsOpen() override {return fs.is_open();}
84  int Pos() override {return fs.tellg();}
85  void Seek(int _pos) override {fs.seekg(_pos);}
86  int Size() override {return size;}
87  void Close() override {fs.close();}
88 
89 
90  unsigned char ReadByte() override
91  {
92  unsigned char value;
93  fs.read(reinterpret_cast<char*>(&value), 1);
94  return value;
95  }
96 
97  int ReadInt() override
98  {
99  int value;
100  fs.read(reinterpret_cast<char*>(&value), 4);
101  return value;
102  }
103 
104  float ReadFloat() override
105  {
106  float value;
107  fs.read(reinterpret_cast<char*>(&value), 4);
108  return value;
109  }
110 
111  std::string ReadString() override;
112 
113 
114 
115 
116 private:
117  std::ifstream fs;
118  int size;
119 };
120 
121 
122 
126 {
127 public:
128 
129 
134  BinaryBufferStreamReader(unsigned char *_buff, int _size, bool _ownedBuffer) : buff(_buff), size(_size), ownedBuffer(_ownedBuffer){}
135 
136 
137  ~BinaryBufferStreamReader() override
138  {
139  if(ownedBuffer && buff!=nullptr){
140  delete [] buff;
141  }
142  }
143 
144  bool IsOpen() override { return size > 0; }
145  int Pos() override {return pos;}
146  void Seek(int _pos) override { pos = _pos; }
147  int Size() override {return size;}
148  void Close() override {}
149  unsigned char* GetBuffer() {return buff;}
150 
151 
152  unsigned char ReadByte() override
153  {
154  unsigned char value = (buff+pos)[0];
155  pos += 1;
156  return value;
157  }
158 
159  int ReadInt() override
160  {
161  int value = reinterpret_cast<int*>(buff+pos)[0];
162  pos +=4;
163  return value;
164  }
165 
166  float ReadFloat() override
167  {
168  float value = reinterpret_cast<float*>(buff+pos)[0];
169  pos +=4;
170  return value;
171  }
172 
173 
174  std::string ReadString() override
175  {
176  int length = ReadInt();
177  std::string value(reinterpret_cast<char*>(buff+pos), length);
178  pos += length;
179  return value;
180  }
181 
182 protected:
183 
184  unsigned char* buff = nullptr;
185  int size = 0;
186  int pos = 0;
187  bool ownedBuffer = false;
188 
189 };
190 
191 
195 {
196 public:
197 
199  virtual ~TextStreamReader() {}
200 
202  virtual bool IsOpen() = 0;
203 
205  virtual void ReadLine(std::string &s) = 0;
206 
208  virtual bool Eof() = 0;
209 
211  virtual void Close() = 0;
212 
213 };
214 
215 
219 {
220 public:
221 
222  StdTextFileStreamReader(const std::string &fileName);
223 
224  virtual ~StdTextFileStreamReader() {}
225 
226 
227  virtual bool IsOpen() override {return fs.is_open();}
228 
229 
230  virtual void ReadLine(std::string &s) override { std::getline(fs, s); };
231 
232 
233  virtual bool Eof() override { return fs.eof(); }
234 
235 
236  virtual void Close() override { fs.close(); }
237 
238 
239 private:
240  std::ifstream fs;
241  int size;
242 };
243 
244 
245 
246 
247 }
248 
249 
250 
251 
252 
253 #endif
jugimap::StdBinaryFileStreamReader::Close
void Close() override
Close the stream.
Definition: jmStreams.h:87
jugimap::BinaryStreamReader::Close
virtual void Close()=0
Close the stream.
jugimap::BinaryBufferStreamReader
Extended BinaryStreamReader class which reads from a memory buffer.
Definition: jmStreams.h:125
jugimap::BinaryBufferStreamReader::BinaryBufferStreamReader
BinaryBufferStreamReader(unsigned char *_buff, int _size, bool _ownedBuffer)
Constructor.
Definition: jmStreams.h:134
jugimap::BinaryStreamReader::Size
virtual int Size()=0
Returns the size of the stream.
jugimap::TextStreamReader::Close
virtual void Close()=0
Close the stream.
jugimap::BinaryStreamReader::ReadFloat
virtual float ReadFloat()=0
Read and returns the float number (4 bytes).
jugimap::BinaryBufferStreamReader::Pos
int Pos() override
Returns the current reading position of the stream.
Definition: jmStreams.h:145
jugimap::StdTextFileStreamReader::Close
virtual void Close() override
Close the stream.
Definition: jmStreams.h:236
jugimap::BinaryBufferStreamReader::ReadString
std::string ReadString() override
Read and returns the string. The length of string is written as the integer at the start.
Definition: jmStreams.h:174
jugimap::StdBinaryFileStreamReader::IsOpen
bool IsOpen() override
Returns true if the stream of this object is open for reading; if not it returns false.
Definition: jmStreams.h:83
jugimap::BinaryStreamReader::IsOpen
virtual bool IsOpen()=0
Returns true if the stream of this object is open for reading; if not it returns false.
jugimap::TextStreamReader::Eof
virtual bool Eof()=0
Returns true if the end of file is reached; if not it returns false.
jugimap::TextStreamReader::ReadLine
virtual void ReadLine(std::string &s)=0
Returns true if the stream of this object is open for reading; if not it returns false.
jugimap::StdBinaryFileStreamReader::Pos
int Pos() override
Returns the current reading position of the stream.
Definition: jmStreams.h:84
jugimap::BinaryStreamReader::ReadInt
virtual int ReadInt()=0
Read and returns the integer number (4 bytes).
jugimap::BinaryBufferStreamReader::ReadInt
int ReadInt() override
Read and returns the integer number (4 bytes).
Definition: jmStreams.h:159
jugimap::StdTextFileStreamReader::IsOpen
virtual bool IsOpen() override
Returns true if the stream of this object is open for reading; if not it returns false.
Definition: jmStreams.h:227
jugimap::BinaryStreamReader
The base abstract class for reading binary streams.
Definition: jmStreams.h:33
jugimap::StdTextFileStreamReader::Eof
virtual bool Eof() override
Returns true if the end of file is reached; if not it returns false.
Definition: jmStreams.h:233
jugimap::TextStreamReader
The base abstract class for reading text streams.
Definition: jmStreams.h:194
jugimap::StdTextFileStreamReader
Extended TextStreamReader class which utilizes the standard library ifstream class for reading.
Definition: jmStreams.h:218
jugimap::StdTextFileStreamReader::ReadLine
virtual void ReadLine(std::string &s) override
Returns true if the stream of this object is open for reading; if not it returns false.
Definition: jmStreams.h:230
jugimap::StdBinaryFileStreamReader::StdBinaryFileStreamReader
StdBinaryFileStreamReader(const std::string &fileName)
Constructor.
Definition: jmStreams.cpp:24
jugimap::BinaryStreamReader::Pos
virtual int Pos()=0
Returns the current reading position of the stream.
jugimap::StdBinaryFileStreamReader
Extended BinaryStreamReader class which utilizes the standard library ifstream class for reading.
Definition: jmStreams.h:73
jugimap::TextStreamReader::IsOpen
virtual bool IsOpen()=0
Returns true if the stream of this object is open for reading; if not it returns false.
jugimap::StdBinaryFileStreamReader::Seek
void Seek(int _pos) override
Set the current reading position of the stream.
Definition: jmStreams.h:85
jugimap::StdBinaryFileStreamReader::ReadByte
unsigned char ReadByte() override
Read and returns the byte number.
Definition: jmStreams.h:90
jugimap::BinaryBufferStreamReader::ReadFloat
float ReadFloat() override
Read and returns the float number (4 bytes).
Definition: jmStreams.h:166
jugimap::StdBinaryFileStreamReader::ReadString
std::string ReadString() override
Read and returns the string. The length of string is written as the integer at the start.
Definition: jmStreams.cpp:37
jugimap::BinaryBufferStreamReader::IsOpen
bool IsOpen() override
Returns true if the stream of this object is open for reading; if not it returns false.
Definition: jmStreams.h:144
jugimap::BinaryStreamReader::~BinaryStreamReader
virtual ~BinaryStreamReader()
Destructor.
Definition: jmStreams.h:38
jugimap::BinaryBufferStreamReader::ReadByte
unsigned char ReadByte() override
Read and returns the byte number.
Definition: jmStreams.h:152
jugimap::BinaryBufferStreamReader::Close
void Close() override
Close the stream.
Definition: jmStreams.h:148
jugimap::BinaryStreamReader::Seek
virtual void Seek(int _pos)=0
Set the current reading position of the stream.
jugimap::BinaryBufferStreamReader::Size
int Size() override
Returns the size of the stream.
Definition: jmStreams.h:147
jugimap::StdBinaryFileStreamReader::Size
int Size() override
Returns the size of the stream.
Definition: jmStreams.h:86
jugimap::StdBinaryFileStreamReader::ReadInt
int ReadInt() override
Read and returns the integer number (4 bytes).
Definition: jmStreams.h:97
jugimap::StdBinaryFileStreamReader::ReadFloat
float ReadFloat() override
Read and returns the float number (4 bytes).
Definition: jmStreams.h:104
jugimap::BinaryStreamReader::ReadString
virtual std::string ReadString()=0
Read and returns the string. The length of string is written as the integer at the start.
jugimap::TextStreamReader::~TextStreamReader
virtual ~TextStreamReader()
Destructor.
Definition: jmStreams.h:199
jugimap::BinaryStreamReader::ReadByte
virtual unsigned char ReadByte()=0
Read and returns the byte number.
jugimap::BinaryBufferStreamReader::Seek
void Seek(int _pos) override
Set the current reading position of the stream.
Definition: jmStreams.h:146