DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
IoStreams.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework includes
15 #include <DDG4/IoStreams.h>
16 
17 // ROOT include files
18 #include <TFile.h>
19 
20 // C/C++ include files
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <cstdio>
25 
26 namespace {
28  class MyTFile : public TFile {
29  private:
31  virtual ~MyTFile() {}
32  public:
34  virtual Int_t SysWrite(Int_t fd, const void* buf, Int_t len) override { return TFile::SysWrite(fd, buf, len); }
36  virtual Int_t SysRead(Int_t fd, void* buf, Int_t len) override { return TFile::SysRead(fd,buf,len); }
38  virtual Long64_t SysSeek(Int_t fd, Long64_t off, Int_t way) override { return TFile::SysSeek(fd, off, way); }
39  };
40 }
41 
42 namespace dd4hep {
43 
45  template<> void dd4hep_file<int>::open(const char* path, BOOST_IOS::openmode mode) {
46  if ( m_handle ) ::close(m_handle);
47  m_handle = ::open(path,mode);
48  }
49 
52  : m_handle(fd), m_flag(flags) { }
53 
55  template<> dd4hep_file<int>::dd4hep_file(const char* fn, BOOST_IOS::openmode mode)
56  : m_handle(0), m_flag(close_handle) {open(fn,mode); }
57 
59  template<> std::streamsize dd4hep_file<int>::read(char_type* s, std::streamsize n)
60  { return ::read(m_handle,s,n); }
61 
63  template<> std::streamsize dd4hep_file<int>::write(const char_type* s, std::streamsize n)
64  { return ::write(m_handle,s,n); }
65 
67  template<> std::streampos dd4hep_file<int>::seek(stream_offset off, BOOST_IOS::seekdir way)
68  { return ::lseek(m_handle,off,way); }
69 
71  template<> void dd4hep_file<int>::close() {
72  if ( m_handle ) ::close(m_handle);
73  m_handle = 0;
74  }
75 
77  template<> void dd4hep_file<TFile*>::open(const char* path, BOOST_IOS::openmode mode) {
78  if ( m_handle ) {
79  m_handle->Close();
80  delete m_handle;
81  }
82  std::string p = path;
83  p += "?filetype=raw";
84  if ( mode&BOOST_IOS::out )
85  m_handle = TFile::Open(p.c_str(),"RECREATE","ROOT");
86  else
87  m_handle = TFile::Open(p.c_str());
88  if ( m_handle->IsZombie() ) {
89  delete m_handle;
90  m_handle = 0;
91  throw 1;
92  }
93  }
94 #define _p(x) (reinterpret_cast<MyTFile*>(x))
95 
98  : m_handle(fd), m_flag(flags) { }
99 
101  template<> dd4hep_file<TFile*>::dd4hep_file(const char* fname, BOOST_IOS::openmode mode)
102  : m_handle(0), m_flag(close_handle) { open(fname,mode); }
103 
105  template<> std::streamsize dd4hep_file<TFile*>::read(char_type* s, std::streamsize n) {
106  if ( m_handle ) {
107  Long64_t nb1 = m_handle->GetBytesRead();
108  Bool_t res = _p(m_handle)->ReadBuffer(s,nb1,n);
109  if ( res ) {
110  Long64_t nb2 = m_handle->GetBytesRead();
111  return nb2-nb1;
112  }
113  }
114  return -1;
115  }
116 
118  template<> std::streamsize dd4hep_file<TFile*>::write(const char_type* s, std::streamsize n)
119  { return m_handle ? _p(m_handle)->SysWrite(m_handle->GetFd(),s,n) : -1; }
120 
122  template<> std::streampos dd4hep_file<TFile*>::seek(stream_offset off, BOOST_IOS::seekdir way)
123  { return m_handle ? _p(m_handle)->SysSeek(m_handle->GetFd(),off,way) : -1; }
124 
126  template<> void dd4hep_file<TFile*>::close()
127  { if ( m_handle ) { m_handle->Close(); delete m_handle; m_handle=0; } }
128 
129 }
dd4hep::dd4hep_file_flags
dd4hep_file_flags
Definition: IoStreams.h:50
dd4hep::dd4hep_file::read
std::streamsize read(char_type *s, std::streamsize n)
Read from input stream.
IoStreams.h
dd4hep::dd4hep_file::open
void open(handle_type fd, dd4hep_file_flags flags)
Default destructor.
dd4hep::dd4hep_file::seek
std::streampos seek(stream_offset off, BOOST_IOS::seekdir way)
Direct access: set file pointer of the stream.
dd4hep::dd4hep_file::write
std::streamsize write(const char_type *s, std::streamsize n)
Write to output stream.
dd4hep::dd4hep_file::stream_offset
boost::iostreams::stream_offset stream_offset
Definition: IoStreams.h:79
dd4hep::dd4hep_file::close
void close()
Close the file stream.
dd4hep::close_handle
@ close_handle
Definition: IoStreams.h:52
dd4hep::dd4hep_file::dd4hep_file
dd4hep_file()=default
Default constructor.
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::dd4hep_file::char_type
char char_type
Definition: IoStreams.h:78
_p
#define _p(x)
Definition: IoStreams.cpp:94
dd4hep::dd4hep_file::handle_type
T handle_type
Definition: IoStreams.h:77