Mon, 03 Feb 2014 11:23:56 +0200
- don't clear marks after merging them in...
88 | 1 | /* |
2 | Copyright 2012-2014 Santeri Piippo | |
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions | |
7 | are met: | |
8 | ||
9 | 1. Redistributions of source code must retain the above copyright | |
10 | notice, this list of conditions and the following disclaimer. | |
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in the | |
13 | documentation and/or other materials provided with the distribution. | |
14 | 3. The name of the author may not be used to endorse or promote products | |
15 | derived from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "DataBuffer.h" | |
30 | ||
31 | // ============================================================================ | |
32 | // | |
33 | DataBuffer::DataBuffer (int size) | |
34 | { | |
35 | SetBuffer (new byte[size]); | |
36 | SetPosition (&GetBuffer()[0]); | |
37 | SetAllocatedSize (size); | |
38 | } | |
39 | ||
40 | // ============================================================================ | |
41 | // | |
42 | DataBuffer::~DataBuffer() | |
43 | { | |
44 | assert (CountMarks() == 0 && CountReferences() == 0); | |
45 | delete GetBuffer(); | |
46 | } | |
47 | ||
48 | // ============================================================================ | |
49 | // | |
50 | void DataBuffer::MergeAndDestroy (DataBuffer* other) | |
51 | { | |
52 | if (!other) | |
53 | return; | |
54 | ||
55 | int oldsize = GetWrittenSize(); | |
56 | CopyBuffer (other); | |
57 | ||
58 | // Assimilate in its marks and references | |
59 | for (ByteMark* mark : other->GetMarks()) | |
60 | { | |
61 | mark->pos += oldsize; | |
62 | PushToMarks (mark); | |
63 | } | |
64 | ||
65 | for (MarkReference* ref : other->GetReferences()) | |
66 | { | |
67 | ref->pos += oldsize; | |
68 | PushToReferences (ref); | |
69 | } | |
70 | ||
71 | delete other; | |
72 | } | |
73 | ||
74 | // ============================================================================ | |
75 | // | |
76 | DataBuffer* DataBuffer::Clone() | |
77 | { | |
78 | DataBuffer* other = new DataBuffer; | |
79 | other->CopyBuffer (this); | |
80 | return other; | |
81 | } | |
82 | ||
83 | // ============================================================================ | |
84 | // | |
85 | void DataBuffer::CopyBuffer (const DataBuffer* buf) | |
86 | { | |
87 | CheckSpace (buf->GetWrittenSize()); | |
88 | memcpy (mPosition, buf->GetBuffer(), buf->GetWrittenSize()); | |
89 | mPosition += buf->GetWrittenSize(); | |
90 | } | |
91 | ||
92 | // ============================================================================ | |
93 | // | |
94 | ByteMark* DataBuffer::AddMark (String name) | |
95 | { | |
96 | ByteMark* mark = new ByteMark; | |
97 | mark->name = name; | |
98 | mark->pos = GetWrittenSize(); | |
99 | PushToMarks (mark); | |
100 | return mark; | |
101 | } | |
102 | ||
103 | // ============================================================================ | |
104 | // | |
105 | MarkReference* DataBuffer::AddReference (ByteMark* mark, bool writePlaceholder) | |
106 | { | |
107 | MarkReference* ref = new MarkReference; | |
108 | ref->target = mark; | |
109 | ref->pos = GetWrittenSize(); | |
110 | PushToReferences (ref); | |
111 | ||
112 | // Write a dummy placeholder for the reference | |
113 | if (writePlaceholder) | |
114 | WriteDWord (0xBEEFCAFE); | |
115 | ||
116 | return ref; | |
117 | } | |
118 | ||
119 | // ============================================================================ | |
120 | // | |
121 | void DataBuffer::AdjustMark (ByteMark* mark) | |
122 | { | |
123 | mark->pos = GetWrittenSize(); | |
124 | } | |
125 | ||
126 | // ============================================================================ | |
127 | // | |
128 | void DataBuffer::OffsetMark (ByteMark* mark, int offset) | |
129 | { | |
130 | mark->pos += offset; | |
131 | } | |
132 | ||
133 | // ============================================================================ | |
134 | // | |
135 | void DataBuffer::WriteFloat (float a) | |
136 | { | |
137 | // TODO: Find a way to store the number without decimal loss. | |
138 | WriteDWord (dhPushNumber); | |
139 | WriteDWord (abs (a)); | |
140 | ||
141 | if (a < 0) | |
142 | WriteDWord (dhUnaryMinus); | |
143 | } | |
144 | ||
145 | // ============================================================================ | |
146 | // | |
147 | void DataBuffer::WriteStringIndex (const String& a) | |
148 | { | |
149 | WriteDWord (dhPushStringIndex); | |
150 | WriteDWord (GetStringTableIndex (a)); | |
151 | } | |
152 | ||
153 | // ============================================================================ | |
154 | // | |
155 | void DataBuffer::Dump() | |
156 | { | |
157 | for (int i = 0; i < GetWrittenSize(); ++i) | |
158 | printf ("%d. [0x%X]\n", i, GetBuffer()[i]); | |
159 | } | |
160 | ||
161 | // ============================================================================ | |
162 | // | |
163 | void DataBuffer::CheckSpace (int bytes) | |
164 | { | |
165 | int writesize = GetWrittenSize(); | |
166 | ||
167 | if (writesize + bytes < GetAllocatedSize()) | |
168 | return; | |
169 | ||
170 | // We don't have enough space in the buffer to write | |
171 | // the stuff - thus resize. First, store the old | |
172 | // buffer temporarily: | |
173 | char* copy = new char[GetAllocatedSize()]; | |
174 | memcpy (copy, GetBuffer(), GetAllocatedSize()); | |
175 | ||
176 | // Remake the buffer with the new size. Have enough space | |
177 | // for the stuff we're going to write, as well as a bit | |
178 | // of leeway so we don't have to resize immediately again. | |
179 | int newsize = GetAllocatedSize() + bytes + 512; | |
180 | ||
181 | delete GetBuffer(); | |
182 | SetBuffer (new byte[newsize]); | |
183 | SetAllocatedSize (newsize); | |
184 | ||
185 | // Now, copy the stuff back. | |
186 | memcpy (mBuffer, copy, GetAllocatedSize()); | |
187 | SetPosition (GetBuffer() + writesize); | |
188 | delete copy; | |
189 | } | |
190 | ||
191 | // ============================================================================= | |
192 | // | |
193 | void DataBuffer::WriteByte (int8_t data) | |
194 | { | |
195 | CheckSpace (1); | |
196 | *mPosition++ = data; | |
197 | } | |
198 | ||
199 | // ============================================================================= | |
200 | // | |
201 | void DataBuffer::WriteWord (int16_t data) | |
202 | { | |
203 | CheckSpace (2); | |
204 | ||
205 | for (int i = 0; i < 2; ++i) | |
206 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
207 | } | |
208 | ||
209 | // ============================================================================= | |
210 | // | |
211 | void DataBuffer::WriteDWord (int32_t data) | |
212 | { | |
213 | CheckSpace (4); | |
214 | ||
215 | for (int i = 0; i < 4; ++i) | |
216 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
217 | } | |
218 | ||
219 | // ============================================================================= | |
220 | // | |
221 | void DataBuffer::WriteString (const String& a) | |
222 | { | |
223 | CheckSpace (a.Length() + 1); | |
224 | ||
225 | for (char c : a) | |
226 | WriteByte (c); | |
227 | ||
228 | WriteByte ('\0'); | |
229 | } | |
230 | ||
231 | ||
232 | // ============================================================================= | |
233 | // | |
234 | ByteMark* DataBuffer::FindMarkByName (const String& target) | |
235 | { | |
236 | for (ByteMark* mark : GetMarks()) | |
237 | if (mark->name == target) | |
238 | return mark; | |
239 | ||
240 | return null; | |
241 | } |