libbbb  1.2.1
Groups common code used in some applications and libraries.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
bytebuffer.h
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* ------------------------------------------------------------------------- *//*
12 
13 
14  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15  Please read COPYING and README files in root folder
16  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 */
18 /* ------------------------------------------------------------------------- */
19 /* ========================================================================= */
20 #ifndef __BYTEBUFFER_INC__
21 #define __BYTEBUFFER_INC__
22 //
23 //
24 //
25 //
26 /* INCLUDES ------------------------------------------------------------ */
27 
28 #include <libbbb/libbbb.h>
29 #include <qglobal.h>
30 #include <QDebug>
31 
32 /* INCLUDES ============================================================ */
33 //
34 //
35 //
36 //
37 /* CLASS --------------------------------------------------------------- */
38 
39 class QFile;
40 
41 namespace bbb {
42 
43 
47 class
48  BBBSHARED_EXPORT
49  ByteBuffer {
50 
51  //
52  //
53  //
54  //
55  /* DEFINITIONS ----------------------------------------------------- */
56 
57 # define BYTEBUFFER_INCREASE_STEP 256
58 
59  /* DEFINITIONS ===================================================== */
60  //
61  //
62  //
63  //
64  /* DATA ------------------------------------------------------------ */
65 
66 private:
67 
68 
72  char * p_;
73 
74 
78  unsigned alloc_;
79 
80 
84  unsigned used_;
85 
86 
90  quint64 iterator_;
91 
92 
96  bool err_;
97 
98 
99 
100 
101  /* DATA ============================================================ */
102  //
103  //
104  //
105  //
106  /* FUNCTIONS ------------------------------------------------------- */
107 
108 public:
109 
110 
114  ByteBuffer ( void );
115 
116 
120  ~ByteBuffer ( void );
121 
122 
126  inline bool isValid ( void ) const
127  { return ( err_ == false ); }
128 
129 
133  inline bool isEmpty ( void ) const
134  { return ( used_ == 0 ); }
135 
136 
140  inline unsigned allocated ( void ) const
141  { return alloc_; }
142 
143 
147  inline unsigned size ( void ) const
148  { return used_; }
149 
150 
154  inline unsigned count ( void ) const
155  { return used_; }
156 
157 
161  inline unsigned lenght ( void ) const
162  { return used_; }
163 
164 
168  inline void seek ( quint64 loc )
169  { iterator_ = loc; if ( iterator_ > used_ ) iterator_ = 0; }
170 
171 
175  inline quint64 position ( void ) const
176  { return iterator_; }
177 
178 
182  void reset ( void );
183 
184 
188  bool reinitFromFile ( QFile & file );
189 
190 
194  bool saveToFile ( QFile & file );
195 
196 
200  unsigned getRaw ( unsigned bytes )
201  {
202  char * ret = (char *)reserve( bytes );
203  if ( ret == NULL ) return 0;
204  return (ret - p_);
205  }
206 
207 
208 
209  /* ******************************************************************* */
211  /* ******************************************************************* */
213 
214 
218  inline void append ( int new_val )
219  {
220  qint64 * loc = (qint64*)reserve( sizeof(qint64) );
221  if ( loc == NULL ) return;
222  *loc = new_val;
223  }
224 
225 
229  inline void append ( unsigned new_val )
230  {
231  quint64 * loc = (quint64*)reserve( sizeof(quint64) );
232  if ( loc == NULL ) return;
233  *loc = new_val;
234  }
235 
236 
240  inline void append ( quint64 new_val )
241  {
242  quint64 * loc = (quint64*)reserve( sizeof(quint64) );
243  if ( loc == NULL ) return;
244  *loc = new_val;
245  }
246 
247 
251  inline void append ( qint64 new_val )
252  {
253  qint64 * loc = (qint64*)reserve( sizeof(qint64) );
254  if ( loc == NULL ) return;
255  *loc = new_val;
256  }
257 
258 
262  inline void append ( bool new_val )
263  {
264  quint64 * loc = (quint64*)reserve( sizeof(quint64) );
265  if ( loc == NULL ) return;
266  *loc = new_val;
267  }
268 
269 
273  inline void append ( double new_val )
274  {
275  double * loc = (double*)reserve( sizeof(double) );
276  if ( loc == NULL ) return;
277  *loc = new_val;
278  }
279 
280 
284  void append ( const QString & new_val );
285 
286 
290  void append (
291  const char * new_val,
292  int len = -1,
293  bool uncounted = false
294  );
295 
296 
298  /* ******************************************************************* */
299 
300 
301  /* ******************************************************************* */
303  /* ******************************************************************* */
305 
306 
311  bool getValue ( bool & out_val, quint64 offset );
312 
313 
322  quint64 getValue ( char * out_val, int max_buf, quint64 offset );
323 
324 
332  bool getValue ( quint64 offset, int count, char * out_val );
333 
334 
343  bool getValue ( char ** out_val, quint64 offset );
344 
345 
350  bool getValue ( double & out_val, quint64 offset );
351 
352 
357  bool getValue ( int & out_val, quint64 offset );
358 
359 
364  bool getValue ( qint64 & out_val, quint64 offset );
365 
366 
373  bool getValue ( QString & out_val, quint64 offset );
374 
375 
380  bool getValue ( quint64 & out_val, quint64 offset );
381 
382 
387  bool getValue ( unsigned & out_val, quint64 offset );
388 
390  /* ******************************************************************* */
391 
392 
393 
394  /* ******************************************************************* */
396  /* ******************************************************************* */
398 
399 
404  inline bool getValue ( int & out_val )
405  { return getValue( out_val, iterator_ ); }
406 
407 
412  inline bool getValue ( unsigned & out_val )
413  { return getValue( out_val, iterator_ ); }
414 
415 
420  inline bool getValue ( qint64 & out_val )
421  { return getValue( out_val, iterator_ ); }
422 
423 
428  inline bool getValue ( quint64 & out_val )
429  { return getValue( out_val, iterator_ ); }
430 
431 
436  inline bool getValue ( bool & out_val )
437  { return getValue( out_val, iterator_ ); }
438 
439 
444  inline bool getValue ( double & out_val )
445  { return getValue( out_val, iterator_ ); }
446 
447 
454  inline bool getValue ( QString & out_val )
455  { return getValue( out_val, iterator_ ); }
456 
457 
466  inline bool getValue ( char ** out_val )
467  { return getValue( out_val, iterator_ ); }
468 
469 
478  inline quint64 getValue ( char * out_val, int max_buf )
479  { return getValue( out_val, max_buf, iterator_ ); }
480 
481 
489  inline bool getValue ( int count, char * out_buf )
490  { return getValue( iterator_, count, out_buf ); }
491 
492 
500  inline bool readUncountedBytes ( int count, char * out_buf )
501  { return getValue( iterator_, count, out_buf ); }
502 
503 
504 
506  /* ******************************************************************* */
507 
508 
509 
510 private:
511 
512 
516  void * reserve ( unsigned bytes );
517 
518 
522  inline void markError ( void )
523  { Q_ASSERT( false ); err_ = true; }
524 
525 
529  inline void markErrorFree ( void )
530  { err_ = false; }
531 
532 
533 
534 
535 
536 
537  /* FUNCTIONS ======================================================= */
538  //
539  //
540  //
541  //
542 
543 }; /* class ByteBuffer */
544 
545 /* CLASS =============================================================== */
546 //
547 //
548 //
549 //
550 
551 } // namespace bbb
552 
553 #endif // __BYTEBUFFER_INC__
554 /* ------------------------------------------------------------------------- */
555 /* ========================================================================= */