Browse Source

Implemented binary file reader

Thomas Buck 9 years ago
parent
commit
a42f966526
3 changed files with 111 additions and 35 deletions
  1. 4
    0
      ChangeLog.md
  2. 21
    12
      include/utils/binary.h
  3. 86
    23
      src/utils/binary.cpp

+ 4
- 0
ChangeLog.md View File

@@ -2,6 +2,10 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140728 ]
6
+    * Implemented binary file reading utility in preparation of
7
+      TombRaider.cpp/h rewrite
8
+
5 9
     [ 20140714 ]
6 10
     * Fixed a bug where std::sort did not use the objects operator<
7 11
       method, but instead sorted based on the objects address in memory

+ 21
- 12
include/utils/binary.h View File

@@ -8,23 +8,32 @@
8 8
 #ifndef _UTILS_BINARY_H_
9 9
 #define _UTILS_BINARY_H_
10 10
 
11
-void binOpen(const char *file);
12
-void binClose();
11
+#include <fstream>
13 12
 
14
-unsigned long binTell();
15
-void binSeek(unsigned long pos);
13
+class BinaryFile {
14
+public:
16 15
 
17
-int8_t binRead8();
18
-uint8_t binReadU8();
16
+    BinaryFile(const char *f);
17
+    ~BinaryFile();
19 18
 
20
-int16_t binRead16();
21
-uint16_t binReadU16();
19
+    long long tell();
20
+    void seek(long long pos);
22 21
 
23
-int32_t binRead32();
24
-uint32_t binReadU32();
22
+    int8_t read8();
23
+    uint8_t readU8();
25 24
 
26
-int64_t binRead64();
27
-uint64_t binReadU64();
25
+    int16_t read16();
26
+    uint16_t readU16();
27
+
28
+    int32_t read32();
29
+    uint32_t readU32();
30
+
31
+    int64_t read64();
32
+    uint64_t readU64();
33
+
34
+private:
35
+    std::ifstream file;
36
+};
28 37
 
29 38
 #endif
30 39
 

+ 86
- 23
src/utils/binary.cpp View File

@@ -8,51 +8,114 @@
8 8
 #include "global.h"
9 9
 #include "utils/binary.h"
10 10
 
11
-void binOpen(const char *file) {
11
+/*! \fixme Left-Shift with signed integer is undefined!
12
+ *  Is there a portable way to read multi-byte signed integers?
13
+ *  Without having to detect the endianness at run-time?
14
+ */
15
+const int bigendiandetection = 1;
16
+#define ISBIGENDIAN() ((*(char *)&bigendiandetection) == 0)
12 17
 
18
+BinaryFile::BinaryFile(const char *f) {
19
+    file.open(f, std::ios_base::in | std::ios_base:: binary);
13 20
 }
14 21
 
15
-void binClose() {
16
-
22
+BinaryFile::~BinaryFile() {
23
+    file.close();
17 24
 }
18 25
 
19
-unsigned long binTell() {
20
-    return 0;
26
+long long BinaryFile::tell() {
27
+    return file.tellg();
21 28
 }
22 29
 
23
-void binSeek(unsigned long pos) {
24
-
30
+void BinaryFile::seek(long long pos) {
31
+    assert(pos >= 0);
32
+    file.seekg(pos);
25 33
 }
26 34
 
27
-int8_t binRead8() {
28
-    return 0;
35
+int8_t BinaryFile::read8() {
36
+    int8_t ret;
37
+    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
38
+    return ret;
29 39
 }
30 40
 
31
-uint8_t binReadU8() {
32
-    return 0;
41
+uint8_t BinaryFile::readU8() {
42
+    uint8_t ret;
43
+    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
44
+    return ret;
33 45
 }
34 46
 
35
-int16_t binRead16() {
36
-    return 0;
47
+int16_t BinaryFile::read16() {
48
+    int16_t ret;
49
+    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
50
+    if (ISBIGENDIAN()) {
51
+        char *p = reinterpret_cast<char *>(&ret);
52
+        char tmp = p[0];
53
+        p[0] = p[1];
54
+        p[1] = tmp;
55
+    }
56
+    return ret;
37 57
 }
38 58
 
39
-uint16_t binReadU16() {
40
-    return 0;
59
+uint16_t BinaryFile::readU16() {
60
+    uint16_t a = readU8();
61
+    uint16_t b = readU8();
62
+    return (uint16_t)(a | (b << 8));
41 63
 }
42 64
 
43
-int32_t binRead32() {
44
-    return 0;
65
+int32_t BinaryFile::read32() {
66
+    int32_t ret;
67
+    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
68
+    if (ISBIGENDIAN()) {
69
+        char *p = reinterpret_cast<char *>(&ret);
70
+        char tmp = p[0];
71
+        p[0] = p[3];
72
+        p[3] = tmp;
73
+        tmp = p[1];
74
+        p[1] = p[2];
75
+        p[2] = tmp;
76
+    }
77
+    return ret;
45 78
 }
46 79
 
47
-uint32_t binReadU32() {
48
-    return 0;
80
+uint32_t BinaryFile::readU32() {
81
+    uint32_t a = readU8();
82
+    uint32_t b = readU8();
83
+    uint32_t c = readU8();
84
+    uint32_t d = readU8();
85
+    return (uint32_t)(a | (b << 8) | (c << 16) | (d << 24));
49 86
 }
50 87
 
51
-int64_t binRead64() {
52
-    return 0;
88
+int64_t BinaryFile::read64() {
89
+    int64_t ret;
90
+    file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
91
+    if (ISBIGENDIAN()) {
92
+        char *p = reinterpret_cast<char *>(&ret);
93
+        char tmp = p[0];
94
+        p[0] = p[7];
95
+        p[7] = tmp;
96
+        tmp = p[1];
97
+        p[1] = p[6];
98
+        p[6] = tmp;
99
+        tmp = p[2];
100
+        p[2] = p[5];
101
+        p[5] = tmp;
102
+        tmp = p[3];
103
+        p[3] = p[4];
104
+        p[4] = tmp;
105
+    }
106
+    return ret;
53 107
 }
54 108
 
55
-uint64_t binReadU64() {
56
-    return 0;
109
+uint64_t BinaryFile::readU64() {
110
+    uint64_t a = readU8();
111
+    uint64_t b = readU8();
112
+    uint64_t c = readU8();
113
+    uint64_t d = readU8();
114
+    uint64_t e = readU8();
115
+    uint64_t f = readU8();
116
+    uint64_t g = readU8();
117
+    uint64_t h = readU8();
118
+    return (uint64_t)(a | (b << 8) | (c << 16) | (d << 24)
119
+            | (e << 32) | (f << 40) | (g << 48) | (h << 56));
57 120
 }
58 121
 

Loading…
Cancel
Save