|
inlib
1.2.0
|


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 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, zip_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 () |
| void | set_compression (uint32 a_level) |
| void | close () |
| directory & | dir () |
| const directory & | dir () const |
| bool | write (uint32 &a_nbytes) |
| bool | add_ziper (char a_key, zip_func a_func) |
Protected Types | |
| enum | EAccessMode { kFileExists = 0, kExecutePermission = 1, kWritePermission = 2, kReadPermission = 4 } |
Protected Member Functions | |
| file (const file &a_from) | |
| file & | operator= (const file &) |
| bool | write_header () |
| bool | write_streamer_infos () |
| 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 std::string | sout (const std::string &a_string) |
| static bool | zip (std::ostream &a_out, ifile::zip_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, zip_func > | m_zipers |
| 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 |
enum inlib::wroot::file::EAccessMode [protected] |
Definition at line 399 of file file.
{
kFileExists = 0,
kExecutePermission = 1,
kWritePermission = 2,
kReadPermission = 4
};
| inlib::wroot::file::file | ( | std::ostream & | a_out, |
| const std::string & | a_path, | ||
| bool | a_verbose = false |
||
| ) | [inline] |
Definition at line 193 of file file.
:m_out(a_out) ,m_path(a_path) ,m_verbose(a_verbose) ,m_file(not_open()) //,m_bytes_write(0) ,m_root_directory(*this,nosuffix(a_path),m_title) // begin of record : ,m_version(0) ,m_BEGIN(0) ,m_END(0) ,m_seek_free(0) ,m_nbytes_free(0) ,m_nbytes_name(0) ,m_units(4) ,m_compress(1) ,m_seek_info(0) ,m_nbytes_info(0) { #ifdef INLIB_MEM mem::increment(s_class().c_str()); #endif m_version = version(); if(access_path(m_path,kFileExists)) unlink(m_path); if(!m_root_directory.is_valid()) { m_out << "inlib::wroot::file::file :" << " " << sout(m_path) << " root directory badly created." << std::endl; return; } m_file = _open(a_path.c_str(), #ifdef WIN32 O_RDWR | O_CREAT | O_BINARY,S_IREAD | S_IWRITE #else O_RDWR | O_CREAT,0644 #endif ); if(m_file==not_open()) { m_out << "inlib::wroot::file::file :" << " can't open " << sout(a_path) << "." << std::endl; return; } //initialize : m_BEGIN = kBegin(); // First used word in file following the file header. m_END = m_BEGIN; // Pointer to end of file. // Write Directory info : uint32 namelen = key::std_string_record_size(m_path) + key::std_string_record_size(m_title); uint32 nbytes = namelen + m_root_directory.record_size(); wroot::key key(*this,0,m_path,m_title,"TFile",nbytes); //set m_END. // m_nbytes_name = start point of directory info from key head. m_nbytes_name = key.key_length() + namelen; m_root_directory.set_nbytes_name(m_nbytes_name); m_root_directory.set_seek_directory(key.seek_key()); //at EOF. //the below write 45 bytes at BOF (Begin Of File). if(!write_header()) { //need m_nbytes_name, m_END after key written. m_out << "inlib::wroot::file::file :" << " can't write file header." << std::endl; return; } {char* pos = key.data_buffer(); wbuf wb(m_out,byte_swap(),key.eob(),pos); if(!wb.write(m_path)) return; if(!wb.write(m_title)) return; if(!m_root_directory.to_buffer(wb)) return;} if(m_verbose) { m_out << "inlib::wroot::file::file :" << " write key (" << namelen << ", " << m_root_directory.record_size() << ", " << nbytes << ", " << m_nbytes_name << ", " << key.seek_key() << ")." << std::endl; } key.set_cycle(1); if(!key.write_self()) { m_out << "inlib::wroot::file::file :" << " key.write_self() failed." << std::endl; return; } //the below write at kBegin + nbytes. //64+52 uint32 n; if(!key.write_file(n)) { m_out << "inlib::wroot::file::file :" << " can't write key in file." << std::endl; return; } //::printf("debug : file::file : write key : %d\n",n); }
| virtual inlib::wroot::file::~file | ( | ) | [inline, virtual] |
| inlib::wroot::file::file | ( | const file & | a_from | ) | [inline, protected] |
Definition at line 316 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 }
| static int inlib::wroot::file::_open | ( | const char * | a_name, |
| int | a_flags, | ||
| unsigned int | a_mode | ||
| ) | [inline, static, protected] |
| static bool inlib::wroot::file::access_path | ( | const std::string & | a_path, |
| EAccessMode | a_mode | ||
| ) | [inline, static, protected] |
Definition at line 405 of file file.
{
// Returns true if one can access a file using the specified access mode.
// Mode is the same as for the WinNT access(2) function.
#ifdef WIN32
return (::_access(a_path.c_str(),a_mode) == 0) ? true : false;
#else
return (::access(a_path.c_str(),a_mode) == 0) ? true : false;
#endif
}
| bool inlib::wroot::file::add_ziper | ( | char | a_key, |
| zip_func | a_func | ||
| ) | [inline] |
| virtual bool inlib::wroot::file::byte_swap | ( | ) | const [inline, virtual] |
| void inlib::wroot::file::close | ( | ) | [inline] |
| virtual void inlib::wroot::file::compress_buffer | ( | const buffer & | a_buffer, |
| char *& | a_kbuf, | ||
| uint32 & | a_klen, | ||
| bool & | a_kdel | ||
| ) | [inline, virtual] |
Implements inlib::wroot::ifile.
Definition at line 140 of file file.
{
//NOTE : if(kdelete) delete [] kbuf;
a_kbuf = 0;
a_klen = 0;
a_kdel = false;
uint32 nbytes = a_buffer.length();
uint32 cxlevel = m_compress;
if(cxlevel && (nbytes>256)) {
ifile::zip_func func;
if(!ziper('Z',func)) {
//m_out << "inlib::wroot::directory::write_object :"
// << " zlib ziper not found."
// << std::endl;
a_kbuf = (char*)a_buffer.buf();
a_klen = a_buffer.length();
a_kdel = false;
} else {
const uint32 kMAXBUF = 0xffffff;
const uint32 HDRSIZE = 9;
uint32 nbuffers = nbytes/kMAXBUF;
uint32 buflen = nbytes+HDRSIZE*(nbuffers+1);
a_kbuf = new char[buflen];
a_kdel = true;
char* src = (char*)a_buffer.buf();
char* tgt = a_kbuf;
uint32 nzip = 0;
for(uint32 i=0;i<=nbuffers;i++) {
uint32 bufmax = ((i == nbuffers) ? nbytes - nzip : kMAXBUF);
uint32 nout;
if(!zip(m_out,func,cxlevel,bufmax,src,bufmax,tgt,nout)) {
delete [] a_kbuf;
a_kbuf = (char*)a_buffer.buf();
a_klen = a_buffer.length();
a_kdel = false;
break;
}
tgt += nout; //nout includes HDRSIZE
a_klen += nout;
src += kMAXBUF;
nzip += kMAXBUF;
}
//::printf("debug : compress : end : %u %u\n",nbytes,klen);
}
} else {
a_kbuf = (char*)a_buffer.buf();
a_klen = a_buffer.length();
a_kdel = false;
}
}
| virtual uint32 inlib::wroot::file::compression | ( | ) | const [inline, virtual] |
| directory& inlib::wroot::file::dir | ( | ) | [inline] |
Definition at line 343 of file file.
{return m_root_directory;}
| const directory& inlib::wroot::file::dir | ( | ) | const [inline] |
Definition at line 344 of file file.
{return m_root_directory;}
| virtual seek inlib::wroot::file::END | ( | ) | const [inline, virtual] |
| int inlib::wroot::file::error_number | ( | ) | [inline, protected] |
| virtual std::ostream& inlib::wroot::file::out | ( | ) | [inline, virtual] |
| void inlib::wroot::file::reset_error_number | ( | ) | [inline, protected] |
| void inlib::wroot::file::set_compression | ( | uint32 | a_level | ) | [inline] |
Definition at line 327 of file file.
{
// level = 0 objects written to this file will not be compressed.
// level = 1 minimal compression level but fast.
// ....
// level = 9 maximal compression level but slow.
m_compress = a_level;
if(m_compress>9) m_compress = 9;
}
| virtual void inlib::wroot::file::set_END | ( | seek | a_end | ) | [inline, virtual] |
| virtual bool inlib::wroot::file::set_pos | ( | seek | a_offset = 0, |
| from | a_from = begin |
||
| ) | [inline, virtual] |
Implements inlib::wroot::ifile.
Definition at line 48 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::wroot::file::set_pos :"
<< " cannot set position " << a_offset
<< " in file " << sout(m_path) << "."
<< std::endl;
return false;
}
return true;
}
| static std::string inlib::wroot::file::sout | ( | const std::string & | a_string | ) | [inline, static, protected] |
| virtual bool inlib::wroot::file::synchronize | ( | ) | [inline, virtual] |
Implements inlib::wroot::ifile.
| static bool inlib::wroot::file::unlink | ( | const std::string & | a_path | ) | [inline, static, protected] |
Definition at line 414 of file file.
{
// Unlink, i.e. remove, a file or directory. Returns true when succesfull,
// false in case of failure.
struct stat finfo;
if (::stat(a_path.c_str(),&finfo) < 0) return false;
#ifdef WIN32
if (finfo.st_mode & S_IFDIR)
return (::_rmdir(a_path.c_str())==-1 ? false : true);
else
return (::unlink(a_path.c_str())==-1 ? false : true);
#else
if (S_ISDIR(finfo.st_mode))
return (::rmdir(a_path.c_str())==-1 ? false : true);
else
return (::unlink(a_path.c_str())==-1 ? false : true);
#endif
}
| virtual bool inlib::wroot::file::verbose | ( | ) | const [inline, virtual] |
| virtual uint32 inlib::wroot::file::version | ( | ) | const [inline, virtual] |
Implements inlib::wroot::ifile.
| bool inlib::wroot::file::write | ( | uint32 & | a_nbytes | ) | [inline] |
Definition at line 346 of file file.
{
// Write memory objects to this file :
// Loop on all objects in m_root_directory (including subdirectories).
// A new key is created in the directories m_keys linked list
// for each object.
// The list of keys is then saved on the file (via write_keys)
// as a single data record.
// The directory header info is rewritten on the directory header record.
// //The linked list of FREE segments is written.
// The file header is written (bytes 1->m_BEGIN).
a_nbytes = 0;
if(m_verbose) {
m_out << "inlib::wroot::file::write :"
<< " writing Name=" << sout(m_path)
<< " Title=" << sout(m_title) << "."
<< std::endl;
}
uint32 nbytes;
if(!m_root_directory.write(nbytes)) return false; // Write directory tree
if(!write_streamer_infos()) {
m_out << "inlib::wroot::file::write :"
<< " write_streamer_infos failed."
<< std::endl;
return false;
}
//if(!writeFreeSegments()) return false; // Write free segments.
if(!write_header()) { //write 45 bytes at BOF.
m_out << "inlib::wroot::file::write :"
<< " can't write file header."
<< std::endl;
return false;
}
a_nbytes = nbytes;
return true;
}
| virtual bool inlib::wroot::file::write_buffer | ( | const char * | a_buffer, |
| uint32 | a_length | ||
| ) | [inline, virtual] |
Implements inlib::wroot::ifile.
Definition at line 81 of file file.
{
// Write a buffer to the file. This is the basic low level write operation.
ssize_t siz;
while ((siz = ::write(m_file,a_buffer,a_length)) < 0 &&
error_number() == EINTR) reset_error_number();
if(siz < 0) {
m_out << "inlib::wroot::file::write_buffer :"
<< " error writing to file " << sout(m_path) << "."
<< std::endl;
return false;
}
if(siz!=(ssize_t)a_length) {
m_out << "inlib::wroot::file::write_buffer :"
<< "error writing all requested bytes to file " << sout(m_path)
<< ", wrote " << long2s(siz) << " of " << a_length
<< std::endl;
return false;
}
//m_bytes_write += siz;
return true;
}
| bool inlib::wroot::file::write_header | ( | ) | [inline, protected] |
Definition at line 442 of file file.
{
const char root[] = "root";
//char psave[kBegin()];
char psave[128];
const char* eob = psave + kBegin();
char* pos = psave;
::memcpy(pos,root,4); pos += 4;
uint32 version = m_version;
if((m_END>START_BIG_FILE()) ||
(m_seek_free>START_BIG_FILE()) ||
(m_seek_info>START_BIG_FILE()) ){
version += 1000000;
m_units = 8;
}
wbuf wb(m_out,byte_swap(),eob,pos);
if(!wb.write(version)) return false;
if(!wb.write((seek32)m_BEGIN)) return false;
if(version>1000000) {
if(!wb.write(m_END)) return false;
if(!wb.write(m_seek_free)) return false;
} else {
if(!wb.write((seek32)m_END)) return false;
if(!wb.write((seek32)m_seek_free)) return false;
}
if(!wb.write(m_nbytes_free)) return false;
//int nfree = fFreeSegments.size();
uint32 nfree = 0; //FIXME
if(!wb.write(nfree)) return false;
if(!wb.write(m_nbytes_name)) return false;
if(!wb.write(m_units)) return false;
if(!wb.write(m_compress)) return false;
if(version>1000000) {
if(!wb.write(m_seek_info)) return false;
} else {
if(!wb.write((seek32)m_seek_info)) return false;
}
if(!wb.write(m_nbytes_info)) return false;
if(!set_pos()) return false; //BOF
uint32 nbytes = pos - psave;
//::printf("debug : write_header : %d\n",nbytes);
if(!write_buffer(psave,nbytes)) return false;
if(!synchronize()) return false;
return true;
}
| bool inlib::wroot::file::write_streamer_infos | ( | ) | [inline, protected] |
Definition at line 487 of file file.
{
List<StreamerInfo> sinfos;
fill_infos(sinfos,m_out);
if(sinfos.empty()) return false;
buffer bref(m_out,byte_swap(),256);
if(!sinfos.stream(bref)) {
m_out << "inlib::wroot::file::write_streamer_infos :"
<< " cannot stream List<StreamerInfo>."
<< std::endl;
return false;
}
uint32 nbytes = bref.length();
wroot::key key(*this,
m_root_directory.seek_directory(),
"StreamerInfo","",
sinfos.store_cls(),
nbytes); //set m_END
if(!key.seek_key()) return false;
if(!bref.displace_mapped(key.key_length())) return false;
::memcpy(key.data_buffer(),bref.buf(),nbytes);
//key.set_cycle(1);
if(!key.write_self()) {
m_out << "inlib::wroot::file::write_streamer_infos :"
<< " key.write_self() failed."
<< std::endl;
return false;
}
m_seek_info = key.seek_key();
m_nbytes_info = key.number_of_bytes();
//FIXME sumBuffer(key.objectSize());
uint32 n;
if(!key.write_file(n)) return false;
if(!n) return false;
return true;
}
| static bool inlib::wroot::file::zip | ( | std::ostream & | a_out, |
| ifile::zip_func | a_func, | ||
| int | a_level, | ||
| uint32 | a_srcsize, | ||
| char * | a_src, | ||
| uint32 | a_tgtsize, | ||
| char * | a_tgt, | ||
| uint32 & | a_irep | ||
| ) | [inline, static, protected] |
Definition at line 534 of file file.
{
// from Rio/Bits/R__zip using zlib.
const uint32 HDRSIZE = 9;
if(a_tgtsize<HDRSIZE) {
a_out << "inlib::rroot::directory::zip :"
<< " target buffer too small."
<< std::endl;
a_irep = 0;
return false;
}
if(a_srcsize>0xffffff) {
a_out << "inlib::rroot::directory::zip :"
<< " source buffer too big."
<< std::endl;
a_irep = 0;
return false;
}
uint32 out_size;
if(!a_func(a_out,a_level,
a_srcsize,a_src,
a_tgtsize,a_tgt+HDRSIZE,
out_size)) {
a_out << "inlib::rroot::directory::zip :"
<< " zipper failed."
<< std::endl;
a_irep = 0;
return false;
}
if((HDRSIZE+out_size)>a_tgtsize) {
a_out << "inlib::rroot::directory::zip :"
<< " target buffer overflow."
<< std::endl;
a_irep = 0;
return false;
}
// HEADER :
a_tgt[0] = 'Z'; // Signature ZLib
a_tgt[1] = 'L';
a_tgt[2] = 8; //DEFLATE
a_tgt[3] = (char)(out_size & 0xff);
a_tgt[4] = (char)((out_size >> 8) & 0xff);
a_tgt[5] = (char)((out_size >> 16) & 0xff);
a_tgt[6] = (char)(a_srcsize & 0xff);
a_tgt[7] = (char)((a_srcsize >> 8) & 0xff);
a_tgt[8] = (char)((a_srcsize >> 16) & 0xff);
a_irep = HDRSIZE+out_size;
return true;
}
| virtual bool inlib::wroot::file::ziper | ( | char | a_key, |
| zip_func & | a_func | ||
| ) | const [inline, virtual] |
Implements inlib::wroot::ifile.
seek inlib::wroot::file::m_BEGIN [protected] |
uint32 inlib::wroot::file::m_compress [protected] |
seek inlib::wroot::file::m_END [protected] |
int inlib::wroot::file::m_file [protected] |
uint32 inlib::wroot::file::m_nbytes_free [protected] |
uint32 inlib::wroot::file::m_nbytes_info [protected] |
uint32 inlib::wroot::file::m_nbytes_name [protected] |
std::ostream& inlib::wroot::file::m_out [protected] |
std::string inlib::wroot::file::m_path [protected] |
directory inlib::wroot::file::m_root_directory [protected] |
seek inlib::wroot::file::m_seek_free [protected] |
seek inlib::wroot::file::m_seek_info [protected] |
std::string inlib::wroot::file::m_title [protected] |
char inlib::wroot::file::m_units [protected] |
bool inlib::wroot::file::m_verbose [protected] |
uint32 inlib::wroot::file::m_version [protected] |
std::map<char,zip_func> inlib::wroot::file::m_zipers [protected] |
1.7.5.1