1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /*
24 * This code is based on Broken Sword 2.5 engine
25 *
26 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
27 *
28 * Licensed under GNU GPL v2
29 *
30 */
31
32 #include "sword25/kernel/outputpersistenceblock.h"
33
34 namespace {
35 const uint INITIAL_BUFFER_SIZE = 1024 * 64;
36 }
37
38 namespace Sword25 {
39
OutputPersistenceBlock()40 OutputPersistenceBlock::OutputPersistenceBlock() {
41 _data.reserve(INITIAL_BUFFER_SIZE);
42 }
43
write(const void * data,uint32 size)44 void OutputPersistenceBlock::write(const void *data, uint32 size) {
45 writeMarker(BLOCK_MARKER);
46
47 write(size);
48 rawWrite(data, size);
49 }
50
write(int32 value)51 void OutputPersistenceBlock::write(int32 value) {
52 writeMarker(SINT_MARKER);
53 value = TO_LE_32(value);
54 rawWrite(&value, sizeof(value));
55 }
56
write(uint32 value)57 void OutputPersistenceBlock::write(uint32 value) {
58 writeMarker(UINT_MARKER);
59 value = TO_LE_32(value);
60 rawWrite(&value, sizeof(value));
61 }
62
write(float value)63 void OutputPersistenceBlock::write(float value) {
64 writeMarker(FLOAT_MARKER);
65 uint32 tmp[1];
66
67 ((float *)tmp)[0] = value;
68 tmp[0] = TO_LE_32(tmp[0]);
69
70 rawWrite(&value, sizeof(value));
71 }
72
write(bool value)73 void OutputPersistenceBlock::write(bool value) {
74 writeMarker(BOOL_MARKER);
75
76 uint uintBool = value ? 1 : 0;
77 uintBool = TO_LE_32(uintBool);
78 rawWrite(&uintBool, sizeof(uintBool));
79 }
80
writeString(const Common::String & string)81 void OutputPersistenceBlock::writeString(const Common::String &string) {
82 writeMarker(STRING_MARKER);
83
84 write((uint32)string.size());
85 rawWrite(string.c_str(), string.size());
86 }
87
writeByteArray(Common::Array<byte> & value)88 void OutputPersistenceBlock::writeByteArray(Common::Array<byte> &value) {
89 writeMarker(BLOCK_MARKER);
90
91 write((uint32)value.size());
92 rawWrite(&value[0], value.size());
93 }
94
writeMarker(byte marker)95 void OutputPersistenceBlock::writeMarker(byte marker) {
96 _data.push_back(marker);
97 }
98
rawWrite(const void * dataPtr,size_t size)99 void OutputPersistenceBlock::rawWrite(const void *dataPtr, size_t size) {
100 if (size > 0) {
101 uint oldSize = _data.size();
102 _data.resize(oldSize + size);
103 memcpy(&_data[oldSize], dataPtr, size);
104 }
105 }
106
107 } // End of namespace Sword25
108