inlib  1.2.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
inlib::rroot::file Class Reference
Inheritance diagram for inlib::rroot::file:
Inheritance graph
[legend]
Collaboration diagram for inlib::rroot::file:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual bool verbose () const
virtual std::ostream & out ()
virtual bool byte_swap () const
virtual bool set_pos (seek a_offset=0, from a_from=begin)
virtual bool read_buffer (char *a_buffer, uint32 a_length)
virtual bool unziper (char a_key, unzip_func &a_func)
 file (std::ostream &a_out, const std::string &a_path, bool a_verbose=false)
virtual ~file ()
uint32 version () const
void close ()
directorydir ()
const directorydir () const
bool add_unziper (char a_key, unzip_func a_func)

Protected Member Functions

 file (const file &a_from)
fileoperator= (const file &)
bool initialize ()
bool read_header ()
int error_number ()
void reset_error_number ()

Static Protected Member Functions

static int _open (const char *a_name, int a_flags, uint32 a_mode)
static std::string sout (const std::string &a_string)
static const std::string & s_class ()

Protected Attributes

std::ostream & m_out
std::string m_path
bool m_verbose
int m_file
inlib::uint64 m_bytes_read
directory m_root_directory
std::map< char, unzip_funcm_unzipers
std::string m_title
uint32 m_version
seek m_BEGIN
seek m_END
seek m_seek_free
uint32 m_nbytes_free
uint32 m_nbytes_name

Detailed Description

Definition at line 29 of file file.


Constructor & Destructor Documentation

inlib::rroot::file::file ( std::ostream &  a_out,
const std::string &  a_path,
bool  a_verbose = false 
) [inline]

Definition at line 99 of file file.

  :m_out(a_out)
  ,m_path(a_path)
  ,m_verbose(a_verbose)
  ,m_file(not_open())
  ,m_bytes_read(0)
  ,m_root_directory(*this)
  // begin of record :
  ,m_version(0)
  ,m_BEGIN(0)
  ,m_END(0)
  ,m_seek_free(0)
  ,m_nbytes_free(0)
  ,m_nbytes_name(0)
  {
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif

    m_file = _open(a_path.c_str(),
#ifdef WIN32
                               O_RDONLY | O_BINARY,S_IREAD | S_IWRITE
#else
                               O_RDONLY,0644
#endif
    );
    if(m_file==not_open()) {
      m_out << "inlib::rroot::file::file :"
            << " can't open " << sout(a_path) << "."
            << std::endl;
      return;
    }
    initialize();
  }
virtual inlib::rroot::file::~file ( ) [inline, virtual]

Definition at line 135 of file file.

                  {
    close();
#ifdef INLIB_MEM
    mem::decrement(s_class().c_str());
#endif
  }
inlib::rroot::file::file ( const file a_from) [inline, protected]

Definition at line 142 of file file.

  : ifile(a_from)
  ,m_out(a_from.m_out)
  ,m_root_directory(*this)
  { 
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif
  }

Member Function Documentation

static int inlib::rroot::file::_open ( const char *  a_name,
int  a_flags,
uint32  a_mode 
) [inline, static, protected]

Definition at line 176 of file file.

                                                                 {
#if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
     return ::open64(a_name,a_flags,a_mode);
#else
     return ::open(a_name,a_flags,a_mode);
#endif
  }
bool inlib::rroot::file::add_unziper ( char  a_key,
unzip_func  a_func 
) [inline]

Definition at line 164 of file file.

                                                {
    std::map<char,unzip_func>::iterator it = m_unzipers.find(a_key);
    if(it!=m_unzipers.end()) {
      //(*it).second = a_func; //override ?
      return false;
    } else {
      m_unzipers[a_key] = a_func;
      return true;
    }
  }
virtual bool inlib::rroot::file::byte_swap ( ) const [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 35 of file file.

{return is_little_endian();}
void inlib::rroot::file::close ( ) [inline]

Definition at line 155 of file file.

               {
    if(m_file!=not_open()) ::close(m_file);
    m_file = not_open();
    m_root_directory.clear_keys();
  }
directory& inlib::rroot::file::dir ( ) [inline]

Definition at line 161 of file file.

{return m_root_directory;}
const directory& inlib::rroot::file::dir ( ) const [inline]

Definition at line 162 of file file.

{return m_root_directory;}
int inlib::rroot::file::error_number ( ) [inline, protected]

Definition at line 390 of file file.

{return errno;}
bool inlib::rroot::file::initialize ( ) [inline, protected]

Definition at line 186 of file file.

                    {
    if(!read_header()) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read header."
            << std::endl;
      return false;
    }
/*
    fRootDirectory->setSeekDirectory(fBEGIN);
    // Read Free segments structure if file is writable :
    if (fWritable) {
      if (fSeekFree > fBEGIN) {
        if(!readFreeSegments()) {
          m_out << "inlib::rroot::file::initialize : Cannot read free segments." 
               << std::endl;
          return false;
        }
      } else {
        m_out << "inlib::rroot::file::initialize : file \"" << fName 
             << "\" probably not closed, cannot read free segments" << std::endl;
      }
    }
*/
    // Read Directory info :
    uint32 nbytes = m_nbytes_name + m_root_directory.record_size(m_version);
    char* header = new char[nbytes];
    char* buffer = header;
    if(!set_pos(m_BEGIN)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't set position."
            << std::endl;
      delete [] header;
      return false;
    }
    if(!read_buffer(buffer,nbytes)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read buffer."
            << std::endl;
      delete [] header;
      return false;
    }
    buffer = header+m_nbytes_name;
    const char* eob = header+nbytes;
    if(!m_root_directory.from_buffer(eob,buffer)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read buffer (2)."
            << std::endl;
      delete [] header;
      return false;
    }
    uint32 nk =          //size of Key
      sizeof(int) +      //Key::fNumberOfBytes
      sizeof(short) +    //Key::fVersion
      2*sizeof(int) +    //Key::fObjectSize, Date
      2*sizeof(short) +  //Key::fKeyLength,fCycle
      2*sizeof(seek32);  //Key::fSeekKey,fSeekParentDirectory
                        //WARNING : the upper is seek32 since at begin of file.
    buffer = header+nk;
    std::string cname;
    rbuf rb(m_out,byte_swap(),eob,buffer);
    // Should be "TFile".
    if(!rb.read(cname)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read buffer (3)."
            << std::endl;
      delete [] header;
      return false;
    }
    if(cname!="TFile") {
      m_out << "inlib::rroot::file::initialize : TFile expected." << std::endl;
      delete [] header;
      return false;
    }
    if(m_verbose) {
      m_out << "inlib::rroot::file::initialize :"
            << " " << sout("TFile") << " found."
            << std::endl;
    }
    if(!rb.read(cname)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read buffer (4)."
            << std::endl;
      delete [] header;
      return false;
    }
    if(m_verbose) {
      m_out << "inlib::rroot::file::initialize :"
            << " found file name " << sout(cname)
            << std::endl;
    }
    if(!rb.read(m_title)) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read buffer (5)."
            << std::endl;
      delete [] header;
      return false;
    }
    delete [] header;
    if(m_verbose) {
      m_out << "inlib::rroot::file::initialize :"
            << " found title " << sout(m_title)
            << std::endl;
    }
    uint32 dirNbytesName = m_root_directory.nbytes_name();
    if (dirNbytesName < 10 || dirNbytesName > 1000) {
      m_out << "inlib::rroot::file::initialize :"
            << " can't read directory info."
            << std::endl;
      return false;
    }
    // Read keys of the top directory :
    if(m_root_directory.seek_keys() > m_BEGIN) {
      uint32 n;
      if(!m_root_directory.read_keys(n)) {
        m_out << "inlib::rroot::file::initialize :"
              << " can't read keys."
              << std::endl;
        return false;
      }
    } else {
      m_out << "inlib::rroot::file::initialize :"
            << " file " << sout(m_path)
            << " probably not closed."
            << std::endl;
      return false;
    }
    return true;
  }
file& inlib::rroot::file::operator= ( const file ) [inline, protected]

Definition at line 151 of file file.

{return *this;}
virtual std::ostream& inlib::rroot::file::out ( ) [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 33 of file file.

{return m_out;}
virtual bool inlib::rroot::file::read_buffer ( char *  a_buffer,
uint32  a_length 
) [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 65 of file file.

                                                           {
    // Read a buffer from the file.
    // This is the basic low level read operation.
    ssize_t siz;
    while ((siz = ::read(m_file,a_buffer,a_length)) < 0 && 
           error_number() == EINTR) reset_error_number();
    if (siz < 0) {
      m_out << "inlib::rroot::file::read_buffer :"
            << " error reading from file " << sout(m_path) << "."
            << std::endl;
      return false;
    }
    if (siz != ssize_t(a_length)) {
      m_out << "inlib::rroot::file::read_buffer :"
            << " error reading all requested bytes from file " 
            << sout(m_path) << ", got " << long2s(siz)
            << " of " << a_length
            << std::endl;
      return false;
    }
    m_bytes_read += siz;
    return true;
  }
bool inlib::rroot::file::read_header ( ) [inline, protected]

Definition at line 314 of file file.

                     {
    static const uint32 kBegin = 64;
    char header[kBegin];
    if(!set_pos()) return false;
    if(!read_buffer(header,kBegin)) return false;
    // make sure this is a root file
    if(::strncmp(header, "root", 4)) {
      m_out << "inlib::rroot::file::read_header :"
            << " " << sout(m_path) << " not a ROOT file."
            << std::endl;
      return false;
    }
    if(m_verbose) {
      m_out << "inlib::rroot::file::read_header :"
            << " file signature is " << sout("root")
            << std::endl;
    }
    char* buffer = header + 4;    // skip the "root" file identifier
    const char* eob = header + kBegin;
    rbuf rb(m_out,byte_swap(),eob,buffer);
   {int v;
    if(!rb.read(v)) return false;
    m_version = v;}
   {seek32 i;
    if(!rb.read(i)) return false;
    m_BEGIN = i;}
    if(m_version>1000000) {
      if(!rb.read(m_END)) return false;
      if(!rb.read(m_seek_free)) return false;
    } else {
     {seek32 i;
      if(!rb.read(i)) return false;
      m_END = i;}
     {seek32 i;
      if(!rb.read(i)) return false;
      m_seek_free = i;}
    }
    if(m_verbose) {
      m_out << "inlib::rroot::file::read_header :"
            << " begin " << m_BEGIN
            << " end " << m_END
            << std::endl;
    }
   {int v;
    if(!rb.read(v)) return false;
    m_nbytes_free = v;}
    int nfree = 0;
    if(!rb.read(nfree)) return false;
   {int v;
    if(!rb.read(v)) return false;
    m_nbytes_name = v;}
    //m_out << "debug : 1002 " << m_nbytes_name << std::endl;
/*
    if(!rb.read(fUnits )) return false;
    if(!rb.read(fCompress)) return false;
    if(fVersion>1000000) {
      if(!rb.read(fSeekInfo)) return false;
    } else {
     {Seek32 i;
      if(!rb.read(i)) return false;
      fSeekInfo = i;}
    }
    if(!rb.read(fNbytesInfo)) return false;
*/
    return true;
  }
void inlib::rroot::file::reset_error_number ( ) [inline, protected]

Definition at line 391 of file file.

{errno = 0;}
static const std::string& inlib::rroot::file::s_class ( ) [inline, static, protected]

Definition at line 381 of file file.

                                    {
    static const std::string s_v("inlib::rroot::file");
    return s_v;
  }
virtual bool inlib::rroot::file::set_pos ( seek  a_offset = 0,
from  a_from = begin 
) [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 36 of file file.

                                                             {
    int whence = 0;
    switch(a_from) {
    case begin:
      whence = SEEK_SET;
      break;
    case current:
      whence = SEEK_CUR;
      break;
    case end:
      whence = SEEK_END;
      break;
    }

#if defined(__linux__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
    if (::lseek64(m_file, a_offset, whence) < 0) {
#elif defined(WIN32)
    if (::_lseeki64(m_file, a_offset, whence) < 0) {
#else
    if (::lseek(m_file, a_offset, whence) < 0) {
#endif
      m_out << "inlib::rroot::file::set_pos :"
            << " cannot set position " << a_offset 
            << " in file " << sout(m_path) << "."
            << std::endl;
      return false;
    }
    return true;
  }
static std::string inlib::rroot::file::sout ( const std::string &  a_string) [inline, static, protected]

Definition at line 183 of file file.

                                                   {
    return std::string("\"")+a_string+"\"";
  }
virtual bool inlib::rroot::file::unziper ( char  a_key,
unzip_func a_func 
) [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 88 of file file.

                                                     {
    std::map<char,unzip_func>::iterator it = m_unzipers.find(a_key);
    if(it==m_unzipers.end()) {
      a_func = 0;
      return false;
    }
    a_func = (*it).second;
    return true;
  }
virtual bool inlib::rroot::file::verbose ( ) const [inline, virtual]

Implements inlib::rroot::ifile.

Definition at line 32 of file file.

{return m_verbose;}
uint32 inlib::rroot::file::version ( ) const [inline]

Definition at line 153 of file file.

{return m_version;}

Member Data Documentation

Definition at line 405 of file file.

Definition at line 399 of file file.

Definition at line 406 of file file.

int inlib::rroot::file::m_file [protected]

Definition at line 398 of file file.

Definition at line 408 of file file.

Definition at line 410 of file file.

std::ostream& inlib::rroot::file::m_out [protected]

Definition at line 395 of file file.

std::string inlib::rroot::file::m_path [protected]

Definition at line 396 of file file.

Definition at line 400 of file file.

Definition at line 407 of file file.

std::string inlib::rroot::file::m_title [protected]

Definition at line 402 of file file.

std::map<char,unzip_func> inlib::rroot::file::m_unzipers [protected]

Definition at line 401 of file file.

Definition at line 397 of file file.

Definition at line 404 of file file.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines