using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace SabreTools.IO { /// /// Methods around path operations /// public static class IOExtensions { /// /// Ensure the output directory is a proper format and can be created /// /// Directory to check /// True if the directory should be created, false otherwise (default) /// Full path to the directory public static string Ensure(this string dir, bool create = false) { // If the output directory is invalid if (string.IsNullOrWhiteSpace(dir)) dir = PathTool.GetRuntimeDirectory(); // Get the full path for the output directory dir = Path.GetFullPath(dir.Trim('"')); // If we're creating the output folder, do so if (create && !Directory.Exists(dir)) Directory.CreateDirectory(dir); return dir; } /// /// Determines a text file's encoding by analyzing its byte order mark (BOM). /// Defaults to ASCII when detection of the text file's endianness fails. /// /// The text file to analyze. /// The detected encoding. /// http://stackoverflow.com/questions/3825390/effective-way-to-find-any-files-encoding public static Encoding GetEncoding(this string filename) { if (string.IsNullOrEmpty(filename)) return Encoding.Default; if (!File.Exists(filename)) return Encoding.Default; // Try to open the file try { FileStream file = File.OpenRead(filename); if (file == null) return Encoding.Default; // Read the BOM var bom = new byte[4]; file.Read(bom, 0, 4); file.Dispose(); // Analyze the BOM if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7; if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8; if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unicode; //UTF-16LE if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32; return Encoding.Default; } catch { return Encoding.Default; } } /// /// Get the extension from the path, if possible /// /// Path to get extension from /// Extension, if possible public static string GetNormalizedExtension(this string path) { // Check null or empty first if (string.IsNullOrWhiteSpace(path)) return null; // Get the extension from the path, if possible string ext = Path.GetExtension(path)?.ToLowerInvariant(); // Check if the extension is null or empty if (string.IsNullOrWhiteSpace(ext)) return null; // Make sure that extensions are valid ext = ext.TrimStart('.'); return ext; } /// /// Get all empty folders within a root folder /// /// Root directory to parse /// IEumerable containing all directories that are empty, an empty enumerable if the root is empty, null otherwise public static List ListEmpty(this string root) { // Check null or empty first if (string.IsNullOrEmpty(root)) return null; // Then, check if the root exists if (!Directory.Exists(root)) return null; // If it does and it is empty, return a blank enumerable if (Directory.EnumerateFileSystemEntries(root, "*", SearchOption.AllDirectories).Count() == 0) return new List(); // Otherwise, get the complete list return Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories) .Where(dir => Directory.EnumerateFileSystemEntries(dir, "*", SearchOption.AllDirectories).Count() == 0) .ToList(); } } }