liblcf
reader_lcf.h
Go to the documentation of this file.
1 /*
2  * This file is part of liblcf. Copyright (c) 2020 liblcf authors.
3  * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4  *
5  * liblcf is Free/Libre Open Source Software, released under the MIT License.
6  * For the full copyright and license information, please view the COPYING
7  * file that was distributed with this source code.
8  */
9 
10 #ifndef LCF_READER_LCF_H
11 #define LCF_READER_LCF_H
12 
13 #include <string>
14 #include <vector>
15 #include <iosfwd>
16 #include <cstring>
17 #include <cassert>
18 #include <cstdint>
19 #include <cinttypes>
20 #include <memory>
21 #include "lcf_options.h"
22 #include "reader_util.h"
23 #include "encoder.h"
24 
25 /*
26  * Calls SkipDebug() instead of Skip() for debug builds.
27  */
28 #ifdef _DEBUG
29  #define Skip(x) SkipDebug(x, __FILE__)
30 #endif
31 
35 class LcfReader {
36 public:
43  LcfReader(std::istream& filestream, std::string encoding = "");
44 
48  ~LcfReader();
49 
55  static const std::string& GetError();
56 
64  static void SetError(const char* fmt, ...);
65 
70  struct Chunk {
71  Chunk() {
72  ID = 0;
73  length = 0;
74  }
75  uint32_t ID;
76  uint32_t length;
77  };
78 
82  enum SeekMode {
86  };
87 
96  size_t Read0(void *ptr, size_t size, size_t nmemb);
97 
105  void Read(void *ptr, size_t size, size_t nmemb);
106 
112  template <class T>
113  void Read(T& ref);
114 
121  template <class T>
122  void Read(std::vector<T> &buffer, size_t size);
123 
129  int ReadInt();
130 
138  void ReadString(std::string& ref, size_t size);
139 
145  bool IsOk() const;
146 
152  bool Eof() const;
153 
161  void Seek(size_t pos, SeekMode mode = FromStart);
162 
169  uint32_t Tell();
170 
176  int Peek();
177 
178 #ifdef _DEBUG
179 
187  void SkipDebug(const struct LcfReader::Chunk& chunk_info, const char* srcfile);
188 #else
189 
197  void Skip(const struct LcfReader::Chunk& chunk_info);
198 #endif
199 
206  void Encode(std::string& str);
207 
214  static int IntSize(unsigned int x);
215 
217  std::vector<int32_t>& IntBuffer();
218 
219 private:
221  std::istream& stream;
223  int64_t offset;
225  static std::string error_str;
229  std::vector<int32_t> buffer;
230 
236  static void SwapByteOrder(int16_t &us);
237 
243  static void SwapByteOrder(uint16_t &us);
244 
250  static void SwapByteOrder(int32_t &us);
251 
257  static void SwapByteOrder(uint32_t &ui);
258 
264  static void SwapByteOrder(double &d);
265 };
266 
267 inline std::vector<int32_t>& LcfReader::IntBuffer() {
268  return buffer;
269 }
270 
271 #endif
std::vector< int32_t > & IntBuffer()
Definition: reader_lcf.h:267
void Seek(size_t pos, SeekMode mode=FromStart)
Definition: reader_lcf.cpp:200
void Skip(const struct LcfReader::Chunk &chunk_info)
Definition: reader_lcf.cpp:273
static void SwapByteOrder(int16_t &us)
Definition: reader_lcf.cpp:338
std::vector< int32_t > buffer
Definition: reader_lcf.h:229
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
bool Eof() const
Definition: reader_lcf.cpp:196
uint32_t length
Definition: reader_lcf.h:76
Encoder encoder
Definition: reader_lcf.h:227
size_t Read0(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:30
LcfReader(std::istream &filestream, std::string encoding="")
Definition: reader_lcf.cpp:20
std::istream & stream
Definition: reader_lcf.h:221
int ReadInt()
Definition: reader_lcf.cpp:84
static const std::string & GetError()
Definition: reader_lcf.cpp:291
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:186
int64_t offset
Definition: reader_lcf.h:223
static int IntSize(unsigned int x)
Definition: reader_lcf.cpp:299
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:278
uint32_t Tell()
Definition: reader_lcf.cpp:228
bool IsOk() const
Definition: reader_lcf.cpp:192
static std::string error_str
Definition: reader_lcf.h:225
int Peek()
Definition: reader_lcf.cpp:239
void Encode(std::string &str)
Definition: reader_lcf.cpp:295