Files
itgmania212121/stepmania/src/RageUtil_FileDB.h
T

151 lines
4.8 KiB
C++
Raw Normal View History

2003-07-03 02:41:27 +00:00
#ifndef RAGE_UTIL_FILEDB
#define RAGE_UTIL_FILEDB 1
2003-12-07 04:14:52 +00:00
#include <set>
#include <map>
#include "RageTimer.h"
2004-01-06 05:44:28 +00:00
#include "RageFileManager.h"
2003-12-07 04:14:52 +00:00
2003-12-15 05:36:27 +00:00
struct FileSet;
2003-12-07 04:14:52 +00:00
struct File
{
CString name;
CString lname;
void SetName( const CString &fn )
{
name = fn;
2004-01-06 05:44:28 +00:00
lname = name;
2003-12-07 04:14:52 +00:00
lname.MakeLower();
}
bool dir;
int size;
/* Modification time of the file. The contents of this is undefined, except that
* when the file has been modified, this value will change. */
2003-12-10 07:07:42 +00:00
int hash;
2003-12-07 04:14:52 +00:00
/* Private data, for RageFileDrivers. */
void *priv;
2003-12-15 05:36:27 +00:00
/* If this is non-NULL, and dir is true, this is a pointer to the FileSet containing
* the directory contents. (This is a cache; it isn't always set.) */
2004-08-30 04:49:57 +00:00
const FileSet *dirp;
2003-12-15 05:36:27 +00:00
File() { dir=false; dirp=NULL; size=-1; hash=-1; priv=NULL;}
2003-12-07 04:14:52 +00:00
File( const CString &fn )
{
SetName( fn );
2003-12-10 07:07:42 +00:00
dir=false; size=-1; hash=-1; priv=NULL;
2003-12-07 04:14:52 +00:00
}
bool operator== (const File &rhs) const { return lname==rhs.lname; }
bool operator< (const File &rhs) const { return lname<rhs.lname; }
bool equal(const File &rhs) const { return lname == rhs.lname; }
bool equal(const CString &rhs) const
{
CString l = rhs;
l.MakeLower();
return lname == l;
}
};
/* This represents a directory. */
struct FileSet
{
set<File> files;
RageTimer age;
void GetFilesMatching(
const CString &beginning, const CString &containing, const CString &ending,
vector<CString> &out, bool bOnlyDirs) const;
void GetFilesEqualTo(const CString &pat, vector<CString> &out, bool bOnlyDirs) const;
2004-01-06 05:44:28 +00:00
RageFileManager::FileType GetFileType( const CString &path ) const;
2003-12-07 04:14:52 +00:00
int GetFileSize(const CString &path) const;
2003-12-10 07:07:42 +00:00
int GetFileHash(const CString &path) const;
2003-12-07 04:14:52 +00:00
};
class FilenameDB
{
protected:
2003-12-16 07:22:43 +00:00
FileSet *GetFileSet( CString dir, bool create=true );
2003-12-07 04:14:52 +00:00
/* Directories we have cached: */
map<CString, FileSet *> dirs;
int ExpireSeconds;
void GetFilesEqualTo(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
void GetFilesMatching(const CString &dir,
const CString &beginning, const CString &containing, const CString &ending,
vector<CString> &out, bool bOnlyDirs);
2003-12-08 00:04:47 +00:00
void AddFileSet( CString sPath, FileSet *fs );
2003-12-07 04:14:52 +00:00
2003-12-08 00:04:47 +00:00
/* The given path wasn't cached. Cache it. */
virtual void PopulateFileSet( FileSet &fs, const CString &sPath ) { }
2003-12-07 04:14:52 +00:00
public:
FilenameDB::FilenameDB():
ExpireSeconds( -1 ) { }
2003-12-07 07:00:36 +00:00
virtual FilenameDB::~FilenameDB() { FlushDirCache(); }
2003-12-07 04:14:52 +00:00
2003-12-10 07:07:42 +00:00
void AddFile( const CString &sPath, int size, int hash, void *priv=NULL );
2003-12-16 07:22:43 +00:00
void DelFile( const CString &sPath );
2004-08-30 04:49:57 +00:00
const File *GetFile( const CString &path );
2004-09-05 03:40:19 +00:00
const void *GetFilePriv( const CString &path );
2003-12-07 06:14:27 +00:00
2003-12-07 04:14:52 +00:00
/* This handles at most two * wildcards. If we need anything more complicated,
* we'll need to use fnmatch or regex. */
void GetFilesSimpleMatch(const CString &dir, const CString &fn, vector<CString> &out, bool bOnlyDirs);
/* Search for "path" case-insensitively and replace it with the correct
2004-02-27 06:24:26 +00:00
* case. If only a portion of the path exists, resolve as much as possible.
* Return true if the entire path was matched. */
bool ResolvePath( CString &path );
2003-12-07 04:14:52 +00:00
2004-01-06 05:44:28 +00:00
RageFileManager::FileType GetFileType( const CString &path );
2003-12-07 04:14:52 +00:00
int GetFileSize(const CString &path);
2003-12-10 07:07:42 +00:00
int GetFileHash( const CString &sFilePath );
2003-12-07 04:14:52 +00:00
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
void FlushDirCache();
};
2003-07-03 02:41:27 +00:00
2004-09-05 03:34:18 +00:00
/* This FilenameDB must be populated in advance. */
class NullFilenameDB: public FilenameDB
{
public:
NullFilenameDB() { ExpireSeconds = -1; }
protected:
void PopulateFileSet( FileSet &fs, const CString &sPath ) { }
};
2003-07-03 02:41:27 +00:00
#endif
2004-05-06 02:40:33 +00:00
/*
* Copyright (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/