D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/files.h

00001 /* LICENSE_BEGIN
00002 SutoiTools library supply support for debugging, testing and 
00003 setting good bases for you application. It is part of the 
00004 SugoiEngine project.
00005 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00006 
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 LICENSE_END
00022 */
00023 
00024 
00025 /* \file SG_TLS_files.h
00026  * \brief Supply two basic class used by the CL_template_manager.
00027  * \author Yannick Allusse
00028 */
00029 #ifndef SG_TLS_FILES_H
00030 #define SG_TLS_FILES_H
00031 
00032 #include "SG_TLS_config.h"
00033 #include "SG_TLS_cl_base_dbl_obj.h"
00034 #include <sys/stat.h>
00035 #include <time.h>
00036 
00037 #if SG_TLS_MEMORY_MANAGER
00038           #include "SG_TLS_debug_new.h"
00039 #endif 
00040 
00041 namespace SGE{
00042 
00048 template <typename IDType2=int>
00049 class _SG_TLS_EXPORT CL_BASE_OBJ_FILE : 
00050           public virtual CL_BASE_DBL_OBJ<std::string, IDType2>
00051 {
00052  public:
00057           _SG_TLS_INLINE 
00058                     CL_BASE_OBJ_FILE(const std::string & _Name)
00059                     : CL_BASE_DBL_OBJ<std::string, IDType2>(_Name)
00060           {
00061                     //_Type = TMANAGER_FILE;
00062                     Loaded = false;
00063                     ParseFilePath(_Name, (std::string*)&(CL_BASE_OBJ<std::string>::ID), &Path);
00064                     ParseFileType((std::string)CL_BASE_OBJ<std::string>::ID, (std::string*)&(CL_BASE_OBJ<std::string>::ID), &Extension);
00065 
00066                     #if _SG_TLS_FILE_TEST
00067                               FileAttrib = NULL;
00068                     #endif
00069           }
00070 
00074     _SG_TLS_INLINE 
00075                     ~CL_BASE_OBJ_FILE()
00076           {
00077                     if (Loaded)
00078                               UnLoad();
00079           }
00080  
00086     _SG_TLS_INLINE 
00087                     bool 
00088                     IsLoaded()const
00089                     {         return Loaded;      }
00090 
00095     _SG_TLS_INLINE virtual 
00096                     int Load()
00097                     {         
00098                     #if _SG_TLS_FILE_TEST
00099                               //get file attribs
00100                               FileAttrib = new struct stat;
00101                               stat(GetFullName().data(), FileAttrib);
00102                               //==================================
00103                     #endif
00104 
00105                               Loaded = true;      
00106                               return 1;
00107                     }
00108 
00109     _SG_TLS_INLINE virtual
00110                     int UnLoad()
00111                     {
00112                               Loaded = false;     
00113                               return 1;
00114                     }
00119     _SG_TLS_INLINE 
00120                     const std::string & GetPath()const
00121                     {return Path;}
00122 
00127           _SG_TLS_INLINE 
00128                     const std::string & GetExtension()const
00129                     {return Extension;}
00130 
00135           _SG_TLS_INLINE 
00136                     const std::string GetFullName()const
00137                     {return Path+ CL_BASE_OBJ<std::string>::ID+ Extension;}
00138 
00139 #if _SG_TLS_FILE_TEST
00140 
00145           _SG_TLS_INLINE bool Exists()const
00146                     {
00147                               return FileExists(GetFullName());
00148                     }
00149 
00155           static bool FileExists(const std::string & _Name)
00156                     {
00157                               std::ifstream File(_Name.c_str());
00158                               if( File.is_open())
00159                               {
00160                                         File.close();
00161                                         return true;
00162                               }
00163 
00164                               return false;
00165                     }
00166 
00167 
00168 #if 0
00169           static bool FileCopy(const std::string & _Src, const std::string & _Dst)
00170                     {
00171                               Sys File::Copy("myfile.txt", "copyofmyfile.txt");  
00172                     }
00173           
00174           static bool FileMove(const std::string & _Src, const std::string & _Dst)
00175                     {
00176                     }
00177           static bool FileDelete(const std::string & _Src)
00178                     {
00179                     }
00180 #endif
00181 
00182           
00188           _SG_TLS_INLINE bool IsFileModified()
00189                     {
00190                               if (!Loaded)
00191                               {
00192                                         SG_NOTICE_LOG("File Not Loaded");
00193                                         return false;
00194                               }
00195 
00196                               struct stat NewFileAttrib;
00197                               stat(GetFullName().data(), &NewFileAttrib);// get the attributes of afile.txt
00198 
00199                               struct tm* OrigFileClock                = gmtime(&(FileAttrib->st_mtime));
00200                               struct tm* CurFileClock                           = gmtime(&(NewFileAttrib.st_mtime));// Get the last modified time and put it into the time structure
00201                               
00202 
00203                               if (CurFileClock->tm_sec =! OrigFileClock->tm_sec) /* seconds after the minute - [0,59] */
00204                                         return true;
00205                               if (CurFileClock->tm_min =! OrigFileClock->tm_min)/* minutes after the hour - [0,59] */
00206                                         return true;
00207                               if (CurFileClock->tm_hour =! OrigFileClock->tm_hour) /* hours since midnight - [0,23] */
00208                                         return true;
00209                     #if 0//???see later...sec, min and hours should be far enought to test in our case...     
00210                               if (CurFileClock->tm_mday =! OrigFileClock->tm_mday)/* day of the month - [1,31] */
00211                                         return true;
00212                               if (CurFileClock->tm_mon =! OrigFileClock->tm_mon)/* months since January - [0,11] */
00213                                         return true;
00214                               if (CurFileClock->tm_year =! OrigFileClock->tm_year)/* years since 1900 */
00215                                         return true;
00216                               if (CurFileClock->tm_wday =! OrigFileClock->tm_wday)/* days since Sunday - [0,6] */
00217                                         return true;
00218                               if (CurFileClock->tm_yday =! OrigFileClock->tm_yday)/* days since January 1 - [0,365] */
00219                                         return true;
00220                               if (CurFileClock->tm_isdst =! OrigFileClock->tm_isdst) /* daylight savings time flag */
00221                                         return true;
00222                     #endif
00223                               return false;
00224                     }
00225 
00226 protected:
00227           struct stat * FileAttrib;               //<! Create a file attribute structure, store date size and other...
00228 #endif
00229 
00230 protected:
00234           _SG_TLS_INLINE                
00235                     void      ParseFileName()
00236                     {
00237                               ParseFilePath(ID, &Path, &ID);
00238                               ParseFileType(ID, &ID, &Extension);
00239                     }
00240 
00241           std::string                             Path;               
00242           std::string                             Extension;  
00243 
00244 private:
00245           bool                                    Loaded; 
00246 };
00247 
00248 };//namespace
00249 #if SG_TLS_MEMORY_MANAGER
00250           #include "SG_TLS_debug_new_off.h"//Must be the last....
00251 #endif 
00252 
00253 #endif //SG_TLS_CL_BASE_OBJ_H

Generated on Mon Mar 19 23:15:11 2007 for SugoiTools by  doxygen 1.4.6-NO