DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IoStreams.cpp
Go to the documentation of this file.
1 // $Id: $
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework includes
16 #include "DDG4/IoStreams.h"
17 
18 // C/C++ include files
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <cstdio>
23 
24 // ROOT include files
25 #include "TFile.h"
26 
27 using namespace DD4hep;
28 
29 namespace {
31  class MyTFile : public TFile {
32  private:
34  virtual ~MyTFile() {}
35  public:
37  virtual Int_t SysWrite(Int_t fd, const void* buf, Int_t len) { return TFile::SysWrite(fd, buf, len); }
39  virtual Int_t SysRead(Int_t fd, void* buf, Int_t len) { return TFile::SysRead(fd,buf,len); }
41  virtual Long64_t SysSeek(Int_t fd, Long64_t off, Int_t way) { return TFile::SysSeek(fd, off, way); }
42  };
43 }
44 
45 namespace DD4hep {
46 
48  template<> void dd4hep_file<int>::open(const char* path, BOOST_IOS::openmode mode) {
49  if ( m_handle ) ::close(m_handle);
50  m_handle = ::open(path,mode);
51  }
52 
55  : m_handle(fd), m_flag(flags) { }
56 
58  template<> dd4hep_file<int>::dd4hep_file(const char* fn, BOOST_IOS::openmode mode)
59  : m_handle(0), m_flag(close_handle) {open(fn,mode); }
60 
62  template<> std::streamsize dd4hep_file<int>::read(char_type* s, std::streamsize n)
63  { return ::read(m_handle,s,n); }
64 
66  template<> std::streamsize dd4hep_file<int>::write(const char_type* s, std::streamsize n)
67  { return ::write(m_handle,s,n); }
68 
70  template<> std::streampos dd4hep_file<int>::seek(stream_offset off, BOOST_IOS::seekdir way)
71  { return ::lseek(m_handle,off,way); }
72 
74  template<> void dd4hep_file<int>::close() {
75  if ( m_handle ) ::close(m_handle);
76  m_handle = 0;
77  }
78 
80  template<> void dd4hep_file<TFile*>::open(const char* path, BOOST_IOS::openmode mode) {
81  if ( m_handle ) {
82  m_handle->Close();
83  delete m_handle;
84  }
85  std::string p = path;
86  p += "?filetype=raw";
87  if ( mode&BOOST_IOS::out )
88  m_handle = TFile::Open(p.c_str(),"RECREATE","ROOT");
89  else
90  m_handle = TFile::Open(p.c_str());
91  if ( m_handle->IsZombie() ) {
92  delete m_handle;
93  m_handle = 0;
94  throw 1;
95  }
96  }
97 #define _p(x) (reinterpret_cast<MyTFile*>(x))
98 
101  : m_handle(fd), m_flag(flags) { }
102 
104  template<> dd4hep_file<TFile*>::dd4hep_file(const char* fname, BOOST_IOS::openmode mode)
105  : m_handle(0), m_flag(close_handle) { open(fname,mode); }
106 
108  template<> std::streamsize dd4hep_file<TFile*>::read(char_type* s, std::streamsize n) {
109  if ( m_handle ) {
110  Long64_t nb1 = m_handle->GetBytesRead();
111  Bool_t res = _p(m_handle)->ReadBuffer(s,nb1,n);
112  if ( res ) {
113  Long64_t nb2 = m_handle->GetBytesRead();
114  return nb2-nb1;
115  }
116  }
117  return -1;
118  }
119 
121  template<> std::streamsize dd4hep_file<TFile*>::write(const char_type* s, std::streamsize n)
122  { return m_handle ? _p(m_handle)->SysWrite(m_handle->GetFd(),s,n) : -1; }
123 
125  template<> std::streampos dd4hep_file<TFile*>::seek(stream_offset off, BOOST_IOS::seekdir way)
126  { return m_handle ? _p(m_handle)->SysSeek(m_handle->GetFd(),off,way) : -1; }
127 
129  template<> void dd4hep_file<TFile*>::close()
130  { if ( m_handle ) { m_handle->Close(); delete m_handle; m_handle=0; } }
131 
132 }
std::streampos seek(stream_offset off, BOOST_IOS::seekdir way)
dd4hep_file_flags
Definition: IoStreams.h:51
TGeoShape * s
Definition: Volumes.cpp:294
#define _p(x)
Definition: IoStreams.cpp:97
boost::iostreams::stream_offset stream_offset
Definition: IoStreams.h:80
std::streamsize read(char_type *s, std::streamsize n)
std::streamsize write(const char_type *s, std::streamsize n)
void open(handle_type fd, dd4hep_file_flags flags)