File indexing completed on 2025-01-18 09:14:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include "DDDB/DDDBReader.h"
0022 #include "DD4hep/IOV.h"
0023
0024
0025 namespace dd4hep {
0026
0027
0028 namespace DDDB {
0029
0030
0031
0032
0033
0034
0035
0036
0037 class DDDBFileReader : public DDDBReader {
0038 long m_validityLowerLimit = 0;
0039 long m_validityUpperLimit = IOV::MAX_KEY;
0040
0041 public:
0042
0043 DDDBFileReader();
0044
0045 virtual ~DDDBFileReader() {}
0046
0047 virtual int getObject(const std::string& system_id, UserContext* ctxt, std::string& data) override;
0048
0049 virtual void parserLoaded(const std::string& system_id) override;
0050
0051 virtual void parserLoaded(const std::string& system_id, UserContext* ctxt) override;
0052
0053 virtual bool load(const std::string& system_id, std::string& buffer) override;
0054
0055 virtual bool load(const std::string& system_id, UserContext* ctxt, std::string& buffer) override;
0056
0057 };
0058 }
0059 }
0060
0061
0062
0063
0064 #include "DD4hep/Factories.h"
0065 #include "DD4hep/Printout.h"
0066 #include "DD4hep/Detector.h"
0067
0068
0069 #include <sys/types.h>
0070 #include <sys/stat.h>
0071 #include <unistd.h>
0072 #include <fcntl.h>
0073
0074
0075 dd4hep::DDDB::DDDBFileReader::DDDBFileReader()
0076 : DDDBReader()
0077 {
0078 declareProperty("ValidityLower", m_context.valid_since);
0079 declareProperty("ValidityUpper", m_context.valid_until);
0080 declareProperty("ValidityLowerLimit",m_validityLowerLimit);
0081 declareProperty("ValidityUpperLimit",m_validityUpperLimit);
0082 }
0083
0084 int dd4hep::DDDB::DDDBFileReader::getObject(const std::string& system_id,
0085 UserContext* ,
0086 std::string& buffer)
0087 {
0088 std::string path = system_id;
0089 int fid = ::open(path.c_str(), O_RDONLY);
0090
0091 if ( fid == -1 ) {
0092 std::string p = m_directory+system_id;
0093 if ( p.substr(0,7) == "file://" ) path = p.substr(6);
0094 else if ( p.substr(0,5) == "file:" ) path = p.substr(5);
0095 else path = p;
0096 fid = ::open(path.c_str(), O_RDONLY);
0097 }
0098 if ( fid != -1 ) {
0099 struct stat buff;
0100 if ( 0 == ::fstat(fid, &buff) ) {
0101 int done = 0, len = buff.st_size;
0102 char* b = new char[len+1];
0103 b[0] = 0;
0104 while ( done<len ) {
0105 int sc = ::read(fid, b+done, buff.st_size-done);
0106 if ( sc >= 0 ) { done += sc; continue; }
0107 break;
0108 }
0109 ::close(fid);
0110 b[done] = 0;
0111 buffer = b;
0112 delete [] b;
0113 if ( done>=len ) {
0114 return 1;
0115 }
0116 return 0;
0117 }
0118 ::close(fid);
0119 }
0120 return 0;
0121 }
0122
0123
0124 bool dd4hep::DDDB::DDDBFileReader::load(const std::string& system_id, std::string& buffer) {
0125 return xml::UriReader::load(system_id, buffer);
0126 }
0127
0128
0129 bool dd4hep::DDDB::DDDBFileReader::load(const std::string& system_id,
0130 UserContext* ctxt,
0131 std::string& buffer)
0132 {
0133 return DDDBReader::load(system_id, ctxt, buffer);
0134 }
0135
0136
0137 void dd4hep::DDDB::DDDBFileReader::parserLoaded(const std::string& system_id) {
0138 DDDBReader::parserLoaded(system_id);
0139 }
0140
0141
0142 void dd4hep::DDDB::DDDBFileReader::parserLoaded(const std::string& , UserContext* ctxt) {
0143 DDDBReaderContext *ct = (DDDBReaderContext *)ctxt;
0144
0145 ct->valid_since = m_context.valid_since;
0146 ct->valid_until = m_context.valid_until;
0147
0148 if (ct->valid_since < m_validityLowerLimit)
0149 ct->valid_since = m_validityLowerLimit;
0150 else if (ct->valid_since > m_validityUpperLimit)
0151 ct->valid_since = m_validityUpperLimit;
0152
0153 if (ct->valid_until < m_validityLowerLimit)
0154 ct->valid_until = m_validityLowerLimit;
0155 else if (ct->valid_until > m_validityUpperLimit)
0156 ct->valid_until = m_validityUpperLimit;
0157
0158 }
0159
0160 namespace {
0161 void* create_dddb_xml_file_reader(const char* ) {
0162 return new dd4hep::DDDB::DDDBFileReader();
0163 }
0164 }
0165 DECLARE_CONSTRUCTOR(DDDB_FileReader,create_dddb_xml_file_reader)