GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FrameBlock.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 #ifndef ADF_FrameBlock
24 #include "FrameBlock.h"
25 #endif
26 #ifndef ADF_Key
27 #include "Key.h"
28 #endif
29 
30 using namespace ADF;
31 
33 {
34  if ( IsMode(ConfAgent::kWrite) || IsEoB() )
35  return false;
36 
37  // try to read a key from the input buffer
38  if ( !key->Link(fBuffer,fBuffer.Offset(),key->GetKeyLength()) )
39  { SetEoB(true); return false;}
40 
41  // try to get the full frame from the input
42  fFrame.SetOffset();
43  if ( !fFrame.Link(fBuffer,fBuffer.Offset(),key->GetFrameLength()) ) {
44  { SetEoB(true); return false;}
45  }
47 
48  return true;
49 }
50 
51 Bool_t OneBlock::SetBlock(Char_t *buf, UInt_t size, char opt_rewind, char opt_link_or_copy, UInt_t /*which*/)
52 {
53  /*
54  if ( buf == 0x0 )
55  { fBuffer.Unlink(); Rewind(GetModeIO()); return true; }
56 
57  // try to link, if ok, Rewind without changing the operating mode
58  Bool_t ok = fBuffer.Link(buf,size);
59  if ( ok ) {
60  fBuffer.SetOffset(size); SetEoB(true);
61  if ( opt_rewind == 'r' )
62  Rewind(GetModeIO());
63  }
64  return ok;
65  */
66  if ( buf == 0x0 )
67  { fBuffer.Unlink(); Rewind(GetModeIO()); return true; } // back to the buffer truly owned by this
68 
69  // try to link, if ok, Rewind without changing the operating mode
70  Bool_t ok;
71  if ( opt_link_or_copy == 'l' )
72  ok = fBuffer.Link(buf,size);
73  else
74  ok = fBuffer.Copy(buf,size);
75  if ( ok ) {
76  fBuffer.SetOffset(size); SetEoB(true);
77  if ( opt_rewind == 'r' )
78  Rewind(GetModeIO());
79  }
80  return ok;
81 }
82 
83 /*
84 Bool_t OneBlock::MoveBlock( FrameBlock &block, char opt_rewind, char opt_link_or_copy, UInt_t which)
85 {
86  BufferIO *io = block.GetBufferIO();
87 
88 
89  return false;
90 }
91 */
92 
93 Bool_t OneBlock::Reserve(UInt_t size, Bool_t switch_eob, UInt_t /*which*/)
94 {
95  if ( IsMode(ConfAgent::kRead) || IsEoB() ) {
96  return false;
97  }
98 
99  // check if enough place
100  Bool_t add_ok = true;
101  if ( fBuffer.FreeSize() < size )
102  add_ok = Expand( fBuffer.Offset() + size );
103  // not enought place
104  if ( !add_ok ) {
105  if ( switch_eob )
106  SetEoB(true);
107  return false;
108  }
109  return true;
110 }
111 
112 Bool_t OneBlock::AddFrame(const BufferIO &ext_frame, UInt_t)
113 {
114  Bool_t ok = Reserve(ext_frame.Size());
115  if ( ok )
116  ok = BufferIO::Copy(ext_frame,fBuffer,ext_frame.Size());
117 
118  SetEoB(!ok); return ok;
119 }
120 
121 Bool_t OneBlock::AddFrame(const Frame &frame, UInt_t)
122 {
123  Bool_t ok = Reserve(frame.GetLength());
124  if ( ok )
125  ok = frame.Export(fBuffer);
126 
127  SetEoB(!ok); return ok;
128 }
129 
131 {
132  // the buffer has just been filled, so keep the current position
133  // as the effective size of the buffer
134  if ( GetModeIO() == ConfAgent::kWrite && mode == ConfAgent::kRead ) {
136  fBuffer.SetOffset();
137  }
138  else {
139  fBuffer.FastReset();
140  }
141  SetEoB(false); SetModeIO(mode);
142 }
143 
145 {
146  // cannot expand anymore
147  if ( ! fBuffer.Expand(size) )
148  return false;
149 
150  return true;
151 }
152 
154  OneBlock(size,mode),
155  fCurrentSize(0L),
156  fMaxSize(kMaxUInt_t),
157  fFileSnapshot(0L),
158  fFile(0x0)
159 {
160 }
161 
163 {
164  fCurrentSize = 0u; fMaxSize = kMaxUInt_t; fFile = 0x0;
165  SetEoB(false);
166 }
167 
169 {
170  if ( IsMode(ConfAgent::kWrite) || fFile == 0x0 )
171  return false;
172 
173  // read from the file a buffer with a length corresponding to a key
174  UInt_t lengthkey = key->GetKeyLength(), lengthkeyread, lengthdata, lengthdataread;
175 
176  // read the key part
177  fBuffer.SetOffset();
178  lengthkeyread =
179  fBuffer.Import(fFile,lengthkey);
180  if ( lengthkeyread != lengthkey ) { // cannot read anymore a key from the file
181  SetEoB(true);
182  return false;
183  }
184  else fCurrentSize += lengthkeyread;
185 
186  // link the key part to get the data length
187  key->Link(fBuffer,0u,lengthkey);
188  // extract from the key the data part
189  lengthdata = key->GetDataLength();
190  // a coherent length has been found
191  if ( lengthdata != 0u ) {
192  // read from file
193  lengthdataread = fBuffer.Import(fFile,lengthdata);
194  // test if reading ok
195  if ( lengthdataread != lengthdata )
196  { SetEoB(true); return false; }
197  }
198  else { SetEoB(true); return false; }
199 
200  // reading ok
201  fCurrentSize += lengthdataread;
202 
203  // now link the content of buffer in fFrame
204  fFrame.SetOffset();
206 
207  return true; // new frame properly loaded
208 }
209 
210 Bool_t FileBlock::AddFrame(const BufferIO &ext_frame, UInt_t)
211 {
212  Bool_t ok = Reserve(ext_frame.Size());
213  if ( ok ) {
214  ok = ext_frame.Export(fFile,ext_frame.Size()) ? true : false;
215  if ( ok )
216  fCurrentSize += ext_frame.Size();
217  }
218  SetEoB(!ok); return ok;
219 }
220 
221 Bool_t FileBlock::AddFrame(const Frame &frame, UInt_t)
222 {
223  const Key *key = frame.GetKey();
224 
225  Bool_t ok = Reserve(frame.GetLength());
226  if ( ok ) {
227  ok = key->GetRealBuffer()->Export(fFile,key->GetKeyLength()) ? true : false;
228  ok = ok && frame.GetRealBuffer()->Export(fFile,key->GetDataLength());
229  if ( ok )
230  fCurrentSize += frame.GetLength();
231  }
232  SetEoB(!ok); return ok;
233 }
234 
235 Bool_t FileBlock::Reserve(UInt_t size, Bool_t switch_eob, UInt_t /*which*/)
236 {
237  if ( size <= (fMaxSize-fCurrentSize) )
238  return true;
239 
240  if ( switch_eob )
241  SetEoB(true);
242 
243  return false;
244 }
245 
246 class NarvalBlock : public OneBlock
247 {
248 protected:
249  virtual Bool_t Expand(UInt_t);
250 
251 public:
252  // in principle just used with external buffers linked
254  {;}
255  virtual ~NarvalBlock()
256  {;}
257 
258  virtual Bool_t Reserve(UInt_t, Bool_t /*switch_eob*/ = false, UInt_t /*which*/ = 0u);
259 
260 };
261 
262 Bool_t NarvalBlock::Reserve(UInt_t /*size*/, Bool_t /*switch_eob*/, UInt_t /*which*/)
263 {
264  // not enought is the current block, try to expand by calling switch_output_block
265 // if ( ! OneBlock::Reserve(size) ) {
266 //
267 // }
268  return false;
269 }
270 
271 Bool_t NarvalBlock::Expand(UInt_t)
272 {
273  /*
274  char **new_ad = new char; UInt_t new_size;
275 
276  void switch_output_block(fBuffer.Size(),void **output_address,UInt_t new_available_length);
277  if( new_available_length < size )
278  { return false ;}
279  */
280 // void switch_output_block(UInt_t *used_length,void **output_address,UInt_t new_available_length);
281  return true;
282 }
283 
284 
285 
286 
virtual const BufferIO * GetRealBuffer() const
Definition: Key.h:118
virtual Bool_t NextFrame(Key *)
return true if there is a new frame available (in reading mode)
Definition: FrameBlock.cpp:32
virtual Key * GetKey()=0
To get the Key associated to this frame.
Base class for a Key.
Definition: Key.h:56
virtual void Rewind(ConfAgent::EMode mode=ConfAgent::kRead)
Rewind to be ready to be used again.
Definition: FrameBlock.cpp:130
virtual Bool_t SetBlock(Char_t *=0x0, UInt_t=0u, char= 'r', char= 'l', UInt_t=0u)
Attach a external block to this.
Definition: FrameBlock.cpp:51
header file for FrameBlock.cpp
Base class for a Frame.
Definition: Frame.h:73
virtual Bool_t Link(const Char_t *from, UInt_t from_size)
Link an external buffer to this class.
Definition: BufferIO.cpp:214
virtual Bool_t Reserve(UInt_t, Bool_t=false, UInt_t=0u)
check if enough place to write something (writing mode)
Definition: FrameBlock.cpp:93
Bool_t IsMode(ConfAgent::EMode mode) const
Definition: FrameBlock.h:83
virtual const BufferIO * GetRealBuffer() const =0
give access to the underlying buffer of the data part
virtual Bool_t Reserve(UInt_t, Bool_t=false, UInt_t=0u)
check if enough place to write something (writing mode)
Definition: FrameBlock.cpp:262
FileBlock(UInt_t size=aMByte, ConfAgent::EMode mode=ConfAgent::kRead)
Definition: FrameBlock.cpp:153
EMode
Operating mode for frame IO.
Definition: ConfAgent.h:67
void SetEoB(Bool_t b)
Definition: FrameBlock.h:89
virtual UInt_t GetDataLength() const
Definition: Key.h:134
ConfAgent::EMode GetModeIO() const
Return the operating mode.
Definition: FrameBlock.h:79
const UInt_t kMaxUInt_t
Definition: ADFConfig.h:100
virtual void SetModeIO(ConfAgent::EMode mode)
Definition: FrameBlock.h:81
virtual Bool_t AddFrame(const BufferIO &, UInt_t i=0u)
Add a Frame to the block.
Definition: FrameBlock.cpp:210
virtual Char_t * Unlink()
Unlink the external buffer to this class.
Definition: BufferIO.cpp:373
header file for Key.cpp
virtual Bool_t Export(BufferIO &buf) const =0
Export the content of the frame in a buffer.
UInt_t Offset() const
it returns the current position in the buffer
Definition: BufferIO.h:240
UInt_t Size() const
it returns the maximum number of bytes in this buffer
Definition: BufferIO.h:220
virtual ~NarvalBlock()
Definition: FrameBlock.cpp:255
virtual UInt_t GetKeyLength() const
Unique number corresponding to a type of Key.
Definition: Key.h:210
Bool_t IsEoB() const
Definition: FrameBlock.h:86
virtual Bool_t AddFrame(const BufferIO &, UInt_t i=0u)
Add a Frame to the block.
Definition: FrameBlock.cpp:112
UInt_t FreeSize() const
it returns the number of free bytes to the end
Definition: BufferIO.h:236
virtual UInt_t Export(Char_t *, UInt_t) const
Export this buffer to an array.
Definition: BufferIO.h:694
virtual Bool_t Copy(const Char_t *from, UInt_t from_size)
copy an external buffer to this.
Definition: BufferIO.cpp:248
const UInt_t aByte
Definition: BaseBuffer.h:34
BufferIO fBuffer
The buffer to look for Frames.
Definition: FrameBlock.h:217
virtual Bool_t NextFrame(Key *)
Read next frame from file.
Definition: FrameBlock.cpp:168
virtual UInt_t GetLength() const =0
total length for that frame
virtual Bool_t Expand(UInt_t new_size)
Expand the size of the current buffer.
Definition: BufferIO.cpp:162
BufferIO fFrame
to wrap the current Frame
Definition: FrameBlock.h:219
virtual Bool_t Link(const Char_t *, UInt_t)
Link an external buffer to this key.
Definition: Key.cpp:74
UInt_t SetEffectiveSize(UInt_t size=kMaxUInt_t)
in case the buffer is partly filled and you would like to read it again
Definition: BufferIO.cpp:152
UInt_t Import(const Char_t *from, UInt_t size_ext_buf)
Import the given array in this buffer.
Definition: BufferIO.cpp:298
NarvalBlock(UInt_t size=aByte)
Definition: FrameBlock.cpp:253
virtual Bool_t Expand(UInt_t)
implements Expand method
Definition: FrameBlock.cpp:144
virtual void FastReset()
Definition: BufferIO.cpp:117
virtual Bool_t Expand(UInt_t)
This kind of Block is not expandable.
Definition: FrameBlock.cpp:271
const Int_t size
Definition: BenchIO.C:24
virtual Bool_t Expand(UInt_t)
This kind of Block is not expandable.
Definition: FrameBlock.h:225
It implements an in-memory block of Frames.
Definition: FrameBlock.h:206
virtual UInt_t GetFrameLength() const
Definition: Key.h:136
virtual Bool_t Reserve(UInt_t, Bool_t=false, UInt_t=0u)
check if enough place to write something (writing mode)
Definition: FrameBlock.cpp:235
UInt_t SetOffset(UInt_t off=0u) const
change the current position.
Definition: BufferIO.cpp:122