GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Buffer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #include "Buffer.h"
24 #include <iostream>
25 
26 using namespace Gws;
27 
28 Buffer::Buffer(UInt_t s): fStatus(kBad), fEndian(Memory::kLittle), fBuffer(0), fSize(0), fCurrent(0)
29 {
30 // if ( Memory::IsBytes(Memory::kBig) ) fEndian = Memory::kBig;
31 // else fEndian = Memory::kLittle;
32 
34 }
35 
37 {
38  if ( fBuffer != 0x0 ) { Memory::Delete(fBuffer); }
39 /*
40  if ( fBuffer == 0x0 ) {
41  if ( IsStatus(Buffer::kCorrupt) ) {
42  std::cerr << " WARNING in Buffer::~Buffer() - the buffer has been corrupted \n " ;
43  }
44  if ( ! IsStatus(Buffer::kBad) ) { Memory::Delete(fBuffer); }
45  } */
46 }
47 
48 void Buffer::DoAlloc(UInt_t s)
49 {
50  // delete the previous one and allocate a new one that is fill with 0
52 
53  // cannot allocate memory
54  if ( fBuffer == 0x0 ) { // becomes a bad buffer
55  fStatus = Buffer::kBad; fSize = 0; fCurrent = 0;
56  }
57  else {
58  fStatus = Buffer::kGood; fSize = s; fCurrent = 0; fBuffer[fSize] = fC1; fBuffer[fSize+1] = fC2;
59  }
60 }
61 
62 /*
63 void Buffer::Copy(Buffer &from, Buffer &to)
64 {
65 
66 } */
67 
69 {
70  Buffer *b = 0x0;
71 
72  if ( e == Memory::GetEndian() ) { /* printf("Buffer allocated \n"); */ b = new Buffer(s); }
73  else { /* printf("EndianBuffer allocated \n");*/ b = new EndianBuffer(s); }
74 
75  if ( b ) b->SetBytes(e); return b;
76 }
77 
79 {
80  if ( fSize > 0 ) { Memory::Zero(fBuffer,fSize); fCurrent = 0; }
81 }
82 
83 void Buffer::Reset(UInt_t newsize)
84 {
85  DoAlloc(newsize);
86 }
87 
88 UInt_t Buffer::SetOffset(UInt_t off)
89 {
90  UInt_t c = fCurrent;
91 
92  if ( off > fSize ) fCurrent = fSize;
93  else fCurrent = off;
94 
95  return c;
96 }
97 
98 Bool_t Buffer::Expand(UInt_t add)
99 {
100  Char_t *newbuf = 0x0; newbuf = Memory::New(newbuf,Size()+add+2);
101 
102  // cannot allocate the corresponding memory
103  if ( newbuf == 0x0 ) return false;
104 
105  // copy the content of the old buffer before deleting it
106  ::memcpy(newbuf,fBuffer,Size()); Memory::Delete(fBuffer); fBuffer = newbuf; fSize = Size()+add; return true;
107 }
108 
110 {
111 // printf("endian buffer allocated \n");
113  else fEndian = Memory::kBig;
114 }
115 
117 
118 
EEndian
The adjectives big-endian and little-endian refer to which bytes are most significant in multi-byte d...
Definition: Memory.h:59
TBrowser * b
Char_t * fBuffer
Definition: Buffer.h:67
Int_t fStatus
Definition: Buffer.h:64
Interface to the memory.
Definition: Memory.h:48
static Int_t GetEndian()
Definition: Memory.h:83
UInt_t Size() const
it returns the maximum number of bytes in this buffer
Definition: Buffer.h:165
Int_t fEndian
Definition: Buffer.h:65
UInt_t fCurrent
Definition: Buffer.h:69
virtual void Reset()
Reset means set all elements to 0 and the current position is 0.
Definition: Buffer.cpp:78
A buffer is used to read/write raw data buffers from/on files.
Definition: Buffer.h:43
static bool IsBytes(Memory::EEndian e)
check out the endian type of the running system
Definition: Memory.h:82
void DoAlloc(UInt_t)
Definition: Buffer.cpp:48
static Buffer * New(Memory::EEndian e, UInt_t s=32 *KBYTE)
copy n bytes from one buffer to another one
Definition: Buffer.cpp:68
UInt_t SetOffset(UInt_t off=0)
change the current position.
Definition: Buffer.cpp:88
static void Zero(Char_t *p, Int_t nb)
fast initialization of the buffer at 0
Definition: Memory.h:130
static Char_t * New(Char_t *p, Int_t nb=32 *KBYTE)
To allocate a buffer that is set to 0 (some compilers don't do it)
Definition: Memory.h:186
Buffer(UInt_t s=32 *KBYTE)
Default size for a Buffer is 32 KBYTE.
Definition: Buffer.cpp:28
void SetBytes(Memory::EEndian)
Definition: Buffer.h:163
UInt_t fSize
Definition: Buffer.h:68
header file for Buffer.cpp
EndianBuffer(UInt_t s=32 *KBYTE)
Definition: Buffer.cpp:109
A Endianbuffer is used to read/write raw data buffers from/on files.
Definition: Buffer.h:269
virtual ~EndianBuffer()
Definition: Buffer.cpp:116
static void Delete(Char_t *p)
Definition: Memory.h:114
virtual Bool_t Expand(UInt_t)
Expand the actual size of the buffer.
Definition: Buffer.cpp:98
virtual ~Buffer()
Definition: Buffer.cpp:36