/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #include "file.h" #include #include #include #include #include #ifdef __unix__ #include #elif __WIN32__ || _MS_DOS_ #include #else #include #endif #include File::File(const std::string& fileName) { this->_name = fileName; File::homeDirCheck(this->_name); this->init(); } File::File(const File& file) { this->_name = file._name; } File::~File() { if (this->_status) delete this->_status; if (this->_handle) assert(0); } /** * @brief initializes the File * * Stats the File, looks if it exists and sets the hadle to 0 */ void File::init() { this->_handle = 0; std::string name = this->_name; this->_status = new struct stat; // Check the End of the FileName and chop away any \ and // if (this->_name[this->_name.size()-1] == '/' || this->_name[this->_name.size()-1] == '\\') name.resize(name.size()-1); if (stat(name.c_str(), this->_status)) { delete this->_status; this->_status = NULL; } } bool File::open(OpenMode mode) { #warning implement } bool File::close() { #warning implement } int File::handle() { return this->_handle; } bool File::exists() { return (this->_status != NULL); } bool File::isLink() { #ifndef __WIN32__ return (this->_status != NULL && this->_status->st_mode & (S_IFLNK)); #else return false; #endif } // only on UNIX bool File::isFile() { return (this->_status != NULL && this->_status->st_mode & (S_IFREG)); } /** * @brief Checks if it is a Directory * @param directoryName the Directory to check for * @returns true if it is a directory/symlink false otherwise */ bool File::isDirectory() { // checking for the termination of the string given. If there is a "/" at the end cut it away //if (this->_name[this->_name.size()-1] == '/' || // this->_name[this->_name.size()-1] == '\\') //{ // tmpDirName.erase(tmpDirName.size()-1); //} return (this->_status != NULL && this->_status->st_mode & (S_IFDIR)); } /// FIXME NEXT THREE FUNCTIONS bool File::isReadeable() { #ifndef __WIN32__ return (this->_status != NULL && this->_status->st_mode & (S_IRUSR)); #else return (this->_status != NULL); #endif } bool File::isWriteable() { #ifndef __WIN32__ return (this->_status != NULL && this->_status->st_mode & (S_IWUSR)); #else return (this->_status != NULL); #endif } bool File::isExecutable() { #ifndef __WIN32__ return (this->_status != NULL && this->_status->st_mode & (S_IXUSR)); #else return (this->_status != NULL); #endif } bool File::copy(const File& destination) { char ch; std::ifstream iFile(this->_name.c_str()); std::ofstream oFile(destination.name().c_str()); while (iFile.get(ch)) { oFile.put(ch); } } bool File::rename(const File& destination) { if (!std::rename(this->_name.c_str(), destination.name().c_str())) { this->close(); delete this->_status; this->_status = NULL; this->_name = destination.name(); this->init(); return true; } return false; } bool File::touch() { FILE* stream; if( (stream = fopen (this->_name.c_str(), "w")) == NULL) { std::cout << "could not touch '" << this->_name << "' for writing\n"; return false; } fclose(stream); return true; } bool File::remove() { unlink(this->_name.c_str()); delete this->_status; this->_status = NULL; /// FIXME HANDLE } void File::relToAbs(std::string& fileName) { if (fileName.empty()) return ; if (fileName[0] != '/') { if (fileName[0] == '.' && fileName[1] != '.') fileName.erase(0); fileName = File::cwd() + fileName; } } void File::absToRel(std::string& fileName) { if (fileName.find(cwd()) == 0) fileName.replace(0, File::cwd().size(), "."); } std::string File::_cwd = ""; /** * @returns the Current Woring Directory */ const std::string& File::cwd() { if (File::_cwd.empty()) { char cwd[1024]; char* errorCode = getcwd(cwd, 1024); if (errorCode == 0) return File::_cwd; File::_cwd = cwd; } return File::_cwd; } void File::homeDirCheck(std::string& fileName) { if (fileName.size() < 2 || fileName[0] != '~' || fileName[1] != '/') return; std::string homeDir; #ifdef __WIN32__ homeDir = getenv("USERPROFILE"); #else homeDir = getenv("HOME"); #endif fileName = homeDir + fileName.substr(1); } #include "file.h"