nlib
BinaryReader.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_BINARYREADER_H_
17 #define INCLUDE_NN_NLIB_BINARYREADER_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 #include "nn/nlib/InputStream.h"
22 
23 NLIB_NAMESPACE_BEGIN
24 NLIB_EXPIMP_TEMPLATE(detail::MiniBufIn<512>);
25 
27  public:
28  enum EndianSetting { kEndianDefault = 0, kEndianLittle, kEndianBig };
29 
30  public:
31  BinaryReader() NLIB_NOEXCEPT : stream_(nullptr), errno_(-1), swap_endian_(false) {}
32  ~BinaryReader() NLIB_NOEXCEPT { this->Close(); }
33  errno_t Init(EndianSetting endian) NLIB_NOEXCEPT;
34  errno_t Init() NLIB_NOEXCEPT { return Init(kEndianDefault); }
35  errno_t Open(InputStream* stream) NLIB_NOEXCEPT;
36 
37 #define NLIB_BINR(tp) return this->Read_(reinterpret_cast<tp*>(x))
38 
39  bool Read(char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
40  bool Read(signed char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
41  bool Read(unsigned char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
42  bool Read(short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); }
43  bool Read(unsigned short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); }
44  bool Read(int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
45  bool Read(unsigned int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
46  bool Read(long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); }
47  bool Read(unsigned long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); }
48  bool Read(long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); }
49  bool Read(unsigned long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); }
50  bool Read(float* x) NLIB_NOEXCEPT { NLIB_BINR(float); }
51  bool Read(double* x) NLIB_NOEXCEPT { NLIB_BINR(double); }
52 
53  template<class T>
54  bool Read(T* x) NLIB_NOEXCEPT NLIB_NONNULL;
55 
56  int Peek() NLIB_NOEXCEPT { return mini_buf_.Peek(stream_); }
57  bool Skip(size_t n) NLIB_NOEXCEPT {
58  size_t rem = mini_buf_.size();
59  if (rem <= n) {
60  mini_buf_.Advance(n);
61  return true;
62  } else {
63  mini_buf_.Advance(rem);
64  n -= rem;
65  return this->Skip_(n);
66  }
67  }
68 #undef NLIB_BINR
69 
70  size_t ReadArray(unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
71  size_t ReadArray(unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
72  size_t ReadArray(unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
73  size_t ReadArray(unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
74  size_t ReadArray(unsigned long* x, size_t n) NLIB_NOEXCEPT {
75 #ifdef NLIB_LP64
76  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n);
77 #else
78  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
79 #endif
80  }
81 
82  size_t ReadArray(float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
83  size_t ReadArray(double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
84 
85  size_t ReadArray(signed char* x, size_t n) NLIB_NOEXCEPT {
86  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
87  }
88  size_t ReadArray(char* x, size_t n) NLIB_NOEXCEPT {
89  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
90  }
91  size_t ReadArray(short* x, size_t n) NLIB_NOEXCEPT {
92  return this->ReadArray(reinterpret_cast<unsigned short*>(x), n);
93  }
94  size_t ReadArray(int* x, size_t n) NLIB_NOEXCEPT {
95  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
96  }
97  size_t ReadArray(long* x, size_t n) NLIB_NOEXCEPT {
98 #ifdef NLIB_LP64
99  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n);
100 #else
101  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
102 #endif
103  }
104  size_t ReadArray(long long* x, size_t n) NLIB_NOEXCEPT {
105  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n);
106  }
107 
108  template<class T>
109  size_t ReadArray(T* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
110  template<class T, size_t N>
111  size_t ReadArray(T (&a)[N]) NLIB_NOEXCEPT;
113  // Do not stream_->Close(). only detach it from BinaryReader.
114  stream_ = nullptr;
115  return true;
116  }
118  if (errno_ == 0) errno_ = e;
119  }
120  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
121 
122  NLIB_SAFE_BOOL(BinaryReader, GetErrorValue() == 0)
123 
124  private:
125  bool Skip_(size_t n) NLIB_NOEXCEPT;
126  bool Read_(uint8_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
127  bool Read_(uint16_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
128  bool Read_(uint32_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
129  bool Read_(uint64_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
130  bool Read_(float* x) NLIB_NOEXCEPT {
131  union {
132  float f;
133  uint32_t i;
134  } v;
135  if (!this->Read_(&v.i)) return false;
136  *x = v.f;
137  return true;
138  }
139  bool Read_(double* x) NLIB_NOEXCEPT {
140  union {
141  double f;
142  uint64_t i;
143  } v;
144  if (!this->Read_(&v.i)) return false;
145  *x = v.f;
146  return true;
147  }
148 
149  private:
150  InputStream* stream_;
151  ErrnoT errno_;
152  bool swap_endian_;
153  detail::MiniBufIn<512> mini_buf_;
154 
155  NLIB_DISALLOW_COPY_AND_ASSIGN(BinaryReader);
156 };
157 
158 namespace binary_reader {
159 
160 template<class T>
162  return r->Read(x);
163 }
164 
165 template<class T>
166 NLIB_ALWAYS_INLINE bool ReadArray(BinaryReader* r, T* x, size_t n) {
167  return r->ReadArray(x, n);
168 }
169 } // namespace binary_reader
170 
171 template<class T>
173  return binary_reader::Read(this, x);
174 }
175 
176 template<class T>
178  return binary_reader::ReadArray(this, x, n);
179 }
180 
181 template<class T, size_t N>
183  return this->ReadArray(&a[0], N);
184 }
185 
186 NLIB_NAMESPACE_END
187 
188 #endif // INCLUDE_NN_NLIB_BINARYREADER_H_
bool Read(unsigned long long *x) noexcept
Reads unsigned long long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:49
bool Skip(size_t n) noexcept
Skips over n bytes when reading from stream.
Definition: BinaryReader.h:57
size_t ReadArray(unsigned char *x, size_t n) noexcept
Reads an unsigned char-type data string as binary from a stream.
bool Read(unsigned char *x) noexcept
Reads unsigned char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:41
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:95
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: BinaryReader.h:120
bool Read(signed char *x) noexcept
Reads signed char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:40
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:199
Defines the base class for input streams.
size_t ReadArray(char *x, size_t n) noexcept
Reads a char-type data string as binary from a stream.
Definition: BinaryReader.h:88
void SetError(errno_t e) noexcept
Sets an error. If an error has been set already, this function does not set an error.
Definition: BinaryReader.h:117
bool Read(int *x) noexcept
Reads int-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:44
bool Read(double *x) noexcept
Reads double-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:51
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:324
Writes in little-endian.
Definition: BinaryReader.h:28
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
BinaryReader() noexcept
Instantiates the object with default parameters (default constructor).
Definition: BinaryReader.h:31
bool Read(short *x) noexcept
Reads short-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:42
size_t ReadArray(unsigned long *x, size_t n) noexcept
Reads an unsigned long-type data string as binary from a stream.
Definition: BinaryReader.h:74
size_t ReadArray(int *x, size_t n) noexcept
Reads an int-type data string as binary from a stream.
Definition: BinaryReader.h:94
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:29
bool Read(unsigned int *x) noexcept
Reads unsigned int-type data as binary from a stream. Returns true if successful. ...
Definition: BinaryReader.h:45
bool Read(float *x) noexcept
Reads float-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:50
size_t ReadArray(long long *x, size_t n) noexcept
Reads a long long-type data string as binary from a stream.
Definition: BinaryReader.h:104
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
A file that contains the configuration information for each development environment.
bool Read(unsigned short *x) noexcept
Reads unsigned short-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:43
size_t ReadArray(short *x, size_t n) noexcept
Reads a short-type data string as binary from a stream.
Definition: BinaryReader.h:91
EndianSetting
Specifies the endianness.
Definition: BinaryReader.h:28
bool Close() noexcept
Closes the binary reader.
Definition: BinaryReader.h:112
size_t ReadArray(signed char *x, size_t n) noexcept
Reads a signed char-type data string as binary from a stream.
Definition: BinaryReader.h:85
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
int Peek() noexcept
References the first byte of the stream.
Definition: BinaryReader.h:56
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:161
The class for reading binary from streams (from InputStream).
Definition: BinaryReader.h:26
bool Read(unsigned long *x) noexcept
Reads unsigned long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:47
errno_t Init() noexcept
Initializes the binary reader using the same endianness as the machine the program is running on...
Definition: BinaryReader.h:34
bool Read(long *x) noexcept
Reads long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:46
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
~BinaryReader() noexcept
Destructor. This function does not close the stream.
Definition: BinaryReader.h:32
size_t ReadArray(long *x, size_t n) noexcept
Reads a long-type data string as binary from a stream.
Definition: BinaryReader.h:97
bool Read(long long *x) noexcept
Reads long long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:48
bool Read(char *x) noexcept
Reads char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:39
bool ReadArray(BinaryReader *r, T *x, size_t n)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:166
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37