move DirectoryIsEmpty to RageUtil

cleanup
add join( Delimitor, iterator, iterator )
This commit is contained in:
Glenn Maynard
2003-12-07 22:54:50 +00:00
parent 96a7ebe210
commit 5f6fedfcda
3 changed files with 48 additions and 35 deletions
-14
View File
@@ -22,20 +22,6 @@ AnnouncerManager* ANNOUNCER = NULL; // global object accessable from anywhere in
const CString EMPTY_ANNOUNCER_NAME = "Empty"; const CString EMPTY_ANNOUNCER_NAME = "Empty";
const CString ANNOUNCERS_DIR = BASE_PATH "Announcers" SLASH; 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() AnnouncerManager::AnnouncerManager()
{ {
LOG->Trace("AnnouncerManager::AnnouncerManager()"); LOG->Trace("AnnouncerManager::AnnouncerManager()");
+36 -6
View File
@@ -95,7 +95,7 @@ bool IsHexVal( const CString &s )
return true; return true;
} }
float TimeToSeconds( CString sHMS ) float TimeToSeconds( const CString &sHMS )
{ {
CStringArray arrayBits; CStringArray arrayBits;
split( sHMS, ":", arrayBits, false ); split( sHMS, ":", arrayBits, false );
@@ -206,6 +206,23 @@ CString join( const CString &Deliminator, const CStringArray& Source)
return csTmp; 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 <class S> template <class S>
void do_split( const S &Source, const S &Delimitor, vector<S> &AddIt, const bool bIgnoreEmpty ) void do_split( const S &Source, const S &Delimitor, vector<S> &AddIt, const bool bIgnoreEmpty )
@@ -285,7 +302,7 @@ CString SetExtension( const CString &path, const CString &ext )
return Dir + FName + (ext.size()? ".":"") + ext; return Dir + FName + (ext.size()? ".":"") + ext;
} }
CString GetExtension( CString sPath ) CString GetExtension( const CString &sPath )
{ {
unsigned pos = sPath.rfind( '.' ); unsigned pos = sPath.rfind( '.' );
if( pos == sPath.npos ) if( pos == sPath.npos )
@@ -312,7 +329,7 @@ CString GetCwd()
/* Reference: http://www.theorem.com/java/CRC32.java, rewritten by Glenn Maynard. /* Reference: http://www.theorem.com/java/CRC32.java, rewritten by Glenn Maynard.
* Public domain. */ * Public domain. */
unsigned int GetHashForString ( CString s ) unsigned int GetHashForString ( const CString &s )
{ {
static unsigned tab[256]; static unsigned tab[256];
static bool initted = false; static bool initted = false;
@@ -338,7 +355,7 @@ unsigned int GetHashForString ( CString s )
return crc; return crc;
} }
unsigned int GetHashForFile( CString sPath ) unsigned int GetHashForFile( const CString &sPath )
{ {
unsigned int hash = 0; unsigned int hash = 0;
@@ -349,7 +366,7 @@ unsigned int GetHashForFile( CString sPath )
return hash; return hash;
} }
unsigned int GetHashForDirectory( CString sDir ) unsigned int GetHashForDirectory( const CString &sDir )
{ {
unsigned int hash = 0; unsigned int hash = 0;
@@ -366,6 +383,19 @@ unsigned int GetHashForDirectory( CString sDir )
return hash; 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) bool CompareCStringsAsc(const CString &str1, const CString &str2)
{ {
return str1.CompareNoCase( str2 ) < 0; return str1.CompareNoCase( str2 ) < 0;
@@ -906,7 +936,7 @@ CString Dirname( const CString &dir )
return dir.substr(0, pos+1); return dir.substr(0, pos+1);
} }
CString Capitalize( CString s ) CString Capitalize( const CString &s )
{ {
if( s.GetLength()==0 ) if( s.GetLength()==0 )
return ""; return "";
+12 -15
View File
@@ -132,7 +132,7 @@ float fmodfp(float x, float y);
int power_of_two(int input); int power_of_two(int input);
bool IsAnInt( const CString &s ); bool IsAnInt( const CString &s );
bool IsHexVal( const CString &s ); bool IsHexVal( const CString &s );
float TimeToSeconds( CString sHMS ); float TimeToSeconds( const CString &sHMS );
CString SecondsToTime( float fSecs ); CString SecondsToTime( float fSecs );
CString ssprintf( const char *fmt, ...); 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 ); void splitpath( const CString &Path, CString &Dir, CString &Filename, CString &Ext );
CString SetExtension( const CString &path, const CString &ext ); CString SetExtension( const CString &path, const CString &ext );
CString GetExtension( CString sPath ); CString GetExtension( const CString &sPath );
typedef int longchar; typedef int longchar;
extern const wchar_t INVALID_CHAR; 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); bool utf8_is_valid(const CString &str);
// Splits a CString into an CStringArray according the Deliminator. // Splits a CString into an CStringArray according the Deliminator.
void split( void split( const CString &Source, const CString &Delimitor, CStringArray& AddIt, const bool bIgnoreEmpty = true );
const CString &Source,
const CString &Deliminator,
CStringArray& AddIt,
const bool bIgnoreEmpty = true
);
void split( const wstring &Source, const wstring &Deliminator, vector<wstring> &AddIt, const bool bIgnoreEmpty = true ); void split( const wstring &Source, const wstring &Deliminator, vector<wstring> &AddIt, const bool bIgnoreEmpty = true );
// Joins a CStringArray to create a CString according the Deliminator. // 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(); CString GetCwd();
unsigned int GetHashForString( CString s ); unsigned int GetHashForString( const CString &s );
unsigned int GetHashForFile( CString sPath ); unsigned int GetHashForFile( const 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 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 CompareCStringsAsc(const CString &str1, const CString &str2);
bool CompareCStringsDesc(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). */ /* Find the mean and standard deviation of all numbers in [start,end). */
float calc_mean(const float *start, const float *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 Basename( const CString &dir );
CString Dirname( const CString &dir ); CString Dirname( const CString &dir );
CString Capitalize( CString s ); CString Capitalize( const CString &s );
#ifndef WIN32 #ifndef WIN32
#include <unistd.h> /* correct place with correct definitions */ #include <unistd.h> /* correct place with correct definitions */
@@ -278,7 +275,7 @@ typedef basic_string<char,char_traits_char_nocase> istring;
/* Compatibility/convenience shortcuts. These are actually defined in RageFileManager.h, but /* Compatibility/convenience shortcuts. These are actually defined in RageFileManager.h, but
* declared here since they're used in many places. */ * 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 DoesFileExist( const CString &sPath );
bool IsAFile( const CString &sPath ); bool IsAFile( const CString &sPath );
bool IsADirectory( const CString &sPath ); bool IsADirectory( const CString &sPath );