Tue, 04 Feb 2014 14:21:06 +0200
- ternary operator now works properly
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 | ||
96
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
55 | // Note: We transfer the marks before the buffer is copied, so that the |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
56 | // offset uses the proper value (which is the written size of @other, which |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
57 | // we don't want our written size to be added to yet). |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
58 | other->TransferMarks (this); |
88 | 59 | CopyBuffer (other); |
60 | delete other; | |
61 | } | |
62 | ||
63 | // ============================================================================ | |
64 | // | |
96
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
65 | // Clones this databuffer to a new one and returns it. Note that the original |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
66 | // transfers its marks and references and loses them in the process. |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
67 | // |
88 | 68 | DataBuffer* DataBuffer::Clone() |
69 | { | |
70 | DataBuffer* other = new DataBuffer; | |
96
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
71 | TransferMarks (other); |
88 | 72 | other->CopyBuffer (this); |
73 | return other; | |
74 | } | |
75 | ||
76 | // ============================================================================ | |
77 | // | |
78 | void DataBuffer::CopyBuffer (const DataBuffer* buf) | |
79 | { | |
80 | CheckSpace (buf->GetWrittenSize()); | |
81 | memcpy (mPosition, buf->GetBuffer(), buf->GetWrittenSize()); | |
82 | mPosition += buf->GetWrittenSize(); | |
83 | } | |
84 | ||
85 | // ============================================================================ | |
86 | // | |
96
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
87 | void DataBuffer::TransferMarks (DataBuffer* other) |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
88 | { |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
89 | int offset = other->GetWrittenSize(); |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
90 | |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
91 | for (ByteMark* mark : GetMarks()) |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
92 | { |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
93 | mark->pos += offset; |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
94 | other->PushToMarks (mark); |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
95 | } |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
96 | |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
97 | for (MarkReference* ref : GetReferences()) |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
98 | { |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
99 | ref->pos += offset; |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
100 | other->PushToReferences (ref); |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
101 | } |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
102 | |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
103 | ClearMarks(); |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
104 | ClearReferences(); |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
105 | } |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
106 | |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
107 | // ============================================================================ |
3384d7aa036a
- ternary operator now works properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
94
diff
changeset
|
108 | // |
88 | 109 | ByteMark* DataBuffer::AddMark (String name) |
110 | { | |
111 | ByteMark* mark = new ByteMark; | |
112 | mark->name = name; | |
113 | mark->pos = GetWrittenSize(); | |
114 | PushToMarks (mark); | |
115 | return mark; | |
116 | } | |
117 | ||
118 | // ============================================================================ | |
119 | // | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
90
diff
changeset
|
120 | MarkReference* DataBuffer::AddReference (ByteMark* mark) |
88 | 121 | { |
122 | MarkReference* ref = new MarkReference; | |
123 | ref->target = mark; | |
124 | ref->pos = GetWrittenSize(); | |
125 | PushToReferences (ref); | |
126 | ||
127 | // Write a dummy placeholder for the reference | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
90
diff
changeset
|
128 | WriteDWord (0xBEEFCAFE); |
88 | 129 | |
130 | return ref; | |
131 | } | |
132 | ||
133 | // ============================================================================ | |
134 | // | |
135 | void DataBuffer::AdjustMark (ByteMark* mark) | |
136 | { | |
137 | mark->pos = GetWrittenSize(); | |
138 | } | |
139 | ||
140 | // ============================================================================ | |
141 | // | |
142 | void DataBuffer::OffsetMark (ByteMark* mark, int offset) | |
143 | { | |
144 | mark->pos += offset; | |
145 | } | |
146 | ||
147 | // ============================================================================ | |
148 | // | |
149 | void DataBuffer::WriteFloat (float a) | |
150 | { | |
151 | // TODO: Find a way to store the number without decimal loss. | |
152 | WriteDWord (dhPushNumber); | |
153 | WriteDWord (abs (a)); | |
154 | ||
155 | if (a < 0) | |
156 | WriteDWord (dhUnaryMinus); | |
157 | } | |
158 | ||
159 | // ============================================================================ | |
160 | // | |
161 | void DataBuffer::WriteStringIndex (const String& a) | |
162 | { | |
163 | WriteDWord (dhPushStringIndex); | |
164 | WriteDWord (GetStringTableIndex (a)); | |
165 | } | |
166 | ||
167 | // ============================================================================ | |
168 | // | |
169 | void DataBuffer::Dump() | |
170 | { | |
171 | for (int i = 0; i < GetWrittenSize(); ++i) | |
172 | printf ("%d. [0x%X]\n", i, GetBuffer()[i]); | |
173 | } | |
174 | ||
175 | // ============================================================================ | |
176 | // | |
177 | void DataBuffer::CheckSpace (int bytes) | |
178 | { | |
179 | int writesize = GetWrittenSize(); | |
180 | ||
181 | if (writesize + bytes < GetAllocatedSize()) | |
182 | return; | |
183 | ||
184 | // We don't have enough space in the buffer to write | |
185 | // the stuff - thus resize. First, store the old | |
186 | // buffer temporarily: | |
187 | char* copy = new char[GetAllocatedSize()]; | |
188 | memcpy (copy, GetBuffer(), GetAllocatedSize()); | |
189 | ||
190 | // Remake the buffer with the new size. Have enough space | |
191 | // for the stuff we're going to write, as well as a bit | |
192 | // of leeway so we don't have to resize immediately again. | |
193 | int newsize = GetAllocatedSize() + bytes + 512; | |
194 | ||
195 | delete GetBuffer(); | |
196 | SetBuffer (new byte[newsize]); | |
197 | SetAllocatedSize (newsize); | |
198 | ||
199 | // Now, copy the stuff back. | |
200 | memcpy (mBuffer, copy, GetAllocatedSize()); | |
201 | SetPosition (GetBuffer() + writesize); | |
202 | delete copy; | |
203 | } | |
204 | ||
205 | // ============================================================================= | |
206 | // | |
207 | void DataBuffer::WriteByte (int8_t data) | |
208 | { | |
209 | CheckSpace (1); | |
210 | *mPosition++ = data; | |
211 | } | |
212 | ||
213 | // ============================================================================= | |
214 | // | |
215 | void DataBuffer::WriteWord (int16_t data) | |
216 | { | |
217 | CheckSpace (2); | |
218 | ||
219 | for (int i = 0; i < 2; ++i) | |
220 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
221 | } | |
222 | ||
223 | // ============================================================================= | |
224 | // | |
225 | void DataBuffer::WriteDWord (int32_t data) | |
226 | { | |
227 | CheckSpace (4); | |
228 | ||
229 | for (int i = 0; i < 4; ++i) | |
230 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
231 | } | |
232 | ||
233 | // ============================================================================= | |
234 | // | |
235 | void DataBuffer::WriteString (const String& a) | |
236 | { | |
94
8915ee6a277d
- expression evaluation implemented! Expressions are now complete for the most part aside from some unary operator quirks in verification.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
237 | CheckSpace (a.Length() + 4); |
8915ee6a277d
- expression evaluation implemented! Expressions are now complete for the most part aside from some unary operator quirks in verification.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
238 | WriteDWord (a.Length()); |
88 | 239 | |
240 | for (char c : a) | |
241 | WriteByte (c); | |
242 | } | |
243 | ||
244 | ||
245 | // ============================================================================= | |
246 | // | |
247 | ByteMark* DataBuffer::FindMarkByName (const String& target) | |
248 | { | |
249 | for (ByteMark* mark : GetMarks()) | |
250 | if (mark->name == target) | |
251 | return mark; | |
252 | ||
253 | return null; | |
254 | } |