From b6e5a87a00a9a27108920769378a8ca49b5b96d1 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 3 Jun 2004 05:15:08 +0000 Subject: [PATCH] path normalization optimziation (don't redo it per-driver) --- stepmania/src/RageFileManager.cpp | 50 ++++++++++++++++++++----------- stepmania/src/RageFileManager.h | 16 +++++----- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/stepmania/src/RageFileManager.cpp b/stepmania/src/RageFileManager.cpp index fb336c3511..8c74c42db9 100644 --- a/stepmania/src/RageFileManager.cpp +++ b/stepmania/src/RageFileManager.cpp @@ -34,7 +34,7 @@ struct LoadedDriver CString Type, Root, MountPoint; LoadedDriver() { driver = NULL; } - CString GetPath( CString path ); + CString GetPath( const CString &path ); }; static vector g_Drivers; @@ -210,15 +210,9 @@ RageFileManager::~RageFileManager() g_Mutex = NULL; } -CString LoadedDriver::GetPath( CString path ) +/* path must be normalized (FixSlashesInPlace, CollapsePath). */ +CString LoadedDriver::GetPath( const CString &path ) { - /* Map all slashes to forward slash. */ - FixSlashesInPlace( path ); - - /* Collapse the path, removing any leading dots. (FilenameDB::ResolvePath requires - * this.) */ - CollapsePath( path, true ); - /* Default mountpoints: */ if( MountPoint.size() == 0 ) { @@ -234,11 +228,19 @@ CString LoadedDriver::GetPath( CString path ) return path.Right( path.size() - MountPoint.size() ); } +static void NormalizePath( CString &sPath ) +{ + FixSlashesInPlace( sPath ); + CollapsePath( sPath ); +} + bool ilt( const CString &a, const CString &b ) { return a.CompareNoCase(b) < 0; } bool ieq( const CString &a, const CString &b ) { return a.CompareNoCase(b) == 0; } -void RageFileManager::GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ) +void RageFileManager::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ) { LockMut( *g_Mutex ); + + NormalizePath( sPath ); for( unsigned i = 0; i < g_Drivers.size(); ++i ) { @@ -264,10 +266,12 @@ void RageFileManager::GetDirListing( const CString &sPath, CStringArray &AddTo, AddTo.erase(it, AddTo.end()); } -bool RageFileManager::Remove( const CString &sPath ) +bool RageFileManager::Remove( CString sPath ) { LockMut( *g_Mutex ); + NormalizePath( sPath ); + /* Multiple drivers may have the same file. */ bool Deleted = false; for( unsigned i = 0; i < g_Drivers.size(); ++i ) @@ -392,10 +396,12 @@ void RageFileManager::GetLoadedDrivers( vector &Mounts ) } } -void RageFileManager::FlushDirCache( const CString &sPath ) +void RageFileManager::FlushDirCache( CString sPath ) { LockMut( *g_Mutex ); + NormalizePath( sPath ); + for( unsigned i = 0; i < g_Drivers.size(); ++i ) { if( sPath.size() == 0 ) @@ -412,10 +418,12 @@ void RageFileManager::FlushDirCache( const CString &sPath ) } } -RageFileManager::FileType RageFileManager::GetFileType( const CString &sPath ) +RageFileManager::FileType RageFileManager::GetFileType( CString sPath ) { LockMut( *g_Mutex ); + NormalizePath( sPath ); + for( unsigned i = 0; i < g_Drivers.size(); ++i ) { const CString p = g_Drivers[i].GetPath( sPath ); @@ -430,10 +438,12 @@ RageFileManager::FileType RageFileManager::GetFileType( const CString &sPath ) } -int RageFileManager::GetFileSizeInBytes( const CString &sPath ) +int RageFileManager::GetFileSizeInBytes( CString sPath ) { LockMut( *g_Mutex ); + NormalizePath( sPath ); + for( unsigned i = 0; i < g_Drivers.size(); ++i ) { const CString p = g_Drivers[i].GetPath( sPath ); @@ -447,10 +457,12 @@ int RageFileManager::GetFileSizeInBytes( const CString &sPath ) return -1; } -int RageFileManager::GetFileHash( const CString &sPath ) +int RageFileManager::GetFileHash( CString sPath ) { LockMut( *g_Mutex ); + NormalizePath( sPath ); + for( unsigned i = 0; i < g_Drivers.size(); ++i ) { const CString p = g_Drivers[i].GetPath( sPath ); @@ -515,7 +527,7 @@ static bool PathUsesSlowFlush( const CString &sPath ) } /* Used only by RageFile: */ -RageFileObj *RageFileManager::Open( const CString &sPath, int mode, RageFile &p, int &err ) +RageFileObj *RageFileManager::Open( CString sPath, int mode, RageFile &p, int &err ) { LockMut( *g_Mutex ); @@ -529,6 +541,8 @@ RageFileObj *RageFileManager::Open( const CString &sPath, int mode, RageFile &p, if( mode & RageFile::WRITE ) return OpenForWriting( sPath, mode, p, err ); + NormalizePath( sPath ); + for( unsigned i = 0; i < g_Drivers.size(); ++i ) { LoadedDriver &ld = g_Drivers[i]; @@ -568,7 +582,7 @@ RageFileObj *RageFileManager::CopyFileObj( const RageFileObj *cpy, RageFile &p ) return ret; } -RageFileObj *RageFileManager::OpenForWriting( const CString &sPath, int mode, RageFile &p, int &err ) +RageFileObj *RageFileManager::OpenForWriting( CString sPath, int mode, RageFile &p, int &err ) { LockMut( *g_Mutex ); @@ -591,6 +605,8 @@ RageFileObj *RageFileManager::OpenForWriting( const CString &sPath, int mode, Ra * If the given path can not be created, return -1. This happens if a path * that needs to be a directory is a file, or vice versa. */ + NormalizePath( sPath ); + vector< pair > Values; unsigned i; for( i = 0; i < g_Drivers.size(); ++i ) diff --git a/stepmania/src/RageFileManager.h b/stepmania/src/RageFileManager.h index c816f3da51..7033fda4de 100644 --- a/stepmania/src/RageFileManager.h +++ b/stepmania/src/RageFileManager.h @@ -18,18 +18,18 @@ public: ~RageFileManager(); void MountInitialFilesystems(); - void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); - bool Remove( const CString &sPath ); + void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); + bool Remove( CString sPath ); enum FileType { TYPE_FILE, TYPE_DIR, TYPE_NONE }; - FileType GetFileType( const CString &sPath ); + FileType GetFileType( CString sPath ); bool IsAFile( const CString &sPath ); bool IsADirectory( const CString &sPath ); bool DoesFileExist( const CString &sPath ); - int GetFileSizeInBytes( const CString &sPath ); - int GetFileHash( const CString &sPath ); + int GetFileSizeInBytes( CString sPath ); + int GetFileHash( CString sPath ); void Mount( CString Type, CString RealPath, CString MountPoint ); void Unmount( CString Type, CString Root, CString MountPoint ); @@ -41,15 +41,15 @@ public: }; void GetLoadedDrivers( vector &Mounts ); - void FlushDirCache( const CString &sPath ); + void FlushDirCache( CString sPath ); /* Used only by RageFile: */ - RageFileObj *Open( const CString &sPath, int mode, RageFile &p, int &err ); + RageFileObj *Open( CString sPath, int mode, RageFile &p, int &err ); void Close( RageFileObj *obj ); RageFileObj *CopyFileObj( const RageFileObj *cpy, RageFile &p ); private: - RageFileObj *OpenForWriting( const CString &sPath, int mode, RageFile &p, int &err ); + RageFileObj *OpenForWriting( CString sPath, int mode, RageFile &p, int &err ); }; extern RageFileManager *FILEMAN;