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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/util.h"
#include "titanic/simple_file.h"
namespace Titanic {
SimpleFile::SimpleFile(): _inStream(nullptr), _outStream(nullptr), _lineCount(1) {
}
SimpleFile::~SimpleFile() {
_file.close();
}
void SimpleFile::open(const Common::String &name) {
close();
if (!_file.open(name))
error("Could not find file - %s", name.c_str());
_inStream = &_file;
}
void SimpleFile::open(Common::SeekableReadStream *stream) {
close();
_inStream = stream;
}
void SimpleFile::open(Common::OutSaveFile *stream) {
close();
_outStream = stream;
}
void SimpleFile::close() {
_file.close();
if (_outStream)
_outStream->finalize();
_inStream = nullptr;
_outStream = nullptr;
}
void SimpleFile::safeRead(void *dst, size_t count) {
if (unsafeRead(dst, count) != count)
error("Could not read %d bytes", (int)count);
}
size_t SimpleFile::unsafeRead(void *dst, size_t count) {
assert(_inStream);
return _inStream->read(dst, count);
}
size_t SimpleFile::write(const void *src, size_t count) {
assert(_outStream);
return _outStream->write(src, count);
}
CString SimpleFile::readString() {
char c;
CString result;
bool backslashFlag = false;
// First skip any spaces
do {
safeRead(&c, 1);
} while (Common::isSpace(c));
// Ensure we've found a starting quote for the string
if (c != '"')
error("Could not find starting quote");
bool endFlag = false;
while (!endFlag) {
// Read the next character
safeRead(&c, 1);
if (backslashFlag) {
backslashFlag = false;
switch (c) {
case 'n':
result += '\n';
break;
case 'r':
result += '\r';
break;
case '\t':
result += '\t';
break;
default:
result += c;
break;
}
} else {
switch (c) {
case '"':
endFlag = true;
break;
case '\\':
backslashFlag = true;
break;
default:
result += c;
break;
}
}
}
// Return the string
return result;
}
int SimpleFile::readNumber() {
char c;
int result = 0;
bool minusFlag = false;
// First skip any spaces
do {
safeRead(&c, 1);
} while (Common::isSpace(c));
// Check for prefix sign
if (c == '+' || c == '-') {
minusFlag = c == '-';
safeRead(&c, 1);
}
// Read in the number
if (!Common::isDigit(c))
error("Invalid number");
while (Common::isDigit(c)) {
result = result * 10 + (c - '0');
safeRead(&c, 1);
}
// Finally, if it's a minus value, then negate it
if (minusFlag)
result = -result;
return result;
}
double SimpleFile::readFloat() {
char c;
Common::String result;
// First skip any spaces
do {
safeRead(&c, 1);
} while (Common::isSpace(c));
// Check for prefix sign
if (c == '+' || c == '-') {
result += c;
safeRead(&c, 1);
}
// Read in the number
if (!Common::isDigit(c))
error("Invalid number");
while (Common::isDigit(c) || c == '.') {
result += c;
safeRead(&c, 1);
}
// Convert to a float and return it
float floatValue;
sscanf(result.c_str(), "%f", &floatValue);
return floatValue;
}
Common::Point SimpleFile::readPoint() {
Common::Point pt;
pt.x = readNumber();
pt.y = readNumber();
return pt;
}
Common::Rect SimpleFile::readRect() {
Common::Rect r;
r.left = readNumber();
r.top = readNumber();
r.right = readNumber();
r.bottom = readNumber();
return r;
}
void SimpleFile::readBuffer(char *buffer, size_t count) {
CString tempString = readString();
if (buffer) {
strncpy(buffer, tempString.c_str(), count);
buffer[count - 1] = '\0';
}
}
void SimpleFile::writeLine(const CString &str) {
write(str.c_str(), str.size());
write("\r\n", 2);
}
void SimpleFile::writeString(const CString &str) {
if (str.empty())
return;
const char *msgP = str.c_str();
char c;
while ((c = *msgP++) != '\0') {
switch (c) {
case '\r':
write("\\r", 2);
break;
case '\n':
write("\\n", 2);
break;
case '\t':
write("\\t", 2);
break;
case '\"':
write("\\\"", 2);
break;
case '\\':
write("\\\\", 2);
break;
case '{':
write("\\{", 2);
break;
case '}':
write("\\}", 2);
break;
default:
write(&c, 1);
break;
}
}
}
void SimpleFile::writeQuotedString(const CString &str) {
write("\"", 1);
writeString(str);
write("\" ", 2);
}
void SimpleFile::writeQuotedLine(const CString &str, int indent) {
writeIndent(indent);
writeQuotedString(str);
write("\n", 1);
}
void SimpleFile::writeNumber(int val) {
CString str = CString::format("%d ", val);
write(str.c_str(), str.size());
}
void SimpleFile::writeNumberLine(int val, int indent) {
writeIndent(indent);
writeNumber(val);
write("\n", 1);
}
void SimpleFile::writeFloat(double val) {
Common::String valStr = Common::String::format("%f ", val);
write(valStr.c_str(), valStr.size());
}
void SimpleFile::writeFloatLine(double val, int indent) {
writeIndent(indent);
writeFloat(val);
write("\n", 1);
}
void SimpleFile::writePoint(const Common::Point &pt, int indent) {
writeIndent(indent);
writeNumber(pt.x);
writeNumber(pt.y);
write("\n", 1);
}
void SimpleFile::writeRect(const Common::Rect &r, int indent) {
writePoint(Common::Point(r.left, r.top), indent);
writePoint(Common::Point(r.right, r.bottom), indent);
}
void SimpleFile::writeIndent(uint indent) {
for (uint idx = 0; idx < indent; ++idx)
write("\t", 1);
}
bool SimpleFile::IsClassStart() {
char c;
do {
safeRead(&c, 1);
} while (Common::isSpace(c));
assert(c == '{' || c == '}');
return c == '{';
}
void SimpleFile::writeClassStart(const CString &classStr, int indent) {
write("\n", 1);
writeIndent(indent);
write("{\n", 2);
writeIndent(indent + 1);
writeQuotedString(classStr);
write("\n", 1);
}
void SimpleFile::writeClassEnd(int indent) {
writeIndent(indent);
write("}\n", 2);
}
} // End of namespace Titanic
|