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 struct IOFileSystem
00025 {
00026
00027
00028
00029
00030
00031
00032 static
00033 std::string pathname( const std::string& filename )
00034 {
00035 size_t slash = filename.find_last_of( '/' );
00036 size_t bslash = filename.find_last_of( '\\' );
00037
00038 if ( slash == std::string::npos && bslash != std::string::npos )
00039 slash = bslash;
00040 else if ( slash != std::string::npos && bslash != std::string::npos && bslash > slash )
00041 slash = bslash;
00042
00043 if ( slash != std::string::npos )
00044 return filename.substr( 0, slash +1 );
00045 else
00046 return "./";
00047 }
00048
00049
00050 static
00051 std::string basename( const std::string& filename )
00052 {
00053 size_t ext= filename.find_last_of('.');
00054 if(ext== std::string::npos)
00055 return filename;
00056 else
00057 return filename.substr(0, ext);
00058 }
00059
00060
00061 static
00062 bool isType( const std::string& filename, const std::string& suffix )
00063 {
00064 size_t ext= filename.find_last_of('.');
00065 if(ext != std::string::npos)
00066 return (filename.substr(ext, std::string::npos).rfind(suffix) != std::string::npos);
00067 else
00068 return (filename.rfind(suffix) != std::string::npos);
00069 }
00070
00071
00072 static
00073 std::string changeType( const std::string& filename, const std::string& suffix )
00074 {
00075 size_t ext= filename.find_last_of('.');
00076 if(ext == std::string::npos)
00077 return filename + suffix;
00078 else
00079 return filename.substr(0, ext) + suffix;
00080 }
00081
00082
00083 static
00084 bool exists( const std::string& filename )
00085 {
00086 #ifndef WIN32
00087 struct stat info;
00088 if(stat(filename.c_str(), &info) < 0)
00089 return -1;
00090
00091
00092 return (S_ISREG(info.st_mode));
00093
00094 #else
00095 struct _stat64 info;
00096 if(_stat64(filename.c_str(), &info) < 0)
00097 return -1;
00098
00099
00100 return ((info.st_mode & _S_IFREG) == _S_IFREG);
00101 #endif
00102 }
00103
00104
00105 static
00106 int infos( const std::string& filename, size_t *size, size_t *time )
00107 {
00108 #ifndef WIN32
00109 struct stat info;
00110 if(stat(filename.c_str(), &info) < 0)
00111 return -1;
00112
00113 if(size != NULL)
00114 *size= info.st_size;
00115 if(time != NULL)
00116 *time= info.st_mtime;
00117 return 0;
00118
00119 #else
00120 struct _stat64 info;
00121 if(_stat64(filename.c_str(), &info) < 0)
00122 return -1;
00123
00124 if(size != NULL)
00125 *size= info.st_size;
00126 if(time != NULL)
00127 *time= info.st_mtime;
00128 return 0;
00129 #endif
00130 }
00131
00132
00133
00134 static
00135 int compare( const size_t a, const size_t b )
00136 {
00137 if(a < b)
00138 return -1;
00139 else if(a > b)
00140 return 1;
00141 else
00142 return 0;
00143 }
00144
00145
00146
00147 static
00148 int uptodate( const std::string& filea, const std::string& fileb )
00149 {
00150 size_t timea;
00151 if(infos(filea, NULL, &timea) < 0)
00152 return -1;
00153
00154 size_t timeb;
00155 if(infos(fileb, NULL, &timeb) < 0)
00156 return 0;
00157
00158 return (compare(timea, timeb) <= 0);
00159
00160 }
00161 };
00162
00163 }
00164
00165 #endif