From 5f6fedfcda9f767c5202674ec6203a3c2a490696 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 7 Dec 2003 22:54:50 +0000 Subject: [PATCH] move DirectoryIsEmpty to RageUtil cleanup add join( Delimitor, iterator, iterator ) --- stepmania/src/AnnouncerManager.cpp | 14 ---------- stepmania/src/RageUtil.cpp | 42 +++++++++++++++++++++++++----- stepmania/src/RageUtil.h | 27 +++++++++---------- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/stepmania/src/AnnouncerManager.cpp b/stepmania/src/AnnouncerManager.cpp index 7e158cfdd5..3546336734 100644 --- a/stepmania/src/AnnouncerManager.cpp +++ b/stepmania/src/AnnouncerManager.cpp @@ -22,20 +22,6 @@ AnnouncerManager* ANNOUNCER = NULL; // global object accessable from anywhere in const CString EMPTY_ANNOUNCER_NAME = "Empty"; const CString ANNOUNCERS_DIR = BASE_PATH "Announcers" SLASH; -/* XXX: move to RageUtil when I feel like spending 20 minutes recompiling */ -/* Return true if "dir" is empty or does not exist. */ -static bool DirectoryIsEmpty( CString dir ) -{ - if(dir == "") - return true; - if(!DoesFileExist(dir)) - return true; - - CStringArray asFileNames; - GetDirListing( dir, asFileNames ); - return asFileNames.empty(); -} - AnnouncerManager::AnnouncerManager() { LOG->Trace("AnnouncerManager::AnnouncerManager()"); diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 6403b7280b..c69a8b5b73 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -95,7 +95,7 @@ bool IsHexVal( const CString &s ) return true; } -float TimeToSeconds( CString sHMS ) +float TimeToSeconds( const CString &sHMS ) { CStringArray arrayBits; split( sHMS, ":", arrayBits, false ); @@ -206,6 +206,23 @@ CString join( const CString &Deliminator, const CStringArray& Source) return csTmp; } +CString join( const CString &Delimitor, CStringArray::const_iterator begin, CStringArray::const_iterator end ) +{ + if( begin == end ) + return ""; + + CString ret; + while( begin != end ) + { + ret += *begin; + ++begin; + if( begin != end ) + ret += Delimitor; + } + + return ret; +} + template void do_split( const S &Source, const S &Delimitor, vector &AddIt, const bool bIgnoreEmpty ) @@ -285,7 +302,7 @@ CString SetExtension( const CString &path, const CString &ext ) return Dir + FName + (ext.size()? ".":"") + ext; } -CString GetExtension( CString sPath ) +CString GetExtension( const CString &sPath ) { unsigned pos = sPath.rfind( '.' ); if( pos == sPath.npos ) @@ -312,7 +329,7 @@ CString GetCwd() /* Reference: http://www.theorem.com/java/CRC32.java, rewritten by Glenn Maynard. * Public domain. */ -unsigned int GetHashForString ( CString s ) +unsigned int GetHashForString ( const CString &s ) { static unsigned tab[256]; static bool initted = false; @@ -338,7 +355,7 @@ unsigned int GetHashForString ( CString s ) return crc; } -unsigned int GetHashForFile( CString sPath ) +unsigned int GetHashForFile( const CString &sPath ) { unsigned int hash = 0; @@ -349,7 +366,7 @@ unsigned int GetHashForFile( CString sPath ) return hash; } -unsigned int GetHashForDirectory( CString sDir ) +unsigned int GetHashForDirectory( const CString &sDir ) { unsigned int hash = 0; @@ -366,6 +383,19 @@ unsigned int GetHashForDirectory( CString sDir ) return hash; } +/* Return true if "dir" is empty or does not exist. */ +bool DirectoryIsEmpty( const CString &dir ) +{ + if(dir == "") + return true; + if(!DoesFileExist(dir)) + return true; + + CStringArray asFileNames; + GetDirListing( dir, asFileNames ); + return asFileNames.empty(); +} + bool CompareCStringsAsc(const CString &str1, const CString &str2) { return str1.CompareNoCase( str2 ) < 0; @@ -906,7 +936,7 @@ CString Dirname( const CString &dir ) return dir.substr(0, pos+1); } -CString Capitalize( CString s ) +CString Capitalize( const CString &s ) { if( s.GetLength()==0 ) return ""; diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index e43264fdd0..af133cc9ae 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -132,7 +132,7 @@ float fmodfp(float x, float y); int power_of_two(int input); bool IsAnInt( const CString &s ); bool IsHexVal( const CString &s ); -float TimeToSeconds( CString sHMS ); +float TimeToSeconds( const CString &sHMS ); CString SecondsToTime( float fSecs ); CString ssprintf( const char *fmt, ...); @@ -149,7 +149,7 @@ CString werr_ssprintf( int err, const char *fmt, ...); void splitpath( const CString &Path, CString &Dir, CString &Filename, CString &Ext ); CString SetExtension( const CString &path, const CString &ext ); -CString GetExtension( CString sPath ); +CString GetExtension( const CString &sPath ); typedef int longchar; extern const wchar_t INVALID_CHAR; @@ -162,26 +162,23 @@ int utf8_get_char_len (const char *p); bool utf8_is_valid(const CString &str); // Splits a CString into an CStringArray according the Deliminator. -void split( - const CString &Source, - const CString &Deliminator, - CStringArray& AddIt, - const bool bIgnoreEmpty = true -); +void split( const CString &Source, const CString &Delimitor, CStringArray& AddIt, const bool bIgnoreEmpty = true ); void split( const wstring &Source, const wstring &Deliminator, vector &AddIt, const bool bIgnoreEmpty = true ); // Joins a CStringArray to create a CString according the Deliminator. -CString join(const CString &Deliminator, const CStringArray& Source); +CString join( const CString &Delimitor, const CStringArray& Source ); +CString join( const CString &Delimitor, CStringArray::const_iterator begin, CStringArray::const_iterator end ); CString GetCwd(); -unsigned int GetHashForString( CString s ); -unsigned int GetHashForFile( CString sPath ); -unsigned int GetHashForDirectory( CString sDir ); // a hash value that remains the same as long as nothing in the directory has changed +unsigned int GetHashForString( const CString &s ); +unsigned int GetHashForFile( const CString &sPath ); +unsigned int GetHashForDirectory( const CString &sDir ); // a hash value that remains the same as long as nothing in the directory has changed +bool DirectoryIsEmpty( const CString &dir ); bool CompareCStringsAsc(const CString &str1, const CString &str2); bool CompareCStringsDesc(const CString &str1, const CString &str2); -void SortCStringArray( CStringArray &AddTo, const bool bSortAcsending = true ); +void SortCStringArray( CStringArray &AddTo, const bool bSortAscending = true ); /* Find the mean and standard deviation of all numbers in [start,end). */ float calc_mean(const float *start, const float *end); @@ -226,7 +223,7 @@ CString WcharDisplayText(wchar_t c); CString Basename( const CString &dir ); CString Dirname( const CString &dir ); -CString Capitalize( CString s ); +CString Capitalize( const CString &s ); #ifndef WIN32 #include /* correct place with correct definitions */ @@ -278,7 +275,7 @@ typedef basic_string istring; /* Compatibility/convenience shortcuts. These are actually defined in RageFileManager.h, but * declared here since they're used in many places. */ -void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs=false, bool bReturnPathToo=false ); +void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs=false, bool bReturnPathToo=false ); bool DoesFileExist( const CString &sPath ); bool IsAFile( const CString &sPath ); bool IsADirectory( const CString &sPath );