Warning, /include/Geant4/tools/file is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_file
0005 #define tools_file
0006
0007 #include "forit"
0008 #include "get_env"
0009 #include "sep"
0010 #include "sout"
0011 #include "tokenize"
0012
0013 #include <cstdlib>
0014 #include <cstring>
0015 #include <ostream>
0016
0017 namespace tools {
0018 namespace file {
0019
0020 inline bool exists(const std::string& a_string) {
0021 FILE* file = ::fopen(a_string.c_str(),"rb");
0022 if(!file) return false;
0023 ::fclose(file);
0024 return true;
0025 }
0026
0027 inline bool read_bytes(const std::string& a_file,char*& a_buffer,long& a_length){
0028 // Returned buffer should be deleted with delete [].
0029 FILE* file = ::fopen(a_file.c_str(),"rb");
0030 if(!file) {
0031 a_buffer = 0;
0032 a_length = 0L;
0033 return false;
0034 }
0035 // Get file size :
0036 ::fseek(file,0L,SEEK_END);
0037 long filesize = ::ftell(file);
0038 if(!filesize) {
0039 ::fclose(file);
0040 a_buffer = new char[1];
0041 a_length = 0L;
0042 return true; //empty file.
0043 }
0044 // Add one byte to be able to add \n if file contain lines.
0045 a_buffer = new char[filesize+1];
0046 if(!a_buffer) {
0047 ::fclose(file);
0048 a_buffer = 0;
0049 a_length = 0L;
0050 return false;
0051 }
0052 ::rewind(file);
0053 size_t nitem = ::fread(a_buffer,(size_t)filesize,(size_t)1,file);
0054 if(nitem!=1){
0055 ::fclose(file);
0056 delete [] a_buffer;
0057 a_buffer = 0;
0058 a_length = 0L;
0059 return false;
0060 }
0061 ::fclose(file);
0062 a_buffer[filesize] = 0;
0063 a_length = filesize;
0064 return true;
0065 }
0066
0067
0068 inline bool find_with_dirs(std::ostream& a_out,
0069 const std::vector<std::string>& a_dirs,
0070 const std::string& a_file,
0071 std::string& a_path,
0072 bool a_verbose = false ) {
0073 std::vector<std::string>::const_iterator it;
0074 for(it=a_dirs.begin();it!=a_dirs.end();++it) {
0075 if((*it).empty()) {
0076 // with a "" in dirs, this case could solve :
0077 // - a_file in the current directory.
0078 // - a_file with an absolute path name.
0079 a_path = a_file; //may be an absolute file name.
0080 } else {
0081 a_path = *it;
0082 a_path += sep();
0083 a_path += a_file;
0084 }
0085
0086 if(a_verbose) {
0087 a_out << "find_with_dirs :"
0088 << " look for " << sout(a_path) << " ..."
0089 << std::endl;
0090 }
0091
0092 if(file::exists(a_path)) {
0093 if(a_verbose) {
0094 a_out << "find_with_dirs :"
0095 << " found " << sout(a_path) << "."
0096 << std::endl;
0097 }
0098 return true;
0099 }
0100 }
0101 a_path.clear();
0102
0103 if(a_verbose) {
0104 a_out << "find_with_dirs :"
0105 << " " << sout(a_file) << " not found."
0106 << std::endl;
0107 }
0108
0109 return false;
0110 }
0111
0112 inline bool find_with_env(std::ostream& a_out,
0113 const std::string& a_env,
0114 const std::string& a_file,
0115 std::string& a_path,
0116 bool a_verbose = false ) {
0117 std::string PATH;
0118 if(!get_env(a_env,PATH)) {
0119 //look for a a_file in current directory.
0120 if(file::exists(a_file)) {
0121 a_path = a_file;
0122 return true;
0123 }
0124 a_out << "tools::find_with_env : env variable " << sout(a_env) << " not defined." << std::endl;
0125 a_path.clear();
0126 return false;
0127 }
0128 if(a_verbose) {
0129 a_out << "find_with_env : env " << sout(a_env) << " is " << sout(PATH) << std::endl;
0130 }
0131
0132 std::vector<std::string> dirs;
0133 words(PATH,psep(),false,dirs); //or true ?
0134
0135 return find_with_dirs(a_out,dirs,a_file,a_path,a_verbose);
0136 }
0137
0138 inline bool std_remove(const std::string& a_file) {
0139 if(a_file.empty()) return true;
0140 return (::remove(a_file.c_str()) ==0 ? true : false);
0141 }
0142
0143 }}
0144
0145 #include "file_reader"
0146
0147 namespace tools {
0148
0149 class FILE_reader : public virtual file::reader {
0150 public: //file_reader
0151 virtual bool open(const std::string& a_file) {
0152 if(m_FILE) return false;
0153 m_FILE = ::fopen(a_file.c_str(),"rb");
0154 if(!m_FILE) return false;
0155 return true;
0156 }
0157 virtual void close() {
0158 if(!m_FILE) return;
0159 ::fclose(m_FILE);
0160 m_FILE = 0;
0161 }
0162 virtual bool is_open() const {return m_FILE?true:false;}
0163 virtual bool read(char* a_buff,unsigned int a_lbuf,size_t& a_length) {
0164 a_length = ::fread(a_buff,1,a_lbuf,m_FILE);
0165 return true;
0166 }
0167 virtual bool get_line(char* a_buff,unsigned int a_lbuf) {
0168 return ::fgets(a_buff,a_lbuf,m_FILE)==NULL?false:true;
0169 }
0170 virtual bool eof() const {
0171 #if defined(_MSC_VER) && _MSC_VER <= 1400
0172 return feof(m_FILE)?true:false;
0173 #else
0174 return ::feof(m_FILE)?true:false;
0175 #endif
0176 }
0177 public:
0178 FILE_reader():m_FILE(0){}
0179 virtual ~FILE_reader() {if(m_FILE) ::fclose(m_FILE);}
0180 protected:
0181 FILE_reader(const FILE_reader& a_from)
0182 :file::reader(a_from)
0183 ,m_FILE(0)
0184 {}
0185 FILE_reader& operator=(const FILE_reader&){return *this;}
0186 protected:
0187 FILE* m_FILE;
0188 };
0189
0190 }
0191
0192 #endif