102 buffer[writesize] = CharByte<T> (stuff, x); |
102 buffer[writesize] = CharByte<T> (stuff, x); |
103 writesize++; |
103 writesize++; |
104 } |
104 } |
105 } |
105 } |
106 |
106 |
|
107 // Merge another data buffer into this one. |
|
108 void Merge (DataBuffer* other) { |
|
109 for (unsigned int x = 0; x < other->writesize; x++) { |
|
110 unsigned char c = *(other->buffer+x); |
|
111 Write<unsigned char> (c); |
|
112 } |
|
113 |
|
114 delete other; |
|
115 } |
|
116 |
107 private: |
117 private: |
108 template <class T> unsigned char CharByte (T a, unsigned int b) { |
118 template <class T> unsigned char CharByte (T a, unsigned int b) { |
109 if (b >= sizeof (T)) |
119 if (b >= sizeof (T)) |
110 error ("CharByte: tried to get byte %u out of a %u-byte %s\n", |
120 error ("CharByte: tried to get byte %u out of a %u-byte %s\n", |
111 b, sizeof (T), typeid(T).name()); |
121 b, sizeof (T), typeid (T).name()); |
112 |
122 |
113 unsigned long p1 = pow<unsigned long> (256, b); |
123 unsigned long p1 = pow<unsigned long> (256, b); |
114 unsigned long p2 = pow<unsigned long> (256, b+1); |
124 unsigned long p2 = pow<unsigned long> (256, b+1); |
115 unsigned long r = (a % p2) / p1; |
125 unsigned long r = (a % p2) / p1; |
116 |
126 |
117 if (r > 256) |
127 if (r > 256) |
118 error ("result %lu too big!", r); |
128 error ("DataBuffer::CharByte: result %lu too big!", r); |
119 |
129 |
120 unsigned char ur = static_cast<unsigned char> (r); |
130 unsigned char ur = static_cast<unsigned char> (r); |
121 return ur; |
131 return ur; |
122 } |
132 } |
123 }; |
133 }; |