src/dataBuffer.cpp

Mon, 21 Jul 2014 17:14:42 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 21 Jul 2014 17:14:42 +0300
changeset 135
8b9132fea327
parent 134
eca2fc0acaa2
child 138
a426c1039655
permissions
-rw-r--r--

- massive refactoring continues (doesn't compile yet)

119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
133
dbbdb870c835 - changed all source files to use my correct legal name instead of my calling name
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
2 Copyright 2012-2014 Teemu Piippo
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 are met:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 3. The name of the author may not be used to endorse or promote products
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 derived from this software without specific prior written permission.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 */
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
29 #include <cstring>
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #include "dataBuffer.h"
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
32 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
34 DataBuffer::DataBuffer (int size) :
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
35 m_buffer (new char[size]),
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
36 m_allocatedSize (size),
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
37 m_position (&buffer()[0]) {}
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
39 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 DataBuffer::~DataBuffer()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 {
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
43 ASSERT (marks().isEmpty());
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
44 ASSERT (references().isEmpty());
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 delete buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
48 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
49 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
50 // Copies the contents of the given buffer into this buffer. The other buffer's marks and
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
51 // references will be moved along and the buffer is then destroyed.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 void DataBuffer::mergeAndDestroy (DataBuffer* other)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 if (other == null)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 return;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 // Note: We transfer the marks before the buffer is copied, so that the
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 // offset uses the proper value (which is the written size of @other, which
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 // we don't want our written size to be added to yet).
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 other->transferMarksTo (this);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 copyBuffer (other);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 delete other;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
66 // -------------------------------------------------------------------------------------------------
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
68 // Clones this databuffer to a new one and returns it. Note that the original transfers its marks
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
69 // and references and loses them in the process.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 DataBuffer* DataBuffer::clone()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 DataBuffer* other = new DataBuffer;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 transferMarksTo (other);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 other->copyBuffer (this);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 return other;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 // ============================================================================
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 void DataBuffer::copyBuffer (const DataBuffer* buf)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 checkSpace (buf->writtenSize());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 memcpy (m_position, buf->buffer(), buf->writtenSize());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 m_position += buf->writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 // ============================================================================
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 void DataBuffer::transferMarksTo (DataBuffer* dest)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 int offset = dest->writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 for (ByteMark* mark : marks())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 mark->pos += offset;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 dest->m_marks << mark;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 for (MarkReference* ref : references())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 ref->pos += offset;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 dest->m_references << ref;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 m_marks.clear();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 m_references.clear();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
110 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
111 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
112 // Adds a new mark to the current position with the given name
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 ByteMark* DataBuffer::addMark (const String& name)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 ByteMark* mark = new ByteMark;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 mark->name = name;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 mark->pos = writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 m_marks << mark;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 return mark;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
123 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
124 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
125 // Adds a new reference to the given mark at the current position. This function will write 4
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
126 // bytes to the buffer whose value will be determined at final output writing.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 MarkReference* DataBuffer::addReference (ByteMark* mark)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 MarkReference* ref = new MarkReference;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 ref->target = mark;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 ref->pos = writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 m_references << ref;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 // Write a dummy placeholder for the reference
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 writeDWord (0xBEEFCAFE);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 return ref;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
141 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
142 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
143 // Moves the given mark to the current bytecode position.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 void DataBuffer::adjustMark (ByteMark* mark)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 mark->pos = writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
150 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
151 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
152 // Shifts the given mark by the amount of bytes
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
154 void DataBuffer::offsetMark (ByteMark* mark, int bytes)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 {
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
156 mark->pos += bytes;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
159 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
160 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
161 // Writes a push of the index of the given string. 8 bytes will be written and the string index
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
162 // will be pushed to stack.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 void DataBuffer::writeStringIndex (const String& a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 {
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
166 writeHeader (DataHeader::PushStringIndex);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 writeDWord (getStringTableIndex (a));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
170 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
171 //
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
172 // Writes a data header. 4 bytes.
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
173 //
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
174 void DataBuffer::writeHeader (DataHeader data)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
175 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
176 writeDWord (static_cast<int32_t> (data));
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
177 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
178
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
179 // -------------------------------------------------------------------------------------------------
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
180 //
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
181 // Prints the buffer to stdout.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 void DataBuffer::dump()
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 for (int i = 0; i < writtenSize(); ++i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 printf ("%d. [0x%X]\n", i, buffer()[i]);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
189 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
190 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
191 // Ensures there's at least the given amount of bytes left in the buffer. Will resize if necessary,
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
192 // no-op if not. On resize, 512 extra bytes are allocated to reduce the amount of resizes.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 void DataBuffer::checkSpace (int bytes)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 int writesize = writtenSize();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 if (writesize + bytes < allocatedSize())
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 return;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 // We don't have enough space in the buffer to write
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 // the stuff - thus resize. First, store the old
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 // buffer temporarily:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 char* copy = new char[allocatedSize()];
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
205 std::memcpy (copy, buffer(), allocatedSize());
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 // Remake the buffer with the new size. Have enough space
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 // for the stuff we're going to write, as well as a bit
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 // of leeway so we don't have to resize immediately again.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 int newsize = allocatedSize() + bytes + 512;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 delete buffer();
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 setBuffer (new char[newsize]);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 setAllocatedSize (newsize);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 // Now, copy the stuff back.
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
217 std::memcpy (m_buffer, copy, allocatedSize());
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 setPosition (buffer() + writesize);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 delete copy;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
222 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
223 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
224 // Writes the given byte into the buffer.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 void DataBuffer::writeByte (int8_t data)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 checkSpace (1);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 *m_position++ = data;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
232 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
233 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
234 // Writes the given word into the buffer. 2 bytes will be written.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 void DataBuffer::writeWord (int16_t data)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 checkSpace (2);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 for (int i = 0; i < 2; ++i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 *m_position++ = (data >> (i * 8)) & 0xFF;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
244 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
245 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
246 // Writes the given dword into the buffer. 4bytes will be written.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 void DataBuffer::writeDWord (int32_t data)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 checkSpace (4);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 for (int i = 0; i < 4; ++i)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 *m_position++ = (data >> (i * 8)) & 0xFF;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
256 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
257 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
258 // Writes the given string to the databuffer. The string will be written as-is without using string
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
259 // indices. This will write 4 + length bytes. No header will be written.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 void DataBuffer::writeString (const String& a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 checkSpace (a.length() + 4);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264 writeDWord (a.length());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 for (char c : a)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267 writeByte (c);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
270 // -------------------------------------------------------------------------------------------------
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
271 //
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
272 // Tries to locate the mark by the given name. Returns null if not found.
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 //
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274 ByteMark* DataBuffer::findMarkByName (const String& name)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 for (ByteMark* mark : marks())
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
277 {
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 if (mark->name == name)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 return mark;
134
eca2fc0acaa2 - massive refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
280 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282 return null;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 }

mercurial