52 // ============================================================================ |
52 // ============================================================================ |
53 // DataBuffer: A dynamic data buffer. |
53 // DataBuffer: A dynamic data buffer. |
54 class DataBuffer { |
54 class DataBuffer { |
55 public: |
55 public: |
56 // The actual buffer |
56 // The actual buffer |
57 unsigned char* buffer; |
57 byte* buffer; |
58 |
58 |
59 // Allocated size of the buffer |
59 // Allocated size of the buffer |
60 unsigned int allocsize; |
60 unsigned int allocsize; |
61 |
61 |
62 // Written size of the buffer |
62 // Written size of the buffer |
98 } |
98 } |
99 } |
99 } |
100 |
100 |
101 // ==================================================================== |
101 // ==================================================================== |
102 // Write stuff to the buffer |
102 // Write stuff to the buffer |
103 template<class T> void Write (T stuff) { |
103 template<class T> void DoWrite (const char* func, T stuff) { |
|
104 // printf ("DoWrite: called from %s\n", func); |
104 if (writesize + sizeof (T) >= allocsize) { |
105 if (writesize + sizeof (T) >= allocsize) { |
105 // We don't have enough space in the buffer to write |
106 // We don't have enough space in the buffer to write |
106 // the stuff - thus resize. First, store the old |
107 // the stuff - thus resize. First, store the old |
107 // buffer temporarily: |
108 // buffer temporarily: |
108 char* copy = new char[allocsize]; |
109 char* copy = new char[allocsize]; |
110 |
111 |
111 // Remake the buffer with the new size. Have enough space |
112 // Remake the buffer with the new size. Have enough space |
112 // for the stuff we're going to write, as well as a bit |
113 // for the stuff we're going to write, as well as a bit |
113 // of leeway so we don't have to resize immediately again. |
114 // of leeway so we don't have to resize immediately again. |
114 size_t newsize = allocsize + sizeof (T) + 128; |
115 size_t newsize = allocsize + sizeof (T) + 128; |
|
116 |
115 delete buffer; |
117 delete buffer; |
116 buffer = new unsigned char[newsize]; |
118 buffer = new unsigned char[newsize]; |
117 allocsize = newsize; |
119 allocsize = newsize; |
118 |
120 |
119 // Now, copy the stuff back. |
121 // Now, copy the stuff back. |
140 if (!other) |
142 if (!other) |
141 return; |
143 return; |
142 int oldsize = writesize; |
144 int oldsize = writesize; |
143 |
145 |
144 for (unsigned int x = 0; x < other->writesize; x++) |
146 for (unsigned int x = 0; x < other->writesize; x++) |
145 Write<byte> (*(other->buffer+x)); |
147 Write (*(other->buffer+x)); |
146 |
148 |
147 // Merge its marks and references |
149 // Merge its marks and references |
148 unsigned int u = 0; |
150 unsigned int u = 0; |
149 for (u = 0; u < MAX_MARKS; u++) { |
151 for (u = 0; u < MAX_MARKS; u++) { |
150 if (other->marks[u]) { |
152 if (other->marks[u]) { |
175 |
177 |
176 // Clones this databuffer to a new one and returns it. |
178 // Clones this databuffer to a new one and returns it. |
177 DataBuffer* Clone () { |
179 DataBuffer* Clone () { |
178 DataBuffer* other = new DataBuffer; |
180 DataBuffer* other = new DataBuffer; |
179 for (unsigned int x = 0; x < writesize; x++) |
181 for (unsigned int x = 0; x < writesize; x++) |
180 other->Write<unsigned char> (*(buffer+x)); |
182 other->Write (*(buffer+x)); |
181 return other; |
183 return other; |
182 } |
184 } |
183 |
185 |
184 // ==================================================================== |
186 // ==================================================================== |
185 // Adds a mark to the buffer. A mark is a "pointer" to a particular |
187 // Adds a mark to the buffer. A mark is a "pointer" to a particular |
284 // Write a float into the buffer |
286 // Write a float into the buffer |
285 void WriteFloat (str floatstring) { |
287 void WriteFloat (str floatstring) { |
286 // TODO: Casting float to word causes the decimal to be lost. |
288 // TODO: Casting float to word causes the decimal to be lost. |
287 // Find a way to store the number without such loss. |
289 // Find a way to store the number without such loss. |
288 float val = atof (floatstring); |
290 float val = atof (floatstring); |
289 Write<word> (DH_PUSHNUMBER); |
291 Write (DH_PUSHNUMBER); |
290 Write<word> (static_cast<word> ((val > 0) ? val : -val)); |
292 Write (static_cast<word> ((val > 0) ? val : -val)); |
291 if (val < 0) |
293 if (val < 0) |
292 Write<word> (DH_UNARYMINUS); |
294 Write (DH_UNARYMINUS); |
293 } |
295 } |
294 |
296 |
295 void WriteString (str string) { |
297 void WriteString (str string) { |
296 Write<word> (DH_PUSHSTRINGINDEX); |
298 Write (DH_PUSHSTRINGINDEX); |
297 Write<word> (PushToStringTable (string)); |
299 Write (PushToStringTable (string)); |
298 } |
300 } |
299 }; |
301 }; |
300 |
302 |
301 #endif // __DATABUFFER_H__ |
303 #endif // __DATABUFFER_H__ |