-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathact_binfile.h
More file actions
216 lines (184 loc) · 4.21 KB
/
act_binfile.h
File metadata and controls
216 lines (184 loc) · 4.21 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma once
#include "act_file.h"
#include "act_utils.h"
#include <vector>
#include <cinttypes>
#include <string>
#include <memory>
namespace Act
{
class BinaryInputFile final : public AbstractInputFile
{
public:
BinaryInputFile(std::vector<std::uint8_t>& data) :
data(std::move(data)),
pos(0)
{
}
BinaryInputFile(std::string filename) :
BinaryInputFile(OpenFile(filename))
{
}
private:
std::vector<std::uint8_t> ReadBytes(std::size_t count)
{
std::vector<std::uint8_t> ret(data.begin() + pos, data.begin() + pos + count);
pos += count;
return ret;
}
template <std::size_t Size>
ByteBlock<Size> ReadBytes()
{
ByteBlock<Size> ret;
ret.Fill(data.begin() + pos);
pos += Size;
return ret;
}
void ReadAny()
{
if (block_rest) --block_rest;
}
public:
virtual std::int32_t ReadInt32(std::string& name = std::string()) override
{
ReadAny();
return ReadBytes<4>().GetValue<std::int32_t>();
}
virtual std::int16_t ReadInt16(std::string& name = std::string()) override
{
ReadAny();
return ReadBytes<2>().GetValue<std::int16_t>();
}
virtual bool ReadBool(std::string& name = std::string()) override
{
ReadAny();
return ReadBytes<1>().GetValue<bool>();
}
virtual std::string ReadString(std::string& name = std::string()) override
{
ReadAny();
int size = ReadBytes<4>().GetValue<std::int32_t>();
std::vector<std::uint8_t> data = ReadBytes(size);
char* raw = (char*) data.data();
return std::string(raw, size);
}
virtual float ReadFloat(std::string& name = std::string()) override
{
ReadAny();
return ReadBytes<4>().GetValue<float>();
}
virtual void ReadObject(std::string& name, Act::Reader r) override
{
ReadAny();
auto last_rest = block_rest;
block_rest = block_next_size;
block_next_size = 0;
r();
block_rest = last_rest;
}
virtual void ReadArray(std::string& name, Reader r) override
{
ReadAny();
auto last_rest = block_rest;
block_rest = block_next_size;
block_next_size = 0;
r();
block_rest = last_rest;
}
virtual bool EndOfBlock() override
{
//Task::SendError("try to get structure on binary file");
//return false;
return block_rest == 0;
}
virtual void SetBlockSize(std::int32_t size) override
{
block_next_size = size;
}
virtual void SkipBytes(size_t size) override
{
pos += size;
}
virtual bool StrictMode() override
{
return true;
}
private:
std::vector<std::uint8_t> data;
std::size_t pos;
std::int32_t block_rest = 0;
std::int32_t block_next_size = 0;
};
struct BinaryInputStream
{
template <typename... T>
static AbstractInputStream Create(T&&... args)
{
return std::make_shared<BinaryInputFile>(std::forward<T>(args)...);
}
};
class BinaryOutputFile final : public AbstractOutputFile
{
public:
virtual void WriteInt32(std::string name, std::int32_t val) override
{
WriteBytes(&val, 4);
}
virtual void WriteInt16(std::string name, std::int16_t val) override
{
WriteBytes(&val, 2);
}
virtual void WriteFloat(std::string name, float val) override
{
WriteBytes(&val, 4);
}
virtual void WriteBool(std::string name, bool val) override
{
char data = val ? 1 : 0;
WriteBytes(&data, 1);
}
virtual void WriteString(std::string name, std::string val) override
{
WriteInt32("len", val.size());
WriteBytes(val.c_str(), val.size());
}
virtual void WriteObject(std::string name, Writer writer) override
{
writer();
}
virtual void WriteArray(std::string name, Writer writer) override
{
writer();
}
virtual void WriteNextPosition() override
{
WriteInt32("pos", data.size() + 4);
}
virtual bool StrictMode() override
{
return true;
}
virtual void Save(std::string filename) override
{
FILE* file_out = fopen(filename.c_str(), "wb");
fwrite(data.data(), 1, data.size(), file_out);
fclose(file_out);
}
private:
void WriteBytes(const void* p, int size)
{
int pos = data.size();
data.resize(pos + size);
memcpy(data.data() + pos, p, size);
}
private:
std::vector<std::uint8_t> data;
};
struct BinaryOutputStream
{
static AbstractOutputStream Create()
{
return AbstractOutputStream(new BinaryOutputFile());
}
};
}