-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaknode.cpp
More file actions
164 lines (138 loc) · 3.56 KB
/
paknode.cpp
File metadata and controls
164 lines (138 loc) · 3.56 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
#include "stdafx.h"
#include "paknode.h"
const char *SIMU_SIGNATURE = "Simutrans object file";
const unsigned short LARGE_RECORD_SIZE = 0xFFFFu;
template<class T> inline void readBin(std::istream &s, T&v)
{
s.read(pointer_cast<char*>(&v), sizeof(T));
if (s.fail()) throw std::runtime_error("read error");
}
template<class T> inline T readInt(std::istream &s)
{
T v;
readBin(s, v);
return v;
}
std::string readStrLen(std::istream &s, int len)
{
std::string x;
for (; len > 0; len--)
{
int ch = s.get();
if (ch == '\0')
{
len--;
for (; len > 0; len--) s.get();
break;
}
x += (char)ch;
}
return x;
}
template<class T> inline void writeInt(std::ostream &o, T v)
{
o.write(pointer_cast<const char*>(&v), sizeof(T));
}
void writeStrLen(std::ostream &o, const std::string& str, int len)
{
o.write(str.data(), std::min(static_cast<int>(str.length()), len));
for (len -= str.length(); len > 0; len--) o.put('\0');
}
/* PakNode */
PakNode* PakNode::clone()
{
PakNode *o = new PakNode();
o->m_data = m_data;
o->m_type = m_type;
for (iterator it = begin(); it != end(); it++)
o->m_items.push_back((*it)->clone());
return o;
}
void PakNode::load(std::istream &src)
{
clear();
int count, size;
m_type = readStrLen(src, NODE_NAME_LENGTH);
count = readInt<unsigned short>(src);
size = readInt<unsigned short>(src);
if (size == LARGE_RECORD_SIZE)
size = readInt<unsigned int>(src);
loadData(src, size);
for (int i = 0; i < count; i++)
{
PakNode* node = new PakNode();
m_items.push_back(node);
node->load(src);
}
}
void PakNode::save(std::ostream &dest) const
{
int count = m_items.size();
int size = 0;
writeStrLen(dest, m_type, NODE_NAME_LENGTH);
dest.write(pointer_cast<char*>(&count), 2);
size = m_data.size();
if (size < LARGE_RECORD_SIZE)
{
writeInt<unsigned short>(dest, size);
}
else
{
writeInt<unsigned short>(dest, LARGE_RECORD_SIZE);
writeInt<unsigned int>(dest, size);
}
if (size) dest.write(pointer_cast<const char*>(dataP()), size);
for (const_iterator it = begin(); it != end(); it++) (*it)->save(dest);
}
void PakNode::clear()
{
for (iterator it = begin(); it != end(); it++) delete *it;
m_items.clear();
m_data.clear();
}
PakNode::~PakNode()
{
clear();
}
void PakNode::loadData(std::istream &src, int size)
{
m_data.resize(size);
if (size)
src.read(reinterpret_cast<char*>(&m_data[0]), size);
}
/* PakFile */
void PakFile::load(std::istream &src)
{
m_signature = "";
while (!src.fail())
{
int ch = src.get();
if (ch == '\x1A') break;
m_signature += static_cast<char>(ch);
}
if (strncmp(m_signature.c_str(), SIMU_SIGNATURE, strlen(SIMU_SIGNATURE)))
throw std::runtime_error("PAKファイルではありません");
m_version = readInt<unsigned long>(src);
m_root.load(src);
}
void PakFile::loadFromFile(std::string filename)
{
std::ifstream src(filename.c_str(), std::ios::in | std::ios::binary);
if (src.fail()) throw std::runtime_error(std::string("ファイルを開けません。: ") + filename);
load(src);
src.close();
}
void PakFile::save(std::ostream &dest) const
{
dest.write(m_signature.data(), m_signature.size());
dest.put('\x1A');
writeInt<unsigned long>(dest, m_version);
m_root.save(dest);
}
void PakFile::saveToFile(const std::string filename) const
{
std::ofstream dest(filename.c_str(), std::ios::out | std::ios::binary);
if (dest.fail()) throw std::runtime_error(std::string("ファイルを開けません。: ") + filename);
save(dest);
dest.close();
}