00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "bdbuffer.h"
00021
00022 #ifdef HAVE_STDLIB_H
00023 # include <stdlib.h>
00024 #endif
00025
00026 #ifdef HAVE_STRING_H
00027 # include <string.h>
00028 #endif
00029
00030 #include "../intl.h"
00031
00032 using namespace YAPET;
00033
00043 uint8_t*
00044 BDBuffer::alloc_mem(uint32_t s) throw(YAPETException) {
00045 uint8_t* tmp = (uint8_t*) malloc(s);
00046 if (tmp == NULL)
00047 throw YAPETException(_("Memory exhausted"));
00048
00049 return tmp;
00050 }
00051
00061 void
00062 BDBuffer::free_mem(uint8_t* d, uint32_t s) {
00063 memset(d, 0, s);
00064 free(d);
00065 }
00066
00072 BDBuffer::BDBuffer(uint32_t is) throw(YAPETException) : _size(is) {
00073 data = alloc_mem(_size);
00074 }
00075
00082 BDBuffer::BDBuffer() : _size(0), data(NULL) { }
00083
00084 BDBuffer::BDBuffer(const BDBuffer& ed) throw(YAPETException) {
00085 if (ed.data == NULL) {
00086 data = NULL;
00087 _size = 0;
00088 return;
00089 }
00090
00091 data = alloc_mem(ed._size);
00092 memcpy(data, ed.data, ed._size);
00093 _size = ed._size;
00094 }
00095
00101 BDBuffer::~BDBuffer() {
00102 if (data == NULL) return;
00103 free_mem(data, _size);
00104 }
00105
00120 void
00121 BDBuffer::resize(uint32_t ns) throw(YAPETException) {
00122 if (data == NULL) {
00123 data = alloc_mem(ns);
00124 _size = ns;
00125 return;
00126 }
00127
00128 uint8_t* newbuf = alloc_mem(ns);
00129
00130 if (ns > _size)
00131 memcpy(newbuf, data, _size);
00132 else
00133 memcpy(newbuf, data, ns);
00134
00135 free_mem(data, _size);
00136
00137 _size = ns;
00138 data = newbuf;
00139 }
00140
00156 uint8_t*
00157 BDBuffer::at(uint32_t pos) throw(std::out_of_range) {
00158 if (pos > (_size - 1))
00159 throw std::out_of_range(_("Position out of range"));
00160
00161 return data + pos;
00162 }
00163
00178 const uint8_t*
00179 BDBuffer::at(uint32_t pos) const throw(std::out_of_range) {
00180 if (pos > (_size - 1))
00181 throw std::out_of_range(_("Position out of range"));
00182
00183 return data + pos;
00184 }
00185
00186 const BDBuffer&
00187 BDBuffer::operator=(const BDBuffer& ed) {
00188 if (this == &ed) return *this;
00189
00190 if (data != NULL)
00191 free_mem(data, _size);
00192
00193 if (ed.data != NULL) {
00194 data = alloc_mem(ed._size);
00195 memcpy(data, ed.data, ed._size);
00196 } else {
00197 data = NULL;
00198 }
00199
00200 _size = ed._size;
00201 return *this;
00202 }