liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
reader_lcf.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 liblcf authors
3  * This file is released under the MIT License
4  * http://opensource.org/licenses/MIT
5  */
6 
7 #ifndef LCF_READER_LCF_H
8 #define LCF_READER_LCF_H
9 
10 #include <string>
11 #include <vector>
12 #include <iosfwd>
13 #include <cstring>
14 #include <cstdio>
15 #include <cassert>
16 #include "reader_types.h"
17 #include "reader_options.h"
18 #include "reader_util.h"
19 
20 /*
21  * Calls SkipDebug() instead of Skip() for debug builds.
22  */
23 #ifdef _DEBUG
24  #define Skip(x) SkipDebug(x, __FILE__)
25 #endif
26 
30 class LcfReader {
31 public:
38  LcfReader(const char* filename, std::string encoding = "");
39 
46  LcfReader(const std::string& filename, std::string encoding = "");
47 
51  ~LcfReader();
52 
56  void Close();
57 
63  static const std::string& GetError();
64 
72  static void SetError(const char* fmt, ...);
73 
78  struct Chunk {
79  Chunk() {
80  ID = 0;
81  length = 0;
82  }
83  uint32_t ID;
84  uint32_t length;
85  };
86 
90  enum SeekMode {
94  };
95 
104  size_t Read0(void *ptr, size_t size, size_t nmemb);
105 
113  void Read(void *ptr, size_t size, size_t nmemb);
114 
120  template <class T>
121  void Read(T& ref);
122 
129  template <class T>
130  void Read(std::vector<T> &buffer, size_t size);
131 
137  int ReadInt();
138 
146  void ReadString(std::string& ref, size_t size);
147 
153  bool IsOk() const;
154 
160  bool Eof() const;
161 
169  void Seek(size_t pos, SeekMode mode = FromStart);
170 
177  uint32_t Tell();
178 
186  bool Ungetch(uint8_t ch);
187 
188 #ifdef _DEBUG
189 
197  void SkipDebug(const struct LcfReader::Chunk& chunk_info, const char* srcfile);
198 #else
199 
207  void Skip(const struct LcfReader::Chunk& chunk_info);
208 #endif
209 
217  std::string Encode(const std::string& str_to_encode);
218 
225  static int IntSize(unsigned int x);
226 
227 private:
229  std::string filename;
231  std::string encoding;
233  FILE* stream;
235  static std::string error_str;
236 
242  static void SwapByteOrder(int16_t &us);
243 
249  static void SwapByteOrder(uint16_t &us);
250 
256  static void SwapByteOrder(uint32_t &ui);
257 
263  static void SwapByteOrder(double &d);
264 };
265 
266 #endif
void Seek(size_t pos, SeekMode mode=FromStart)
Definition: reader_lcf.cpp:178
void Skip(const struct LcfReader::Chunk &chunk_info)
Definition: reader_lcf.cpp:229
static void SwapByteOrder(int16_t &us)
Definition: reader_lcf.cpp:294
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:52
FILE * stream
Definition: reader_lcf.h:233
uint32_t length
Definition: reader_lcf.h:84
LcfReader(const char *filename, std::string encoding="")
Definition: reader_lcf.cpp:18
bool IsOk() const
Definition: reader_lcf.cpp:170
size_t Read0(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:42
std::string Encode(const std::string &str_to_encode)
Definition: reader_lcf.cpp:251
void Close()
Definition: reader_lcf.cpp:36
int ReadInt()
Definition: reader_lcf.cpp:82
static const std::string & GetError()
Definition: reader_lcf.cpp:247
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
std::string filename
Definition: reader_lcf.h:229
bool Ungetch(uint8_t ch)
Definition: reader_lcf.cpp:198
static int IntSize(unsigned int x)
Definition: reader_lcf.cpp:255
bool Eof() const
Definition: reader_lcf.cpp:174
std::string encoding
Definition: reader_lcf.h:231
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:234
uint32_t Tell()
Definition: reader_lcf.cpp:194
static std::string error_str
Definition: reader_lcf.h:235