Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RageUtil_FileDB.h

Go to the documentation of this file.
00001 #ifndef RAGE_UTIL_FILEDB 00002 #define RAGE_UTIL_FILEDB 1 00003 00004 #include <set> 00005 #include <map> 00006 #include "RageTimer.h" 00007 #include "RageThreads.h" 00008 #include "RageFileManager.h" 00009 00010 struct FileSet; 00011 struct File 00012 { 00013 CString name; 00014 CString lname; 00015 00016 void SetName( const CString &fn ) 00017 { 00018 name = fn; 00019 lname = name; 00020 lname.MakeLower(); 00021 } 00022 00023 bool dir; 00024 int size; 00025 /* Modification time of the file. The contents of this is undefined, except that 00026 * when the file has been modified, this value will change. */ 00027 int hash; 00028 00029 /* Private data, for RageFileDrivers. */ 00030 void *priv; 00031 00032 /* If this is non-NULL, and dir is true, this is a pointer to the FileSet containing 00033 * the directory contents. (This is a cache; it isn't always set.) */ 00034 const FileSet *dirp; 00035 00036 File() { dir=false; dirp=NULL; size=-1; hash=-1; priv=NULL;} 00037 File( const CString &fn ) 00038 { 00039 SetName( fn ); 00040 dir=false; size=-1; hash=-1; priv=NULL; 00041 } 00042 00043 bool operator== (const File &rhs) const { return lname==rhs.lname; } 00044 bool operator< (const File &rhs) const { return lname<rhs.lname; } 00045 00046 bool equal(const File &rhs) const { return lname == rhs.lname; } 00047 bool equal(const CString &rhs) const 00048 { 00049 CString l = rhs; 00050 l.MakeLower(); 00051 return lname == l; 00052 } 00053 }; 00054 00055 /* This represents a directory. */ 00056 struct FileSet 00057 { 00058 set<File> files; 00059 RageTimer age; 00060 00061 /* 00062 * If m_bFilled is false, this FileSet hasn't completed being filled in yet; it's 00063 * owned by the thread filling it in. Wait on FilenameDB::m_Mutex and retry until 00064 * it becomes true. 00065 */ 00066 bool m_bFilled; 00067 00068 FileSet() { m_bFilled = true; } 00069 00070 void GetFilesMatching( 00071 const CString &beginning, const CString &containing, const CString &ending, 00072 vector<CString> &out, bool bOnlyDirs) const; 00073 void GetFilesEqualTo(const CString &pat, vector<CString> &out, bool bOnlyDirs) const; 00074 00075 RageFileManager::FileType GetFileType( const CString &path ) const; 00076 int GetFileSize(const CString &path) const; 00077 int GetFileHash(const CString &path) const; 00078 }; 00079 00080 class FilenameDB 00081 { 00082 protected: 00083 RageEvent m_Mutex; 00084 00085 FileSet *GetFileSet( CString dir, bool create=true ); 00086 00087 /* Directories we have cached: */ 00088 map<CString, FileSet *> dirs; 00089 00090 int ExpireSeconds; 00091 00092 void GetFilesEqualTo(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs); 00093 void GetFilesMatching(const CString &dir, 00094 const CString &beginning, const CString &containing, const CString &ending, 00095 vector<CString> &out, bool bOnlyDirs); 00096 void DelFileSet( map<CString, FileSet *>::iterator dir ); 00097 00098 /* The given path wasn't cached. Cache it. */ 00099 virtual void PopulateFileSet( FileSet &fs, const CString &sPath ) { } 00100 00101 public: 00102 FilenameDB::FilenameDB(): 00103 m_Mutex("FilenameDB"), ExpireSeconds( -1 ) { } 00104 virtual FilenameDB::~FilenameDB() { FlushDirCache(); } 00105 00106 void AddFile( const CString &sPath, int size, int hash, void *priv=NULL ); 00107 void DelFile( const CString &sPath ); 00108 const File *GetFile( const CString &path ); 00109 const void *GetFilePriv( const CString &path ); 00110 00111 /* This handles at most two * wildcards. If we need anything more complicated, 00112 * we'll need to use fnmatch or regex. */ 00113 void GetFilesSimpleMatch(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs); 00114 00115 /* Search for "path" case-insensitively and replace it with the correct 00116 * case. If only a portion of the path exists, resolve as much as possible. 00117 * Return true if the entire path was matched. */ 00118 bool ResolvePath( CString &path ); 00119 00120 RageFileManager::FileType GetFileType( const CString &path ); 00121 int GetFileSize(const CString &path); 00122 int GetFileHash( const CString &sFilePath ); 00123 void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); 00124 00125 void FlushDirCache(); 00126 00127 void GetFileSetCopy( CString dir, FileSet &out ); 00128 }; 00129 00130 /* This FilenameDB must be populated in advance. */ 00131 class NullFilenameDB: public FilenameDB 00132 { 00133 public: 00134 NullFilenameDB() { ExpireSeconds = -1; } 00135 00136 protected: 00137 void PopulateFileSet( FileSet &fs, const CString &sPath ) { } 00138 }; 00139 00140 #endif 00141 00142 /* 00143 * Copyright (c) 2003-2004 Glenn Maynard 00144 * All rights reserved. 00145 * 00146 * Permission is hereby granted, free of charge, to any person obtaining a 00147 * copy of this software and associated documentation files (the 00148 * "Software"), to deal in the Software without restriction, including 00149 * without limitation the rights to use, copy, modify, merge, publish, 00150 * distribute, and/or sell copies of the Software, and to permit persons to 00151 * whom the Software is furnished to do so, provided that the above 00152 * copyright notice(s) and this permission notice appear in all copies of 00153 * the Software and that both the above copyright notice(s) and this 00154 * permission notice appear in supporting documentation. 00155 * 00156 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00157 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00158 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00159 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00160 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00161 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00162 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00163 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00164 * PERFORMANCE OF THIS SOFTWARE. 00165 */

Generated on Thu Jan 27 20:57:30 2005 for StepMania by doxygen 1.3.7