GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Frame.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_KeyFactory
24 #include "KeyFactory.h"
25 #endif
26 #ifndef ADF_Frame
27 #include "Frame.h"
28 #endif
29 #ifndef ADF_ConfigurationFrame
30 #include "ConfigurationFrame.h"
31 #endif
32 #include <sstream>
33 #include <fstream>
34 
35 using namespace ADF;
36 
37 
39 fSignature("Default","",Version(0,0)),
40 // Migration SharedFP fZombie(false),
41 // Migration SharedFP fKeyChange(ChangeOfVersion),
42 // Migration SharedFP fFrameChange(NoChange),
43 fCheckLevel(0)
44 {
45 }
46 
47 
49 fSignature("Default","",Version(0,0)),
50 fCheckLevel(0)
51 {
52 }
53 
55 {
56 }
57 
58 Bool_t Frame::Configure(const char *name, const char *option)
59 {
60  Bool_t ok; std::string opt = option;
61  if ( opt == "in" ) {
62  std::ifstream filein(name);
63  if ( filein.is_open() == true ) {
64  ok = DoConfigure(filein);
65  }
66  else
67  ok = false;
68  filein.close();
69  }
70  else {
71  std::ofstream fileout(name);
72  if ( fileout.is_open() == true ) {
73  ok = DoConfigure(fileout);
74  }
75  else
76  ok = false;
77  fileout.close();
78  }
79  return ok;
80 }
81 
82 Bool_t Frame::Configure(ConfigurationFrame *confframe, const char *option)
83 {
84  Bool_t ok; std::string opt = option;
85 
86  if ( opt == "in" ) {
87  // load the content of the frame in the string
88  confframe->Read();
89  // use stream facility
90  std::istringstream in;
91  in.clear();
92  in.str((confframe->String()));
93  ok = DoConfigure(in);
94  }
95  else {
96  // use stream facility
97  std::ostringstream out;
98  ok = DoConfigure(out);
99  confframe->String() = out.str();
100  confframe->Write();
101  }
102  return ok;
103 }
104 
105 /*
106  ConcreteFrame::ConcreteFrame():
107  Frame(),
108  fBuffer(0),
109  fKey(0)
110  {
111  fBuffer = new BufferIO(); Reset(); // default is length 1 byte, endian type of the system
112  }
113  */
114 
116 Frame(),
117 fBuffer(0),
118 fKey(key)
119 {
120  // same endian as the key and extract the length from it.
122  if ( fKey )
123  { e = fKey->GetEndian(); s = fKey->GetDataLength(); }
124  //
125  fBuffer = BufferIO::New(e,s);
126  //
127  Reset();
128 }
129 
131 {
132  // destructor
133  if ( fKey )
134  delete fKey;
135  fKey = 0x0;
136 
137  if ( fBuffer )
138  delete fBuffer;
139  fBuffer = 0x0;
140 }
141 
142 Bool_t ConcreteFrame::Copy(const Frame *frame)
143 {
144  Bool_t ok;
145 
146  // have to be the same frame and the same key
147  // if ( !(GetSignature() == frame->GetSignature()) )
148  // return false;
149  // only the true implemtation should be the same
150  if ( !(fKey->GetSignature() == frame->GetKey()->GetSignature()) )
151  return false;
152  if ( !(GetSignature() == frame->GetSignature()) )
153  return false;
154 
155  ok = Copy(frame->GetKey()->GetRealBuffer()->GetAddress(),frame->GetKey()->GetKeyLength(),'k');
156  ok = ok &&
157  Copy(frame->GetRealBuffer()->GetAddress(),frame->GetKey()->GetDataLength(),'f');
158 
159  return ok;
160 }
161 
162 Bool_t ConcreteFrame::Link(const Frame *frame)
163 {
164  Bool_t ok;
165 
166  // have to be the same frame and the same key
167  // if ( !(GetSignature() == frame->GetSignature()) )
168  // return false;
169  if ( !(fKey->GetSignature() == frame->GetKey()->GetSignature()) )
170  return false;
171  if ( !(GetSignature() == frame->GetSignature()) )
172  return false;
173 
174  ok = Link(frame->GetKey()->GetRealBuffer()->GetAddress(),frame->GetKey()->GetKeyLength(),'k');
175  ok = ok && Link(frame->GetRealBuffer()->GetAddress(),frame->GetKey()->GetDataLength(),'f');
176 
177  return ok;
178 }
179 
180 void ConcreteFrame::Dump(const Char_t *filename, Bool_t anew) const
181 {
182  FILE *dfile = NULL;
183 
184  if ( anew )
185  dfile = ::fopen(filename,"wb"); // try to open the file in writting mode
186  else
187  dfile = ::fopen(filename,"ab"); // try to open the file in append mode
188 
189  if ( dfile == NULL )
190  return;
191 
192  ::fwrite(fKey->GetRealBuffer()->GetAddress(),1,fKey->GetKeyLength(),dfile);
193  ::fwrite(fBuffer->GetAddress(),1,fKey->GetDataLength(),dfile);
194 
195  fclose(dfile);
196 }
197 
198 Bool_t ConcreteFrame::Dump(FILE *dfile) const
199 {
200  size_t count;
201 
202  count = 0;
203  count += ::fwrite(fKey->GetRealBuffer()->GetAddress(),1,fKey->GetKeyLength(),dfile);
204  count += ::fwrite(fBuffer->GetAddress(),1,fKey->GetDataLength(),dfile);
205 
206  return count == fKey->GetFrameLength();
207 }
208 
209 Bool_t ConcreteFrame::Load(FILE *dfile)
210 {
211  UInt_t count;
212 
213  // reset the Frame
214  Reset();
215 
216  // check if enough space to read the key part and after the data and do it if all right
217  count = 0u;
218  fKey->RealBuffer()->SetOffset();
219  count += fKey->RealBuffer()->Import(dfile,fKey->GetKeyLength());
220 
221  fBuffer->SetOffset();
222  if ( ! fBuffer->Reserve(fKey->GetDataLength()) )
223  return false;
224  count += fBuffer->Import(dfile,fKey->GetDataLength());
225 
226  return count == fKey->GetFrameLength();
227 }
228 
229 void ConcreteFrame::Stallion(UInt_t repetition)
230 {
231  // to produce 64 bits like stallions
232  UInt_t bwritten, offset, toadd; std::string s, send;
233 
234  send = "||||||||||||";
235 
236  s = "|";
237  s+= GetSignature().GetFactoryName();
238  s+= ":";
239  s+= GetSignature().GetItemName();
241  s+= "|";
242  s+= GetSignature().GetFactoryName();
243  s+= ":";
244  s+= GetSignature().GetItemName();
245  s+= GetSignature().GetVersion().GetString();
246  s+= "|";
247 
248  offset = (GetKey()->GetKeyLength() + repetition*s.size()) % 8u;
249  toadd = 8u - offset;
250 
251  Reset(); (*fBuffer).Reserve(repetition*UInt_t(s.size())+toadd);
252  bwritten = 0u;
253  for (UInt_t i = 0u; i < repetition; i++ ) {
254  if ( (*fBuffer).Import(s.data(),UInt_t(s.size())) != s.size() )
255  break;
256 
257  bwritten += UInt_t(s.size());
258  }
259  if ( toadd > 0u ) {
260  (*fBuffer).Import(send.data(),toadd); bwritten += toadd;
261  }
262  fKey->SetDataLength(bwritten);
263 }
264 
265 
267 fFrame(0x0),
268 fIsValid(false),
269 fKeyChange(ChangeOfVersion),
270 fFrameChange(ChangeOfVersion)
271 {
272  SetFrame(f);
273 }
274 
276 {
277  Frame *oldf = fFrame;
278  fFrame = newf;
279 
280  if ( fFrame )
281  fIsValid = true;
282  else
283  fIsValid = false;
284 
285  return oldf;
286 }
287 
288 
289 
290 
291 
292 
293 
virtual const BufferIO * GetRealBuffer() const
Definition: Key.h:118
virtual Bool_t Reserve(UInt_t size)
to reserve a given size (from the current position) to be sure one are able to write something ...
Definition: BufferIO.cpp:199
virtual Bool_t Copy(const Char_t *buf, UInt_t size, const char opt= 'b')
copy a buffer to this Frame
Definition: Frame.h:387
virtual Key * GetKey()=0
To get the Key associated to this frame.
Base class for a Key.
Definition: Key.h:56
Base class for a Frame.
Definition: Frame.h:73
header file for KeyFactory.cpp
Bool_t ChangeOfVersion(const FactoryItem &item_from, const FactoryItem &item_to)
change from item_from to item_to is allowed if they differ only by the item version number ...
Definition: FactoryItem.h:130
const FactoryItem & GetSignature() const
Definition: Key.h:115
virtual BufferIO * RealBuffer()
Used by specific methods.
Definition: Key.h:87
A configuration frame is just an encapsulation of a string.
virtual Bool_t Load(FILE *)
load a Frame from a C file (for debugging) - return the number of bytes read
Definition: Frame.cpp:209
virtual const BufferIO * GetRealBuffer() const =0
give access to the underlying buffer of the data part
virtual void Dump(const Char_t *filename, Bool_t anew=false) const
Dump the content of the frame in a file (for debugging)
Definition: Frame.cpp:180
virtual Key * GetKey()
To get the Key associated to this frame.
Definition: Frame.h:344
virtual UInt_t GetDataLength() const
Definition: Key.h:134
virtual Bool_t Link(const Char_t *buf, UInt_t size, const char opt= 'b')
Link a buffer to this Frame.
Definition: Frame.h:374
virtual BaseBuffer::EEndian GetEndian() const
Definition: Key.h:126
virtual UInt_t Read()
It reads the content of the string from the Frame.
BufferIO * fBuffer
Definition: Frame.h:310
virtual Bool_t Configure(const char *name, const char *option)
configuration from/to a file
Definition: Frame.cpp:58
virtual void Stallion(UInt_t repetition=4u)
Fill the frame with a given pattern (used for debugging)
Definition: Frame.cpp:229
header file for Frame.cpp
virtual UInt_t GetKeyLength() const
Unique number corresponding to a type of Key.
Definition: Key.h:210
virtual void Reset()
copy the sub-frame #i to the Frame given in the second argument
Definition: Frame.h:522
Base class for version numbers.
Definition: Version.h:38
static EEndian SysEndian()
Definition: BaseBuffer.h:96
virtual UInt_t Write()
It writes to the Frame the content of the string.
const Char_t * GetAddress() const
Pointer to the current underlying array of bytes.
Definition: BufferIO.h:216
ConcreteFrame(Key *)
Definition: Frame.cpp:115
const std::string & GetFactoryName() const
Definition: FactoryItem.h:76
std::string GetString() const
Definition: Version.cpp:44
static BufferIO * New(BaseBuffer::EEndian e, UInt_t s=aByte)
return the right buffer taking Into account the endian argument compared to the underlying endian sys...
Definition: BufferIO.cpp:94
virtual ~Frame()
Definition: Frame.cpp:54
const UInt_t aByte
Definition: BaseBuffer.h:34
Frame()
To avoid direct creation of a Frame.
Definition: Frame.cpp:38
Version GetVersion() const
Definition: FactoryItem.h:88
virtual Frame * SetFrame(Frame *)
change the frame, returns the current one
Definition: Frame.cpp:275
UInt_t Import(const Char_t *from, UInt_t size_ext_buf)
Import the given array in this buffer.
Definition: BufferIO.cpp:298
SharedFP(Frame *f=0x0)
Definition: Frame.cpp:266
virtual Bool_t DoConfigure(std::istream &)
configure this from an input stream
Definition: Frame.h:101
virtual ~ConcreteFrame()
Definition: Frame.cpp:130
const std::string & GetItemName() const
Definition: FactoryItem.h:78
virtual UInt_t GetFrameLength() const
Definition: Key.h:136
const FactoryItem & GetSignature() const
Signature of that frame.
Definition: Frame.h:127
UInt_t SetOffset(UInt_t off=0u) const
change the current position.
Definition: BufferIO.cpp:122