inlib  1.2.0
Classes | Enumerations | Functions
inlib::file Namespace Reference

Classes

class  cache
class  base_cache
class  reader

Enumerations

enum  chmod_who { ch_user, ch_group, ch_other, ch_all }
 by using ::system(<file system>) ///////////////////////////////////////// More...
enum  chmod_operation { ch_add, ch_sub }
enum  chmod_what { ch_read, ch_write, ch_exec }

Functions

bool exists (const std::string &a_string)
bool size (const std::string &a_file, long &a_size)
bool is_empty (const std::string &a_file)
bool write (const std::string &a_file, const std::string &a_string)
bool write (const std::string &a_file, const std::vector< std::string > &a_text)
bool read_buff (FILE *a_file, char *a_buff, unsigned int a_lbuf, size_t &a_length)
bool read_cstring (FILE *a_file, char *a_buff, unsigned int a_lbuf, size_t &a_length)
bool read (const std::string &a_file, std::vector< std::string > &a_text)
bool read_num (const std::string &a_file, std::vector< std::string >::size_type a_num, std::vector< std::string > &a_text)
bool read_bytes (const std::string &a_file, char *&a_buffer, long &a_length)
bool write_bytes (const std::string &a_file, const char *a_buffer, size_t a_length)
bool signature (const std::string &a_file, unsigned char a_head[], unsigned int a_num=4)
bool is_gzip (const std::string &a_file, bool &a_is)
bool is_jpeg (const std::string &a_file, bool &a_is)
bool is_png (const std::string &a_file, bool &a_is)
bool is_root (const std::string &a_file, bool &a_is)
bool is_iv (const std::string &a_file, bool &a_is)
bool is_aida (const std::string &a_file, bool &a_is)
bool is_exsg (const std::string &a_file, bool &a_is)
bool found (const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
bool std_remove (const std::string &a_file)
bool std_remove (std::vector< std::string > &a_files)
bool std_rename (const std::string &a_from, const std::string &a_to)
bool chmod (const std::string &a_file, chmod_who a_who, chmod_operation a_op, chmod_what a_what)
std::string quote (const std::string &a_path)
bool mv (const std::string &a_from, const std::string &a_to)
bool cp (const std::string &a_from, const std::string &a_to)
bool rm (const std::string &a_file)

Enumeration Type Documentation

Enumerator:
ch_add 
ch_sub 

Definition at line 336 of file file.

{ ch_add, ch_sub };
Enumerator:
ch_read 
ch_write 
ch_exec 

Definition at line 337 of file file.

by using ::system(<file system>) /////////////////////////////////////////

Enumerator:
ch_user 
ch_group 
ch_other 
ch_all 

Definition at line 335 of file file.


Function Documentation

bool inlib::file::chmod ( const std::string &  a_file,
chmod_who  a_who,
chmod_operation  a_op,
chmod_what  a_what 
) [inline]

Definition at line 339 of file file.

                                                                                                 {
#ifdef WIN32
  //FIXME
  return false;
#else
  std::string cmd = "chmod ";
  switch(a_who){
  case ch_user: cmd += "u";break;
  case ch_group: cmd += "g";break;
  case ch_other: cmd += "o";break;
  case ch_all: cmd += "a";break;
  //default:break;
  }
  switch(a_op){
  case ch_add: cmd += "+";break;
  case ch_sub: cmd += "-";break;
  //default:break;
  }
  switch(a_what){
  case ch_read: cmd += "r";break;
  case ch_write: cmd += "w";break;
  case ch_exec: cmd += "x";break;
  //default:break;
  }
  cmd += " ";
  cmd += a_file;
  int ret = ::system(cmd.c_str());
  return (ret==(-1)?false:true);
#endif  
}
bool inlib::file::cp ( const std::string &  a_from,
const std::string &  a_to 
) [inline]

Definition at line 393 of file file.

                                                              {
  if(a_to==a_from) return true;
#ifdef WIN32
  std::string cmd = "COPY /Y";
#else
  std::string cmd = "/bin/cp";
#endif
  cmd += " ";
  cmd += quote(a_from);
  cmd += " ";
  cmd += quote(a_to);
  int ret = ::system(cmd.c_str());
  return (ret==(-1)?false:true);
}
bool inlib::file::exists ( const std::string &  a_string) [inline]

Definition at line 16 of file file.

                                              {
  FILE* file = ::fopen(a_string.c_str(),"rb");
  if(!file) return false;
  ::fclose(file);
  return true;
}
bool inlib::file::found ( const std::string &  a_file,
const std::string &  a_what,
std::vector< std::string > &  a_found 
) [inline]

Definition at line 293 of file file.

                                                                                                {
  a_found.clear();
  std::vector<std::string> text;
  if(!inlib::file::read(a_file,text)) return false;
  std::vector<std::string>::const_iterator it;
  for(it=text.begin();it!=text.end();++it) {
    if((*it).find(a_what)!=std::string::npos) {
      a_found.push_back(*it);
    }
  }
  return true;
}
bool inlib::file::is_aida ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 256 of file file.

                                                       {
  //NOTE : it assumes that the file is not compressed.
  // AIDA XML expected header :
  //   <?xml version="1.0" encoding="UTF-8"?>
  //   <!DOCTYPE aida SYSTEM "http://aida.freehep.org/schemas/3.0/aida.dtd">
  //   <aida version="3.2.1">
  std::vector<std::string> lines;
  if(!read_num(a_file,2,lines)) {a_is = false;return false;}
  if(lines.size()<2) {a_is = false;return false;}
  //static const std::string saida = "<!DOCTYPE aida"; //CINT don't want that.
  static const char s_aida[] = "<!DOCTYPE aida";
  if(lines[1].substr(0,::strlen(s_aida))!=s_aida) {a_is = false;return true;}
  a_is = true;  
  return true;
}
bool inlib::file::is_empty ( const std::string &  a_file) [inline]

Definition at line 36 of file file.

                                             {
  long sz;
  if(!size(a_file,sz)) return true; //if not existing, consider it empty.
  return (sz==0L)?true:false;  
}
bool inlib::file::is_exsg ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 272 of file file.

                                                       {
  unsigned char head[5];
  if(!signature(a_file,head,5)) {a_is = false;return false;}
  if(head[0]!='<') {a_is = false;return true;}
  if(head[1]!='e') {a_is = false;return true;}
  if(head[2]!='x') {a_is = false;return true;}
  if(head[3]!='s') {a_is = false;return true;}
  if(head[4]!='g') {a_is = false;return true;}
  a_is = true;  
  return true;
}
bool inlib::file::is_gzip ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 195 of file file.

                                                       {
  unsigned char head[4];
  if(!signature(a_file,head)) {a_is = false;return false;}
  if(head[0]!=31) {a_is = false;return true;}
  if(head[1]!=139) {a_is = false;return true;}
  //if(head[2]!=8) {a_is = false;return true;}
  //if(head[3]!=8) {a_is = false;return true;}
  a_is = true;  
  return true;
}
bool inlib::file::is_iv ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 240 of file file.

                                                     {
  unsigned char head[9];
  if(!signature(a_file,head,9)) {a_is = false;return false;}
  if(head[0]!='#') {a_is = false;return true;}
  if(head[1]!='I') {a_is = false;return true;}
  if(head[2]!='n') {a_is = false;return true;}
  if(head[3]!='v') {a_is = false;return true;}
  if(head[4]!='e') {a_is = false;return true;}
  if(head[5]!='n') {a_is = false;return true;}
  if(head[6]!='t') {a_is = false;return true;}
  if(head[7]!='o') {a_is = false;return true;}
  if(head[8]!='r') {a_is = false;return true;}
  a_is = true;  
  return true;
}
bool inlib::file::is_jpeg ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 206 of file file.

                                                       {
  unsigned char head[4];
  if(!signature(a_file,head)) {a_is = false;return false;}
  if(head[0]!=255) {a_is = false;return true;}
  if(head[1]!=216) {a_is = false;return true;}
  if(head[2]!=255) {a_is = false;return true;}
  //if(head[3]!=224) {a_is = false;return true;} //LRI.jpg is 225 !
  a_is = true;  
  return true;
}
bool inlib::file::is_png ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 217 of file file.

                                                      {
  unsigned char head[4];
  if(!signature(a_file,head)) {a_is = false;return false;}
  if(head[0]!=137) {a_is = false;return true;}
  if(head[1]!='P') {a_is = false;return true;}
  if(head[2]!='N') {a_is = false;return true;}
  if(head[3]!='G') {a_is = false;return true;}
  a_is = true;  
  return true;
}
bool inlib::file::is_root ( const std::string &  a_file,
bool &  a_is 
) [inline]

Definition at line 228 of file file.

                                                       {
  unsigned char head[4];
  if(!signature(a_file,head)) {a_is = false;return false;}
  if(head[0]!='r') {a_is = false;return true;}
  if(head[1]!='o') {a_is = false;return true;}
  if(head[2]!='o') {a_is = false;return true;}
  if(head[3]!='t') {a_is = false;return true;}
  // Aie, we are in a mess.
  a_is = true;  
  return true;
}
bool inlib::file::mv ( const std::string &  a_from,
const std::string &  a_to 
) [inline]

Definition at line 378 of file file.

                                                             {
  if(a_to==a_from) return true;
#ifdef WIN32
  std::string cmd = "MOVE /Y";
#else
  std::string cmd = "/bin/mv";
#endif
  cmd += " ";
  cmd += quote(a_from);
  cmd += " ";
  cmd += quote(a_to);
  int ret = ::system(cmd.c_str());
  return (ret==(-1)?false:true);
}
std::string inlib::file::quote ( const std::string &  a_path) [inline]

Definition at line 370 of file file.

                                                {
  if(a_path.find(' ')==std::string::npos) return a_path;
  // path with spaces :
  if(a_path[0]=='"') return a_path; //Already in double quote.
  return std::string("\"")+a_path+"\"";
}
bool inlib::file::read ( const std::string &  a_file,
std::vector< std::string > &  a_text 
) [inline]

Definition at line 95 of file file.

                                                                      {
  a_text.clear();
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) return false;
  unsigned int BUFSIZE = 65536;
  char* buffer = new char[BUFSIZE+1];
  if(!buffer) {::fclose(file);return false;}
  while(true) {
    size_t l;
    if(!read_cstring(file,buffer,BUFSIZE,l)) break; // EOF.
    a_text.push_back(buffer);
  }
  delete [] buffer;
  ::fclose(file);
  return true;
}
bool inlib::file::read_buff ( FILE *  a_file,
char *  a_buff,
unsigned int  a_lbuf,
size_t &  a_length 
) [inline]

Definition at line 68 of file file.

                                                                                      {
  a_length = ::fread(a_buff,1,a_lbuf,a_file);
  return true;
}
bool inlib::file::read_bytes ( const std::string &  a_file,
char *&  a_buffer,
long &  a_length 
) [inline]

Definition at line 133 of file file.

                                                                              {
  // Returned buffer should be deleted with delete [].
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) {
    a_buffer = 0;
    a_length = 0L;
    return false;
  }
  // Get file size :
  ::fseek(file,0L,SEEK_END);
  long filesize = ::ftell(file);
  if(!filesize) {
    ::fclose(file);
    a_buffer = new char[1];
    a_length = 0L;
    return true; //empty file.
  }
  // Add one byte to be able to add \n if file contain lines.
  a_buffer = new char[filesize+1];
  if(!a_buffer) {
    ::fclose(file);
    a_buffer = 0;
    a_length = 0L;
    return false;
  }
  ::rewind(file);
  size_t nitem = ::fread(a_buffer,(size_t)filesize,(size_t)1,file);
  if(nitem!=1){
    ::fclose(file);
    delete [] a_buffer;
    a_buffer = 0;
    a_length = 0L;
    return false;
  }
  ::fclose(file);
  a_buffer[filesize] = 0;
  a_length = filesize;
  return true;
}  
bool inlib::file::read_cstring ( FILE *  a_file,
char *  a_buff,
unsigned int  a_lbuf,
size_t &  a_length 
) [inline]

Definition at line 73 of file file.

                                                                                         {
  if(::fgets(a_buff,a_lbuf,a_file)==NULL) {
    a_length = 0;
    return false; //EOF
  }

  size_t l = ::strlen(a_buff);
  //  On Windows, editors when saving binary files,
  // put \r\n at place of \n ; we then look for \r\n.
  if( (l>=2) && (a_buff[l-2]=='\r') && (a_buff[l-1]=='\n') ) {
    a_buff[l-2] = '\0';
    l -= 2;
  } else if( (l>=1) && (a_buff[l-1]=='\n') ) {
    a_buff[l-1] = '\0';
    l -= 1;
  }

  a_length = l;
  return true;
}
bool inlib::file::read_num ( const std::string &  a_file,
std::vector< std::string >::size_type  a_num,
std::vector< std::string > &  a_text 
) [inline]

Definition at line 112 of file file.

                                                                                                              {
  a_text.clear();
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) return false;
  unsigned int BUFSIZE = 65536;
  char* buffer = new char[BUFSIZE+1];
  if(!buffer) {::fclose(file);return false;}
  while(true) {
    size_t l;
    if(!read_cstring(file,buffer,BUFSIZE,l)) break; // EOF.
    if(a_text.size()<a_num) {
      a_text.push_back(buffer);
    } else {
      break;
    }
  }
  delete [] buffer;
  ::fclose(file);
  return true;
}
bool inlib::file::rm ( const std::string &  a_file) [inline]

Definition at line 408 of file file.

                                       {
  // WARNING : do not confuse with inlib::file::std_remove which is 
  //           an encapsulation of the remove of stdio.h that
  //           does not support wildcards.
#ifdef WIN32
  std::string cmd = "DEL /Q";
#else
  std::string cmd = "/bin/rm -f";
#endif
  cmd += " ";
  cmd += quote(a_file);
  int ret = ::system(cmd.c_str());
  return (ret==(-1)?false:true);
}
bool inlib::file::signature ( const std::string &  a_file,
unsigned char  a_head[],
unsigned int  a_num = 4 
) [inline]

Definition at line 186 of file file.

                                                                                            { //it is assumed a_head[] can contain a_num chars.
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) return false;
  size_t nitem = ::fread(a_head,1,a_num,file);
  ::fclose(file);
  if(nitem!=a_num) return false;
  return true;
}
bool inlib::file::size ( const std::string &  a_file,
long &  a_size 
) [inline]

Definition at line 23 of file file.

                                                      {
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) {
    a_size = 0L;
    return false;
  }
  //::rewind(file);
  ::fseek(file,0L,SEEK_END);
  a_size = ::ftell(file);
  ::fclose(file);
  return true;
}
bool inlib::file::std_remove ( const std::string &  a_file) [inline]

Definition at line 306 of file file.

                                                {
  return (::remove(a_file.c_str()) ==0 ? true : false);
}
bool inlib::file::std_remove ( std::vector< std::string > &  a_files) [inline]

Definition at line 310 of file file.

                                                      {
  bool status = true;
  std::vector<std::string>::iterator it;
  for(it=a_files.begin();it!=a_files.end();++it) {
    if(!std_remove(*it)) status = false;
  }
  a_files.clear();
  return status;
}
bool inlib::file::std_rename ( const std::string &  a_from,
const std::string &  a_to 
) [inline]

Definition at line 320 of file file.

                                                                      {
  //NOTE : a_from must not be a path !
  //       Darwin is ok with a path but not Linux !
  //       For example : 
  //         ::rename("/tmp/tmp01"."x");
  //       return -1 on Linux.
  //       To do the upper then someone must use move. 
  //       But there is no move in the standard lib C !
  return (::rename(a_from.c_str(),a_to.c_str()) == 0 ? true : false);
}
bool inlib::file::write ( const std::string &  a_file,
const std::string &  a_string 
) [inline]

Definition at line 42 of file file.

                                                                     {
  // a_string could contain \n separated lines.
  FILE* file = ::fopen(a_file.c_str(),"wb");
  if(!file) return false;
  if(::fprintf(file,"%s",a_string.c_str())<0) {
    ::fclose(file);
    return false;
  }
  ::fclose(file);
  return true;
}
bool inlib::file::write ( const std::string &  a_file,
const std::vector< std::string > &  a_text 
) [inline]

Definition at line 54 of file file.

                                                                             {
  FILE* file = ::fopen(a_file.c_str(),"wb");
  if(!file) return false;
  std::vector<std::string>::const_iterator it;
  for(it=a_text.begin();it!=a_text.end();++it) {
    if(::fprintf(file,"%s\n",(*it).c_str())<0) {
      ::fclose(file);
      return false;
    }
  }
  ::fclose(file);
  return true;
}
bool inlib::file::write_bytes ( const std::string &  a_file,
const char *  a_buffer,
size_t  a_length 
) [inline]

Definition at line 173 of file file.

                                                                                     {
  FILE* file = ::fopen(a_file.c_str(),"wb");
  if(!file) return false;
  if(!a_length) {
    ::fclose(file);
    return true;
  }
  size_t nitem = ::fwrite((char*)a_buffer,a_length,(size_t)1,file);
  ::fclose(file);
  return (nitem!=1?false:true);
}  
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines