// The code is copyrighted 2008 by Carsten Frigaard. // All rights placed in public domain under GNU licence V2, 2008 // // © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software // and its documentation for any purpose and without fee is hereby granted, provided that // the above copyright notice appear in all copies and that both that copyright notice // and this permission notice appear in supporting documentation. #ifndef __FILE_H__ #define __FILE_H__ inline bool FileExists(const std::string& f) { ifstream s(f.c_str()); return (!s)==false; } inline void AssertFileExists(const std::string& f) { if (!FileExists(f)) throw_("File <" + f + "> does not exist"); } inline size_t FileSize(const std::string& f) { AssertFileExists(f); ifstream s(f.c_str()); if (!s) throw_("Stream is bad (file <" + f + ">)"); s.seekg(0,ios::end); if (!s) throw_("Stream is bad (file <" + f + ">)"); return s.tellg(); } inline void AssertFileNotEmpty(const std::string& f) { if (FileSize(f)==0) throw_("File <" + f + "> is empty"); } inline size_t FileTime(const string& file) { AssertFileExists(file); const string t=System("date -r " + file + " +%s",true,true); // seconds since 1970-01-01 00:00:00 UTC return totype(t); // #include // #include // struct stat s; // int n=stat(file.c_str(),&s); // if (n!=0) throw_("cannot stat file <" + file + ">"); // assert( sizeof(time_t)==sizeof(size_t) ); // return s.st_mtime; } inline bool isFileNewer(const string& file0,const string& file1) { return FileTime(file0)>FileTime(file1); } inline bool DirExists(const std::string& f) { const string file=f + "/.dummy.txt"; ofstream s(file.c_str()); if(!s) return false; s << "testfile"; if(!s) return false; s.close(); return FileSize(file)==8; } inline string MakeSuffix(const int n) { assert(n>=0); if (n<=9) return "00" + tostring(n); else if (n<=99) return "0" + tostring(n); else return tostring(n); } template void Readdata(const string& tok,istream& is,T& t) { if (!is) throw_("Stream is bad"); is >> t; if (!is) throw_("Reading {" + tok + "} settings"); } inline string Readline(istream& is) { char buff[16*1024]; is.getline(buff,16*1024); return string(buff); } template inline T Readtyp(ifstream& s){ T x; s.read(reinterpret_cast(&x),sizeof(x)); if(!s) throw_("bad stream"); return x; } inline string Readstring(ifstream& s){ char c=0; string t; do{ c=Readtyp(s); if(c!=0) t+=c; } while (c!=0); return t; } template inline vector Readbin(std::ifstream& s,const int size) { if(!s) throw_("bad stream"); vector x(size); s.read(reinterpret_cast(&x.front()),x.size()*sizeof(T)); if(!s) throw_( "bad write"); return x; } template inline void Writetyp(ofstream& s,const T& x){ s.write(reinterpret_cast(&x),sizeof(x)); if(!s) throw_( "bad stream"); } template inline void Writebin(std::ofstream& s,const std::vector& x,const bool writetag) { if(!s) throw_("bad stream"); const size_t sz=x.size()*sizeof(T); if(writetag){ Writetyp(s,sz); } if(!s) throw_( "bad stream" ); s.write(reinterpret_cast(&x.front()),sz); if(!s) throw_( "bad write"); if (writetag) Writetyp(s,sz); if(!s) throw_( "bad stream"); } template inline void Writebin(std::ofstream& s,const std::map& x,const bool writetag) { vector t; vector r; t.reserve(x.size()); r.reserve(x.size()); for(typename std::map::const_iterator itt=x.begin();itt!=x.end();++itt){ t.push_back(itt->first); r.push_back(itt->second); } if (writetag) { Writetyp(s,x.size()); Writetyp(s,static_cast(sizeof(T))); Writetyp(s,static_cast(sizeof(R))); } Writebin(s,t,writetag); Writebin(s,r,writetag); } template inline void Readbin(std::ifstream& s,vector& x) { if(!s) throw_("bad stream"); const size_t sz=Readtyp(s); if(!s) throw_( "bad stream" ); if(sz%sizeof(T)!=0) throw_("bad size tag"); x.resize(sz/sizeof(T)); s.read(reinterpret_cast(&x.front()),sz); if(!s) throw_( "bad write"); if (Readtyp(s)!=sz) throw_("bad size tag"); if(!s) throw_( "bad stream"); } template inline void Readbin(std::ifstream& s,map& x) { vector t; vector r; const size_t sz=Readtyp(s); const size_t szT=Readtyp(s); const size_t szR=Readtyp(s); if (szT!=sizeof(T)) throw_("type T size mismatch in Readbin (map)"); if (szR!=sizeof(R)) throw_("type R size mismatch in Readbin (map)"); Readbin(s,t); Readbin(s,r); if (t.size()!=r.size()) throw_("size mismatch in Readbin (map)"); x.clear(); for(size_t i=0;i inline void Writeascii(const string& filename,const std::vector& x,const string& comment="",const char& newline='\n') { ofstream s(filename.c_str()); if(!s) throw_("bad file <" + filename + ">"); s << "% Writeascii: size=" << x.size() << " " << comment << "\n"; for(size_t i=0;i"); } #endif // __FILE_H__