(VFS): added GetDirectoryName methods.

This commit is contained in:
morkt 2016-05-20 03:58:22 +04:00
parent 1a20e2c18b
commit d20fed194c

View File

@ -2,7 +2,7 @@
//! \date Fri Jun 05 15:32:27 2015 //! \date Fri Jun 05 15:32:27 2015
//! \brief Gameres file system abstraction. //! \brief Gameres file system abstraction.
// //
// Copyright (C) 2015 by morkt // Copyright (C) 2015-2016 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@ -72,6 +72,11 @@ namespace GameRes
/// </summary> /// </summary>
string CombinePath (string path1, string path2); string CombinePath (string path1, string path2);
/// <summary>
/// System.IO.Path.GetDirectoryName() analog.
/// </summary>
string GetDirectoryName (string path);
/// <summary> /// <summary>
/// Recursively enumerates files in the current directory and its subdirectories. /// Recursively enumerates files in the current directory and its subdirectories.
/// Subdirectory entries are omitted from resulting set. /// Subdirectory entries are omitted from resulting set.
@ -105,6 +110,11 @@ namespace GameRes
return Path.Combine (path1, path2); return Path.Combine (path1, path2);
} }
public string GetDirectoryName (string path)
{
return Path.GetDirectoryName (path);
}
public Entry FindFile (string filename) public Entry FindFile (string filename)
{ {
var attr = File.GetAttributes (filename); var attr = File.GetAttributes (filename);
@ -138,7 +148,7 @@ namespace GameRes
public IEnumerable<Entry> GetFiles (string pattern) public IEnumerable<Entry> GetFiles (string pattern)
{ {
string path = Path.GetDirectoryName (pattern); string path = GetDirectoryName (pattern);
pattern = Path.GetFileName (pattern); pattern = Path.GetFileName (pattern);
path = CombinePath (CurrentDirectory, path); path = CombinePath (CurrentDirectory, path);
var info = new DirectoryInfo (path); var info = new DirectoryInfo (path);
@ -237,6 +247,8 @@ namespace GameRes
public abstract string CombinePath (string path1, string path2); public abstract string CombinePath (string path1, string path2);
public abstract string GetDirectoryName (string path);
public abstract IEnumerable<Entry> GetFiles (string pattern); public abstract IEnumerable<Entry> GetFiles (string pattern);
#region IDisposable Members #region IDisposable Members
@ -311,6 +323,11 @@ namespace GameRes
{ {
return Path.Combine (path1, path2); return Path.Combine (path1, path2);
} }
public override string GetDirectoryName (string path)
{
return "";
}
} }
public class TreeArchiveFileSystem : ArchiveFileSystem public class TreeArchiveFileSystem : ArchiveFileSystem
@ -395,15 +412,20 @@ namespace GameRes
public override IEnumerable<Entry> GetFiles (string pattern) public override IEnumerable<Entry> GetFiles (string pattern)
{ {
string path = Path.GetDirectoryName (pattern); string path = GetDirectoryName (pattern);
if (string.IsNullOrEmpty (path))
path = CurrentDirectory;
pattern = Path.GetFileName (pattern); pattern = Path.GetFileName (pattern);
path = CombinePath (CurrentDirectory, path);
if (!string.IsNullOrEmpty (path))
path += PathDelimiter;
var glob = new FileNameGlob (pattern); var glob = new FileNameGlob (pattern);
return from file in m_arc.Dir if (string.IsNullOrEmpty (path))
where file.Name.StartsWith (path) && glob.IsMatch (Path.GetFileName (file.Name)) {
select file; return m_arc.Dir.Where (f => glob.IsMatch (Path.GetFileName (f.Name)));
}
else
{
path += PathDelimiter;
return m_arc.Dir.Where (f => f.Name.StartsWith (path) && glob.IsMatch (Path.GetFileName (f.Name)));
}
} }
public IEnumerable<Entry> GetFilesRecursive (IEnumerable<Entry> list) public IEnumerable<Entry> GetFilesRecursive (IEnumerable<Entry> list)
@ -466,6 +488,14 @@ namespace GameRes
} }
m_cwd = new_path; m_cwd = new_path;
} }
public override string GetDirectoryName (string path)
{
int sep = path.LastIndexOfAny (m_path_delimiters);
if (-1 == sep)
return "";
return path.Substring (0, sep);
}
} }
public sealed class FileSystemStack : IDisposable public sealed class FileSystemStack : IDisposable
@ -497,7 +527,7 @@ namespace GameRes
Pop(); Pop();
if (!string.IsNullOrEmpty (LastVisitedPath)) if (!string.IsNullOrEmpty (LastVisitedPath))
{ {
Top.CurrentDirectory = Path.GetDirectoryName (LastVisitedPath); Top.CurrentDirectory = Top.GetDirectoryName (LastVisitedPath);
} }
} }
else else
@ -649,6 +679,11 @@ namespace GameRes
return m_vfs.Top.CombinePath (path1, path2); return m_vfs.Top.CombinePath (path1, path2);
} }
public static string GetDirectoryName (string path)
{
return m_vfs.Top.GetDirectoryName (path);
}
public static Entry FindFile (string filename) public static Entry FindFile (string filename)
{ {
if (".." == filename) if (".." == filename)