DbiBinaryFile Class Reference

#include <DbiBinaryFile.h>

List of all members.

Public Member Functions

 DbiBinaryFile (const char *fileName="", Bool_t input=kTRUE)
 ~DbiBinaryFile ()
string GetFileName () const
Bool_t IsOK () const
Bool_t IsReading () const
Bool_t IsWriting () const
void Close ()
DbiBinaryFileoperator>> (Bool_t &num)
DbiBinaryFileoperator<< (const Bool_t &num)
DbiBinaryFileoperator>> (Int_t &num)
DbiBinaryFileoperator<< (const Int_t &num)
DbiBinaryFileoperator>> (UInt_t &num)
DbiBinaryFileoperator<< (const UInt_t &num)
DbiBinaryFileoperator>> (Double_t &num)
DbiBinaryFileoperator<< (const Double_t &num)
DbiBinaryFileoperator>> (VldTimeStamp &ts)
DbiBinaryFileoperator<< (const VldTimeStamp &ts)
DbiBinaryFileoperator>> (string &str)
DbiBinaryFileoperator<< (const string &str)
DbiBinaryFileoperator>> (VldRange &vr)
DbiBinaryFileoperator<< (const VldRange &vr)
DbiBinaryFileoperator>> (vector< DbiTableRow * > &arr)
DbiBinaryFileoperator<< (vector< DbiTableRow * > &arr)
char * ReleaseArrayBuffer ()

Static Public Member Functions

static Bool_t CanReadL2Cache ()
static Bool_t CanWriteL2Cache ()
static void SetWorkDir (const string &dir)
static void SetReadAccess (Bool_t access=kTRUE)
static void SetWriteAccess (Bool_t access=kTRUE)

Private Member Functions

Bool_t CanRead ()
Bool_t CanWrite ()
void CheckFileStatus ()
Bool_t Read (char *bytes, UInt_t numBytes)
Bool_t Write (const char *bytes, UInt_t numBytes)

Private Attributes

fstream * fFile
 Associated file, may be null.
Bool_t fReading
Bool_t fHasErrors
char * fArrayBuffer
string fFileName

Static Private Attributes

static string fgWorkDir
static Bool_t fgReadAccess = kTRUE
static Bool_t fgWriteAccess = kTRUE

Detailed Description

Id
DbiBinaryFile.h,v 1.6 2006/08/08 10:51:32 west Exp

Definition at line 45 of file DbiBinaryFile.h.


Constructor & Destructor Documentation

DbiBinaryFile::DbiBinaryFile ( const char *  fileName = "",
Bool_t  input = kTRUE 
)

Definition at line 54 of file DbiBinaryFile.cxx.

References fFile, fFileName, fgReadAccess, fgWorkDir, fgWriteAccess, fHasErrors, Msg::kDebug, mode, and MSG.

00054                                                                 :
00055 fFile(0),
00056 fReading(input),
00057 fHasErrors(kFALSE),
00058 fArrayBuffer(0)
00059 {
00060 //
00061 //
00062 //  Purpose:  Default Constructor.
00063 //
00064 //  Arguments: 
00065 //    fileName     in    File name (default: "" => file is a dummy)
00066 //    input        in    true if reading (default = kTRUE)
00067 
00068 //  Specification:-
00069 //  =============
00070 //  
00071 //  If file name or fgWorkDir is dummy, or the appropriate access is not set
00072 //  then name is set to dummy otherwise fgWorkDir is prepended to the name.
00073 
00074   // Complete the file name.
00075   fFileName = fileName;
00076   if ( fFileName != "" ) {
00077     Bool_t access = input ? fgReadAccess : fgWriteAccess;
00078     if ( fgWorkDir == "" || ! access ) fFileName = "";
00079     else                               fFileName = fgWorkDir +  fFileName;
00080   }
00081 
00082   // Open the file.
00083   ios_base::openmode mode = ios_base::in|ios_base::binary;
00084   if ( ! input ) mode = ios_base::out|ios_base::binary;
00085 
00086   if ( fFileName == "" ) fHasErrors = kTRUE;
00087   else {
00088     fFile = new fstream(fFileName.c_str(),mode);
00089     if ( ! fFile->is_open() || ! fFile->good() ) {
00090       MSG("Dbi",Msg::kDebug) << "Cannot open " << fFileName
00091                              << "; all I/O will fail." << endl;
00092       fHasErrors = kTRUE;
00093     }
00094   }
00095 
00096 }
//.....................................................................

DbiBinaryFile::~DbiBinaryFile (  ) 

Definition at line 99 of file DbiBinaryFile.cxx.

References Close(), fArrayBuffer, and fFile.

00100 {
00101 //
00102 //
00103 //  Purpose:  Default Destructor.
00104 
00105   delete[] fArrayBuffer;
00106   fArrayBuffer = 0;
00107   this->Close();
00108   delete fFile;
00109   fFile = 0;
00110 
00111 }


Member Function Documentation

Bool_t DbiBinaryFile::CanRead (  )  [private]

Definition at line 402 of file DbiBinaryFile.cxx.

References fReading, IsOK(), Msg::kError, and MAXMSG.

Referenced by operator>>(), and Read().

00402                               {
00403 
00404   if ( ! fReading ) {
00405     MAXMSG("Dbi",Msg::kError,20) << "Attempting to read from a write-only file" << endl;
00406     return kFALSE;
00407   }
00408   return this->IsOK();
00409 
00410 }

static Bool_t DbiBinaryFile::CanReadL2Cache (  )  [inline, static]

Definition at line 104 of file DbiBinaryFile.h.

References fgReadAccess, and fgWorkDir.

00104 { return fgWorkDir.size() && fgReadAccess; }

Bool_t DbiBinaryFile::CanWrite (  )  [private]

Definition at line 413 of file DbiBinaryFile.cxx.

References fReading, IsOK(), Msg::kError, and MAXMSG.

Referenced by operator<<(), and Write().

00413                                {
00414 
00415   if ( fReading ) {
00416     MAXMSG("Dbi",Msg::kError,20) << "Attempting to write to a read-only file" << endl;
00417     return kFALSE;
00418   }
00419   return this->IsOK();
00420 
00421 }

static Bool_t DbiBinaryFile::CanWriteL2Cache (  )  [inline, static]

Definition at line 105 of file DbiBinaryFile.h.

References fgWorkDir, and fgWriteAccess.

00105 { return fgWorkDir.size() && fgWriteAccess; }

void DbiBinaryFile::CheckFileStatus (  )  [private]

Definition at line 425 of file DbiBinaryFile.cxx.

References Close(), fFile, fFileName, fHasErrors, fReading, gSystem(), Msg::kError, and MAXMSG.

Referenced by operator>>(), Read(), and Write().

00425                                     {
00426 
00427 //  If file was good but has just gone bad, report and close it.
00428 //  Delete it if writing.
00429 
00430   if (    fFile 
00431        && ! fHasErrors  
00432        && ( ! fFile->is_open() || ! fFile->good() ) ) {
00433     MAXMSG("Dbi",Msg::kError,20) << "File not open or has gone bad,"
00434                            << " all further I/O will fail." << endl;
00435     fHasErrors = kTRUE;
00436     this->Close();
00437 
00438     //Delete file if writing.
00439     if ( ! fReading ) {
00440       MAXMSG("Dbi",Msg::kError,20) << "Erasing " << fFileName << endl;
00441       gSystem->Unlink(fFileName.c_str());
00442     }
00443 
00444   }
00445 
00446 }

void DbiBinaryFile::Close (  ) 

Definition at line 114 of file DbiBinaryFile.cxx.

References fFile.

Referenced by CheckFileStatus(), operator>>(), and ~DbiBinaryFile().

00115 {
00116 //
00117 //
00118 //  Purpose:  Close file.
00119 
00120   if ( fFile ) fFile->close();
00121 }

string DbiBinaryFile::GetFileName (  )  const [inline]

Definition at line 56 of file DbiBinaryFile.h.

References fFileName.

00056 { return fFileName; }

Bool_t DbiBinaryFile::IsOK (  )  const [inline]

Definition at line 57 of file DbiBinaryFile.h.

References fHasErrors.

Referenced by CanRead(), CanWrite(), IsReading(), and IsWriting().

00057 { return ! fHasErrors;}

Bool_t DbiBinaryFile::IsReading (  )  const [inline]

Definition at line 58 of file DbiBinaryFile.h.

References fReading, and IsOK().

Referenced by DbiResult::Streamer(), DbiResultNonAgg::Streamer(), and DbiValidityRec::Streamer().

00058 { return this->IsOK() && fReading; }

Bool_t DbiBinaryFile::IsWriting (  )  const [inline]

Definition at line 59 of file DbiBinaryFile.h.

References fReading, and IsOK().

Referenced by DbiResult::Streamer(), DbiResultNonAgg::Streamer(), and DbiValidityRec::Streamer().

00059 { return this->IsOK() && ! fReading; }

DbiBinaryFile & DbiBinaryFile::operator<< ( vector< DbiTableRow * > &  arr  ) 

Definition at line 349 of file DbiBinaryFile.cxx.

References EndMarker, and StartMarker.

00349                                                                     { 
00350 //
00351 //
00352 //  Purpose: Write a vector of objects inheriting from DbiTableRow.
00353 
00354 //  Format of record:-
00355 //  
00356 //  Int_t   StartMarker  Start of record marker = 0xaabbccdd
00357 //  Int_t   arrSize      Size of vector
00358 //  
00359 //  If size of vector > 0 this is folowed by:-
00360 //  
00361 //  string  objName      Name of object
00362 //  Int_t   objSize      Size of object
00363 //  char*                The data arrSize*objSize bytes long
00364 //  
00365 //  The record concludes:-
00366 //  
00367 //  Int_t     EndMarker  End of record marker = 0xddbbccaa
00368 
00369 
00370   if ( ! this->CanWrite() ) return *this;
00371 
00372   UInt_t marker = StartMarker;
00373   (*this) << marker;
00374   Int_t arrSize = arr.size();
00375   (*this) << arrSize;
00376 
00377   if ( arrSize ) {
00378     DbiTableRow* obj = arr[0];
00379     Int_t objSize  = obj->IsA()->Size();
00380     string objName = obj->ClassName();
00381     (*this) << objName << objSize;
00382     for (int row = 0; row < arrSize; ++row ) {
00383       obj = arr[row];
00384       const char* p = reinterpret_cast<const char*>(arr[row]);
00385       this->Write(p,objSize);
00386     }
00387 
00388   }
00389 
00390   marker = EndMarker;
00391   (*this) << marker;
00392 
00393   return *this;
00394 
00395 }

DbiBinaryFile & DbiBinaryFile::operator<< ( const VldRange vr  ) 

Definition at line 224 of file DbiBinaryFile.cxx.

References CanWrite(), VldRange::GetDataSource(), VldRange::GetDetectorMask(), VldRange::GetSimMask(), VldRange::GetTimeEnd(), and VldRange::GetTimeStart().

00224                                                              {
00225 
00226   if ( this->CanWrite() ) {
00227     string str(vr.GetDataSource().Data());
00228     (*this) << vr.GetDetectorMask()
00229             << vr.GetSimMask()
00230             << vr.GetTimeStart()
00231             << vr.GetTimeEnd()
00232             << str;
00233   }
00234   return *this;
00235 }

DbiBinaryFile & DbiBinaryFile::operator<< ( const string &  str  ) 

Definition at line 194 of file DbiBinaryFile.cxx.

References Write().

00194                                                             {
00195 
00196   UInt_t numBytes = str.size()+1;
00197   this->Write(str.c_str(),numBytes);
00198   return *this;
00199 }

DbiBinaryFile& DbiBinaryFile::operator<< ( const VldTimeStamp ts  ) 
DbiBinaryFile& DbiBinaryFile::operator<< ( const Double_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator<< ( const UInt_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator<< ( const Int_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator<< ( const Bool_t &  num  ) 
DbiBinaryFile & DbiBinaryFile::operator>> ( vector< DbiTableRow * > &  arr  ) 

Definition at line 242 of file DbiBinaryFile.cxx.

References CanRead(), CheckFileStatus(), Close(), EndMarker, fArrayBuffer, GetVTptr(), Msg::kError, Msg::kVerbose, MAXMSG, MSG, Read(), SetVTptr(), and StartMarker.

00242                                                                     {
00243 //
00244 //
00245 //  Purpose: Read a vector of objects inheriting from DbiTableRow.
00246 
00247 //   NB:     On entry, array must be empty.
00248 //
00249 //           
00250 //           The objects are written into a buffer that is a contiguous 
00251 //           area of memory that is allocated to receive it. After a 
00252 //           successful read the user must call ReleaseArrayBuffer to take 
00253 //           control over this buffer as it will be automatically release
00254 //           when the next array input occurs otherwise.
00255 
00256 //  For the format of record see the operator <<.
00257 
00258   if ( ! this->CanRead() ) return *this;
00259 
00260   if ( arr.size() ) {
00261     MAXMSG("Dbi",Msg::kError,20) << "Attempting to read into non-empty array" << endl;
00262     return *this;
00263   }
00264 
00265 // Check for start of array marker.
00266 
00267   UInt_t marker = 0;
00268   (*this) >> marker;
00269   if ( marker != StartMarker ) {
00270     MAXMSG("Dbi",Msg::kError,20) << "Cannot find start of array marker" << endl;
00271     this->Close();
00272     this->CheckFileStatus();
00273     return *this;
00274   }
00275 
00276 //  Get array size and deal with non-empty arrays.
00277 
00278   Int_t arrSize = 0;
00279   (*this) >> arrSize;
00280 
00281   if ( arrSize ) {
00282     Int_t objSize  = 0;
00283     string objName;
00284     (*this) >> objName >> objSize;
00285 
00286 //  Ensure that sizes look sensible and use ROOT to instatiate
00287 //  an example object so that we can get the address of the 
00288 //  virtual table.
00289 
00290     TClass objClass(objName.c_str());
00291     Int_t objSizefromRoot = objClass.Size();
00292     void* obj = objClass.New();
00293     void* vt  = GetVTptr(obj);
00294 //  This only works if the address of the sub-class object is the same
00295 //  as the underlying base class, which should be true in this simple case.
00296     DbiTableRow* tr = reinterpret_cast<DbiTableRow*>(obj);
00297     delete tr;
00298 
00299     MSG("Dbi",Msg::kVerbose) 
00300          << "Restoring array of " << arrSize << " " 
00301          << objName << " objects"
00302          << "  VTaddr " << hex << vt << dec 
00303          << " object size "  << objSize << "(from file) "
00304          << objSizefromRoot << "(from ROOT)"
00305          << endl;
00306 
00307     if ( arrSize < 0 || objSize != objSizefromRoot ) {
00308       MAXMSG("Dbi",Msg::kError,20) << "Illegal  array size ("<< arrSize 
00309                             << ") or object size(" << objSize 
00310                             << "," << objSizefromRoot << ")" << endl;
00311       this->Close();
00312       this->CheckFileStatus();
00313       return *this;
00314     }
00315 
00316 //  Allocate buffer and load in array.
00317     delete[] fArrayBuffer;
00318     Int_t buffSize = arrSize*objSize;
00319     fArrayBuffer = new char[buffSize];
00320     this->Read(fArrayBuffer,buffSize);
00321 
00322 //  Fix up VT pointers and populate the vector.
00323 
00324     char* elem = fArrayBuffer;
00325     arr.reserve(arrSize);
00326     for (int row = 0; row < arrSize; ++row ) {
00327       SetVTptr(elem,vt);
00328       arr.push_back(reinterpret_cast<DbiTableRow*>(elem));
00329       elem += objSize;
00330     }
00331 
00332   }
00333 
00334 //  Check for end of array marker.
00335 
00336   (*this) >> marker;
00337   if ( marker != EndMarker ) {
00338     MAXMSG("Dbi",Msg::kError,20) << "Cannot find end of array marker" << endl;
00339     this->Close();
00340     this->CheckFileStatus();
00341   }
00342 
00343   return *this;
00344 
00345 }

DbiBinaryFile & DbiBinaryFile::operator>> ( VldRange vr  ) 

Definition at line 203 of file DbiBinaryFile.cxx.

References CanRead().

00203                                                        {
00204 
00205   if ( this->CanRead() ) {
00206     Int_t        detectorMask; 
00207     Int_t        simMask;
00208     VldTimeStamp timeStart;
00209     VldTimeStamp timeEnd;
00210     string str;
00211     (*this) >> detectorMask
00212             >> simMask
00213             >> timeStart
00214             >> timeEnd
00215             >> str;
00216     TString dataSource(str.c_str());
00217     VldRange tmp(detectorMask,simMask,timeStart,timeEnd,dataSource);
00218     vr = tmp;
00219   }
00220   return *this; 
00221 }

DbiBinaryFile & DbiBinaryFile::operator>> ( string &  str  ) 

Definition at line 184 of file DbiBinaryFile.cxx.

00184                                                       {
00185 
00186   if ( this->CanRead() ) {
00187     getline(*fFile,str,'\0');
00188     this->CheckFileStatus();
00189   }
00190   return *this; 
00191 }

DbiBinaryFile& DbiBinaryFile::operator>> ( VldTimeStamp ts  ) 
DbiBinaryFile& DbiBinaryFile::operator>> ( Double_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator>> ( UInt_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator>> ( Int_t &  num  ) 
DbiBinaryFile& DbiBinaryFile::operator>> ( Bool_t &  num  ) 
Bool_t DbiBinaryFile::Read ( char *  bytes,
UInt_t  numBytes 
) [private]

Definition at line 450 of file DbiBinaryFile.cxx.

References CanRead(), CheckFileStatus(), fFile, and fHasErrors.

Referenced by operator>>().

00450                                                        {
00451 //
00452 //
00453 //  Purpose: Low-level I/O with error checking. 
00454 //
00455 
00456   if ( ! this->CanRead() ) return kFALSE;
00457 
00458   fFile->read(bytes,numBytes);
00459   this->CheckFileStatus();
00460   return ! fHasErrors;
00461 }

char* DbiBinaryFile::ReleaseArrayBuffer (  )  [inline]

Definition at line 98 of file DbiBinaryFile.h.

References fArrayBuffer.

Referenced by DbiResultNonAgg::Streamer().

00098                              { char* buff = fArrayBuffer;
00099                                fArrayBuffer = 0;
00100                                return buff; }

static void DbiBinaryFile::SetReadAccess ( Bool_t  access = kTRUE  )  [inline, static]

Definition at line 108 of file DbiBinaryFile.h.

References fgReadAccess.

00108 { fgReadAccess = access; }

static void DbiBinaryFile::SetWorkDir ( const string &  dir  )  [inline, static]

Definition at line 106 of file DbiBinaryFile.h.

References fgWorkDir.

Referenced by DbiTableProxyRegistry::Config().

00106                                               { fgWorkDir = dir;
00107                   if ( fgWorkDir[fgWorkDir.size()-1] != '/' ) fgWorkDir += '/'; }

static void DbiBinaryFile::SetWriteAccess ( Bool_t  access = kTRUE  )  [inline, static]

Definition at line 109 of file DbiBinaryFile.h.

References fgWriteAccess.

00109 { fgWriteAccess = access; }

Bool_t DbiBinaryFile::Write ( const char *  bytes,
UInt_t  numBytes 
) [private]

Definition at line 465 of file DbiBinaryFile.cxx.

References CanWrite(), CheckFileStatus(), fFile, and fHasErrors.

Referenced by operator<<().

00465                                                               {
00466 //
00467 //
00468 //  Purpose: Low-level I/O with error checking. 
00469 //
00470 
00471   if ( ! this->CanWrite() ) return kFALSE;
00472 
00473   fFile->write(bytes,numBytes);
00474   this->CheckFileStatus();
00475   return ! fHasErrors;
00476 }


Member Data Documentation

char* DbiBinaryFile::fArrayBuffer [private]

Definition at line 131 of file DbiBinaryFile.h.

Referenced by operator>>(), ReleaseArrayBuffer(), and ~DbiBinaryFile().

fstream* DbiBinaryFile::fFile [private]

Associated file, may be null.

Definition at line 126 of file DbiBinaryFile.h.

Referenced by CheckFileStatus(), Close(), DbiBinaryFile(), Read(), Write(), and ~DbiBinaryFile().

string DbiBinaryFile::fFileName [private]

Definition at line 132 of file DbiBinaryFile.h.

Referenced by CheckFileStatus(), DbiBinaryFile(), and GetFileName().

Bool_t DbiBinaryFile::fgReadAccess = kTRUE [static, private]

Definition at line 135 of file DbiBinaryFile.h.

Referenced by CanReadL2Cache(), DbiBinaryFile(), and SetReadAccess().

string DbiBinaryFile::fgWorkDir [static, private]

Definition at line 134 of file DbiBinaryFile.h.

Referenced by CanReadL2Cache(), CanWriteL2Cache(), DbiBinaryFile(), and SetWorkDir().

Bool_t DbiBinaryFile::fgWriteAccess = kTRUE [static, private]

Definition at line 136 of file DbiBinaryFile.h.

Referenced by CanWriteL2Cache(), DbiBinaryFile(), and SetWriteAccess().

Bool_t DbiBinaryFile::fHasErrors [private]

Definition at line 130 of file DbiBinaryFile.h.

Referenced by CheckFileStatus(), DbiBinaryFile(), IsOK(), Read(), and Write().

Bool_t DbiBinaryFile::fReading [private]

Definition at line 129 of file DbiBinaryFile.h.

Referenced by CanRead(), CanWrite(), CheckFileStatus(), IsReading(), and IsWriting().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1