g4tools  5.4.0
directory
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_rroot_directory
5 #define tools_rroot_directory
6 
7 #include "seek"
8 #include "date"
9 #include "key"
10 #include "ifile"
11 #include "../forit"
12 #include "../vmanip"
13 
14 namespace tools {
15 namespace rroot {
16 
17 class directory {
18 public:
19  static const std::string& s_class() {
20  static const std::string s_v("tools::rroot::directory");
21  return s_v;
22  }
23 public:
24  directory(ifile& a_file)
25  :m_file(a_file)
26  {
27 #ifdef TOOLS_MEM
28  mem::increment(s_class().c_str());
29 #endif
30  m_date_C = 0; //.set();
31  m_date_M = 0; //.set();
32  m_nbytes_keys = 0;
33  m_nbytes_name = 0;
34  m_seek_directory = 0;
35  m_seek_parent = 0;
36  m_seek_keys = 0;
37  }
38  virtual ~directory(){
39  clear_keys();
40 #ifdef TOOLS_MEM
41  mem::decrement(s_class().c_str());
42 #endif
43  }
44 protected:
45  directory(const directory& a_from):m_file(a_from.m_file){
46 #ifdef TOOLS_MEM
47  mem::increment(s_class().c_str());
48 #endif
49  }
50  directory& operator=(const directory &){return *this;}
51 public:
52  ifile& file() {return m_file;}
53 
54  const std::vector<key*>& keys() const {return m_keys;}
55  std::vector<key*>& keys() {return m_keys;}
56 
57  key* find_key(const std::string& a_name) {
58  if(m_file.verbose()) {
59  m_file.out() << "tools::rroot::directory::find_key :"
60  << " " << sout(a_name) << " ..."
61  << std::endl;
62  }
63  tools_vforcit(key*,m_keys,it) {
64  if((*it)->object_name()==a_name) return *it;
65  }
66 
67  return 0;
68  }
69 
70  key* find_key_from_class(const std::string& a_class) {
71  if(m_file.verbose()) {
72  m_file.out() << "tools::rroot::directory::find_key_from_class :"
73  << " " << sout(a_class) << " ..."
74  << std::endl;
75  }
76  tools_vforcit(key*,m_keys,it) {
77  if((*it)->object_class()==a_class) return *it;
78  }
79 
80  return 0;
81  }
82 
83  uint32 nbytes_name() const {return m_nbytes_name;}
84  seek seek_keys() const {return m_seek_keys;}
85  uint32 record_size(uint32 a_version) const {
86  uint32 nbytes = sizeof(short);
87  nbytes += sizeof(date); //m_date_C.record_size();
88  nbytes += sizeof(date); //m_date_M.record_size();
89  nbytes += sizeof(m_nbytes_keys);
90  nbytes += sizeof(m_nbytes_name);
91  if(a_version>=40000) {
92  nbytes += sizeof(seek);
93  nbytes += sizeof(seek);
94  nbytes += sizeof(seek);
95  } else {
96  nbytes += sizeof(seek32);
97  nbytes += sizeof(seek32);
98  nbytes += sizeof(seek32);
99  }
100  return nbytes;
101  }
102  bool from_buffer(const char* aEOB,char*& a_buffer){
103  // Decode input buffer.
104  // (Name, title) are stored in the (name, title) of the associated key.
105  rbuf rb(m_file.out(),m_file.byte_swap(),aEOB,a_buffer);
106  short versiondir;
107  if(!rb.read(versiondir)) return false;
108  unsigned int _date;
109  if(!rb.read(_date)) return false;
110  //fDateC.setDate(_date);
111  if(!rb.read(_date)) return false;
112  //fDateM.setDate(_date);
113  {int v;
114  if(!rb.read(v)) return false;
115  m_nbytes_keys = v;}
116  {int v;
117  if(!rb.read(v)) return false;
118  m_nbytes_name = v;}
119  if(versiondir>(short)big_file_version_tag()) {
120  if(!rb.read(m_seek_directory)) return false;
121  if(!rb.read(m_seek_parent)) return false;
122  if(!rb.read(m_seek_keys)) return false;
123  } else {
124  {seek32 i;
125  if(!rb.read(i)) return false;
126  m_seek_directory = i;}
127 
128  {seek32 i;
129  if(!rb.read(i)) return false;
130  m_seek_parent = i;}
131 
132  {seek32 i;
133  if(!rb.read(i)) return false;
134  m_seek_keys = i;}
135  }
136  if(m_file.verbose()) {
137  m_file.out() << "tools::rroot::key::from_buffer :"
138  << " nbytes keys : " << m_nbytes_keys
139  << ", pos keys : " << m_seek_keys
140  << std::endl;
141  }
142  return true;
143  }
144  bool read_keys(uint32& a_number) {
145  // Read the KEYS :
146  // Every directory has a list (fKeys). This list has been
147  // written on the file via CERN-ROOT::TDirectory::writeKeys
148  // as a single data record.
149  a_number = 0;
150 
151  clear_keys();
152 
153  key headerkey(m_file.out(),m_seek_keys,m_nbytes_keys);
154  if(!headerkey.read_file(m_file)) return false;
155  char* buffer = headerkey.data_buffer();
156  if(!headerkey.from_buffer(m_file.byte_swap(),headerkey.eob(),buffer,m_file.verbose())) return false;
157  int nkeys = 0;
158  rbuf rb(m_file.out(),m_file.byte_swap(),headerkey.eob(),buffer);
159  if(!rb.read(nkeys)) return false;
160  if(m_file.verbose()) {
161  m_file.out() << "tools::rroot::directory::read_keys :"
162  << " nkeys " << nkeys
163  << "."
164  << std::endl;
165  }
166  for(int i=0;i<nkeys;i++) {
167  key* k = new key(m_file.out());
168  if(!k->from_buffer(m_file.byte_swap(),headerkey.eob(),buffer,m_file.verbose())) {
169  delete k;
170  return false;
171  }
172  m_keys.push_back(k);
173  }
174  a_number = nkeys;
175  return true;
176  }
177  void clear_keys() {
178  safe_clear<key>(m_keys);
179  }
180 protected:
182  std::vector<key*> m_keys;
183  // Record (stored in file):
184  date m_date_C; //Date and time when directory is created
185  date m_date_M; //Date and time of last modification
186  uint32 m_nbytes_keys; //Number of bytes for the keys
187  uint32 m_nbytes_name; //Number of bytes in TNamed at creation time
188  seek m_seek_directory; //Location of directory on file
189  seek m_seek_parent; //Location of parent directory on file
190  seek m_seek_keys; //Location of Keys record on file
191 };
192 
193 }}
194 
195 #endif
tools::rroot::directory::s_class
static const std::string & s_class()
Definition: directory:19
tools::rroot::seek32
int seek32
Definition: seek:17
tools::rroot::directory::keys
const std::vector< key * > & keys() const
Definition: directory:54
tools::rroot::date
unsigned int date
Definition: date:10
tools::rroot::directory::from_buffer
bool from_buffer(const char *aEOB, char *&a_buffer)
Definition: directory:102
tools::rroot::ifile::out
virtual std::ostream & out() const =0
tools::rroot::directory::clear_keys
void clear_keys()
Definition: directory:177
tools::rroot::directory::m_nbytes_name
uint32 m_nbytes_name
Definition: directory:187
tools::rroot::big_file_version_tag
uint32 big_file_version_tag()
Definition: seek:19
tools::rroot::key::eob
const char * eob() const
Definition: key:202
ifile
tools::rroot::directory::m_seek_directory
seek m_seek_directory
Definition: directory:188
tools::rroot::directory::record_size
uint32 record_size(uint32 a_version) const
Definition: directory:85
tools::rroot::directory::operator=
directory & operator=(const directory &)
Definition: directory:50
tools::rroot::directory::find_key_from_class
key * find_key_from_class(const std::string &a_class)
Definition: directory:70
tools::rroot::directory::m_keys
std::vector< key * > m_keys
Definition: directory:182
tools::rroot::directory::find_key
key * find_key(const std::string &a_name)
Definition: directory:57
tools::rroot::directory::seek_keys
seek seek_keys() const
Definition: directory:84
tools::rroot::directory::keys
std::vector< key * > & keys()
Definition: directory:55
tools::rroot::directory
Definition: directory:17
tools::rroot::directory::directory
directory(const directory &a_from)
Definition: directory:45
tools::sout
Definition: sout:17
key
tools::rroot::directory::m_seek_keys
seek m_seek_keys
Definition: directory:190
tools::rroot::directory::directory
directory(ifile &a_file)
Definition: directory:24
tools::rroot::seek
int64 seek
Definition: seek:16
tools::rroot::directory::m_date_M
date m_date_M
Definition: directory:185
tools::rroot::directory::nbytes_name
uint32 nbytes_name() const
Definition: directory:83
tools::rroot::rbuf
Definition: rbuf:22
tools::rroot::directory::m_file
ifile & m_file
Definition: directory:181
tools::rroot::directory::m_seek_parent
seek m_seek_parent
Definition: directory:189
tools::rroot::directory::~directory
virtual ~directory()
Definition: directory:38
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::rroot::ifile
Definition: ifile:16
tools::rroot::buffer
Definition: buffer:43
seek
tools::rroot::directory::read_keys
bool read_keys(uint32 &a_number)
Definition: directory:144
tools::rroot::directory::m_date_C
date m_date_C
Definition: directory:184
tools::rroot::ifile::byte_swap
virtual bool byte_swap() const =0
tools::rroot::rbuf::read
bool read(unsigned char &a_x)
Definition: rbuf:146
tools::rroot::key
Definition: key:37
tools::rroot::directory::m_nbytes_keys
uint32 m_nbytes_keys
Definition: directory:186
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::rroot::key::from_buffer
bool from_buffer(bool a_byte_swap, const char *aEOB, char *&a_pos, bool a_verbose)
Definition: key:206
tools::rroot::directory::file
ifile & file()
Definition: directory:52
tools::rroot::key::data_buffer
char * data_buffer() const
Definition: key:201
date
tools::rroot::ifile::verbose
virtual bool verbose() const =0
tools::rroot::key::read_file
bool read_file(ifile &a_file)
Definition: key:186