Wed, 12 Feb 2014 06:15:11 +0200
- refactored enums, macros split from Main.h to Macros.h
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 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
96
diff
changeset
|
35 | SetBuffer (new char[size]); |
88 | 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::WriteStringIndex (const String& a) | |
150 | { | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
96
diff
changeset
|
151 | WriteDWord (DH_PushStringIndex); |
88 | 152 | WriteDWord (GetStringTableIndex (a)); |
153 | } | |
154 | ||
155 | // ============================================================================ | |
156 | // | |
157 | void DataBuffer::Dump() | |
158 | { | |
159 | for (int i = 0; i < GetWrittenSize(); ++i) | |
160 | printf ("%d. [0x%X]\n", i, GetBuffer()[i]); | |
161 | } | |
162 | ||
163 | // ============================================================================ | |
164 | // | |
165 | void DataBuffer::CheckSpace (int bytes) | |
166 | { | |
167 | int writesize = GetWrittenSize(); | |
168 | ||
169 | if (writesize + bytes < GetAllocatedSize()) | |
170 | return; | |
171 | ||
172 | // We don't have enough space in the buffer to write | |
173 | // the stuff - thus resize. First, store the old | |
174 | // buffer temporarily: | |
175 | char* copy = new char[GetAllocatedSize()]; | |
176 | memcpy (copy, GetBuffer(), GetAllocatedSize()); | |
177 | ||
178 | // Remake the buffer with the new size. Have enough space | |
179 | // for the stuff we're going to write, as well as a bit | |
180 | // of leeway so we don't have to resize immediately again. | |
181 | int newsize = GetAllocatedSize() + bytes + 512; | |
182 | ||
183 | delete GetBuffer(); | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
96
diff
changeset
|
184 | SetBuffer (new char[newsize]); |
88 | 185 | SetAllocatedSize (newsize); |
186 | ||
187 | // Now, copy the stuff back. | |
188 | memcpy (mBuffer, copy, GetAllocatedSize()); | |
189 | SetPosition (GetBuffer() + writesize); | |
190 | delete copy; | |
191 | } | |
192 | ||
193 | // ============================================================================= | |
194 | // | |
195 | void DataBuffer::WriteByte (int8_t data) | |
196 | { | |
197 | CheckSpace (1); | |
198 | *mPosition++ = data; | |
199 | } | |
200 | ||
201 | // ============================================================================= | |
202 | // | |
203 | void DataBuffer::WriteWord (int16_t data) | |
204 | { | |
205 | CheckSpace (2); | |
206 | ||
207 | for (int i = 0; i < 2; ++i) | |
208 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
209 | } | |
210 | ||
211 | // ============================================================================= | |
212 | // | |
213 | void DataBuffer::WriteDWord (int32_t data) | |
214 | { | |
215 | CheckSpace (4); | |
216 | ||
217 | for (int i = 0; i < 4; ++i) | |
218 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
219 | } | |
220 | ||
221 | // ============================================================================= | |
222 | // | |
223 | void DataBuffer::WriteString (const String& a) | |
224 | { | |
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
|
225 | 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
|
226 | WriteDWord (a.Length()); |
88 | 227 | |
228 | for (char c : a) | |
229 | WriteByte (c); | |
230 | } | |
231 | ||
232 | ||
233 | // ============================================================================= | |
234 | // | |
235 | ByteMark* DataBuffer::FindMarkByName (const String& target) | |
236 | { | |
237 | for (ByteMark* mark : GetMarks()) | |
238 | if (mark->name == target) | |
239 | return mark; | |
240 | ||
241 | return null; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
96
diff
changeset
|
242 | } |