gKitGL
|
00001 00002 #ifndef _IOFILESYSTEM_H 00003 #define _IOFILESYSTEM_H 00004 00005 #ifdef APPLE_OSX 00006 #include <sys/stat.h> 00007 00008 #elif defined WIN32 00009 #include <sys/types.h> 00010 #include <sys/stat.h> 00011 00012 #else 00013 #include <sys/types.h> 00014 #include <sys/stat.h> 00015 #include <unistd.h> 00016 #endif 00017 00018 #include <cstdio> 00019 #include <string> 00020 00021 00022 namespace gk { 00023 00024 //! representation des informations sur un fichier. 00025 struct IOInfo 00026 { 00027 size_t size; //!< longueur en octets. 00028 size_t time; //!< date du fichier. 00029 00030 IOInfo( ) 00031 : 00032 size(0), 00033 time(0) 00034 {} 00035 00036 bool operator == ( const IOInfo& b ) const 00037 { 00038 return (size == b.size && time == b.time); 00039 } 00040 00041 bool operator != ( const IOInfo& b ) const 00042 { 00043 return !(*this == b); 00044 } 00045 }; 00046 00047 struct IOFileSystem 00048 { 00049 //! renvoie le chemin d'acces a un fichier. 00050 00051 /*! le chemin est toujours termine par / 00052 pathname("path/to/file") == "path/to/" 00053 pathname("file") == "./" 00054 */ 00055 static 00056 std::string pathname( const std::string& filename ) 00057 { 00058 size_t slash = filename.find_last_of( '/' ); 00059 size_t bslash = filename.find_last_of( '\\' ); 00060 00061 if ( slash == std::string::npos && bslash != std::string::npos ) 00062 slash = bslash; 00063 else if ( slash != std::string::npos && bslash != std::string::npos && bslash > slash ) 00064 slash = bslash; 00065 00066 if ( slash != std::string::npos ) 00067 return filename.substr( 0, slash +1 ); // inclus le slash 00068 else 00069 return "./"; 00070 } 00071 00072 //! renvoie le nom du fichier sans son extension (chemin inclus). 00073 static 00074 std::string basename( const std::string& filename ) 00075 { 00076 size_t ext= filename.find_last_of('.'); 00077 if(ext== std::string::npos) 00078 return filename; // renvoie le nom tel quel 00079 else 00080 return filename.substr(0, ext); // n'inclus pas le point 00081 } 00082 00083 //! verifie que le fichier est bien du type represente par 'suffix'. 00084 static 00085 bool isType( const std::string& filename, const std::string& suffix ) 00086 { 00087 size_t ext= filename.find_last_of('.'); 00088 if(ext != std::string::npos) 00089 return (filename.substr(ext, std::string::npos).rfind(suffix) != std::string::npos); 00090 else 00091 return (filename.rfind(suffix) != std::string::npos); 00092 } 00093 00094 //! change l'extension du fichier. 00095 static 00096 std::string changeType( const std::string& filename, const std::string& suffix ) 00097 { 00098 size_t ext= filename.find_last_of('.'); 00099 if(ext == std::string::npos) 00100 return filename + suffix; 00101 else 00102 return filename.substr(0, ext) + suffix; 00103 } 00104 00105 //! verifie l'existance d'un fichier. 00106 static 00107 int exists( const std::string& filename ) 00108 { 00109 #ifndef WIN32 00110 struct stat info; 00111 if(stat(filename.c_str(), &info) < 0) 00112 return -1; 00113 00114 // verifie aussi que c'est bien un fichier standard 00115 return (S_ISREG(info.st_mode)) ? 0 : -1; 00116 00117 #else 00118 //! \todo a modifier pour compiler avec codeblock/mingw sous windows, utiliser la version linux. 00119 struct _stat64 info; 00120 if(_stat64(filename.c_str(), &info) < 0) 00121 return -1; 00122 00123 // verifie aussi que c'est bien un fichier standard 00124 return ((info.st_mode & _S_IFREG) == _S_IFREG); 00125 #endif 00126 } 00127 00128 //! renvoie les informations taille et date sur un fichier. 00129 //! \param size peut etre NULL si l'information n'est pas souhaitee. 00130 //! \param time peut etre NULL si l'information n'est pas souhaitee. 00131 static 00132 int infos( const std::string& filename, size_t *size, size_t *time ) 00133 { 00134 #ifndef WIN32 00135 struct stat info; 00136 if(stat(filename.c_str(), &info) < 0) 00137 return -1; 00138 00139 if(size != NULL) 00140 *size= info.st_size; 00141 if(time != NULL) 00142 *time= info.st_mtime; 00143 return 0; 00144 00145 #else 00146 //! \todo a modifier pour compiler avec codeblock/mingw sous windows, utiliser la version linux. 00147 struct _stat64 info; 00148 if(_stat64(filename.c_str(), &info) < 0) 00149 return -1; 00150 00151 if(size != NULL) 00152 *size= info.st_size; 00153 if(time != NULL) 00154 *time= info.st_mtime; 00155 return 0; 00156 #endif 00157 } 00158 00159 //! renvoie les informations sur un fichier. 00160 //! renvoie -1 en cas d'erreur (le fichier n'existe pas, par exemple). 00161 static 00162 int infos( const std::string& filename, IOInfo& info ) 00163 { 00164 return infos(filename, &info.size, &info.time); 00165 } 00166 00167 //! renvoie 1 si le fichier a ete modifie depuis qu'il a ete lu, 0 sinon, et -1 en cas d'erreur. 00168 static 00169 int modified( const std::string& filename, const IOInfo& last_info ) 00170 { 00171 IOInfo info; 00172 if(infos(filename, info) < 0) 00173 return -1; 00174 00175 return (last_info != info) ? 1 : 0; 00176 } 00177 }; 00178 00179 } 00180 00181 #endif