fix up path handling

This commit is contained in:
Glenn Maynard
2003-12-11 07:24:32 +00:00
parent 65248e01d7
commit a7190307f3
2 changed files with 26 additions and 15 deletions
+8 -1
View File
@@ -32,8 +32,9 @@ CString FixSlashes( CString sPath )
void CollapsePath( CString &sPath )
{
/* Don't ignore empty: we do want to keep trailing slashes. */
CStringArray as;
split( sPath, "/", as, true );
split( sPath, "/", as, false );
for( unsigned i=0; i<as.size(); i++ )
{
@@ -43,6 +44,12 @@ void CollapsePath( CString &sPath )
as.erase( as.begin()+i-1 );
i -= 2;
}
else if( as[i] == "" && i+1 < as.size() )
{
/* Remove empty parts that aren't at the end; "foo//bar/" -> "foo/bar/". */
as.erase( as.begin()+i );
i -= 1;
}
else if( as[i] == "." )
{
as.erase( as.begin()+i );
+18 -14
View File
@@ -77,29 +77,30 @@ int FileSet::GetFileHash(const CString &path) const
return i->hash + i->size;
}
/* Given "foo/bar/baz/" or "foo/bar/baz", return "foo/bar/" and "baz". */
/*
* Given "foo/bar/baz/" or "foo/bar/baz", return "foo/bar/" and "baz".
* "foo" -> "", "foo"
*/
static void SplitPath( CString Path, CString &Dir, CString &Name )
{
/* Strip off any trailing slashes. */
if( Path.size() > 0 && Path.Right(1) == "/" )
CollapsePath( Path );
if( Path.Right(1) == "/" )
Path.erase( Path.size()-1 );
static Regex split("(.*/)([^/]+)");
CStringArray match;
if(split.Compare(Path, match)) {
/* At least one slash. */
Dir = match[0];
Name = match[1];
} else {
/* No slash. */
Dir = "./";
unsigned sep = Path.find_last_of( '/' );
if( sep == CString::npos )
{
Dir = "";
Name = Path;
}
else
{
Dir = Path.substr( 0, sep+1 );
Name = Path.substr( sep+1 );
}
}
FileType FilenameDB::GetFileType( const CString &sPath )
{
CString Dir, Name;
@@ -211,6 +212,9 @@ FileSet &FilenameDB::GetFileSet( CString dir )
dir.Replace("\\", "/"); /* foo\bar -> foo/bar */
dir.Replace("//", "/"); /* foo//bar -> foo/bar */
if( dir == "" )
dir = ".";
CString lower = dir;
lower.MakeLower();