Mon, 03 Feb 2014 20:12:44 +0200
- committed work so far done on expressions
| 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 | // | |
|
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
90
diff
changeset
|
105 | MarkReference* DataBuffer::AddReference (ByteMark* mark) |
| 88 | 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 | |
|
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
90
diff
changeset
|
113 | WriteDWord (0xBEEFCAFE); |
| 88 | 114 | |
| 115 | return ref; | |
| 116 | } | |
| 117 | ||
| 118 | // ============================================================================ | |
| 119 | // | |
| 120 | void DataBuffer::AdjustMark (ByteMark* mark) | |
| 121 | { | |
| 122 | mark->pos = GetWrittenSize(); | |
| 123 | } | |
| 124 | ||
| 125 | // ============================================================================ | |
| 126 | // | |
| 127 | void DataBuffer::OffsetMark (ByteMark* mark, int offset) | |
| 128 | { | |
| 129 | mark->pos += offset; | |
| 130 | } | |
| 131 | ||
| 132 | // ============================================================================ | |
| 133 | // | |
| 134 | void DataBuffer::WriteFloat (float a) | |
| 135 | { | |
| 136 | // TODO: Find a way to store the number without decimal loss. | |
| 137 | WriteDWord (dhPushNumber); | |
| 138 | WriteDWord (abs (a)); | |
| 139 | ||
| 140 | if (a < 0) | |
| 141 | WriteDWord (dhUnaryMinus); | |
| 142 | } | |
| 143 | ||
| 144 | // ============================================================================ | |
| 145 | // | |
| 146 | void DataBuffer::WriteStringIndex (const String& a) | |
| 147 | { | |
| 148 | WriteDWord (dhPushStringIndex); | |
| 149 | WriteDWord (GetStringTableIndex (a)); | |
| 150 | } | |
| 151 | ||
| 152 | // ============================================================================ | |
| 153 | // | |
| 154 | void DataBuffer::Dump() | |
| 155 | { | |
| 156 | for (int i = 0; i < GetWrittenSize(); ++i) | |
| 157 | printf ("%d. [0x%X]\n", i, GetBuffer()[i]); | |
| 158 | } | |
| 159 | ||
| 160 | // ============================================================================ | |
| 161 | // | |
| 162 | void DataBuffer::CheckSpace (int bytes) | |
| 163 | { | |
| 164 | int writesize = GetWrittenSize(); | |
| 165 | ||
| 166 | if (writesize + bytes < GetAllocatedSize()) | |
| 167 | return; | |
| 168 | ||
| 169 | // We don't have enough space in the buffer to write | |
| 170 | // the stuff - thus resize. First, store the old | |
| 171 | // buffer temporarily: | |
| 172 | char* copy = new char[GetAllocatedSize()]; | |
| 173 | memcpy (copy, GetBuffer(), GetAllocatedSize()); | |
| 174 | ||
| 175 | // Remake the buffer with the new size. Have enough space | |
| 176 | // for the stuff we're going to write, as well as a bit | |
| 177 | // of leeway so we don't have to resize immediately again. | |
| 178 | int newsize = GetAllocatedSize() + bytes + 512; | |
| 179 | ||
| 180 | delete GetBuffer(); | |
| 181 | SetBuffer (new byte[newsize]); | |
| 182 | SetAllocatedSize (newsize); | |
| 183 | ||
| 184 | // Now, copy the stuff back. | |
| 185 | memcpy (mBuffer, copy, GetAllocatedSize()); | |
| 186 | SetPosition (GetBuffer() + writesize); | |
| 187 | delete copy; | |
| 188 | } | |
| 189 | ||
| 190 | // ============================================================================= | |
| 191 | // | |
| 192 | void DataBuffer::WriteByte (int8_t data) | |
| 193 | { | |
| 194 | CheckSpace (1); | |
| 195 | *mPosition++ = data; | |
| 196 | } | |
| 197 | ||
| 198 | // ============================================================================= | |
| 199 | // | |
| 200 | void DataBuffer::WriteWord (int16_t data) | |
| 201 | { | |
| 202 | CheckSpace (2); | |
| 203 | ||
| 204 | for (int i = 0; i < 2; ++i) | |
| 205 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
| 206 | } | |
| 207 | ||
| 208 | // ============================================================================= | |
| 209 | // | |
| 210 | void DataBuffer::WriteDWord (int32_t data) | |
| 211 | { | |
| 212 | CheckSpace (4); | |
| 213 | ||
| 214 | for (int i = 0; i < 4; ++i) | |
| 215 | *mPosition++ = (data >> (i * 8)) & 0xFF; | |
| 216 | } | |
| 217 | ||
| 218 | // ============================================================================= | |
| 219 | // | |
| 220 | void DataBuffer::WriteString (const String& a) | |
| 221 | { | |
| 222 | CheckSpace (a.Length() + 1); | |
| 223 | ||
| 224 | for (char c : a) | |
| 225 | WriteByte (c); | |
| 226 | ||
| 227 | WriteByte ('\0'); | |
| 228 | } | |
| 229 | ||
| 230 | ||
| 231 | // ============================================================================= | |
| 232 | // | |
| 233 | ByteMark* DataBuffer::FindMarkByName (const String& target) | |
| 234 | { | |
| 235 | for (ByteMark* mark : GetMarks()) | |
| 236 | if (mark->name == target) | |
| 237 | return mark; | |
| 238 | ||
| 239 | return null; | |
| 240 | } |