Files
FileConverter/Application/FileConverter/PathHelpers.cs
T

120 lines
4.0 KiB
C#

// <copyright file="PathHelpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverter
{
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class PathHelpers
{
private static Regex driveLetterRegex = new Regex(@"[a-zA-Z]:\\");
private static Regex pathRegex = new Regex(@"^[a-zA-Z]:\\(?:[^\\/:*?""<>|\r\n]+\\)*[^\.\\/:*?""<>|\r\n][^\\/:*?""<>|\r\n]*$");
private static Regex filenameRegex = new Regex(@"[^\\]*", RegexOptions.RightToLeft);
private static Regex directoryRegex = new Regex(@"(?:([^\\]*)\\)*");
public static bool IsPathDriveLetterValid(string path)
{
return PathHelpers.driveLetterRegex.IsMatch(path);
}
public static bool IsPathValid(string path)
{
return PathHelpers.pathRegex.IsMatch(path);
}
public static string GetFileName(string path)
{
MatchCollection matchCollection = PathHelpers.filenameRegex.Matches(path);
Match filenameMatch = matchCollection.Count > 0 ? matchCollection[0] : null;
return filenameMatch?.Groups[0].Value;
}
public static IEnumerable<string> GetDirectories(string path)
{
MatchCollection matchCollection = PathHelpers.directoryRegex.Matches(path);
Match match = matchCollection.Count > 0 ? matchCollection[0] : null;
if (match == null || match.Groups.Count < 2)
{
yield break;
}
Group matchGroup = match.Groups[1];
for (int index = 0; index < matchGroup.Captures.Count; index++)
{
yield return matchGroup.Captures[index].Value;
}
}
public static string GenerateUniquePath(string path)
{
string baseExtension = System.IO.Path.GetExtension(path);
string basePath = path.Substring(0, path.Length - baseExtension.Length);
int index = 2;
while (System.IO.File.Exists(path))
{
path = string.Format("{0} ({1}){2}", basePath, index, baseExtension);
index++;
}
return path;
}
public static string GetExtensionCategory(string extension)
{
switch (extension)
{
case "aac":
case "aiff":
case "ape":
case "flac":
case "mp3":
case "m4a":
case "ogg":
case "wav":
case "wma":
return InputCategoryNames.Audio;
case "avi":
case "flv":
case "mp4":
case "mkv":
return InputCategoryNames.Video;
}
return InputCategoryNames.Misc;
}
public static bool IsOutputTypeCompatibleWithCategory(OutputType outputType, string category)
{
if (category == InputCategoryNames.Misc)
{
// Misc category contains unsorted input extensions, so we consider that they are compatible to be tolerant.
return true;
}
switch (outputType)
{
case OutputType.Aac:
case OutputType.Flac:
case OutputType.Mp3:
case OutputType.Ogg:
case OutputType.Wav:
return category == InputCategoryNames.Audio || category == InputCategoryNames.Video;
case OutputType.Mkv:
return category == InputCategoryNames.Video;
default:
return false;
}
}
public static class InputCategoryNames
{
public const string Audio = "Audio";
public const string Video = "Video";
public const string Misc = "Misc";
}
}
}