g4tools  5.4.0
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::wroot::file Class Reference
Inheritance diagram for tools::wroot::file:
Inheritance graph
[legend]
Collaboration diagram for tools::wroot::file:
Collaboration graph
[legend]

Public Member Functions

virtual const std::string & s_cls () const
 
virtual bool verbose () const
 
virtual std::ostream & out () const
 
virtual bool byte_swap () const
 
virtual bool set_pos (seek a_offset=0, from a_from=begin)
 
virtual seek END () const
 
virtual void set_END (seek a_end)
 
virtual bool write_buffer (const char *a_buffer, uint32 a_length)
 
virtual uint32 version () const
 
virtual bool synchronize ()
 
virtual bool ziper (char a_key, compress_func &a_func) const
 
virtual uint32 compression () const
 
virtual void compress_buffer (const buffer &a_buffer, char *&a_kbuf, uint32 &a_klen, bool &a_kdel)
 
 file (std::ostream &a_out, const std::string &a_path, bool a_verbose=false)
 
virtual ~file ()
 
const std::string & path () const
 
void set_compression (uint32 a_level)
 
bool is_open () const
 
void close ()
 
directorydir ()
 
const directorydir () const
 
bool write (uint32 &a_nbytes)
 
bool add_ziper (char a_key, compress_func a_func)
 
- Public Member Functions inherited from tools::wroot::ifile
virtual ~ifile ()
 

Static Public Member Functions

static const std::string & s_class ()
 

Protected Types

enum  EAccessMode { kFileExists = 0, kExecutePermission = 1, kWritePermission = 2, kReadPermission = 4 }
 

Protected Member Functions

 file (const file &a_from)
 
fileoperator= (const file &)
 
bool write_header ()
 
bool write_streamer_infos ()
 
bool make_free_seg (seek a_first, seek a_last)
 
bool write_free_segments ()
 
int error_number ()
 
void reset_error_number ()
 

Static Protected Member Functions

static bool access_path (const std::string &a_path, EAccessMode a_mode)
 
static bool unlink (const std::string &a_path)
 
static int _open (const char *a_name, int a_flags, unsigned int a_mode)
 
static bool zip (std::ostream &a_out, compress_func a_func, int a_level, uint32 a_srcsize, char *a_src, uint32 a_tgtsize, char *a_tgt, uint32 &a_irep)
 

Protected Attributes

std::ostream & m_out
 
std::string m_path
 
bool m_verbose
 
int m_file
 
std::string m_title
 
directory m_root_directory
 
std::map< char, compress_funcm_zipers
 
std::list< free_seg * > m_free_segs
 
uint32 m_version
 
seek m_BEGIN
 
seek m_END
 
seek m_seek_free
 
uint32 m_nbytes_free
 
uint32 m_nbytes_name
 
char m_units
 
uint32 m_compress
 
seek m_seek_info
 
uint32 m_nbytes_info
 

Additional Inherited Members

- Public Types inherited from tools::wroot::ifile
enum  from { begin, current, end }
 

Detailed Description

Definition at line 34 of file file.

Member Enumeration Documentation

◆ EAccessMode

Enumerator
kFileExists 
kExecutePermission 
kWritePermission 
kReadPermission 

Definition at line 458 of file file.

458  {
459  kFileExists = 0,
460  kExecutePermission = 1,
461  kWritePermission = 2,
462  kReadPermission = 4
463  };

Constructor & Destructor Documentation

◆ file() [1/2]

tools::wroot::file::file ( std::ostream &  a_out,
const std::string &  a_path,
bool  a_verbose = false 
)
inline

Definition at line 221 of file file.

222  :m_out(a_out)
223  ,m_path(a_path)
224  ,m_verbose(a_verbose)
225  ,m_file(not_open())
226  //,m_bytes_write(0)
227  ,m_root_directory(get_me(),nosuffix(a_path),m_title)
228  // begin of record :
229  ,m_version(0)
230  ,m_BEGIN(0)
231  ,m_END(0)
232  ,m_seek_free(0)
233  ,m_nbytes_free(0)
234  ,m_nbytes_name(0)
235  ,m_units(4)
236  ,m_compress(1)
237  ,m_seek_info(0)
238  ,m_nbytes_info(0)
239  {
240 #ifdef TOOLS_MEM
241  mem::increment(s_class().c_str());
242 #endif
243 
244  m_version = version();
245 
247 
248  if(!m_root_directory.is_valid()) {
249  m_out << "tools::wroot::file::file :"
250  << " " << sout(m_path) << " root directory badly created."
251  << std::endl;
252  return;
253  }
254 
255  m_file = _open(a_path.c_str(),
256 #ifdef _MSC_VER
257  O_RDWR | O_CREAT | O_BINARY,S_IREAD | S_IWRITE
258 #else
259  O_RDWR | O_CREAT,0644
260 #endif
261  );
262  if(m_file==not_open()) {
263  m_out << "tools::wroot::file::file :"
264  << " can't open " << sout(a_path) << "."
265  << std::endl;
266  return;
267  }
268 
269  //initialize :
270 
271  m_BEGIN = kBegin(); // First used word in file following the file header.
272  m_END = m_BEGIN; // Pointer to end of file.
273 
274  m_free_segs.push_back(new free_seg(m_out,m_BEGIN,START_BIG_FILE()));
275 
276  // Write Directory info :
277  uint32 namelen =
280  uint32 nbytes = namelen + m_root_directory.record_size();
281  wroot::key key(m_out,*this,0,m_path,m_title,"TFile",nbytes); // It does a (*this).set_END().
282 
283  // m_nbytes_name = start point of directory info from key head.
284  m_nbytes_name = key.key_length() + namelen;
286  m_root_directory.set_seek_directory(key.seek_key()); //at EOF.
287 
288  //the below write 45 bytes at BOF (Begin Of File).
289  if(!write_header()) { //need m_nbytes_name, m_END after key written.
290  m_out << "tools::wroot::file::file :"
291  << " can't write file header."
292  << std::endl;
293  return;
294  }
295 
296  {char* pos = key.data_buffer();
297  wbuf wb(m_out,byte_swap(),key.eob(),pos);
298  if(!wb.write(m_path)) return;
299  if(!wb.write(m_title)) return;
300  if(!m_root_directory.to_buffer(wb)) return;}
301 
302  if(m_verbose) {
303  m_out << "tools::wroot::file::file :"
304  << " write key ("
305  << namelen
306  << ", "
308  << ", "
309  << nbytes
310  << ", "
311  << m_nbytes_name
312  << ", "
313  << key.seek_key()
314  << ")."
315  << std::endl;
316  }
317 
318  key.set_cycle(1);
319  if(!key.write_self(*this)) {
320  m_out << "tools::wroot::file::file :"
321  << " key.write_self() failed."
322  << std::endl;
323  return;
324  }
325 
326  //the below write at kBegin + nbytes.
327  //64+52
328  uint32 n;
329  if(!key.write_file(*this,n)) {
330  m_out << "tools::wroot::file::file :"
331  << " can't write key in file."
332  << std::endl;
333  return;
334  }
335  //::printf("debug : file::file : write key : %d\n",n);
336 
337  }

◆ ~file()

virtual tools::wroot::file::~file ( )
inlinevirtual

Definition at line 338 of file file.

338  {
339  close();
340 #ifdef TOOLS_MEM
341  mem::decrement(s_class().c_str());
342 #endif
343  }

◆ file() [2/2]

tools::wroot::file::file ( const file a_from)
inlineprotected

Definition at line 345 of file file.

346  :ifile(a_from)
347  ,m_out(a_from.m_out)
348  ,m_root_directory(get_me())
349  {
350 #ifdef TOOLS_MEM
351  mem::increment(s_class().c_str());
352 #endif
353  }

Member Function Documentation

◆ _open()

static int tools::wroot::file::_open ( const char *  a_name,
int  a_flags,
unsigned int  a_mode 
)
inlinestaticprotected

Definition at line 491 of file file.

491  {
492 #if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
493  return ::open64(a_name,a_flags,a_mode);
494 #else
495  return ::open(a_name,a_flags,a_mode);
496 #endif
497  }

◆ access_path()

static bool tools::wroot::file::access_path ( const std::string &  a_path,
EAccessMode  a_mode 
)
inlinestaticprotected

Definition at line 464 of file file.

464  {
465  // Returns true if one can access a file using the specified access mode.
466  // Mode is the same as for the WinNT access(2) function.
467 #ifdef _MSC_VER
468  return (::_access(a_path.c_str(),a_mode) == 0) ? true : false;
469 #else
470  return (::access(a_path.c_str(),a_mode) == 0) ? true : false;
471 #endif
472  }

◆ add_ziper()

bool tools::wroot::file::add_ziper ( char  a_key,
compress_func  a_func 
)
inline

Definition at line 447 of file file.

447  {
448  std::map<char,compress_func>::const_iterator it = m_zipers.find(a_key);
449  if(it!=m_zipers.end()) {
450  //(*it).second = a_func; //override ?
451  return false;
452  } else {
453  m_zipers[a_key] = a_func;
454  return true;
455  }
456  }

◆ byte_swap()

virtual bool tools::wroot::file::byte_swap ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 48 of file file.

48 {return is_little_endian();}

◆ close()

void tools::wroot::file::close ( )
inline

Definition at line 369 of file file.

369  {
370  if(m_file==not_open()) return;
372 
373  if(m_free_segs.size()) {
374  if(!write_free_segments()) {
375  m_out << "tools::wroot::file::close :"
376  << " can't write free segments."
377  << std::endl;
378  }
379  if(!write_header()) { // Now write file header
380  m_out << "tools::wroot::file::close :"
381  << " can't write file header."
382  << std::endl;
383  }
384  }
385 
386  {std::list<free_seg*>::iterator it;
387  for(it=m_free_segs.begin();
388  it!=m_free_segs.end();
389  it = m_free_segs.erase(it)) {
390  delete (*it);
391  }}
392 
393  ::close(m_file);
394  m_file = not_open();
395  }

◆ compress_buffer()

virtual void tools::wroot::file::compress_buffer ( const buffer a_buffer,
char *&  a_kbuf,
uint32 a_klen,
bool &  a_kdel 
)
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 169 of file file.

169  {
170  //NOTE : if(kdelete) delete [] kbuf;
171 
172  a_kbuf = 0;
173  a_klen = 0;
174  a_kdel = false;
175 
176  uint32 nbytes = a_buffer.length();
177  uint32 cxlevel = m_compress;
178  if(cxlevel && (nbytes>256)) {
179  compress_func func;
180  if(!ziper('Z',func)) {
181  //m_out << "tools::wroot::directory::write_object :"
182  // << " zlib ziper not found."
183  // << std::endl;
184  a_kbuf = (char*)a_buffer.buf();
185  a_klen = a_buffer.length();
186  a_kdel = false;
187  } else {
188  const uint32 kMAXBUF = 0xffffff;
189  const uint32 HDRSIZE = 9;
190  uint32 nbuffers = nbytes/kMAXBUF;
191  uint32 buflen = nbytes+HDRSIZE*(nbuffers+1);
192  a_kbuf = new char[buflen];
193  a_kdel = true;
194  char* src = (char*)a_buffer.buf();
195  char* tgt = a_kbuf;
196  uint32 nzip = 0;
197  for(uint32 i=0;i<=nbuffers;i++) {
198  uint32 bufmax = ((i == nbuffers) ? nbytes - nzip : kMAXBUF);
199  uint32 nout;
200  if(!zip(m_out,func,cxlevel,bufmax,src,bufmax,tgt,nout)) {
201  delete [] a_kbuf;
202  a_kbuf = (char*)a_buffer.buf();
203  a_klen = a_buffer.length();
204  a_kdel = false;
205  break;
206  }
207  tgt += nout; //nout includes HDRSIZE
208  a_klen += nout;
209  src += kMAXBUF;
210  nzip += kMAXBUF;
211  }
212  //::printf("debug : compress : end : %u %u\n",nbytes,klen);
213  }
214  } else {
215  a_kbuf = (char*)a_buffer.buf();
216  a_klen = a_buffer.length();
217  a_kdel = false;
218  }
219  }

◆ compression()

virtual uint32 tools::wroot::file::compression ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 168 of file file.

168 {return m_compress;}

◆ dir() [1/2]

directory& tools::wroot::file::dir ( )
inline

Definition at line 397 of file file.

397 {return m_root_directory;}

◆ dir() [2/2]

const directory& tools::wroot::file::dir ( ) const
inline

Definition at line 398 of file file.

398 {return m_root_directory;}

◆ END()

virtual seek tools::wroot::file::END ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 79 of file file.

79 {return m_END;}

◆ error_number()

int tools::wroot::file::error_number ( )
inlineprotected

Definition at line 761 of file file.

761 {return errno;}

◆ is_open()

bool tools::wroot::file::is_open ( ) const
inline

Definition at line 367 of file file.

367 {return (m_file==not_open()?false:true);}

◆ make_free_seg()

bool tools::wroot::file::make_free_seg ( seek  a_first,
seek  a_last 
)
inlineprotected

Definition at line 589 of file file.

589  {
590  // Mark unused bytes on the file :
591  // The list of free segments is in the m_free_segs list
592  // When an object is deleted from the file, the freed space is added
593  // into the FREE linked list (m_free_segs). The FREE list consists
594  // of a chain of consecutive free segments on the file. At the same
595  // time, the first 4 bytes of the freed record on the file
596  // are overwritten by GAPSIZE where
597  // GAPSIZE = -(Number of bytes occupied by the record).
598 
599  if(m_free_segs.empty()) {
600  m_out << "tools::wroot::file::make_free_seg :"
601  << " free_seg list should not be empty here."
602  << std::endl;
603  return false;
604  }
605 
606  free_seg* newfree = add_free(m_free_segs,a_first,a_last);
607  if(!newfree) {
608  m_out << "tools::wroot::file::make_free_seg :"
609  << " add_free failed."
610  << std::endl;
611  return false;
612  }
613 
614  seek nfirst = newfree->first();
615  seek nlast = newfree->last();
616 
617  seek _nbytes = nlast-nfirst+1;
618  if(_nbytes>START_BIG_FILE()) _nbytes = START_BIG_FILE();
619  int nbytes = -int(_nbytes);
620 
621  int nb = sizeof(int);
622 
623  char psave[128];
624  const char* eob = psave + nb;
625  char* pos = psave;
626 
627  wbuf wb(m_out,byte_swap(),eob,pos);
628  if(!wb.write(nbytes)) return false;
629 
630  if(nlast == (m_END-1)) m_END = nfirst;
631  if(!set_pos(nfirst)) return false;
632  if(!write_buffer(psave,nb)) return false;
633  if(!synchronize()) return false;
634  return true;
635  }

◆ operator=()

file& tools::wroot::file::operator= ( const file )
inlineprotected

Definition at line 354 of file file.

354 {return *this;}

◆ out()

virtual std::ostream& tools::wroot::file::out ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 46 of file file.

46 {return m_out;}

◆ path()

const std::string& tools::wroot::file::path ( ) const
inline

Definition at line 356 of file file.

356 {return m_path;}

◆ reset_error_number()

void tools::wroot::file::reset_error_number ( )
inlineprotected

Definition at line 762 of file file.

762 {errno = 0;}

◆ s_class()

static const std::string& tools::wroot::file::s_class ( )
inlinestatic

Definition at line 39 of file file.

39  {
40  static const std::string s_v("tools::wroot::file");
41  return s_v;
42  }

◆ s_cls()

virtual const std::string& tools::wroot::file::s_cls ( ) const
inlinevirtual

Definition at line 43 of file file.

43 {return s_class();}

◆ set_compression()

void tools::wroot::file::set_compression ( uint32  a_level)
inline

Definition at line 358 of file file.

358  {
359  // level = 0 objects written to this file will not be compressed.
360  // level = 1 minimal compression level but fast.
361  // ....
362  // level = 9 maximal compression level but slow.
363  m_compress = a_level;
364  if(m_compress>9) m_compress = 9;
365  }

◆ set_END()

virtual void tools::wroot::file::set_END ( seek  a_end)
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 80 of file file.

80  {
81  m_END = a_end;
82 
83  if(m_free_segs.empty()) {
84  m_out << "tools::wroot::file::set_END :"
85  << " free_seg list should not be empty here."
86  << std::endl;
87  } else {
88  free_seg* end_seg = m_free_segs.back();
89  if(end_seg->last()!=START_BIG_FILE()) {
90  m_out << "tools::wroot::file::set_END :"
91  << " last free_seg is not the ending of file one."
92  << " free_seg list looks corrupted."
93  << std::endl;
94  } else {
95  m_free_segs.back()->set_first(m_END);
96  }
97  }
98  }

◆ set_pos()

virtual bool tools::wroot::file::set_pos ( seek  a_offset = 0,
from  a_from = begin 
)
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 49 of file file.

49  {
50  int whence = 0;
51  switch(a_from) {
52  case begin:
53  whence = SEEK_SET;
54  break;
55  case current:
56  whence = SEEK_CUR;
57  break;
58  case end:
59  whence = SEEK_END;
60  break;
61  }
62 
63 #if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
64  if (::lseek64(m_file, a_offset, whence) < 0) {
65 #elif defined(_MSC_VER)
66  if (::_lseeki64(m_file, a_offset, whence) < 0) {
67 #else
68  if (::lseek(m_file, a_offset, whence) < 0) {
69 #endif
70  m_out << "tools::wroot::file::set_pos :"
71  << " cannot set position " << a_offset
72  << " in file " << sout(m_path) << "."
73  << std::endl;
74  return false;
75  }
76  return true;
77  }

◆ synchronize()

virtual bool tools::wroot::file::synchronize ( )
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 137 of file file.

137  {
138  // Synchornize a file's in-core and on-disk states.
139 #ifdef _MSC_VER
140  if(::_commit(m_file)) {
141  m_out << "tools::wroot::file::synchronize :"
142  << " in _commit() for file " << sout(m_path) << "."
143  << std::endl;
144  return false;
145  }
146 #elif defined(__MINGW32__) || defined(__MINGW64__)
147  return true;
148 #else
149  if (::fsync(m_file) < 0) {
150  m_out << "tools::wroot::file::synchronize :"
151  << " error in fsync() for file " << sout(m_path) << "."
152  << std::endl;
153  return false;
154  }
155 #endif
156  return true;
157  }

◆ unlink()

static bool tools::wroot::file::unlink ( const std::string &  a_path)
inlinestaticprotected

Definition at line 473 of file file.

473  {
474  // Unlink, i.e. remove, a file or directory. Returns true when succesfull,
475  // false in case of failure.
476  struct stat finfo;
477  if (::stat(a_path.c_str(),&finfo) < 0) return false;
478 #ifdef _MSC_VER
479  if (finfo.st_mode & S_IFDIR)
480  return (::_rmdir(a_path.c_str())==-1 ? false : true);
481  else
482  return (::unlink(a_path.c_str())==-1 ? false : true);
483 #else
484  if (S_ISDIR(finfo.st_mode))
485  return (::rmdir(a_path.c_str())==-1 ? false : true);
486  else
487  return (::unlink(a_path.c_str())==-1 ? false : true);
488 #endif
489  }

◆ verbose()

virtual bool tools::wroot::file::verbose ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 45 of file file.

45 {return m_verbose;}

◆ version()

virtual uint32 tools::wroot::file::version ( ) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 126 of file file.

126  {
127  // Return version id as an integer, i.e. "2.22/04" -> 22204.
128  static const uint32 ROOT_MAJOR_VERSION = 4;
129  static const uint32 ROOT_MINOR_VERSION = 0;
130  static const uint32 ROOT_PATCH_VERSION = 0;
131  return
132  10000 * ROOT_MAJOR_VERSION +
133  100 * ROOT_MINOR_VERSION +
134  ROOT_PATCH_VERSION;
135  }

◆ write()

bool tools::wroot::file::write ( uint32 a_nbytes)
inline

Definition at line 400 of file file.

400  {
401  // Write memory objects to this file :
402  // Loop on all objects in m_root_directory (including subdirectories).
403  // A new key is created in the directories m_keys linked list
404  // for each object.
405  // The list of keys is then saved on the file (via write_keys)
406  // as a single data record.
407  // The directory header info is rewritten on the directory header record.
408  // //The linked list of FREE segments is written.
409  // The file header is written (bytes 1->m_BEGIN).
410  a_nbytes = 0;
411 
412  if(m_verbose) {
413  m_out << "tools::wroot::file::write :"
414  << " writing Name=" << sout(m_path)
415  << " Title=" << sout(m_title) << "."
416  << std::endl;
417  }
418 
419  uint32 nbytes;
420  if(!m_root_directory.write(nbytes)) return false; // Write directory tree
421 
422  if(!write_streamer_infos()) {
423  m_out << "tools::wroot::file::write :"
424  << " write_streamer_infos failed."
425  << std::endl;
426  return false;
427  }
428 
429  if(!write_free_segments()) {
430  m_out << "tools::wroot::file::write :"
431  << " can't write free segments."
432  << std::endl;
433  return false;
434  }
435 
436  if(!write_header()) { //write 45 bytes at BOF.
437  m_out << "tools::wroot::file::write :"
438  << " can't write file header."
439  << std::endl;
440  return false;
441  }
442 
443  a_nbytes = nbytes;
444  return true;
445  }

◆ write_buffer()

virtual bool tools::wroot::file::write_buffer ( const char *  a_buffer,
uint32  a_length 
)
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 100 of file file.

100  {
101  // Write a buffer to the file. This is the basic low level write operation.
102 #ifdef _MSC_VER
103  typedef int ssize_t;
104 #endif
105  ssize_t siz;
106  while ((siz = ::write(m_file,a_buffer,a_length)) < 0 &&
107  error_number() == EINTR) reset_error_number();
108 
109  if(siz < 0) {
110  m_out << "tools::wroot::file::write_buffer :"
111  << " error writing to file " << sout(m_path) << "."
112  << std::endl;
113  return false;
114  }
115  if(siz!=(ssize_t)a_length) {
116  m_out << "tools::wroot::file::write_buffer :"
117  << "error writing all requested bytes to file " << sout(m_path)
118  << ", wrote " << long_out(siz) << " of " << a_length
119  << std::endl;
120  return false;
121  }
122  //m_bytes_write += siz;
123  return true;
124  }

◆ write_free_segments()

bool tools::wroot::file::write_free_segments ( )
inlineprotected

Definition at line 637 of file file.

637  {
638  // The linked list of FREE segments (fFree) is written as a single data record.
639 
640  // Delete old record if it exists :
641  if(m_seek_free){
643  m_out << "tools::wroot::file::write_free_segments :"
644  << " key.write_self() failed."
645  << std::endl;
646  return false;
647  }
648  }
649 
650  //::printf("debug : write_free_segments : seg list :\n");
651 
652  uint32 nbytes = 0;
653  {tools_lforcit(free_seg*,m_free_segs,it) {
654  nbytes += (*it)->record_size();
655  //::printf("debug : write_free_segments : %lu %lu\n",
656  // (*it)->first(),(*it)->last());
657  }}
658  if(!nbytes) return true;
659 
660  wroot::key key(m_out,*this,
662  m_path,m_title,"TFile",
663  nbytes); // It does a (*this).set_END().
664  if(!key.seek_key()) return false;
665 
666  {char* pos = key.data_buffer();
667  wbuf wb(m_out,byte_swap(),key.eob(),pos);
668  tools_lforcit(free_seg*,m_free_segs,it) {
669  if(!(*it)->fill_buffer(wb)) return false;
670  }}
671 
672  //key.set_cycle(1);
673  if(!key.write_self(*this)) {
674  m_out << "tools::wroot::file::write_free_segments :"
675  << " key.write_self() failed."
676  << std::endl;
677  return false;
678  }
679 
680  m_seek_free = key.seek_key();
681  m_nbytes_free = key.number_of_bytes();
682  if(m_verbose) {
683  m_out << "tools::wroot::file::write_free_segments :"
684  << " write key." << std::endl;
685  }
686 
687  uint32 n;
688  if(!key.write_file(*this,n)) return false;
689  if(!n) return false;
690 
691  return true;
692  }

◆ write_header()

bool tools::wroot::file::write_header ( )
inlineprotected

Definition at line 498 of file file.

498  {
499  const char root[] = "root";
500  //char psave[kBegin()];
501  char psave[128];
502  const char* eob = psave + kBegin();
503  char* pos = psave;
504  ::memcpy(pos,root,4); pos += 4;
505  uint32 vers = m_version;
506  if((m_END>START_BIG_FILE()) ||
509  vers += 1000000;
510  m_units = 8;
511  }
512  wbuf wb(m_out,byte_swap(),eob,pos);
513  if(!wb.write(vers)) return false;
514  if(!wb.write((seek32)m_BEGIN)) return false;
515  if(vers>1000000) {
516  if(!wb.write(m_END)) return false;
517  if(!wb.write(m_seek_free)) return false;
518  } else {
519  if(!wb.write((seek32)m_END)) return false;
520  if(!wb.write((seek32)m_seek_free)) return false;
521  }
522  if(!wb.write(m_nbytes_free)) return false;
523  //int nfree = fFreeSegments.size();
524  uint32 nfree = 0; //FIXME
525  if(!wb.write(nfree)) return false;
526  if(!wb.write(m_nbytes_name)) return false;
527  if(!wb.write(m_units)) return false;
528  if(!wb.write(m_compress)) return false;
529  if(vers>1000000) {
530  if(!wb.write(m_seek_info)) return false;
531  } else {
532  if(!wb.write((seek32)m_seek_info)) return false;
533  }
534  if(!wb.write(m_nbytes_info)) return false;
535  if(!set_pos()) return false; //BOF
536  uint32 nbytes = uint32(pos - psave);
537  //::printf("debug : write_header : %d\n",nbytes);
538  if(!write_buffer(psave,nbytes)) return false;
539  if(!synchronize()) return false;
540  return true;
541  }

◆ write_streamer_infos()

bool tools::wroot::file::write_streamer_infos ( )
inlineprotected

Definition at line 543 of file file.

543  {
544  obj_list<streamer_info> sinfos;
545  fill_infos(sinfos,m_out);
546 
547  if(sinfos.empty()) return false;
548 
549  buffer bref(m_out,byte_swap(),256);
550 
551  if(!sinfos.stream(bref)) {
552  m_out << "tools::wroot::file::write_streamer_infos :"
553  << " cannot stream obj_list<streamer_info>."
554  << std::endl;
555  return false;
556  }
557  uint32 nbytes = bref.length();
558 
559  wroot::key key(m_out,*this,
561  "StreamerInfo","",
562  sinfos.store_cls(),
563  nbytes); // It does a (*this).set_END().
564  if(!key.seek_key()) return false;
565 
566  if(!bref.displace_mapped(key.key_length())) return false;
567 
568  ::memcpy(key.data_buffer(),bref.buf(),nbytes);
569 
570  //key.set_cycle(1);
571  if(!key.write_self(*this)) {
572  m_out << "tools::wroot::file::write_streamer_infos :"
573  << " key.write_self() failed."
574  << std::endl;
575  return false;
576  }
577 
578  m_seek_info = key.seek_key();
579  m_nbytes_info = key.number_of_bytes();
580  //FIXME sumBuffer(key.objectSize());
581 
582  uint32 n;
583  if(!key.write_file(*this,n)) return false;
584  if(!n) return false;
585 
586  return true;
587  }

◆ zip()

static bool tools::wroot::file::zip ( std::ostream &  a_out,
compress_func  a_func,
int  a_level,
uint32  a_srcsize,
char *  a_src,
uint32  a_tgtsize,
char *  a_tgt,
uint32 a_irep 
)
inlinestaticprotected

Definition at line 694 of file file.

699  {
700 
701  // from Rio/Bits/R__zip using zlib.
702 
703  const uint32 HDRSIZE = 9;
704 
705  if(a_tgtsize<HDRSIZE) {
706  a_out << "tools::wroot::directory::zip :"
707  << " target buffer too small."
708  << std::endl;
709  a_irep = 0;
710  return false;
711  }
712  if(a_srcsize>0xffffff) {
713  a_out << "tools::wroot::directory::zip :"
714  << " source buffer too big."
715  << std::endl;
716  a_irep = 0;
717  return false;
718  }
719 
720  uint32 out_size;
721  if(!a_func(a_out,a_level,
722  a_srcsize,a_src,
723  a_tgtsize,a_tgt+HDRSIZE,
724  out_size)) {
725  a_out << "tools::wroot::directory::zip :"
726  << " zipper failed."
727  << std::endl;
728  a_irep = 0;
729  return false;
730  }
731  if((HDRSIZE+out_size)>a_tgtsize) {
732  a_out << "tools::wroot::directory::zip :"
733  << " target buffer overflow."
734  << std::endl;
735  a_irep = 0;
736  return false;
737  }
738 
739  // HEADER :
740  a_tgt[0] = 'Z'; // Signature ZLib
741  a_tgt[1] = 'L';
742  a_tgt[2] = 8; //DEFLATE
743 
744  a_tgt[3] = (char)(out_size & 0xff);
745  a_tgt[4] = (char)((out_size >> 8) & 0xff);
746  a_tgt[5] = (char)((out_size >> 16) & 0xff);
747 
748  a_tgt[6] = (char)(a_srcsize & 0xff);
749  a_tgt[7] = (char)((a_srcsize >> 8) & 0xff);
750  a_tgt[8] = (char)((a_srcsize >> 16) & 0xff);
751 
752  a_irep = HDRSIZE+out_size;
753 
754  return true;
755  }

◆ ziper()

virtual bool tools::wroot::file::ziper ( char  a_key,
compress_func a_func 
) const
inlinevirtual

Implements tools::wroot::ifile.

Definition at line 159 of file file.

159  {
160  std::map<char,compress_func>::const_iterator it = m_zipers.find(a_key);
161  if(it==m_zipers.end()) {
162  a_func = 0;
163  return false;
164  }
165  a_func = (*it).second;
166  return true;
167  }

Member Data Documentation

◆ m_BEGIN

seek tools::wroot::file::m_BEGIN
protected

Definition at line 778 of file file.

◆ m_compress

uint32 tools::wroot::file::m_compress
protected

Definition at line 785 of file file.

◆ m_END

seek tools::wroot::file::m_END
protected

Definition at line 779 of file file.

◆ m_file

int tools::wroot::file::m_file
protected

Definition at line 769 of file file.

◆ m_free_segs

std::list<free_seg*> tools::wroot::file::m_free_segs
protected

Definition at line 774 of file file.

◆ m_nbytes_free

uint32 tools::wroot::file::m_nbytes_free
protected

Definition at line 781 of file file.

◆ m_nbytes_info

uint32 tools::wroot::file::m_nbytes_info
protected

Definition at line 787 of file file.

◆ m_nbytes_name

uint32 tools::wroot::file::m_nbytes_name
protected

Definition at line 783 of file file.

◆ m_out

std::ostream& tools::wroot::file::m_out
protected

Definition at line 766 of file file.

◆ m_path

std::string tools::wroot::file::m_path
protected

Definition at line 767 of file file.

◆ m_root_directory

directory tools::wroot::file::m_root_directory
protected

Definition at line 772 of file file.

◆ m_seek_free

seek tools::wroot::file::m_seek_free
protected

Definition at line 780 of file file.

◆ m_seek_info

seek tools::wroot::file::m_seek_info
protected

Definition at line 786 of file file.

◆ m_title

std::string tools::wroot::file::m_title
protected

Definition at line 771 of file file.

◆ m_units

char tools::wroot::file::m_units
protected

Definition at line 784 of file file.

◆ m_verbose

bool tools::wroot::file::m_verbose
protected

Definition at line 768 of file file.

◆ m_version

uint32 tools::wroot::file::m_version
protected

Definition at line 777 of file file.

◆ m_zipers

std::map<char,compress_func> tools::wroot::file::m_zipers
protected

Definition at line 773 of file file.


The documentation for this class was generated from the following file:
tools::wroot::fill_infos
void fill_infos(obj_list< streamer_info > &a_infos, std::ostream &a_out)
Definition: infos:1564
tools::wroot::file::m_nbytes_free
uint32 m_nbytes_free
Definition: file:781
tools::wroot::seek
int64 seek
Definition: seek:16
tools::wroot::file::m_compress
uint32 m_compress
Definition: file:785
tools::wroot::file::m_file
int m_file
Definition: file:769
tools::wroot::file::reset_error_number
void reset_error_number()
Definition: file:762
tools::wroot::file::m_out
std::ostream & m_out
Definition: file:766
tools::wroot::file::kWritePermission
@ kWritePermission
Definition: file:461
tools::wroot::file::close
void close()
Definition: file:369
tools::wroot::file::write_free_segments
bool write_free_segments()
Definition: file:637
tools::wroot::file::m_nbytes_info
uint32 m_nbytes_info
Definition: file:787
tools::is_little_endian
bool is_little_endian()
Definition: platform:13
tools::wroot::file::m_units
char m_units
Definition: file:784
tools::wroot::file::ziper
virtual bool ziper(char a_key, compress_func &a_func) const
Definition: file:159
tools::wroot::seek32
int seek32
Definition: seek:17
tools::wroot::file::make_free_seg
bool make_free_seg(seek a_first, seek a_last)
Definition: file:589
tools::wroot::file::unlink
static bool unlink(const std::string &a_path)
Definition: file:473
tools::wroot::directory::close
bool close()
Definition: directory:245
tools::wroot::ifile::current
@ current
Definition: ifile:27
tools::wroot::file::synchronize
virtual bool synchronize()
Definition: file:137
tools::wroot::ifile::end
@ end
Definition: ifile:28
tools::wroot::file::m_root_directory
directory m_root_directory
Definition: file:772
tools::wroot::file::zip
static bool zip(std::ostream &a_out, compress_func a_func, int a_level, uint32 a_srcsize, char *a_src, uint32 a_tgtsize, char *a_tgt, uint32 &a_irep)
Definition: file:694
tools::wroot::directory::record_size
uint32 record_size() const
Definition: directory:232
tools::wroot::file::write
bool write(uint32 &a_nbytes)
Definition: file:400
tools::wroot::file::m_seek_free
seek m_seek_free
Definition: file:780
tools::compress_func
bool(* compress_func)(std::ostream &, unsigned int, unsigned int, const char *, unsigned int, char *, unsigned int &)
Definition: press_func:11
tools_lforcit
#define tools_lforcit(a__T, a__l, a__it)
Definition: forit:40
tools::wroot::file::m_path
std::string m_path
Definition: file:767
tools::wroot::file::access_path
static bool access_path(const std::string &a_path, EAccessMode a_mode)
Definition: file:464
tools::wroot::file::m_nbytes_name
uint32 m_nbytes_name
Definition: file:783
tools::wroot::file::set_pos
virtual bool set_pos(seek a_offset=0, from a_from=begin)
Definition: file:49
tools::wroot::file::write_streamer_infos
bool write_streamer_infos()
Definition: file:543
tools::wroot::file::byte_swap
virtual bool byte_swap() const
Definition: file:48
tools::wroot::key::std_string_record_size
static unsigned int std_string_record_size(const std::string &x)
Definition: key:29
tools::wroot::file::kExecutePermission
@ kExecutePermission
Definition: file:460
tools::wroot::file::m_zipers
std::map< char, compress_func > m_zipers
Definition: file:773
tools::wroot::file::m_version
uint32 m_version
Definition: file:777
tools::wroot::file::_open
static int _open(const char *a_name, int a_flags, unsigned int a_mode)
Definition: file:491
tools::wroot::file::error_number
int error_number()
Definition: file:761
tools::wroot::file::kFileExists
@ kFileExists
Definition: file:459
tools::wroot::file::s_class
static const std::string & s_class()
Definition: file:39
tools::wroot::directory::set_seek_directory
void set_seek_directory(seek a_seek)
Definition: directory:199
tools::wroot::file::m_title
std::string m_title
Definition: file:771
tools::wroot::file::version
virtual uint32 version() const
Definition: file:126
tools::wroot::file::kReadPermission
@ kReadPermission
Definition: file:462
tools::wroot::file::write_header
bool write_header()
Definition: file:498
tools::wroot::file::m_BEGIN
seek m_BEGIN
Definition: file:778
tools::nosuffix
void nosuffix(const std::string &a_string, std::string &a_value, bool a_back=true)
Definition: path:20
tools::wroot::directory::is_valid
bool is_valid() const
Definition: directory:198
tools::wroot::add_free
free_seg * add_free(std::list< free_seg * > &a_list, seek a_first, seek a_last)
Definition: free_seg:126
tools::wroot::file::m_END
seek m_END
Definition: file:779
tools::wroot::file::m_verbose
bool m_verbose
Definition: file:768
tools::wroot::file::write_buffer
virtual bool write_buffer(const char *a_buffer, uint32 a_length)
Definition: file:100
tools::wroot::ifile::begin
@ begin
Definition: ifile:26
tools::wroot::file::m_free_segs
std::list< free_seg * > m_free_segs
Definition: file:774
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::wroot::directory::seek_directory
virtual seek seek_directory() const
Definition: directory:35
tools::wroot::file::m_seek_info
seek m_seek_info
Definition: file:786
tools::wroot::START_BIG_FILE
uint32 START_BIG_FILE()
Definition: seek:19
tools::wroot::directory::set_nbytes_name
void set_nbytes_name(uint32 a_n)
Definition: directory:230
tools::wroot::directory::to_buffer
bool to_buffer(wbuf &a_wb)
Definition: directory:253
tools::wroot::directory::write
bool write(uint32 &a_nbytes)
Definition: directory:277