using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Ubisoft
{
///
/// Class responsible for implementing System.IO related extensions
///
public class HtSystemIO
{
public static string NormalizePath(string path)
{
return path.Replace('\\', '/');
}
public static bool File_Exists(string path)
{
return !string.IsNullOrEmpty(path) && File.Exists(path);
}
public static bool Directory_Exists(string path)
{
return !string.IsNullOrEmpty(path) && Directory.Exists(path);
}
public static int Directory_FileCount(string path)
{
string[] paths = Directory.GetFiles(path);
return paths.Length;
}
///
/// Create the directory passed in, if it doesn't exist, along with all intermediate directories.
///
/// Path to a directory to create if it doesn't already exist.
public static void CreateDirectory(string path)
{
if (!Directory_Exists(path))
{
path = NormalizePath(path);
string[] tokens = path.Split('/');
string partialPath = "";
int count = tokens.Length;
for (int i = 0; i < count; ++i)
{
if (i > 0)
{
partialPath = Path.Combine(partialPath, tokens[i]);
}
else
{
partialPath = tokens[i];
}
if (!Directory_Exists(partialPath))
{
Directory.CreateDirectory(partialPath);
}
}
}
}
///
/// Copies the directory at fullSrcPath to fullDstPath. fullDstPath directory is created
///
/// Full source path of the directory to copy
/// Full destination path where the fullSrcPath will be copied to
public static void CopyDirectory(string fullSrcPath, string fullDstPath)
{
CopyDirectory(new DirectoryInfo(fullSrcPath), new DirectoryInfo(fullDstPath));
}
///
/// Copies the directory sr to dst. dst directory is created
///
/// DirectoryInfo the directory to copy
/// DirectoryInfo where the src will be copied to
public static void CopyDirectory(DirectoryInfo src, DirectoryInfo dst)
{
_ = Directory.CreateDirectory(dst.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in src.GetFiles())
{
_ = fi.CopyTo(Path.Combine(dst.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in src.GetDirectories())
{
DirectoryInfo nextTargetSubDir = dst.CreateSubdirectory(diSourceSubDir.Name);
CopyDirectory(diSourceSubDir, nextTargetSubDir);
}
}
///
/// Copies a file at fullSrcPath to fullDstPath creating all inexistent folders in fullDstPath.
///
/// Full source path of the file to copy
/// Full destination path where the file at fullSrcPath will be copied to
public static void CopyFile(string fullSrcPath, string fullDstPath)
{
if (File_Exists(fullSrcPath))
{
// Create dst folder if it doesn't exist
FileInfo fileInfo = new FileInfo(fullDstPath);
fileInfo.Directory.Create();
File.Copy((fullSrcPath), fullDstPath);
}
}
//
/// Recursive find all files starting at root directory at path
/// and return a list of full paths to those files.
///
/// Full path to the root directory< which files are requested/param>
/// Whether or not directories should be included in the returned value
/// A list of full paths to the files found
public static IEnumerable GetAllFiles(string fullPath, bool includeDirectories = true)
{
List filePaths = new List();
if (Directory.Exists(fullPath))
{
filePaths.AddRange(Directory.GetFiles(
fullPath,
"*",
SearchOption.AllDirectories));
if (includeDirectories)
{
filePaths.AddRange(Directory.GetDirectories(
fullPath,
"*",
SearchOption.AllDirectories));
}
}
return filePaths.Distinct().OrderBy(x =>
{
return x;
});
}
///
/// Returns the part of path that goes after directory.
/// Example:
/// GetPathFromDirectory("dir_01/dir_02/dir_03/filename, "dir_02", true) returns dir_02/dir_03/filename
/// GetPathFromDirectory("dir_01/dir_02/dir_03/filename, "dir_02", false) returns dir_03/filename
/// GetPathFromDirectory("dir_01/dir_02/dir_03/filename, "dir_xx", false) returns null
///
/// Source path
/// Directory to search for
/// Whether or not directory should be part of the returned value
/// Returns the part of path that goes after directory. It returns null if path doesn't contain directory
public static string GetPathFromDirectory(string path, string directory, bool includeDirectory)
{
string returnValue = null;
if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(directory))
{
returnValue = "";
bool addDirectory = false;
string[] tokens = path.Split(Path.DirectorySeparatorChar);
int count = tokens.Length;
for (int i = 0; i < count; i++)
{
if (includeDirectory && tokens[i] == directory)
{
addDirectory = true;
}
if (addDirectory)
{
returnValue = Path.Combine(returnValue, tokens[i]);
}
if (tokens[i] == directory)
{
addDirectory = true;
}
}
}
return returnValue;
}
///
/// Returns the last entry of a path
/// Example:
/// GetPathLastEntry("dir_01/dir_02/filename") returns "fileName"
/// GetPathLastEntry("dir_01/dir_02") returns "dir_02"
/// GetPathLastEntry("") returns ""
///
/// Source path
/// Returns the last entry of a path
public static string GetPathLastEntry(string path)
{
string originalPath = path;
path = NormalizePath(path);
return string.IsNullOrEmpty(path) ? path : path.Split('/').Last();
}
}
}