Nemo  2.3.46
binarystoragebuffer.h
Go to the documentation of this file.
1 
29 #ifndef BINARYSTORAGEBUFFER_H
30 #define BINARYSTORAGEBUFFER_H
31 
32 #include <iostream>
33 #include <string.h>
34 #include "output.h"
35 
36 #define MAX_BUFF 1024000
37 
41 
42 private:
43  char* _buff;
44  unsigned int _len, _bytes_in, _bytes_out;
45 
46 public:
47 
48  BinaryStorageBuffer() : _buff(NULL),_len(0),_bytes_in(0),_bytes_out(0) {}
49  ~BinaryStorageBuffer() {if(_buff != NULL) delete[]_buff;}
50 
51  char* getBuffer ( ) const {return _buff;}
52  unsigned int getByteLength ( ) const {return _bytes_in;}
53  unsigned int getBytesOut ( ) const {return _bytes_out;}
54  void clear ( )
55  {
56  if(_buff != NULL) delete [] _buff;
57  _buff = NULL;
58  _len = _bytes_in = _bytes_out = 0;
59  }
60  // ----------------------------------------------------------------------------------------
61  // set_buff
62  // ----------------------------------------------------------------------------------------
63  inline void set_buff()
64  {
65 #ifdef __DEBUG__
66  std::cout<<"BinaryStorageBuffer::set_buff";
67 #endif
68 
69  if(_buff != NULL) delete [] _buff;
70 
71  _buff = new char[MAX_BUFF];
72 
73  if(_buff == NULL) fatal("BinaryStorageBuffer::set_buff::memory exhausted !!\n");
74 
75  _len = MAX_BUFF;
76  _bytes_in = 0;
77  memset(_buff,'\0',MAX_BUFF);
78 
79 #ifdef _DEBUG_
80  std::cout<<"[ok]"<<std::endl;
81 #endif
82  }
83  // ----------------------------------------------------------------------------------------
84  // set_buff
85  // ----------------------------------------------------------------------------------------
86  inline void set_buff(void* zone, unsigned int length)
87  {
88  if(_buff != NULL) delete [] _buff;
89 
90  _buff = new char [length];
91 
92  memcpy(_buff, zone, length);
93 
94  _bytes_in = _len = length ;
95 
96  _bytes_out = 0;
97  }
98  // ----------------------------------------------------------------------------------------
99  // extend_buff
100  // ----------------------------------------------------------------------------------------
101  inline void extend_buff()
102  {
103 #ifdef _DEBUG_
104  std::cout<<"BinaryStorageBuffer::extend_buff"<<std::flush;
105 #endif
106 
107  char *old_buff, *new_buff;
108 
109  old_buff = _buff;
110 
111  new_buff = new char[_len + MAX_BUFF];
112 
113  if(new_buff == NULL) fatal("BinaryStorageBuffer::extend_buff::memory exhausted !!\n");
114 
115  memcpy(new_buff,_buff,_len);
116 
117  _buff = new_buff;
118 
119  _len += MAX_BUFF;
120 
121  delete [] old_buff;
122 
123 #ifdef _DEBUG_
124  std::cout<<"["<<_len/1024<<"kB]"<<std::endl;
125 #endif
126  }
127  // ----------------------------------------------------------------------------------------
128  // store
129  // ----------------------------------------------------------------------------------------
130  inline void store (void* stream, unsigned int nb_bytes)
131  {
132 
133  while( !((_bytes_in + nb_bytes) < _len) ) extend_buff();
134 
135  if(nb_bytes == 1) {
136  _buff[_bytes_in] = *(char*)stream;
137  } else {
138  char *tab = (char*)stream;
139  for(unsigned int i = 0; i < nb_bytes; i++)
140  _buff[_bytes_in + i] = tab[i];
141  }
142  _bytes_in += nb_bytes;
143  }
144  // ----------------------------------------------------------------------------------------
145  // read
146  // ----------------------------------------------------------------------------------------
147  inline void read (void *out, unsigned int nb_bytes)
148  {
149 
150  if(((_bytes_out + nb_bytes) > _bytes_in) )
151  fatal("BinaryStorageBuffer::read::attempt to read beyond buffer length (asked %i bytes)\n",nb_bytes);
152  else {
153  char *tab = (char*)out;
154  if(nb_bytes == 1) {
155  (*tab) = _buff[_bytes_out];
156  } else {
157  for(unsigned int i = 0; i < nb_bytes; ++i)
158  tab[i] = _buff[_bytes_out + i];
159  }
160  }
161  _bytes_out += nb_bytes;
162  }
163 
164  void BSBread(void *out, unsigned int nb_bytes)
165  { read(out,nb_bytes); }
166 };
167 
168 #endif
#define MAX_BUFF
Definition: binarystoragebuffer.h:36
char * _buff
Definition: binarystoragebuffer.h:43
void extend_buff()
Definition: binarystoragebuffer.h:101
void set_buff(void *zone, unsigned int length)
Definition: binarystoragebuffer.h:86
void clear()
Definition: binarystoragebuffer.h:54
void fatal(const char *str,...)
Definition: output.cc:90
unsigned int _len
Definition: binarystoragebuffer.h:44
void store(void *stream, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:130
BinaryStorageBuffer()
Definition: binarystoragebuffer.h:48
A class to store any kind of data in a char buffer before unloading it in a binary data file...
Definition: binarystoragebuffer.h:40
char * getBuffer() const
Definition: binarystoragebuffer.h:51
unsigned int _bytes_out
Definition: binarystoragebuffer.h:44
void read(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:147
unsigned int getBytesOut() const
Definition: binarystoragebuffer.h:53
void BSBread(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:164
~BinaryStorageBuffer()
Definition: binarystoragebuffer.h:49
unsigned int getByteLength() const
Definition: binarystoragebuffer.h:52
unsigned int _bytes_in
Definition: binarystoragebuffer.h:44
void set_buff()
Definition: binarystoragebuffer.h:63

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR