-
Notifications
You must be signed in to change notification settings - Fork 51
/
writer_lcf.cpp
165 lines (139 loc) · 3.58 KB
/
writer_lcf.cpp
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
/*
* This file is part of liblcf. Copyright (c) 2017 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#include <ostream>
#include "writer_lcf.h"
LcfWriter::LcfWriter(std::ostream& filestream, std::string encoding) :
encoding(encoding),
stream(filestream)
{
}
LcfWriter::~LcfWriter() {
}
void LcfWriter::Write(const void *ptr, size_t size, size_t nmemb) {
#ifdef NDEBUG
stream.write(reinterpret_cast<const char*>(ptr), size*nmemb);
#else
assert(stream.write(reinterpret_cast<const char*>(ptr), size*nmemb).good());
#endif
}
template <>
void LcfWriter::Write<uint8_t>(uint8_t val) {
Write(&val, 1, 1);
}
template <>
void LcfWriter::Write<int16_t>(int16_t val) {
SwapByteOrder(val);
Write(&val, 2, 1);
}
template <>
void LcfWriter::Write<uint32_t>(uint32_t val) {
SwapByteOrder(val);
Write(&val, 4, 1);
}
void LcfWriter::WriteInt(int val) {
uint32_t value = (uint32_t) val;
for (int i = 28; i >= 0; i -= 7)
if (value >= (1U << i) || i == 0)
Write<uint8_t>((uint8_t)(((value >> i) & 0x7F) | (i > 0 ? 0x80 : 0)));
}
template <>
void LcfWriter::Write<int32_t>(int32_t val) {
WriteInt(val);
}
template <>
void LcfWriter::Write<bool>(bool val) {
uint8_t x = val ? 1 : 0;
Write(x);
}
template <>
void LcfWriter::Write<double>(double val) {
SwapByteOrder(val);
Write(&val, 8, 1);
}
template <>
void LcfWriter::Write<bool>(const std::vector<bool>& buffer) {
std::vector<bool>::const_iterator it;
for (it = buffer.begin(); it != buffer.end(); it++) {
uint8_t val = *it ? 1 : 0;
Write(val);
}
}
template <>
void LcfWriter::Write<uint8_t>(const std::vector<uint8_t>& buffer) {
Write(&buffer.front(), 1, buffer.size());
}
template <>
void LcfWriter::Write<int16_t>(const std::vector<int16_t>& buffer) {
std::vector<int16_t>::const_iterator it;
for (it = buffer.begin(); it != buffer.end(); it++)
Write(*it);
}
template <>
void LcfWriter::Write<int32_t>(const std::vector<int32_t>& buffer) {
std::vector<int32_t>::const_iterator it;
for (it = buffer.begin(); it != buffer.end(); it++) {
int32_t val = *it;
SwapByteOrder(val);
// Write<int32_t> writes a compressed integer
Write(&val, 4, 1);
}
}
template <>
void LcfWriter::Write<uint32_t>(const std::vector<uint32_t>& buffer) {
std::vector<uint32_t>::const_iterator it;
for (it = buffer.begin(); it != buffer.end(); it++)
Write(*it);
}
void LcfWriter::Write(const std::string& _str) {
std::string str = Decode(_str);
if (!str.empty()) {
Write(&*str.begin(), 1, str.size());
}
}
bool LcfWriter::IsOk() const {
return (stream.good());
}
std::string LcfWriter::Decode(const std::string& str_to_encode) {
return ReaderUtil::Recode(str_to_encode, "UTF-8", encoding);
}
#ifdef WORDS_BIGENDIAN
void LcfWriter::SwapByteOrder(uint16_t& us)
{
us = (us >> 8) |
(us << 8);
}
void LcfWriter::SwapByteOrder(uint32_t& ui)
{
ui = (ui >> 24) |
((ui<<8) & 0x00FF0000) |
((ui>>8) & 0x0000FF00) |
(ui << 24);
}
void LcfWriter::SwapByteOrder(double& d)
{
uint32_t *p = reinterpret_cast<uint32_t *>(&d);
SwapByteOrder(p[0]);
SwapByteOrder(p[1]);
uint32_t tmp = p[0];
p[0] = p[1];
p[1] = tmp;
}
#else
void LcfWriter::SwapByteOrder(uint16_t& /* us */) {}
void LcfWriter::SwapByteOrder(uint32_t& /* ui */) {}
void LcfWriter::SwapByteOrder(double& /* d */) {}
#endif
void LcfWriter::SwapByteOrder(int16_t& s)
{
SwapByteOrder((uint16_t&) s);
}
void LcfWriter::SwapByteOrder(int32_t& s)
{
SwapByteOrder((uint32_t&) s);
}