diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index 78699f66..0bf641e0 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -5583,7 +5583,7 @@ namespace SabreTools.Library.DatFiles } // Get the outfile names - Dictionary outfiles = Style.CreateOutfileNames(outDir, this, overwrite); + Dictionary outfiles = CreateOutfileNames(outDir, overwrite); try { @@ -5667,6 +5667,202 @@ namespace SabreTools.Library.DatFiles return true; } + /// + /// Generate a proper outfile name based on a DAT and output directory + /// + /// Output directory + /// True if we ignore existing files (default), false otherwise + /// Dictionary of output formats mapped to file names + private Dictionary CreateOutfileNames(string outDir, bool overwrite = true) + { + // Create the output dictionary + Dictionary outfileNames = new Dictionary(); + + // Double check the outDir for the end delim + if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString())) + { + outDir += Path.DirectorySeparatorChar; + } + + // Get the extensions from the output type + + // AttractMode + if ((DatFormat & DatFormat.AttractMode) != 0) + { + outfileNames.Add(DatFormat.AttractMode, CreateOutfileNamesHelper(outDir, ".txt", overwrite)); + } + + // ClrMamePro + if ((DatFormat & DatFormat.ClrMamePro) != 0) + { + outfileNames.Add(DatFormat.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", overwrite)); + }; + + // CSV + if ((DatFormat & DatFormat.CSV) != 0) + { + outfileNames.Add(DatFormat.CSV, CreateOutfileNamesHelper(outDir, ".csv", overwrite)); + }; + + // DOSCenter + if ((DatFormat & DatFormat.DOSCenter) != 0 + && (DatFormat & DatFormat.ClrMamePro) == 0 + && (DatFormat & DatFormat.RomCenter) == 0) + { + outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dat", overwrite)); + }; + if ((DatFormat & DatFormat.DOSCenter) != 0 + && ((DatFormat & DatFormat.ClrMamePro) != 0 + || (DatFormat & DatFormat.RomCenter) != 0)) + { + outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", overwrite)); + } + + //MAME Listroms + if ((DatFormat & DatFormat.Listroms) != 0 + && (DatFormat & DatFormat.AttractMode) == 0) + { + outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".txt", overwrite)); + } + if ((DatFormat & DatFormat.Listroms) != 0 + && (DatFormat & DatFormat.AttractMode) != 0) + { + outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".lr.txt", overwrite)); + } + + // Logiqx XML + if ((DatFormat & DatFormat.Logiqx) != 0) + { + outfileNames.Add(DatFormat.Logiqx, CreateOutfileNamesHelper(outDir, ".xml", overwrite)); + } + + // Missfile + if ((DatFormat & DatFormat.MissFile) != 0 + && (DatFormat & DatFormat.AttractMode) == 0) + { + outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".txt", overwrite)); + } + if ((DatFormat & DatFormat.MissFile) != 0 + && (DatFormat & DatFormat.AttractMode) != 0) + { + outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".miss.txt", overwrite)); + } + + // OfflineList + if (((DatFormat & DatFormat.OfflineList) != 0) + && (DatFormat & DatFormat.Logiqx) == 0 + && (DatFormat & DatFormat.SabreDat) == 0 + && (DatFormat & DatFormat.SoftwareList) == 0) + { + outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".xml", overwrite)); + } + if (((DatFormat & DatFormat.OfflineList) != 0 + && ((DatFormat & DatFormat.Logiqx) != 0 + || (DatFormat & DatFormat.SabreDat) != 0 + || (DatFormat & DatFormat.SoftwareList) != 0))) + { + outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".ol.xml", overwrite)); + } + + // Redump MD5 + if ((DatFormat & DatFormat.RedumpMD5) != 0) + { + outfileNames.Add(DatFormat.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", overwrite)); + }; + + // Redump SFV + if ((DatFormat & DatFormat.RedumpSFV) != 0) + { + outfileNames.Add(DatFormat.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", overwrite)); + }; + + // Redump SHA-1 + if ((DatFormat & DatFormat.RedumpSHA1) != 0) + { + outfileNames.Add(DatFormat.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", overwrite)); + }; + + // Redump SHA-256 + if ((DatFormat & DatFormat.RedumpSHA256) != 0) + { + outfileNames.Add(DatFormat.RedumpSHA256, CreateOutfileNamesHelper(outDir, ".sha256", overwrite)); + }; + + // RomCenter + if ((DatFormat & DatFormat.RomCenter) != 0 + && (DatFormat & DatFormat.ClrMamePro) == 0) + { + outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".dat", overwrite)); + }; + if ((DatFormat & DatFormat.RomCenter) != 0 + && (DatFormat & DatFormat.ClrMamePro) != 0) + { + outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", overwrite)); + }; + + // SabreDAT + if ((DatFormat & DatFormat.SabreDat) != 0 && (DatFormat & DatFormat.Logiqx) == 0) + { + outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".xml", overwrite)); + }; + if ((DatFormat & DatFormat.SabreDat) != 0 && (DatFormat & DatFormat.Logiqx) != 0) + { + outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", overwrite)); + }; + + // Software List + if ((DatFormat & DatFormat.SoftwareList) != 0 + && (DatFormat & DatFormat.Logiqx) == 0 + && (DatFormat & DatFormat.SabreDat) == 0) + { + outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".xml", overwrite)); + } + if ((DatFormat & DatFormat.SoftwareList) != 0 + && ((DatFormat & DatFormat.Logiqx) != 0 + || (DatFormat & DatFormat.SabreDat) != 0)) + { + outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".sl.xml", overwrite)); + } + + // TSV + if ((DatFormat & DatFormat.TSV) != 0) + { + outfileNames.Add(DatFormat.TSV, CreateOutfileNamesHelper(outDir, ".tsv", overwrite)); + }; + + return outfileNames; + } + + /// + /// Help generating the outfile name + /// + /// Output directory + /// Extension to use for the file + /// True if we ignore existing files, false otherwise + /// String containing the new filename + private string CreateOutfileNamesHelper(string outDir, string extension, bool overwrite) + { + string filename = (String.IsNullOrEmpty(FileName) ? Description : FileName); + string outfile = outDir + filename + extension; + outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? + outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : + outfile); + if (!overwrite) + { + int i = 1; + while (File.Exists(outfile)) + { + outfile = outDir + filename + "_" + i + extension; + outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? + outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : + outfile); + i++; + } + } + + return outfile; + } + #endregion #endregion // Instance Methods @@ -5701,7 +5897,7 @@ namespace SabreTools.Library.DatFiles outDir = Path.GetFullPath(outDir); // Get the dictionary of desired output report names - Dictionary outputs = Style.CreateOutStatsNames(outDir, statDatFormat, reportName); + Dictionary outputs = CreateOutStatsNames(outDir, statDatFormat, reportName); // Make sure we have all files and then order them List files = FileTools.GetOnlyFilesFromInputs(inputs); @@ -5851,6 +6047,80 @@ namespace SabreTools.Library.DatFiles Please check the log folder if the stats scrolled offscreen", false); } + /// + /// Get the proper extension for the stat output format + /// + /// Output path to use + /// StatDatFormat to get the extension for + /// Name of the input file to use + /// Dictionary of output formats mapped to file names + private static Dictionary CreateOutStatsNames(string outDir, StatReportFormat statDatFormat, string reportName, bool overwrite = true) + { + Dictionary output = new Dictionary(); + + // First try to create the output directory if we need to + if (!Directory.Exists(outDir)) + { + Directory.CreateDirectory(outDir); + } + + // Double check the outDir for the end delim + if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString())) + { + outDir += Path.DirectorySeparatorChar; + } + + // For each output format, get the appropriate stream writer + if ((statDatFormat & StatReportFormat.None) != 0) + { + output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite)); + } + if ((statDatFormat & StatReportFormat.CSV) != 0) + { + output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite)); + } + if ((statDatFormat & StatReportFormat.HTML) != 0) + { + output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite)); + } + if ((statDatFormat & StatReportFormat.TSV) != 0) + { + output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite)); + } + + return output; + } + + /// + /// Help generating the outstats name + /// + /// Output directory + /// Extension to use for the file + /// Name of the input file to use + /// True if we ignore existing files, false otherwise + /// String containing the new filename + private static string CreateOutStatsNamesHelper(string outDir, string extension, string reportName, bool overwrite) + { + string outfile = outDir + reportName + extension; + outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? + outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : + outfile); + if (!overwrite) + { + int i = 1; + while (File.Exists(outfile)) + { + outfile = outDir + reportName + "_" + i + extension; + outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? + outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : + outfile); + i++; + } + } + + return outfile; + } + #endregion #endregion // Static Methods diff --git a/SabreTools.Library/Tools/FileTools.cs b/SabreTools.Library/Tools/FileTools.cs index 13f8e733..4c13f036 100644 --- a/SabreTools.Library/Tools/FileTools.cs +++ b/SabreTools.Library/Tools/FileTools.cs @@ -41,6 +41,162 @@ namespace SabreTools.Library.Tools /// public static class FileTools { + #region BinaryReader Extensions + + /// + /// Reads the specified number of bytes from the stream, starting from a specified point in the byte array. + /// + /// The buffer to read data into. + /// The starting point in the buffer at which to begin reading into the buffer. + /// The number of bytes to read. + /// The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached. + public static int ReadReverse(this BinaryReader reader, byte[] buffer, int index, int count) + { + int retval = reader.Read(buffer, index, count); + buffer = buffer.Reverse().ToArray(); + return retval; + } + + /// + /// Reads the specified number of characters from the stream, starting from a specified point in the character array. + /// + /// The buffer to read data into. + /// The starting point in the buffer at which to begin reading into the buffer. + /// The number of characters to read. + /// The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached. + public static int ReadReverse(this BinaryReader reader, char[] buffer, int index, int count) + + { + int retval = reader.Read(buffer, index, count); + buffer = buffer.Reverse().ToArray(); + return retval; + } + + /// + /// Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes. + /// + /// The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur. + /// A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached. + public static byte[] ReadBytesReverse(this BinaryReader reader, int count) + { + byte[] retval = reader.ReadBytes(count); + retval = retval.Reverse().ToArray(); + return retval; + } + + /// + /// Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes. + /// + /// A decimal value read from the current stream. + public static decimal ReadDecimalReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(16); + retval = retval.Reverse().ToArray(); + + int i1 = BitConverter.ToInt32(retval, 0); + int i2 = BitConverter.ToInt32(retval, 4); + int i3 = BitConverter.ToInt32(retval, 8); + int i4 = BitConverter.ToInt32(retval, 12); + + return new decimal(new int[] { i1, i2, i3, i4 }); + } + + /// + /// eads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes. + /// + /// An 8-byte floating point value read from the current stream. + public static double ReadDoubleReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToDouble(retval, 0); + } + + /// + /// Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes. + /// + /// A 2-byte signed integer read from the current stream. + public static short ReadInt16Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt16(retval, 0); + } + + /// + /// Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. + /// + /// A 4-byte signed integer read from the current stream. + public static int ReadInt32Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt32(retval, 0); + } + + /// + /// Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes. + /// + /// An 8-byte signed integer read from the current stream. + public static long ReadInt64Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToInt64(retval, 0); + } + + /// + /// Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes. + /// + /// A 4-byte floating point value read from the current stream. + public static float ReadSingleReverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToSingle(retval, 0); + } + + /// + /// Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes. + /// + /// This API is not CLS-compliant. + /// + /// A 2-byte unsigned integer read from this stream. + public static ushort ReadUInt16Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt16(retval, 0); + } + + /// + /// Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes. + /// + /// This API is not CLS-compliant. + /// + /// A 4-byte unsigned integer read from this stream. + public static uint ReadUInt32Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt32(retval, 0); + } + + /// + /// Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes. + /// + /// This API is not CLS-compliant. + /// + /// An 8-byte unsigned integer read from this stream. + public static ulong ReadUInt64Reverse(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + retval = retval.Reverse().ToArray(); + return BitConverter.ToUInt64(retval, 0); + } + + #endregion + #region Factories /// @@ -1017,7 +1173,7 @@ namespace SabreTools.Library.Tools } /// - /// Try to open a file for write, optionally throwing the error + /// Try to open an existing file for write, optionally throwing the error /// /// Name of the file to open /// True if the error that is thrown should be thrown back to the caller, false otherwise diff --git a/SabreTools.Library/Tools/Style.cs b/SabreTools.Library/Tools/Style.cs index 71b979f0..6868ff22 100644 --- a/SabreTools.Library/Tools/Style.cs +++ b/SabreTools.Library/Tools/Style.cs @@ -7,14 +7,12 @@ using System.Text.RegularExpressions; using System.Web; using SabreTools.Library.Data; -using SabreTools.Library.DatFiles; #if MONO using System.IO; #else using Alphaleonis.Win32.Filesystem; -using BinaryReader = System.IO.BinaryReader; using FileStream = System.IO.FileStream; #endif @@ -126,434 +124,6 @@ namespace SabreTools.Library.Tools return hash; } - /// - /// Generate a proper outfile name based on a DAT and output directory - /// - /// Output directory - /// DAT information - /// True if we ignore existing files (default), false otherwise - /// Dictionary of output formats mapped to file names - public static Dictionary CreateOutfileNames(string outDir, DatFile datdata, bool overwrite = true) - { - // Create the output dictionary - Dictionary outfileNames = new Dictionary(); - - // Double check the outDir for the end delim - if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString())) - { - outDir += Path.DirectorySeparatorChar; - } - - // Get the extensions from the output type - - // AttractMode - if ((datdata.DatFormat & DatFormat.AttractMode) != 0) - { - outfileNames.Add(DatFormat.AttractMode, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite)); - } - - // ClrMamePro - if ((datdata.DatFormat & DatFormat.ClrMamePro) != 0) - { - outfileNames.Add(DatFormat.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite)); - }; - - // CSV - if ((datdata.DatFormat & DatFormat.CSV) != 0) - { - outfileNames.Add(DatFormat.CSV, CreateOutfileNamesHelper(outDir, ".csv", datdata, overwrite)); - }; - - // DOSCenter - if ((datdata.DatFormat & DatFormat.DOSCenter) != 0 - && (datdata.DatFormat & DatFormat.ClrMamePro) == 0 - && (datdata.DatFormat & DatFormat.RomCenter) == 0) - { - outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite)); - }; - if ((datdata.DatFormat & DatFormat.DOSCenter) != 0 - && ((datdata.DatFormat & DatFormat.ClrMamePro) != 0 - || (datdata.DatFormat & DatFormat.RomCenter) != 0)) - { - outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", datdata, overwrite)); - } - - //MAME Listroms - if ((datdata.DatFormat & DatFormat.Listroms) != 0 - && (datdata.DatFormat & DatFormat.AttractMode) == 0) - { - outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite)); - } - if ((datdata.DatFormat & DatFormat.Listroms) != 0 - && (datdata.DatFormat & DatFormat.AttractMode) != 0) - { - outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".lr.txt", datdata, overwrite)); - } - - // Logiqx XML - if ((datdata.DatFormat & DatFormat.Logiqx) != 0) - { - outfileNames.Add(DatFormat.Logiqx, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite)); - } - - // Missfile - if ((datdata.DatFormat & DatFormat.MissFile) != 0 - && (datdata.DatFormat & DatFormat.AttractMode) == 0) - { - outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite)); - } - if ((datdata.DatFormat & DatFormat.MissFile) != 0 - && (datdata.DatFormat & DatFormat.AttractMode) != 0) - { - outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".miss.txt", datdata, overwrite)); - } - - // OfflineList - if (((datdata.DatFormat & DatFormat.OfflineList) != 0) - && (datdata.DatFormat & DatFormat.Logiqx) == 0 - && (datdata.DatFormat & DatFormat.SabreDat) == 0 - && (datdata.DatFormat & DatFormat.SoftwareList) == 0) - { - outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite)); - } - if (((datdata.DatFormat & DatFormat.OfflineList) != 0 - && ((datdata.DatFormat & DatFormat.Logiqx) != 0 - || (datdata.DatFormat & DatFormat.SabreDat) != 0 - || (datdata.DatFormat & DatFormat.SoftwareList) != 0))) - { - outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".ol.xml", datdata, overwrite)); - } - - // Redump MD5 - if ((datdata.DatFormat & DatFormat.RedumpMD5) != 0) - { - outfileNames.Add(DatFormat.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", datdata, overwrite)); - }; - - // Redump SFV - if ((datdata.DatFormat & DatFormat.RedumpSFV) != 0) - { - outfileNames.Add(DatFormat.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", datdata, overwrite)); - }; - - // Redump SHA-1 - if ((datdata.DatFormat & DatFormat.RedumpSHA1) != 0) - { - outfileNames.Add(DatFormat.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", datdata, overwrite)); - }; - - // Redump SHA-256 - if ((datdata.DatFormat & DatFormat.RedumpSHA256) != 0) - { - outfileNames.Add(DatFormat.RedumpSHA256, CreateOutfileNamesHelper(outDir, ".sha256", datdata, overwrite)); - }; - - // RomCenter - if ((datdata.DatFormat & DatFormat.RomCenter) != 0 - && (datdata.DatFormat & DatFormat.ClrMamePro) == 0) - { - outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite)); - }; - if ((datdata.DatFormat & DatFormat.RomCenter) != 0 - && (datdata.DatFormat & DatFormat.ClrMamePro) != 0) - { - outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", datdata, overwrite)); - }; - - // SabreDAT - if ((datdata.DatFormat & DatFormat.SabreDat) != 0 && (datdata.DatFormat & DatFormat.Logiqx) == 0) - { - outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite)); - }; - if ((datdata.DatFormat & DatFormat.SabreDat) != 0 && (datdata.DatFormat & DatFormat.Logiqx) != 0) - { - outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", datdata, overwrite)); - }; - - // Software List - if ((datdata.DatFormat & DatFormat.SoftwareList) != 0 - && (datdata.DatFormat & DatFormat.Logiqx) == 0 - && (datdata.DatFormat & DatFormat.SabreDat) == 0) - { - outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite)); - } - if ((datdata.DatFormat & DatFormat.SoftwareList) != 0 - && ((datdata.DatFormat & DatFormat.Logiqx) != 0 - || (datdata.DatFormat & DatFormat.SabreDat) != 0)) - { - outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".sl.xml", datdata, overwrite)); - } - - // TSV - if ((datdata.DatFormat & DatFormat.TSV) != 0) - { - outfileNames.Add(DatFormat.TSV, CreateOutfileNamesHelper(outDir, ".tsv", datdata, overwrite)); - }; - - return outfileNames; - } - - /// - /// Help generating the outfile name - /// - /// Output directory - /// Extension to use for the file - /// DAT information - /// True if we ignore existing files, false otherwise - /// String containing the new filename - private static string CreateOutfileNamesHelper(string outDir, string extension, DatFile datdata, bool overwrite) - { - string filename = (String.IsNullOrEmpty(datdata.FileName) ? datdata.Description : datdata.FileName); - string outfile = outDir + filename + extension; - outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? - outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : - outfile); - if (!overwrite) - { - int i = 1; - while (File.Exists(outfile)) - { - outfile = outDir + filename + "_" + i + extension; - outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? - outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : - outfile); - i++; - } - } - - return outfile; - } - - /// - /// Get the proper extension for the stat output format - /// - /// Output path to use - /// StatDatFormat to get the extension for - /// Name of the input file to use - /// Dictionary of output formats mapped to file names - public static Dictionary CreateOutStatsNames(string outDir, StatReportFormat statDatFormat, string reportName, bool overwrite = true) - { - Dictionary output = new Dictionary(); - - // First try to create the output directory if we need to - if (!Directory.Exists(outDir)) - { - Directory.CreateDirectory(outDir); - } - - // Double check the outDir for the end delim - if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString())) - { - outDir += Path.DirectorySeparatorChar; - } - - // For each output format, get the appropriate stream writer - if ((statDatFormat & StatReportFormat.None) != 0) - { - output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite)); - } - if ((statDatFormat & StatReportFormat.CSV) != 0) - { - output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite)); - } - if ((statDatFormat & StatReportFormat.HTML) != 0) - { - output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite)); - } - if ((statDatFormat & StatReportFormat.TSV) != 0) - { - output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite)); - } - - return output; - } - - /// - /// Help generating the outstats name - /// - /// Output directory - /// Extension to use for the file - /// Name of the input file to use - /// True if we ignore existing files, false otherwise - /// String containing the new filename - private static string CreateOutStatsNamesHelper(string outDir, string extension, string reportName, bool overwrite) - { - string outfile = outDir + reportName + extension; - outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? - outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : - outfile); - if (!overwrite) - { - int i = 1; - while (File.Exists(outfile)) - { - outfile = outDir + reportName + "_" + i + extension; - outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? - outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : - outfile); - i++; - } - } - - return outfile; - } - - #endregion - - #region Extensions - - /// - /// Reads the specified number of bytes from the stream, starting from a specified point in the byte array. - /// - /// The buffer to read data into. - /// The starting point in the buffer at which to begin reading into the buffer. - /// The number of bytes to read. - /// The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached. - public static int ReadReverse(this BinaryReader reader, byte[] buffer, int index, int count) - { - int retval = reader.Read(buffer, index, count); - buffer = buffer.Reverse().ToArray(); - return retval; - } - - /// - /// Reads the specified number of characters from the stream, starting from a specified point in the character array. - /// - /// The buffer to read data into. - /// The starting point in the buffer at which to begin reading into the buffer. - /// The number of characters to read. - /// The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached. - public static int ReadReverse(this BinaryReader reader, char[] buffer, int index, int count) - - { - int retval = reader.Read(buffer, index, count); - buffer = buffer.Reverse().ToArray(); - return retval; - } - - /// - /// Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes. - /// - /// The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur. - /// A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached. - public static byte[] ReadBytesReverse(this BinaryReader reader, int count) - { - byte[] retval = reader.ReadBytes(count); - retval = retval.Reverse().ToArray(); - return retval; - } - - /// - /// Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes. - /// - /// A decimal value read from the current stream. - public static decimal ReadDecimalReverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(16); - retval = retval.Reverse().ToArray(); - - int i1 = BitConverter.ToInt32(retval, 0); - int i2 = BitConverter.ToInt32(retval, 4); - int i3 = BitConverter.ToInt32(retval, 8); - int i4 = BitConverter.ToInt32(retval, 12); - - return new decimal(new int[] { i1, i2, i3, i4 }); - } - - /// - /// eads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes. - /// - /// An 8-byte floating point value read from the current stream. - public static double ReadDoubleReverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - retval = retval.Reverse().ToArray(); - return BitConverter.ToDouble(retval, 0); - } - - /// - /// Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes. - /// - /// A 2-byte signed integer read from the current stream. - public static short ReadInt16Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(2); - retval = retval.Reverse().ToArray(); - return BitConverter.ToInt16(retval, 0); - } - - /// - /// Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. - /// - /// A 4-byte signed integer read from the current stream. - public static int ReadInt32Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - retval = retval.Reverse().ToArray(); - return BitConverter.ToInt32(retval, 0); - } - - /// - /// Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes. - /// - /// An 8-byte signed integer read from the current stream. - public static long ReadInt64Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - retval = retval.Reverse().ToArray(); - return BitConverter.ToInt64(retval, 0); - } - - /// - /// Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes. - /// - /// A 4-byte floating point value read from the current stream. - public static float ReadSingleReverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - retval = retval.Reverse().ToArray(); - return BitConverter.ToSingle(retval, 0); - } - - /// - /// Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes. - /// - /// This API is not CLS-compliant. - /// - /// A 2-byte unsigned integer read from this stream. - public static ushort ReadUInt16Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(2); - retval = retval.Reverse().ToArray(); - return BitConverter.ToUInt16(retval, 0); - } - - /// - /// Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes. - /// - /// This API is not CLS-compliant. - /// - /// A 4-byte unsigned integer read from this stream. - public static uint ReadUInt32Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - retval = retval.Reverse().ToArray(); - return BitConverter.ToUInt32(retval, 0); - } - - /// - /// Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes. - /// - /// This API is not CLS-compliant. - /// - /// An 8-byte unsigned integer read from this stream. - public static ulong ReadUInt64Reverse(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - retval = retval.Reverse().ToArray(); - return BitConverter.ToUInt64(retval, 0); - } - #endregion #region String Manipulation