1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#include <rez/io.h>
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
int rez::io::read(unsigned char &b)
{
std::ifstream::pos_type off = f.tellg();
f.read((char *)&b, sizeof b);
if (!f || f.eof())
{
std::cerr << m_path << ": failed to read at offset " << off
<< ", eof=" << f.eof() << std::endl;
return -1;
}
return 0;
}
int rez::io::check(char byte)
{
char b;
std::ifstream::pos_type off = f.tellg();
f.read(&b, sizeof b);
if (!f || f.eof())
{
std::cerr << m_path << ": failed to read at offset " << off
<< ", eof=" << f.eof() << std::endl;
return -1;
}
else if (b != byte)
{
std::cerr << m_path << ": expected " << byte << " at offset: " << off
<< ", got " << b << std::endl;
return -1;
}
return 0;
}
int rez::io::check(const char *s)
{
while (*s)
if (check(*s++))
return -1;
return 0;
}
int rez::io::read_le(uint32_t &w)
{
std::ifstream::pos_type off = f.tellg();
unsigned char buf[sizeof w];
f.read((char *)buf, sizeof buf);
if (!f || f.eof())
{
std::cerr << m_path << ": failed to read word at offset " << off
<< ", eof=" << f.eof() << std::endl;
return -1;
}
w = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
return 0;
}
int rez::io::read(std::string &s, unsigned maxlen)
{
unsigned i = 0;
while (i++ < maxlen)
{
unsigned char b;
if (read(b))
return -1;
else if (!b)
return 0;
s += b;
}
std::cerr << m_path << ": exceeded maximum string length (" << maxlen
<< "): \"" << s << "\"" << std::endl;
return -1;
}
int rez::io::read(void *b, size_t n)
{
std::ifstream::pos_type off = f.tellg();
f.read((char *)(b), n);
if (!f)
{
std::cerr << m_path << ": failed to read " << n << " bytes at offset "
<< off << ", eof=" << f.eof() << std::endl;
return -1;
}
return 0;
}
long rez::io::tell()
{
std::ifstream::pos_type offset = f.tellg();
if (!f)
{
std::cerr << m_path << ": failed to retrieve offset" << std::endl;
return -1;
}
return offset;
}
int rez::io::seek(long offset)
{
long cur = f.tellg();
if (!f)
{
std::cerr << m_path << ": failed to tell offset " << std::endl;
return -1;
}
else if (cur != offset)
{
f.seekg(static_cast<std::ifstream::pos_type>(offset));
if (!f)
{
std::cerr << m_path << ": failed to seek to offset " << offset
<< std::endl;
return -1;
}
}
return 0;
}
int rez::io::open()
{
if (!f.is_open())
{
const char *error;
#ifdef _WIN32
error = GetLastError();
#else
error = strerror(errno);
#endif
std::cerr << "Failed to open " << m_path << ": " << error << std::endl;
return -1;
}
return 0;
}
const std::string &rez::io::path() const
{
return m_path;
}
rez::io::io(const char *path) :
f(path, std::ios::binary),
m_path(path)
{
}
|