path.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000, 2001 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_PATH_H
00025 #define NL_PATH_H
00026 
00027 #include "types_nl.h"
00028 #include "time_nl.h"
00029 
00030 #include <map>
00031 #include <string>
00032 #include <vector>
00033 
00034 #include "common.h"
00035 #include "string_mapper.h"
00036 
00037 namespace NLMISC {
00038 
00040 struct EPathNotFound : public Exception
00041 {
00042     EPathNotFound (const std::string& filename) : Exception ("Path not found for " + filename) { }
00043 };
00044 
00051 class CFileContainer
00052 {
00053     // no copy allowed
00054     CFileContainer(const CFileContainer &/* other */)
00055     {}
00056 
00057     CFileContainer &operator =(const CFileContainer &/* other */)
00058     {
00059         return *this;
00060     }
00061 
00062 public:
00063     CFileContainer()
00064     {
00065         _MemoryCompressed = false;
00066         _AllFileNames = NULL;
00067     }
00068 
00069     ~CFileContainer();
00070 
00071 
00072     void            addSearchPath (const std::string &path, bool recurse, bool alternative, class IProgressCallback *progressCallBack = NULL);
00073 
00075     void            addSearchPath (const std::string &path) { addSearchPath (path, false, true, NULL); }
00076 
00078     void            addSearchFile (const std::string &file, bool remap = false, const std::string &virtual_ext = "", class NLMISC::IProgressCallback *progressCallBack = NULL);
00079 
00081     void            addSearchListFile (const std::string &filename, bool recurse, bool alternative);
00082 
00084     void            addSearchBigFile (const std::string &filename, bool recurse, bool alternative, class NLMISC::IProgressCallback *progressCallBack = NULL);
00085 
00087     void            addSearchXmlpackFile (const std::string &sXmlpackFilename, bool recurse, bool alternative, class NLMISC::IProgressCallback *progressCallBack = NULL);
00088 
00090     void            removeAllAlternativeSearchPath ();
00091 
00092     // Remove a set of big file from the search paths (and also from CBigFile)
00093     void            removeBigFiles(const std::vector<std::string> &bnpFilenames);
00094 
00116     std::string lookup (const std::string &filename, bool throwException = true, bool displayWarning = true, bool lookupInLocalDirectory = true);
00117 
00127     bool            exists (const std::string &filename);
00128 
00129 
00132     void clearMap ();
00133 
00143     void remapExtension (const std::string &ext1, const std::string &ext2, bool substitute);
00144 
00149     void remapFile (const std::string &file1, const std::string &file2);
00150 
00155     void loadRemappedFiles (const std::string &file);
00156 
00157     void display ();
00158 
00162     std::string standardizePath (const std::string &path, bool addFinalSlash = true);
00163 
00169     std::string standardizeDosPath (const std::string &path);
00170 
00171 
00181     void            getPathContent (const std::string &path, bool recurse, bool wantDir, bool wantFile, std::vector<std::string> &result, class IProgressCallback *progressCallBack = NULL, bool showEverything=false);
00182 
00193     std::string getFullPath (const std::string &path, bool addFinalSlash = true);
00194 
00197     std::string getCurrentPath ();
00198 
00201     bool setCurrentPath (const std::string &path);
00202 
00205     void getFileList(const std::string &extension, std::vector<std::string> &filenames);
00206 
00209     void getFileListByName(const std::string &extension, const std::string &name, std::vector<std::string> &filenames);
00210 
00216     bool makePathRelative (const char *basePath, std::string &relativePath);
00217 
00220     void addIgnoredDoubleFile(const std::string &ignoredFile);
00221 
00224     void memoryCompress();
00225 
00226     void memoryUncompress();
00227 
00228     bool isMemoryCompressed()   { return _MemoryCompressed; }
00229 
00232     std::string getWindowsDirectory();
00233 
00234 private:
00235 
00236     // All path in this vector must have a terminated '/'
00237     std::vector<std::string> _AlternativePaths;
00238 
00239     std::vector<std::string> IgnoredFiles;
00240 
00241     std::map<std::string, std::string> _RemappedFiles;
00242 
00243     // ----------------------------------------------
00244     // MEMORY WISE
00245     // ----------------------------------------------
00246 
00247     bool _MemoryCompressed;
00248     CStaticStringMapper SSMext;
00249     CStaticStringMapper SSMpath;
00250 
00251     // If NOT memory compressed use this
00252     // ---------------------------------
00253 
00254     struct CFileEntry
00255     {
00256         std::string Name;       // Normal case
00257         uint32  idPath   : 16;
00258         uint32  idExt    : 15;
00259         uint32  Remapped : 1;
00260     };
00261 
00262     typedef std::map<std::string, CFileEntry>   TFiles;
00263     TFiles   _Files; // first is the filename in lowercase (can be with a remapped extension)
00264 
00265 
00266 
00267     // If memory compressed use this
00268     // -----------------------------
00269 
00270     struct CMCFileEntry
00271     {
00272         char *Name;             // Normal case (the search is done by using nlstricmp)
00273         uint32  idPath   : 16;  // Path (not with file at the end) - look in the SSMpath (65536 different path allowed)
00274         uint32  idExt    : 15;  // real extension of the file if remapped - look in the SSMext (32768 different extension allowed)
00275         uint32  Remapped : 1;   // true if the file is remapped
00276     };
00277 
00278     char *_AllFileNames;
00279 
00280     // first is the filename that can be with a remapped extension
00281     std::vector<CMCFileEntry> _MCFiles;
00282 
00283     // Compare a MCFileEntry with a lowered string (useful for MCfind)
00284     class CMCFileComp
00285     {
00286     public:
00287         sint specialCompare(const CMCFileEntry &fe, const char *rhs)
00288         {
00289             char *lhs = fe.Name;
00290 
00291             uint8 lchar, rchar;
00292             while (*lhs != '\0' && *rhs != '\0')
00293             {
00294                 // lower case compare because name is in normal case
00295                 lchar = uint8(::tolower(*lhs));
00296                 rchar = uint8(::tolower(*rhs));
00297                 if (lchar != rchar) return ((sint)lchar) - ((sint)rchar);
00298                 ++lhs;
00299                 ++rhs;
00300             }
00301             if (*lhs != 0) return 1;
00302             if (*rhs != 0) return -1;
00303             return 0;
00304         }
00305 
00306         bool operator()( const CMCFileEntry &fe, const CMCFileEntry &rhs )
00307         {
00308             return specialCompare( fe, rhs.Name ) < 0;
00309         }
00310     };
00311 
00313     std::vector<std::pair<std::string, std::string> > _Extensions;
00314 
00315     CMCFileEntry    *MCfind (const std::string &filename);
00316     sint            findExtension (const std::string &ext1, const std::string &ext2);
00317     void            insertFileInMap (const std::string &filename, const std::string &filepath, bool remap, const std::string &extension);
00318 };
00319 
00320 
00333 class CPath
00334 {
00335     NLMISC_SAFE_SINGLETON_DECL_PTR(CPath);
00336 public:
00354     static void         addSearchPath (const std::string &path, bool recurse, bool alternative, class IProgressCallback *progressCallBack = NULL);
00355 
00357     static void         addSearchPath (const std::string &path) { addSearchPath (path, false, true, NULL); }
00358 
00360     static void         addSearchFile (const std::string &file, bool remap = false, const std::string &virtual_ext = "", class NLMISC::IProgressCallback *progressCallBack = NULL);
00361 
00363     static void         addSearchListFile (const std::string &filename, bool recurse, bool alternative);
00364 
00366     static void         addSearchBigFile (const std::string &filename, bool recurse, bool alternative, class NLMISC::IProgressCallback *progressCallBack = NULL);
00367 
00369     static void         addSearchXmlpackFile (const std::string &sXmlpackFilename, bool recurse, bool alternative, class NLMISC::IProgressCallback *progressCallBack = NULL);
00370 
00372     static void         removeAllAlternativeSearchPath ();
00373 
00374     // Remove a set of big file from the search paths (and also from CBigFile)
00375     static void         removeBigFiles(const std::vector<std::string> &bnpFilenames);
00376 
00398     static std::string  lookup (const std::string &filename, bool throwException = true, bool displayWarning = true, bool lookupInLocalDirectory = true);
00399 
00409     static bool         exists (const std::string &filename);
00410 
00411 
00414     static void clearMap ();
00415 
00425     static void remapExtension (const std::string &ext1, const std::string &ext2, bool substitute);
00426 
00431     static void remapFile (const std::string &file1, const std::string &file2);
00432 
00437     static void loadRemappedFiles (const std::string &file);
00438 
00439     static void display ();
00440 
00444     static std::string  standardizePath (const std::string &path, bool addFinalSlash = true);
00445 
00451     static std::string  standardizeDosPath (const std::string &path);
00452 
00453 
00463     static void         getPathContent (const std::string &path, bool recurse, bool wantDir, bool wantFile, std::vector<std::string> &result, class IProgressCallback *progressCallBack = NULL, bool showEverything=false);
00464 
00475     static std::string getFullPath (const std::string &path, bool addFinalSlash = true);
00476 
00479     static std::string getCurrentPath ();
00480 
00483     static bool setCurrentPath (const std::string &path);
00484 
00487     static void getFileList(const std::string &extension, std::vector<std::string> &filenames);
00488 
00491     static void getFileListByName(const std::string &extension, const std::string &name, std::vector<std::string> &filenames);
00492 
00498     static bool makePathRelative (const char *basePath, std::string &relativePath);
00499 
00502     static void addIgnoredDoubleFile(const std::string &ignoredFile);
00503 
00506     static void memoryCompress();
00507 
00508     static void memoryUncompress();
00509 
00510     static bool isMemoryCompressed()    { return getInstance()->_FileContainer.isMemoryCompressed(); }
00511 
00514     static std::string getWindowsDirectory();
00515 
00516     // release singleton
00517     static void releaseInstance();
00518 
00519 private:
00520 
00521     CPath()
00522     {
00523     }
00524 
00526     CFileContainer      _FileContainer;
00527 };
00528 
00529 
00530 
00537 struct CFile
00538 {
00543     static std::string getFilename (const std::string &filename);
00544 
00549     static std::string getPath (const std::string &filename);
00550 
00555     static bool isDirectory (const std::string &filename);
00556 
00562     static bool fileExists (const std::string &filename);
00563 
00569     static bool isExists (const std::string& filename);
00570 
00576     static std::string findNewFile (const std::string &filename);
00577 
00582     static int getLastSeparator (const std::string &filename);
00583 
00584     static std::string getFilenameWithoutExtension (const std::string &filename);
00585     static std::string getExtension (const std::string &filename);
00586 
00592     static uint32   getFileSize (const std::string &filename);
00593 
00597     static uint32   getFileSize (FILE *f);
00598 
00605     static uint32   getFileModificationDate(const std::string &filename);
00606 
00614     static bool     setFileModificationDate(const std::string &filename, uint32 modTime);
00615 
00621     static uint32   getFileCreationDate(const std::string &filename);
00622 
00634     static void addFileChangeCallback (const std::string &filename, void (*)(const std::string &filename));
00635 
00639     static void removeFileChangeCallback (const std::string &filename);
00640 
00648     static void checkFileChange (TTime frequency = 1000);
00649 
00655     static bool copyFile(const char *dest, const char *src, bool failIfExists = false, class IProgressCallback *progress = NULL);
00656 
00660     static bool quickFileCompare(const std::string &fileName0, const std::string &fileName1);
00661 
00666     static bool thoroughFileCompare(const std::string &fileName0, const std::string &fileName1,uint32 maxBufSize=1024*1024*2);
00667 
00671     static bool moveFile(const char *dest, const char *src);
00672 
00676     static bool createDirectory(const std::string &dirname);
00677 
00681     static bool createDirectoryTree(const std::string &dirname);
00682 
00688     static bool setRWAccess(const std::string &filename);
00689 
00693     static bool deleteFile(const std::string &filename);
00694 
00698     static bool deleteDirectory(const std::string &filename);
00699 
00703     static void getTemporaryOutputFilename (const std::string &originalFilename, std::string &tempFilename);
00704 };
00705 
00706 } // NLMISC
00707 
00708 #endif // NL_PATH_H
00709 
00710 /* End of path.h */

Generated on Thu Jan 7 08:26:23 2010 for NeL by  doxygen 1.6.1