OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
binary.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "utils/binary.h"
10 
12 }
13 
15  uint8_t ret;
16  char* c = reinterpret_cast<char*>(&ret);
17  read(c, 1);
18  return ret;
19 }
20 
22  uint16_t a = readU8();
23  uint16_t b = readU8();
24  return (a | (b << 8));
25 }
26 
28  uint32_t a = readU16();
29  uint32_t b = readU16();
30  return (a | (b << 16));
31 }
32 
34  uint64_t a = readU32();
35  uint64_t b = readU32();
36  return (a | (b << 32));
37 }
38 
40  uint32_t val = readU32();
41  char* a = reinterpret_cast<char*>(&val);
42 
43  float ret;
44  char* b = reinterpret_cast<char*>(&ret);
45 
46  for (int i = 0; i < 4; i++)
47  b[i] = a[i];
48 
49  return ret;
50 }
51 
57 const static int bigendiandetection = 1;
58 #define ISBIGENDIAN() ((*reinterpret_cast<const char *>(&bigendiandetection)) == 0)
59 
60 static void swapByteOrder(char* d, unsigned int n) {
61  if (ISBIGENDIAN()) {
62  for (unsigned int i = 0; i < (n / 2); i++) {
63  char tmp = d[i];
64  d[i] = d[n - i - 1];
65  d[n - i - 1] = tmp;
66  }
67  }
68 }
69 
71  int8_t ret;
72  char* p = reinterpret_cast<char*>(&ret);
73  read(p, sizeof(ret));
74  return ret;
75 }
76 
78  int16_t ret;
79  char* p = reinterpret_cast<char*>(&ret);
80  read(p, sizeof(ret));
81  swapByteOrder(p, 2);
82  return ret;
83 }
84 
86  int32_t ret;
87  char* p = reinterpret_cast<char*>(&ret);
88  read(p, sizeof(ret));
89  swapByteOrder(p, 4);
90  return ret;
91 }
92 
94  int64_t ret;
95  char* p = reinterpret_cast<char*>(&ret);
96  read(p, sizeof(ret));
97  swapByteOrder(p, 8);
98  return ret;
99 }
100 
101 // ----------------------------------------------------------------------------
102 
103 BinaryFile::BinaryFile(std::string f) {
104  assertEqual(open(f), 0);
105 }
106 
108  if (file.is_open())
109  file.close();
110 }
111 
112 int BinaryFile::open(std::string f) {
113  if (file.is_open()) {
114  return 1;
115  } else {
116  if (f == "")
117  return 0;
118  file.open(f, std::ios_base::in | std::ios_base::binary);
119  return (file ? 0 : 1);
120  }
121 }
122 
123 long long BinaryFile::tell() {
124  assert(file.is_open());
125  return file.tellg();
126 }
127 
128 void BinaryFile::seek(long long pos) {
129  assert(file.is_open());
130  file.seekg(pos);
131 }
132 
134  file.peek();
135  return file.eof();
136 }
137 
138 void BinaryFile::read(char* d, int c) {
139  assert(file.is_open());
140  assert(file.good());
141  file.read(d, c);
142 }
143 
144 // ----------------------------------------------------------------------------
145 
146 BinaryMemory::BinaryMemory(const char* d, long long m) : data(nullptr), offset(0), max(-1) {
147  assertEqual(open(d, m), 0);
148 }
149 
151 }
152 
153 int BinaryMemory::open(const char* d, long long m) {
154  if (data != nullptr)
155  return 1;
156 
157  if (d != nullptr) {
158  data = d;
159  offset = 0;
160  max = m;
161  }
162 
163  return 0;
164 }
165 
166 long long BinaryMemory::tell() {
168  return offset;
169 }
170 
171 void BinaryMemory::seek(long long pos) {
172  assertGreaterThanEqual(pos, 0);
173  offset = pos;
174 }
175 
177  return (offset >= max);
178 }
179 
180 void BinaryMemory::read(char* d, int c) {
182  assertGreaterThan(c, 0);
184  for (int i = 0; i < c; i++) {
185  d[i] = data[offset + i];
186  }
187  offset += c;
188 }
189 
virtual void read(char *d, int c)=0
const char * data
Definition: binary.h:71
#define assertLessThanEqual(x, y)
Definition: global.h:154
virtual uint8_t readU8()
Definition: binary.cpp:14
virtual int8_t read8()
Definition: binary.cpp:70
virtual void seek(long long pos=0)
Definition: binary.cpp:171
virtual ~BinaryMemory()
Definition: binary.cpp:150
virtual ~BinaryReader()
Definition: binary.cpp:11
virtual bool eof()
Definition: binary.cpp:176
Binary file reading utilities.
virtual void read(char *d, int c)
Definition: binary.cpp:180
virtual long long tell()
Definition: binary.cpp:123
Included everywhere.
virtual void seek(long long pos=0)
Definition: binary.cpp:128
virtual int16_t read16()
Definition: binary.cpp:77
virtual ~BinaryFile()
Definition: binary.cpp:107
long long max
Definition: binary.h:73
std::ifstream file
Definition: binary.h:54
#define assert(x)
Definition: global.h:124
static void swapByteOrder(char *d, unsigned int n)
Definition: binary.cpp:60
BinaryMemory(const char *d=nullptr, long long max=-1)
Definition: binary.cpp:146
virtual float readFloat()
Definition: binary.cpp:39
#define ISBIGENDIAN()
Definition: binary.cpp:58
virtual uint16_t readU16()
Definition: binary.cpp:21
virtual int64_t read64()
Definition: binary.cpp:93
#define assertGreaterThanEqual(x, y)
Definition: global.h:170
#define assertEqual(x, y)
Definition: global.h:130
virtual uint32_t readU32()
Definition: binary.cpp:27
#define assertGreaterThan(x, y)
Definition: global.h:162
static const int bigendiandetection
Definition: binary.cpp:57
virtual int32_t read32()
Definition: binary.cpp:85
virtual bool eof()
Definition: binary.cpp:133
long long offset
Definition: binary.h:72
virtual long long tell()
Definition: binary.cpp:166
virtual uint64_t readU64()
Definition: binary.cpp:33
virtual void read(char *d, int c)
Definition: binary.cpp:138
int open(const char *d=nullptr, long long max=-1)
Definition: binary.cpp:153
int open(std::string f="")
Definition: binary.cpp:112
BinaryFile(std::string f="")
Definition: binary.cpp:103