Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "bdbuffer.h"
00032
00033 #ifdef HAVE_STDLIB_H
00034 # include <stdlib.h>
00035 #endif
00036
00037 #ifdef HAVE_STRING_H
00038 # include <string.h>
00039 #endif
00040
00041 #include "../intl.h"
00042
00043 using namespace YAPET;
00044
00054 uint8_t*
00055 BDBuffer::alloc_mem (uint32_t s) throw (YAPETException) {
00056 uint8_t* tmp = (uint8_t*) malloc (s);
00057
00058 if (tmp == NULL)
00059 throw YAPETException (_ ("Memory exhausted") );
00060
00061 return tmp;
00062 }
00063
00073 void
00074 BDBuffer::free_mem (uint8_t* d, uint32_t s) {
00075 memset (d, 0, s);
00076 free (d);
00077 }
00078
00084 BDBuffer::BDBuffer (uint32_t is) throw (YAPETException) : _size (is) {
00085 data = alloc_mem (_size);
00086 }
00087
00094 BDBuffer::BDBuffer() : _size (0), data (NULL) { }
00095
00096 BDBuffer::BDBuffer (const BDBuffer& ed) throw (YAPETException) {
00097 if (ed.data == NULL) {
00098 data = NULL;
00099 _size = 0;
00100 return;
00101 }
00102
00103 data = alloc_mem (ed._size);
00104 memcpy (data, ed.data, ed._size);
00105 _size = ed._size;
00106 }
00107
00113 BDBuffer::~BDBuffer() {
00114 if (data == NULL) return;
00115
00116 free_mem (data, _size);
00117 }
00118
00133 void
00134 BDBuffer::resize (uint32_t ns) throw (YAPETException) {
00135 if (data == NULL) {
00136 data = alloc_mem (ns);
00137 _size = ns;
00138 return;
00139 }
00140
00141 uint8_t* newbuf = alloc_mem (ns);
00142
00143 if (ns > _size)
00144 memcpy (newbuf, data, _size);
00145 else
00146 memcpy (newbuf, data, ns);
00147
00148 free_mem (data, _size);
00149 _size = ns;
00150 data = newbuf;
00151 }
00152
00168 uint8_t*
00169 BDBuffer::at (uint32_t pos) throw (std::out_of_range) {
00170 if (pos > (_size - 1) )
00171 throw std::out_of_range (_ ("Position out of range") );
00172
00173 return data + pos;
00174 }
00175
00190 const uint8_t*
00191 BDBuffer::at (uint32_t pos) const throw (std::out_of_range) {
00192 if (pos > (_size - 1) )
00193 throw std::out_of_range (_ ("Position out of range") );
00194
00195 return data + pos;
00196 }
00197
00198 const BDBuffer&
00199 BDBuffer::operator= (const BDBuffer & ed) {
00200 if (this == &ed) return *this;
00201
00202 if (data != NULL)
00203 free_mem (data, _size);
00204
00205 if (ed.data != NULL) {
00206 data = alloc_mem (ed._size);
00207 memcpy (data, ed.data, ed._size);
00208 } else {
00209 data = NULL;
00210 }
00211
00212 _size = ed._size;
00213 return *this;
00214 }