1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/* libClunk - cross-platform 3D audio API built on top SDL library
* Copyright (C) 2007-2008 Netive Media Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "buffer.h"
#include <stdlib.h>
#include <string.h>
#include "clunk_ex.h"
#include <assert.h>
using namespace clunk;
void Buffer::fill(const int b) {
if (ptr == NULL)
return;
memset(ptr, b, size);
}
const Buffer& Buffer::operator=(const Buffer& c) {
if (this == &c)
return *this; // same object
if (c.ptr == NULL) {
free();
return *this;
}
assert(c.size > 0);
void *p = realloc(ptr, c.size);
if (p == NULL)
throw_io(("realloc (%p, %u)", ptr, (unsigned)c.size));
ptr = p;
size = c.size;
memcpy(ptr, c.ptr, c.size);
return *this;
}
void Buffer::set_size(size_t s) {
if (s == size)
return;
if (s == 0) {
free();
return;
}
void * x = realloc(ptr, s);
if (x == NULL)
throw_io(("realloc (%p, %u)", ptr, (unsigned)s));
ptr = x;
size = s;
}
void Buffer::set_data(const void *p, const size_t s) {
if (p == NULL || s == 0)
throw_ex(("calling set_data(%p, %u) is invalid", p, (unsigned)s));
void *x = realloc(ptr, s);
if (x == NULL)
throw_io(("realloc (%p, %d)", ptr, (unsigned)s));
ptr = x;
memcpy(ptr, p, s);
size = s;
}
void Buffer::set_data(void *p, const size_t s, const bool own) {
if (p == NULL || s == 0)
throw_ex(("calling set_data(%p, %u, %s) is invalid", p, (unsigned)s, own?"true":"false"));
if (own) {
free();
ptr = p;
size = s;
} else {
void *x = realloc(ptr, s);
if (x == NULL)
throw_io(("realloc(%p, %d)", ptr, (unsigned)s));
ptr = x;
size = s;
memcpy(ptr, p, s);
}
}
void Buffer::append(const Buffer &other) {
size_t s1 = size, s2 = other.get_size();
if (s2 == 0)
return;
set_size(s1 + s2);
memcpy((char *) ptr + s1, other.ptr, s2);
}
void Buffer::append(const void *data, const size_t data_size) {
if (data_size == 0)
return;
size_t s = size;
set_size(s + data_size);
memcpy((char *) ptr + s, data, data_size);
}
void* Buffer::reserve(const int more) {
set_size(size + more);
return ptr;
}
void Buffer::free() {
if (ptr != NULL) {
::free(ptr);
ptr = NULL;
size = 0;
}
}
const std::string Buffer::dump() const {
if (ptr == NULL)
return "empty memory buffer";
assert(ptr != 0);
std::string result = clunk::format_string("-[memory dump]-[size: %u]---", (unsigned)size);
size_t n = (size - 1)/ 16 + 1;
for(size_t i = 0; i < n; ++i) {
result += clunk::format_string("\n%06x\t", (unsigned)(i * 16));
size_t j, m = (size - i * 16);
if (m > 16)
m = 16;
for(j = 0; j < m; ++j) {
const unsigned char *p = ((unsigned char *)ptr) + i*16 + j;
result += clunk::format_string("%02x ", *p);
if (j == 7)
result += " ";
}
for(; j < 16; ++j) {
if (j == 7)
result += " ";
result += " ";
}
result += "\t\t";
for(j = 0; j < m; ++j) {
const unsigned char *p = ((unsigned char *)ptr) + i*16 + j;
result += clunk::format_string("%c", (*p>=32 && *p < 127)? *p: '.');
if (j == 7)
result += " ";
}
for(; j < m; ++j) {
if (j == 7)
result += " ";
result += " ";
}
}
return result;
}
void Buffer::pop(size_t n) {
if (ptr == NULL)
return;
if (n >= size) {
free();
return;
}
memmove(ptr, (unsigned char *)ptr + n, size - n);
set_size(size - n);
}
|