#ifndef AASPI_FILE_INFO_H #define AASPI_FILE_INFO_H //! \file //! \brief Provides System Independent Information about files #include #include #include #include "aaspi_types.h" #ifdef _WIN32 #include #include #include #include #define AASPI_WRITABLE 0x02 #define AASPI_READABLE 0x04 typedef struct __stat64 aaspi_stat; #else #include #define AASPI_WRITABLE W_OK #define AASPI_READABLE R_OK typedef struct stat aaspi_stat; #endif using namespace std; //! \brief Provides System independent information about files //! //! Also provides several static query functions class AASPI_FileInfo { public: //! \brief File Information associated with path //! AASPI_FileInfo(const string &path); ~AASPI_FileInfo() {} //! \brief Check if file exists //! bool exists() { return dep_exists(err); } //! \brief Check if file is a directory //! bool isADirectory(); //! \brief Check if file is a regular file //! bool isAFile(); //! \brief Check if file is readable by this process //! inline bool isReadable() { return readable; } //! \brief Check if file is writable by this process //! inline bool isWritable() { return writable; } //! \brief Return file size in bytes //! //! \returns Returns zero on failure aaspi_int64 size(); // Static functions //! \brief Check if file exists //! static bool exists(const string &path); //! \brief Check if file is a directory //! static bool isADirectory(const string &path); //! \brief Check if file is a regular file //! static bool isAFile(const string &path); //! \brief Check if file is writable by this process //! static bool isReadable(const string &path); //! \brief Check if file is writable by this process //! static bool isWritable(const string &path); //! \brief Return file size in bytes //! //! \returns Returns zero on failure static aaspi_int64 size(const string &path); //! \brief Get an acceptable name for a temporary file //! static string get_temp_file(const string &basepath="./", const string &basename="temp_", const string &suffix=""); protected: static int Stat(const string &path, aaspi_stat *stat_struct); static int Access(const string &path, int mode); static bool dep_exists(int eno); string cur_path; aaspi_stat cur_stat; int err; bool readable, writable; }; #ifdef _WIN32 //--------------------------------------------------------------------------- // Windows definitions //--------------------------------------------------------------------------- inline int AASPI_FileInfo::Stat(const string &path, aaspi_stat *stat_struct) { int rv=0; rv= _stat64(path.c_str(),stat_struct); if (rv != 0) rv=errno; return rv; } inline int AASPI_FileInfo::Access(const string &path, int mode) { return _access(path.c_str(),mode); } inline bool AASPI_FileInfo::dep_exists(int eno) { return !(eno == ENOENT); } #else //--------------------------------------------------------------------------- // Linux definitions //--------------------------------------------------------------------------- inline int AASPI_FileInfo::Stat(const string &path, aaspi_stat *stat_struct) { int rv=0; rv=stat(path.c_str(),stat_struct); if (rv != 0) rv=errno; return rv; } inline int AASPI_FileInfo::Access(const string &path, int mode) { return access(path.c_str(),mode); } inline bool AASPI_FileInfo::dep_exists(int eno) { return !( (eno == ENOENT) || (eno == ENOTDIR) || (eno == ELOOP) ); } #endif #endif