From a00685d1d151bd9e9dd19cc619de50192df1d286 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 24 Jul 2003 01:28:53 +0000 Subject: [PATCH] Add GetPackageDirectory and IsValidPackageDirectory. --- stepmania/src/smpackage/SMPackageUtil.cpp | 48 +++++++++++++++++++++++ stepmania/src/smpackage/smpackageUtil.h | 3 ++ 2 files changed, 51 insertions(+) diff --git a/stepmania/src/smpackage/SMPackageUtil.cpp b/stepmania/src/smpackage/SMPackageUtil.cpp index 3643aeae54..43c5e16128 100644 --- a/stepmania/src/smpackage/SMPackageUtil.cpp +++ b/stepmania/src/smpackage/SMPackageUtil.cpp @@ -89,4 +89,52 @@ bool SetPref( CString name, bool val ) } +/* Get a package directory. For most paths, this is the first two components. For + * songs, this is the first three. */ +CString GetPackageDirectory(CString path) +{ + if( path.Find("CVS") != -1 ) + return ""; // skip + + CStringArray Parts; + split( path, "\\", Parts ); + + unsigned NumParts = 2; + if( !Parts[0].CompareNoCase("songs") ) + NumParts = 3; + if( Parts.size() < NumParts ) + return ""; + + Parts.erase(Parts.begin() + NumParts, Parts.end()); + + CString ret = join( "\\", Parts ); + if( !IsADirectory(ret) ) + return ""; + return ret; +} + + +bool IsValidPackageDirectory(CString path) +{ + /* Make sure the path contains only second-level directories, and doesn't + * contain any ".", "..", "...", etc. dirs. */ + CStringArray Parts; + split( path, "\\", Parts, true ); + if( Parts.size() == 0 ) + return false; + + /* Make sure we're not going to "uninstall" an entire Songs subfolder. */ + unsigned NumParts = 2; + if( !Parts[0].CompareNoCase("songs") ) + NumParts = 3; + if( Parts.size() < NumParts ) + return false; + + /* Make sure the path doesn't contain any ".", "..", "...", etc. dirs. */ + for( unsigned i = 0; i < Parts.size(); ++i ) + if( Parts[i][0] == '.' ) + return false; + + return true; +} diff --git a/stepmania/src/smpackage/smpackageUtil.h b/stepmania/src/smpackage/smpackageUtil.h index 98998018f5..a711698e04 100644 --- a/stepmania/src/smpackage/smpackageUtil.h +++ b/stepmania/src/smpackage/smpackageUtil.h @@ -10,4 +10,7 @@ void AddStepManiaInstallDir( CString sNewInstallDir ); bool GetPref( CString name, bool &val ); bool SetPref( CString name, bool val ); +CString GetPackageDirectory(CString path); +bool IsValidPackageDirectory(CString path); + #endif