void File::Rename(const String &source, const String &destination) { if(!Exist(source)) throw Exception(String("Rename: source file does not exist: ") + source); if(Exist(destination)) Remove(destination); if(std::rename(source.pathEncode().c_str(), destination.pathEncode().c_str()) == 0) return; // std::rename will fail to copy between filesystems, so we need to try manually File sourceFile(source, Read); File destinationFile(destination, Truncate); sourceFile.read(destinationFile); sourceFile.close(); destinationFile.close(); Remove(source); }
void File::open(const String &filename, OpenMode mode) { close(); if(filename.empty()) throw Exception("Empty file name"); mName = filename; mMode = mode; std::ios_base::openmode m; switch(mode) { case Read: m = std::ios_base::in; break; case Write: m = std::ios_base::out; break; case Append: m = std::ios_base::app; break; case Truncate: m = std::ios_base::out|std::ios_base::trunc; break; case TruncateReadWrite: m = std::ios_base::in|std::ios_base::out|std::ios_base::trunc; break; default: m = std::ios_base::in|std::ios_base::out; break; } std::fstream::open(filename.pathEncode().c_str(), m|std::ios_base::binary); if(!std::fstream::is_open() || std::fstream::bad()) throw Exception(String("Unable to open file: ")+filename); mReadPosition = 0; mWritePosition = 0; if(m & std::ios_base::app) mWritePosition = size(); }
bool File::Exist(const String &filename) { if(filename.empty()) return false; /*std::fstream file; file.open(filename.pathEncode().c_str(), std::ios_base::in); return file.is_open();*/ stat_t st; if(pla::stat(filename.pathEncode().c_str(), &st)) return false; if(!S_ISDIR(st.st_mode)) return true; else return false; }
pla::Time File::Time(const String &filename) { stat_t st; if(pla::stat(filename.pathEncode().c_str(), &st)) throw Exception("File does not exist: "+filename); return pla::Time(st.st_mtime); }
uint64_t File::Size(const String &filename) { stat_t st; if(pla::stat(filename.pathEncode().c_str(), &st)) throw Exception("File does not exist: "+filename); return uint64_t(st.st_size); }
bool File::Remove(const String &filename) { return (std::remove(filename.pathEncode().c_str()) == 0); }