gKitGL
IOFileManager.h
00001 
00002 #ifndef _IOFILE_MANAGER_H
00003 #define _IOFILE_MANAGER_H
00004 
00005 #include <string>
00006 #include <vector>
00007 #include <map>
00008 
00009 #include "IOFile.h"
00010 
00011 
00012 namespace gk {
00013     
00014 //! representation de l'ensemble de fichiers ouverts par l'application.
00015 class IOFileManager
00016 {
00017     // non copyable
00018     IOFileManager( const IOFileManager& );
00019     IOFileManager& operator=( const IOFileManager& );
00020     
00021 protected:
00022     typedef std::map<std::string, unsigned int> files_map_type;
00023     files_map_type m_files_map;
00024     std::vector<IOFile *> m_files;
00025 
00026     IOFileManager( )
00027         :
00028         m_files()
00029     {}
00030 
00031     virtual ~IOFileManager( ) 
00032     {
00033         const int n= m_files.size();
00034         for(int i= 0; i < n; i++)
00035             delete m_files[i];
00036     }
00037 
00038 public:
00039     //! renvoie un description du fichier 'filename'.
00040     IOFileInfo file( const std::string& filename )
00041     {
00042         std::pair<files_map_type::iterator, bool>
00043             found= m_files_map.insert( std::make_pair(filename, m_files.size()) );
00044         if(found.second == false)
00045             return IOFileInfo(m_files[found.first->second]);
00046         
00047         // insertion du nouveau fichier
00048         IOInfo info;
00049         if(IOFileSystem::infos(filename, info) < 0)
00050             info= IOInfo();
00051         
00052     #ifdef VERBOSE_DEBUG
00053         printf("IOFileManager::insert('%s') %d: length %lu, time %lu\n",
00054             filename.c_str(), (int) m_files.size(), info.size, info.time);
00055     #endif
00056         
00057         m_files.push_back( new IOFile(filename, info) );
00058         return IOFileInfo(m_files.back());
00059     }
00060     
00061     //! renvoie 1 si le fichier a ete modifie.
00062     int modified( const IOFileInfo& b )
00063     {
00064         if(b.file == NULL)
00065             return -1;
00066         // compare la date des infos a celles du fichier
00067         return (b.info != b.file->info) ? 1 : 0;
00068     }
00069     
00070     //! met a jour le descripteur.
00071     int update( IOFileInfo& b )
00072     {
00073         if(b.file == NULL)
00074             return -1;
00075         // renvoie les nouvelles infos
00076         b= IOFileInfo(b.file);
00077         return 0;
00078     }
00079 
00080     //! met a jour les descripteurs de l'ensemble de fichiers ouverts par l'application.
00081     int reload( )
00082     {
00083     #ifdef VERBOSE
00084         printf("scanning files...\n");
00085     #endif
00086         const int n= m_files.size();
00087         for(int i= 0; i < n; i++)
00088         {
00089             if(m_files[i] == NULL || m_files[i]->filename.empty())
00090                 continue;
00091             
00092             IOInfo info;
00093             if(IOFileSystem::infos(m_files[i]->filename, info) < 0)
00094                 // erreur ?
00095                 continue;
00096             
00097             // ne pas modifier les infos en cas d'erreur
00098         #ifdef VERBOSE  //_DEBUG
00099             printf("  %s: ", m_files[i]->filename.c_str());
00100             if(info != m_files[i]->info)
00101                 printf("modified.\n");
00102         #endif
00103             
00104         #if VERBOSE_DEBUG
00105             else
00106                 printf("no modifications.\n");
00107         #endif
00108             
00109             m_files[i]->info= info;
00110         }
00111         
00112     #ifdef VERBOSE
00113         printf("done.\n");
00114     #endif
00115         return 0;
00116     }
00117     
00118     static 
00119     IOFileManager& manager( )   //!< singleton
00120     {
00121         static IOFileManager manager;
00122         return manager;
00123     }
00124 };
00125 
00126 }       // namespace gk
00127 
00128 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerator Friends