databuffer.h

changeset 32
d11a034aabfd
child 34
0a9a5902beaa
equal deleted inserted replaced
31:ad027ea58097 32:d11a034aabfd
1 /*
2 * botc source code
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of the developer nor the names of its contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 * 4. Redistributions in any form must be accompanied by information on how to
18 * obtain complete source code for the software and any accompanying
19 * software that uses the software. The source code must either be included
20 * in the distribution or be available for no more than the cost of
21 * distribution plus a nominal fee, and must be freely redistributable
22 * under reasonable conditions. For an executable file, complete source
23 * code means the source code for all modules it contains. It does not
24 * include source code for modules or files that typically accompany the
25 * major components of the operating system on which the executable file
26 * runs.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #ifndef __DATABUFFER_H__
42 #define __DATABUFFER_H__
43 #include <stdio.h>
44 #include <string.h>
45 #include "common.h"
46
47 // ============================================================================
48 // DataBuffer: A dynamic data buffer.
49 class DataBuffer {
50 public:
51 // The actual buffer
52 unsigned char* buffer;
53
54 // Allocated size of the buffer
55 unsigned int allocsize;
56
57 // Written size of the buffer
58 unsigned int writesize;
59
60 // METHODS
61 DataBuffer (unsigned int size=128) {
62 writesize = 0;
63
64 buffer = new unsigned char[size];
65 allocsize = size;
66 }
67
68 ~DataBuffer () {
69 delete buffer;
70 }
71
72 template<class T> void Write(T stuff) {
73 if (sizeof (char) != 1) {
74 error ("DataBuffer: sizeof(char) must be 1!\n");
75 }
76
77 // Out of space, must resize
78 if (writesize + sizeof(T) >= allocsize) {
79 // First, store the old buffer temporarily
80 char* copy = new char[allocsize];
81 memcpy (copy, buffer, allocsize);
82
83 // Remake the buffer with the new size.
84 // Have a bit of leeway so we don't have to
85 // resize immediately again.
86 size_t newsize = allocsize + sizeof (T) + 128;
87 delete buffer;
88 buffer = new unsigned char[newsize];
89 allocsize = newsize;
90
91 // Now, copy the new stuff over.
92 memcpy (buffer, copy, allocsize);
93
94 // Nuke the copy now as it's no longer needed
95 delete copy;
96 }
97
98 // Write the new stuff one byte at a time
99 for (unsigned int x = 0; x < sizeof (T); x++) {
100 if (writesize >= allocsize)
101 error ("DataBuffer: written size exceeds allocated size!\n");
102 buffer[writesize] = CharByte<T> (stuff, x);
103 writesize++;
104 }
105 }
106
107 private:
108 template <class T> unsigned char CharByte (T a, unsigned int b) {
109 if (b >= sizeof (T))
110 error ("CharByte: tried to get byte %u out of a %u-byte %s\n",
111 b, sizeof (T), typeid(T).name());
112
113 unsigned long p1 = pow<unsigned long> (256, b);
114 unsigned long p2 = pow<unsigned long> (256, b+1);
115 unsigned long r = (a % p2) / p1;
116
117 if (r > 256)
118 error ("result %lu too big!", r);
119
120 unsigned char ur = static_cast<unsigned char> (r);
121 return ur;
122 }
123 };
124
125 #endif // __DATABUFFER_H__

mercurial