// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.InteropServices; namespace Spludlow.Io { public class Paths { public static string CygwinName(string filename) { if (filename.StartsWith(@"\\") == true) return filename.Replace(@"\", "/"); if (filename[1] != ':' || filename[2] != '\\') throw new ApplicationException("CygwinName; Not vaild windows path:\t" + filename); StringBuilder result = new StringBuilder(); result.Append("/cygdrive/"); result.Append(Char.ToLower(filename[0])); result.Append(filename.Substring(2)); result.Replace('\\', '/'); return result.ToString(); } private static List InvalidFileNameChars; private static List InvalidPathChars; static Paths() { InvalidFileNameChars = new List(Path.GetInvalidFileNameChars()); InvalidPathChars = new List(Path.GetInvalidPathChars()); Spludlow.Log.Warning("Paths Static Constructor"); } public static string LegalFileName(string fileName) { return LegalFileName(fileName, "_"); } public static string LegalFileName(string fileName, string replaceBadWith) { return LegalName(fileName, InvalidFileNameChars, replaceBadWith); } public static bool IsLegalDirectoryName(string name) { foreach (char c in name) { if (InvalidPathChars.Contains(c) == true) return false; } return true; } public static bool IsLegalFileName(string name) { foreach (char c in name) { if (InvalidFileNameChars.Contains(c) == true) return false; } return true; } public static string LegalDirectoryName(string directoryName) { return LegalDirectoryName(directoryName, "_"); } public static string LegalDirectoryName(string directoryName, string replaceBadWith) { return LegalName(directoryName, InvalidPathChars, replaceBadWith); } private static string LegalName(string name, List invalidChars, string replaceBadWith) { StringBuilder sb = new StringBuilder(); foreach (char c in name) { if (invalidChars.Contains(c) == true) sb.Append(replaceBadWith); else sb.Append(c); } return sb.ToString(); } public const int MAX_PATH = 260; public static string TruncateLongPath(string pathName) { if (pathName.Length < MAX_PATH) return pathName; int index = pathName.LastIndexOf('\\'); string directory = pathName.Substring(0, index); string filename = pathName.Substring(index + 1); string name = Path.GetFileNameWithoutExtension(filename); string extention = Path.GetExtension(filename); // dir + 1 + name + extention int nameLength = MAX_PATH - (2 + directory.Length + extention.Length); string newPath = directory + @"\" + name.Substring(0, nameLength) + extention; Spludlow.Log.Warning("Long Path Truncated:\t" + pathName.Length, new object[] { pathName, newPath }); return newPath; } public static bool IsSymbolicLink(string path) { FileInfo pathInfo = new FileInfo(path); return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint); } public static string UniqueExistingName(string pathName) { int existingCount = 2; string originalName = pathName; while (Directory.Exists(pathName) == true || File.Exists(pathName) == true) { pathName = originalName + " (" + existingCount + ")"; ++existingCount; } return pathName; } } }