File indexing completed on 2025-01-18 09:14:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DDG4/IoStreams.h>
0016
0017
0018 #include <TFile.h>
0019
0020
0021 #include <sys/types.h>
0022 #include <sys/stat.h>
0023 #include <fcntl.h>
0024 #include <cstdio>
0025
0026 namespace {
0027
0028 class MyTFile : public TFile {
0029 private:
0030
0031 virtual ~MyTFile() {}
0032 public:
0033
0034 virtual Int_t SysWrite(Int_t fd, const void* buf, Int_t len) override { return TFile::SysWrite(fd, buf, len); }
0035
0036 virtual Int_t SysRead(Int_t fd, void* buf, Int_t len) override { return TFile::SysRead(fd,buf,len); }
0037
0038 virtual Long64_t SysSeek(Int_t fd, Long64_t off, Int_t way) override { return TFile::SysSeek(fd, off, way); }
0039 };
0040 }
0041
0042 namespace dd4hep {
0043
0044
0045 template<> void dd4hep_file<int>::open(const char* path, BOOST_IOS::openmode mode) {
0046 if ( m_handle ) ::close(m_handle);
0047 m_handle = ::open(path,mode);
0048 }
0049
0050
0051 template<> dd4hep_file<int>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
0052 : m_handle(fd), m_flag(flags) { }
0053
0054
0055 template<> dd4hep_file<int>::dd4hep_file(const char* fn, BOOST_IOS::openmode mode)
0056 : m_handle(0), m_flag(close_handle) {open(fn,mode); }
0057
0058
0059 template<> std::streamsize dd4hep_file<int>::read(char_type* s, std::streamsize n)
0060 { return ::read(m_handle,s,n); }
0061
0062
0063 template<> std::streamsize dd4hep_file<int>::write(const char_type* s, std::streamsize n)
0064 { return ::write(m_handle,s,n); }
0065
0066
0067 template<> std::streampos dd4hep_file<int>::seek(stream_offset off, BOOST_IOS::seekdir way)
0068 { return ::lseek(m_handle,off,way); }
0069
0070
0071 template<> void dd4hep_file<int>::close() {
0072 if ( m_handle ) ::close(m_handle);
0073 m_handle = 0;
0074 }
0075
0076
0077 template<> void dd4hep_file<TFile*>::open(const char* path, BOOST_IOS::openmode mode) {
0078 if ( m_handle ) {
0079 m_handle->Close();
0080 delete m_handle;
0081 }
0082 std::string p = path;
0083 p += "?filetype=raw";
0084 if ( mode&BOOST_IOS::out )
0085 m_handle = TFile::Open(p.c_str(),"RECREATE","ROOT");
0086 else
0087 m_handle = TFile::Open(p.c_str());
0088 if ( m_handle->IsZombie() ) {
0089 delete m_handle;
0090 m_handle = 0;
0091 throw 1;
0092 }
0093 }
0094 #define _p(x) (reinterpret_cast<MyTFile*>(x))
0095
0096
0097 template<> dd4hep_file<TFile*>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
0098 : m_handle(fd), m_flag(flags) { }
0099
0100
0101 template<> dd4hep_file<TFile*>::dd4hep_file(const char* fname, BOOST_IOS::openmode mode)
0102 : m_handle(0), m_flag(close_handle) { open(fname,mode); }
0103
0104
0105 template<> std::streamsize dd4hep_file<TFile*>::read(char_type* s, std::streamsize n) {
0106 if ( m_handle ) {
0107 Long64_t nb1 = m_handle->GetBytesRead();
0108 Bool_t res = _p(m_handle)->ReadBuffer(s,nb1,n);
0109 if ( res ) {
0110 Long64_t nb2 = m_handle->GetBytesRead();
0111 return nb2-nb1;
0112 }
0113 }
0114 return -1;
0115 }
0116
0117
0118 template<> std::streamsize dd4hep_file<TFile*>::write(const char_type* s, std::streamsize n)
0119 { return m_handle ? _p(m_handle)->SysWrite(m_handle->GetFd(),s,n) : -1; }
0120
0121
0122 template<> std::streampos dd4hep_file<TFile*>::seek(stream_offset off, BOOST_IOS::seekdir way)
0123 { return m_handle ? _p(m_handle)->SysSeek(m_handle->GetFd(),off,way) : -1; }
0124
0125
0126 template<> void dd4hep_file<TFile*>::close()
0127 { if ( m_handle ) { m_handle->Close(); delete m_handle; m_handle=0; } }
0128
0129 }