12 Commits

Author SHA1 Message Date
Matt Nadareski
c125dc4ec0 Bump version 2024-10-24 00:22:28 -04:00
Matt Nadareski
f154ae47c0 Disable warnings, don't ignore them 2024-10-24 00:20:46 -04:00
Matt Nadareski
0d0e960b98 Use converters properly 2024-10-24 00:17:09 -04:00
Matt Nadareski
4a9f84ab66 Add LogLevel enum converters 2024-10-24 00:10:50 -04:00
Matt Nadareski
39277ee443 Add generic byte array extensions 2024-10-24 00:06:58 -04:00
Matt Nadareski
ed367ace6d Import logger from SabreTools 2024-10-24 00:04:50 -04:00
Matt Nadareski
80e72832a4 Add WORD/DWORD extensions, for fun 2024-10-15 20:52:32 -04:00
Matt Nadareski
8924a50432 Bump version 2024-10-01 13:24:53 -04:00
Matt Nadareski
97f00a2565 Update packages 2024-10-01 13:23:04 -04:00
Matt Nadareski
f35231d95b Remove Linq requirement from old .NET 2024-10-01 02:32:38 -04:00
Matt Nadareski
96c6bba93e Bump version 2024-05-13 16:19:17 -04:00
Matt Nadareski
b0d81f225b Fix thrown exception statements 2024-05-12 10:56:15 -04:00
24 changed files with 1143 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
using System.Runtime.InteropServices;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Test.Extensions
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
@@ -10,13 +10,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
@@ -9,6 +11,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>
@@ -71,6 +74,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt16(buffer, 0);
}
/// <summary>
/// Read a WORD (2-byte) from the base stream
/// </summary>
public static ushort ReadWORD(this BinaryReader reader)
=> reader.ReadUInt16();
/// <summary>
/// Read a WORD (2-byte) from the base stream
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static ushort ReadWORDBigEndian(this BinaryReader reader)
=> reader.ReadUInt16BigEndian();
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <inheritdoc cref="BinaryReader.ReadHalf"/>
@@ -152,6 +168,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt32(buffer, 0);
}
/// <summary>
/// Read a DWORD (4-byte) from the base stream
/// </summary>
public static uint ReadDWORD(this BinaryReader reader)
=> reader.ReadUInt32();
/// <summary>
/// Read a DWORD (4-byte) from the base stream
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static uint ReadDWORDBigEndian(this BinaryReader reader)
=> reader.ReadUInt32BigEndian();
/// <inheritdoc cref="BinaryReader.ReadSingle"/>
/// <remarks>Reads in big-endian format</remarks>
public static float ReadSingleBigEndian(this BinaryReader reader)
@@ -638,7 +667,18 @@ namespace SabreTools.IO.Extensions
/// </summary>
private static string? ReadStringType(BinaryReader reader, Encoding encoding, FieldInfo? fi)
{
var marshalAsAttr = fi?.GetCustomAttributes(typeof(MarshalAsAttribute), true)?.FirstOrDefault() as MarshalAsAttribute;
#if NET20 || NET35
var attributes = fi?.GetCustomAttributes(typeof(MarshalAsAttribute), true);
MarshalAsAttribute? marshalAsAttr;
if (attributes == null || attributes.Length == 0)
marshalAsAttr = default;
else
marshalAsAttr = attributes[0] as MarshalAsAttribute;
#else
var marshalAsAttr = fi?
.GetCustomAttributes(typeof(MarshalAsAttribute), true)?
.FirstOrDefault() as MarshalAsAttribute;
#endif
switch (marshalAsAttr?.Value)
{

View File

@@ -7,6 +7,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>

View File

@@ -0,0 +1,53 @@
using System;
namespace SabreTools.IO.Extensions
{
public static class ByteArrayExtensions
{
/// <summary>
/// Convert a byte array to a hex string
/// </summary>
public static string? ByteArrayToString(byte[]? bytes)
{
// If we get null in, we send null out
if (bytes == null)
return null;
try
{
string hex = BitConverter.ToString(bytes);
return hex.Replace("-", string.Empty).ToLowerInvariant();
}
catch
{
return null;
}
}
/// <summary>
/// Convert a hex string to a byte array
/// </summary>
public static byte[]? StringToByteArray(string? hex)
{
// If we get null in, we send null out
if (string.IsNullOrEmpty(hex))
return null;
try
{
int NumberChars = hex!.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
catch
{
return null;
}
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>
@@ -105,6 +106,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt16(buffer, 0);
}
/// <summary>
/// Read a WORD (2-byte) and increment the pointer to an array
/// </summary>
public static ushort ReadWORD(this byte[] content, ref int offset)
=> content.ReadUInt16(ref offset);
/// <summary>
/// Read a WORD (2-byte) and increment the pointer to an array
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static ushort ReadWORDBigEndian(this byte[] content, ref int offset)
=> content.ReadUInt16BigEndian(ref offset);
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -220,6 +234,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt32(buffer, 0);
}
/// <summary>
/// Read a DWORD (4-byte) and increment the pointer to an array
/// </summary>
public static uint ReadDWORD(this byte[] content, ref int offset)
=> content.ReadUInt32(ref offset);
/// <summary>
/// Read a DWORD (4-byte) and increment the pointer to an array
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static uint ReadDWORDBigEndian(this byte[] content, ref int offset)
=> content.ReadUInt32BigEndian(ref offset);
/// <summary>
/// Read a Single and increment the pointer to an array
/// </summary>
@@ -862,7 +889,7 @@ namespace SabreTools.IO.Extensions
{
// If we have an invalid length
if (length < 0)
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value");
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested");
// Handle the 0-byte case
if (length == 0)
@@ -870,7 +897,7 @@ namespace SabreTools.IO.Extensions
// If there are not enough bytes
if (offset + length > content.Length)
throw new System.IO.EndOfStreamException($"Requested to read {nameof(length)} bytes from {nameof(content)}, {content.Length - offset} returned");
throw new System.IO.EndOfStreamException($"Requested to read {length} bytes from {nameof(content)}, {content.Length - offset} returned");
// Handle the general case, forcing a read of the correct length
byte[] buffer = new byte[length];

View File

@@ -6,6 +6,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>

View File

@@ -3,10 +3,9 @@ using System;
#endif
using System.Collections.Generic;
using System.IO;
#if NETCOREAPP3_1_OR_GREATER
using System.IO.Enumeration;
#endif
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text;
namespace SabreTools.IO.Extensions
@@ -124,13 +123,28 @@ namespace SabreTools.IO.Extensions
return null;
// If it does and it is empty, return a blank enumerable
#if NET20 || NET35
if (new List<string>(root!.SafeEnumerateFileSystemEntries("*", SearchOption.AllDirectories)).Count == 0)
#else
if (!root!.SafeEnumerateFileSystemEntries("*", SearchOption.AllDirectories).Any())
#endif
return [];
// Otherwise, get the complete list
#if NET20 || NET35
var empty = new List<string>();
foreach (var dir in root!.SafeEnumerateDirectories("*", SearchOption.AllDirectories))
{
if (new List<string>(dir!.SafeEnumerateFileSystemEntries("*", SearchOption.AllDirectories)).Count == 0)
empty.Add(dir);
}
return empty;
#else
return root!.SafeEnumerateDirectories("*", SearchOption.AllDirectories)
.Where(dir => !dir.SafeEnumerateFileSystemEntries("*", SearchOption.AllDirectories).Any())
.ToList();
#endif
}
#region Safe Directory Enumeration

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
@@ -27,7 +29,7 @@ namespace SabreTools.IO.Extensions
return null;
// Get the first attribute that matches
return attributes.First() as T;
return attributes[0] as T;
}
/// <summary>
@@ -45,7 +47,7 @@ namespace SabreTools.IO.Extensions
return null;
// Get the first attribute that matches
return attributes.First() as T;
return attributes[0] as T;
}
/// <summary>
@@ -115,7 +117,21 @@ namespace SabreTools.IO.Extensions
var nextFields = nextType.GetFields();
foreach (var field in nextFields)
{
#if NET20 || NET35
bool any = false;
foreach (var f in fieldsList)
{
if (f.Name == field.Name && f.FieldType == field.FieldType)
{
any = true;
break;
}
}
if (!any)
#else
if (!fieldsList.Any(f => f.Name == field.Name && f.FieldType == field.FieldType))
#endif
fieldsList.Add(field);
}
}

View File

@@ -8,6 +8,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>
@@ -89,6 +90,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt16(buffer, 0);
}
/// <summary>
/// Read a WORD (2-byte) from the stream
/// </summary>
public static ushort ReadWORD(this Stream stream)
=> stream.ReadUInt16();
/// <summary>
/// Read a WORD (2-byte) from the stream
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static ushort ReadWORDBigEndian(this Stream stream)
=> stream.ReadUInt16BigEndian();
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -204,6 +218,19 @@ namespace SabreTools.IO.Extensions
return BitConverter.ToUInt32(buffer, 0);
}
/// <summary>
/// Read a DWORD (4-byte) from the stream
/// </summary>
public static uint ReadDWORD(this Stream stream)
=> stream.ReadUInt32();
/// <summary>
/// Read a DWORD (4-byte) from the stream
/// </summary>
/// <remarks>Reads in big-endian format</remarks>
public static uint ReadDWORDBigEndian(this Stream stream)
=> stream.ReadUInt32BigEndian();
/// <summary>
/// Read a Single from the stream
/// </summary>
@@ -846,7 +873,7 @@ namespace SabreTools.IO.Extensions
{
// If we have an invalid length
if (length < 0)
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value");
throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested");
// Handle the 0-byte case
if (length == 0)
@@ -856,7 +883,7 @@ namespace SabreTools.IO.Extensions
byte[] buffer = new byte[length];
int read = stream.Read(buffer, 0, length);
if (read < length)
throw new EndOfStreamException($"Requested to read {nameof(length)} bytes from {nameof(stream)}, {read} returned");
throw new EndOfStreamException($"Requested to read {length} bytes from {nameof(stream)}, {read} returned");
return buffer;
}

View File

@@ -7,6 +7,7 @@ using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable CS0618 // Obsolete unmanaged types
namespace SabreTools.IO.Extensions
{
/// <summary>

View File

@@ -2,7 +2,9 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text;
using SabreTools.IO.Readers;
using SabreTools.IO.Writers;
@@ -179,7 +181,17 @@ namespace SabreTools.IO
using IniWriter writer = new(stream, Encoding.UTF8);
// Order the dictionary by keys to link sections together
#if NET20 || NET35
var orderedKeyValuePairs = new List<KeyValuePair<string, string?>>();
foreach (var kvp in _keyValuePairs)
{
orderedKeyValuePairs.Add(kvp);
}
orderedKeyValuePairs.Sort((x, y) => x.Key.CompareTo(y.Key));
#else
var orderedKeyValuePairs = _keyValuePairs.OrderBy(kvp => kvp.Key);
#endif
string section = string.Empty;
foreach (var keyValuePair in orderedKeyValuePairs)
@@ -196,7 +208,13 @@ namespace SabreTools.IO
// If the key contains an '.', we need to put them back in
string newSection = data[0].Trim();
#if NET20 || NET35
string[] dataKey = new string[data.Length - 1];
Array.Copy(data, 1, dataKey, 0, dataKey.Length);
key = string.Join(".", dataKey).Trim();
#else
key = string.Join(".", data.Skip(1).ToArray()).Trim();
#endif
// If we have a new section, write it out
if (!string.Equals(newSection, section, StringComparison.OrdinalIgnoreCase))
@@ -221,9 +239,39 @@ namespace SabreTools.IO
#region IDictionary Impelementations
#if NET20 || NET35
public ICollection<string> Keys
{
get
{
var keys = _keyValuePairs?.Keys;
if (keys == null || keys.Count == 0)
return [];
var keyArr = new string[keys.Count];
keys.CopyTo(keyArr, 0);
return keyArr;
}
}
public ICollection<string?> Values
{
get
{
var values = _keyValuePairs?.Values;
if (values == null || values.Count == 0)
return [];
var valueArr = new string[values.Count];
values.CopyTo(valueArr, 0);
return valueArr;
}
}
#else
public ICollection<string> Keys => _keyValuePairs?.Keys?.ToArray() ?? [];
public ICollection<string?> Values => _keyValuePairs?.Values?.ToArray() ?? [];
#endif
public int Count => (_keyValuePairs as ICollection<KeyValuePair<string, string>>)?.Count ?? 0;

View File

@@ -0,0 +1,47 @@
namespace SabreTools.IO.Logging
{
public static class Converters
{
#region String to Enum
/// <summary>
/// Get the LogLevel value for an input string, if possible
/// </summary>
/// <param name="value">String value to parse/param>
/// <returns></returns>
public static LogLevel AsLogLevel(this string? value)
{
return value?.ToLowerInvariant() switch
{
"verbose" => LogLevel.VERBOSE,
"user" => LogLevel.USER,
"warning" => LogLevel.WARNING,
"error" => LogLevel.ERROR,
_ => LogLevel.VERBOSE,
};
}
#endregion
#region Enum to String
/// <summary>
/// Get string value from input LogLevel
/// </summary>
/// <param name="value">LogLevel to get value from</param>
/// <returns>String corresponding to the LogLevel</returns>
public static string? FromLogLevel(this LogLevel value)
{
return value switch
{
LogLevel.VERBOSE => "VERBOSE",
LogLevel.USER => "USER",
LogLevel.WARNING => "WARNING",
LogLevel.ERROR => "ERROR",
_ => null,
};
}
#endregion
}
}

View File

@@ -0,0 +1,13 @@
namespace SabreTools.IO.Logging
{
/// <summary>
/// Severity of the logging statement
/// </summary>
public enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}
}

View File

@@ -0,0 +1,61 @@
using System;
namespace SabreTools.IO.Logging
{
/// <summary>
/// Stopwatch class for keeping track of duration in the code
/// </summary>
public class InternalStopwatch
{
private string _subject;
private DateTime _startTime;
private readonly Logger _logger;
/// <summary>
/// Constructor that initalizes the stopwatch
/// </summary>
public InternalStopwatch()
{
_subject = string.Empty;
_logger = new Logger(this);
}
/// <summary>
/// Constructor that initalizes the stopwatch with a subject and starts immediately
/// </summary>
/// <param name="subject">Subject of the stopwatch</param>
public InternalStopwatch(string subject)
{
_subject = subject;
_logger = new Logger(this);
Start();
}
/// <summary>
/// Start the stopwatch and display subject text
/// </summary>
public void Start()
{
_startTime = DateTime.Now;
_logger.User($"{_subject}...");
}
/// <summary>
/// Start the stopwatch and display subject text
/// </summary>
/// <param name="subject">Text to show on stopwatch start</param>
public void Start(string subject)
{
_subject = subject;
Start();
}
/// <summary>
/// End the stopwatch and display subject text
/// </summary>
public void Stop()
{
_logger.User($"{_subject} completed in {DateTime.Now.Subtract(_startTime):G}");
}
}
}

View File

@@ -0,0 +1,79 @@
using System;
namespace SabreTools.IO.Logging
{
/// <summary>
/// Generic delegate type for log events
/// </summary>
public delegate void LogEventHandler(object? sender, LogEventArgs args);
/// <summary>
/// Logging specific event arguments
/// </summary>
public class LogEventArgs : EventArgs
{
/// <summary>
/// LogLevel for the event
/// </summary>
public readonly LogLevel LogLevel;
/// <summary>
/// Log statement to be printed
/// </summary>
public readonly string? Statement = null;
/// <summary>
/// Exception to be passed along to the event handler
/// </summary>
public readonly Exception? Exception = null;
/// <summary>
/// Total count for progress log events
/// </summary>
public readonly long? TotalCount = null;
/// <summary>
/// Current count for progress log events
/// </summary>
public readonly long? CurrentCount = null;
/// <summary>
/// Statement constructor
/// </summary>
public LogEventArgs(LogLevel logLevel, string statement)
{
LogLevel = logLevel;
Statement = statement;
}
/// <summary>
/// Statement constructor
/// </summary>
public LogEventArgs(LogLevel logLevel, Exception exception)
{
LogLevel = logLevel;
Exception = exception;
}
/// <summary>
/// Statement and exception constructor
/// </summary>
public LogEventArgs(LogLevel logLevel, string statement, Exception exception)
{
LogLevel = logLevel;
Statement = statement;
Exception = exception;
}
/// <summary>
/// Progress constructor
/// </summary>
public LogEventArgs(long total, long current, LogLevel logLevel, string? statement = null)
{
LogLevel = logLevel;
Statement = statement;
TotalCount = total;
CurrentCount = current;
}
}
}

View File

@@ -0,0 +1,180 @@
using System;
namespace SabreTools.IO.Logging
{
/// <summary>
/// Per-class logging
/// </summary>
public class Logger
{
/// <summary>
/// Instance associated with this logger
/// </summary>
/// TODO: Derive class name for this object, if possible
private readonly object? _instance;
/// <summary>
/// Constructor
/// </summary>
public Logger(object? instance = null)
{
_instance = instance;
}
#region Log Event Triggers
#region Verbose
/// <summary>
/// Write the given string as a verbose message to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Verbose(string output)
=> LoggerImpl.Verbose(_instance, output);
/// <summary>
/// Write the given exception as a verbose message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Verbose(Exception ex)
=> LoggerImpl.Verbose(_instance, ex);
/// <summary>
/// Write the given exception and string as a verbose message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Verbose(Exception ex, string output)
=> LoggerImpl.Verbose(_instance, ex, output);
/// <summary>
/// Write the given verbose progress message to the log output
/// </summary>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public void Verbose(long total, long current, string? output = null)
=> LoggerImpl.Verbose(_instance, total, current, output);
#endregion
#region User
/// <summary>
/// Write the given string as a user message to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void User(string output)
=> LoggerImpl.User(_instance, output);
/// <summary>
/// Write the given exception as a user message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void User(Exception ex)
=> LoggerImpl.User(_instance, ex);
/// <summary>
/// Write the given exception and string as a user message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void User(Exception ex, string output)
=> LoggerImpl.User(_instance, ex, output);
/// <summary>
/// Write the given user progress message to the log output
/// </summary>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public void User(long total, long current, string? output = null)
=> LoggerImpl.User(_instance, total, current, output);
#endregion
#region Warning
/// <summary>
/// Write the given string as a warning to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Warning(string output)
=> LoggerImpl.Warning(_instance, output);
/// <summary>
/// Write the given exception as a warning to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Warning(Exception ex)
=> LoggerImpl.Warning(_instance, ex);
/// <summary>
/// Write the given exception and string as a warning to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Warning(Exception ex, string output)
=> LoggerImpl.Warning(_instance, ex, output);
/// <summary>
/// Write the given warning progress message to the log output
/// </summary>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public void Warning(long total, long current, string? output = null)
=> LoggerImpl.Warning(_instance, total, current, output);
#endregion
#region Error
/// <summary>
/// Writes the given string as an error in the log
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Error(string output)
=> LoggerImpl.Error(_instance, output);
/// <summary>
/// Writes the given exception as an error in the log
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Error(Exception ex)
=> LoggerImpl.Error(_instance, ex);
/// <summary>
/// Writes the given exception and string as an error in the log
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public void Error(Exception ex, string output)
=> LoggerImpl.Error(_instance, ex, output);
/// <summary>
/// Write the given error progress message to the log output
/// </summary>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public void Error(long total, long current, string? output = null)
=> LoggerImpl.Error(_instance, total, current, output);
#endregion
#endregion
}
}

View File

@@ -0,0 +1,458 @@
using System;
using System.IO;
using System.Text;
using SabreTools.IO.Extensions;
namespace SabreTools.IO.Logging
{
/// <summary>
/// Internal logging implementation
/// </summary>
public static class LoggerImpl
{
#region Fields
/// <summary>
/// Optional output filename for logs
/// </summary>
public static string? Filename { get; private set; } = null;
/// <summary>
/// Determines if we're logging to file or not
/// </summary>
public static bool LogToFile { get { return !string.IsNullOrEmpty(Filename); } }
/// <summary>
/// Optional output log directory
/// </summary>
public static string? LogDirectory { get; private set; } = null;
/// <summary>
/// Determines the lowest log level to output
/// </summary>
public static LogLevel LowestLogLevel { get; set; } = LogLevel.VERBOSE;
/// <summary>
/// Determines whether to prefix log lines with level and datetime
/// </summary>
public static bool AppendPrefix { get; set; } = true;
/// <summary>
/// Determines whether to throw if an exception is logged
/// </summary>
public static bool ThrowOnError { get; set; } = false;
/// <summary>
/// Logging start time for metrics
/// </summary>
public static DateTime StartTime { get; private set; }
/// <summary>
/// Determines if there were errors logged
/// </summary>
public static bool LoggedErrors { get; private set; } = false;
/// <summary>
/// Determines if there were warnings logged
/// </summary>
public static bool LoggedWarnings { get; private set; } = false;
#endregion
#region Private variables
/// <summary>
/// StreamWriter representing the output log file
/// </summary>
private static StreamWriter? _log;
/// <summary>
/// Object lock for multithreaded logging
/// </summary>
private static readonly object _lock = new();
#endregion
#region Control
/// <summary>
/// Generate and set the log filename
/// </summary>
/// <param name="filename">Base filename to use</param>
/// <param name="addDate">True to append a date to the filename, false otherwise</param>
public static void SetFilename(string filename, bool addDate = true)
{
// Get the full log path
string fullPath = Path.GetFullPath(filename);
// Set the log directory
LogDirectory = Path.GetDirectoryName(fullPath);
// Set the
if (addDate)
Filename = $"{Path.GetFileNameWithoutExtension(fullPath)} ({DateTime.Now:yyyy-MM-dd HH-mm-ss}).{fullPath.GetNormalizedExtension()}";
else
Filename = Path.GetFileName(fullPath);
}
/// <summary>
/// Start logging by opening output file (if necessary)
/// </summary>
/// <returns>True if the logging was started correctly, false otherwise</returns>
public static bool Start()
{
// Setup the logging handler to always use the internal log
LogEventHandler += HandleLogEvent;
// Start the logging
StartTime = DateTime.Now;
if (!LogToFile)
return true;
// Setup file output and perform initial log
try
{
if (!string.IsNullOrEmpty(LogDirectory) && !Directory.Exists(LogDirectory))
Directory.CreateDirectory(LogDirectory);
FileStream logfile = File.Create(Path.Combine(LogDirectory ?? string.Empty, Filename ?? string.Empty));
#if NET20 || NET35 || NET40
_log = new StreamWriter(logfile, Encoding.UTF8, 4096)
#else
_log = new StreamWriter(logfile, Encoding.UTF8, 4096, true)
#endif
{
AutoFlush = true
};
_log.WriteLine($"Logging started {StartTime:yyyy-MM-dd HH:mm:ss}");
_log.WriteLine($"Command run: {string.Join(" ", Environment.GetCommandLineArgs())}");
}
catch
{
return false;
}
return true;
}
/// <summary>
/// End logging by closing output file (if necessary)
/// </summary>
/// <param name="suppress">True if all ending output is to be suppressed, false otherwise (default)</param>
/// <returns>True if the logging was ended correctly, false otherwise</returns>
public static bool Close(bool suppress = false)
{
if (!suppress)
{
if (LoggedWarnings)
Console.WriteLine("There were warnings in the last run! Check the log for more details");
if (LoggedErrors)
Console.WriteLine("There were errors in the last run! Check the log for more details");
TimeSpan span = DateTime.Now.Subtract(StartTime);
#if NET20 || NET35
string total = span.ToString();
#else
// Special case for multi-day runs
string total;
if (span >= TimeSpan.FromDays(1))
total = span.ToString(@"d\:hh\:mm\:ss");
else
total = span.ToString(@"hh\:mm\:ss");
#endif
if (!LogToFile)
{
Console.WriteLine($"Total runtime: {total}");
return true;
}
try
{
_log?.WriteLine($"Logging ended {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
_log?.WriteLine($"Total runtime: {total}");
Console.WriteLine($"Total runtime: {total}");
_log?.Close();
}
catch
{
return false;
}
}
else
{
try
{
_log?.Close();
}
catch
{
return false;
}
}
return true;
}
#endregion
#region Event Handling
/// <summary>
/// Handler for log events
/// </summary>
public static event LogEventHandler LogEventHandler = delegate { };
/// <summary>
/// Default log event handling
/// </summary>
public static void HandleLogEvent(object? sender, LogEventArgs args)
{
// Null args means we can't handle it
if (args == null)
return;
// If we have an exception and we're throwing on that
if (ThrowOnError && args.Exception != null)
throw args.Exception;
// If we have a warning or error, set the flags accordingly
if (args.LogLevel == LogLevel.WARNING)
LoggedWarnings = true;
if (args.LogLevel == LogLevel.ERROR)
LoggedErrors = true;
// Setup the statement based on the inputs
string logLine;
if (args.Exception != null)
{
logLine = $"{(args.Statement != null ? args.Statement + ": " : string.Empty)}{args.Exception}";
}
else if (args.TotalCount != null && args.CurrentCount != null)
{
double percentage = ((double)args.CurrentCount.Value / args.TotalCount.Value) * 100;
logLine = $"{percentage:N2}%{(args.Statement != null ? ": " + args.Statement : string.Empty)}";
}
else
{
logLine = args.Statement ?? string.Empty;
}
// Then write to the log
Log(logLine, args.LogLevel);
}
/// <summary>
/// Write the given string to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <param name="loglevel">Severity of the information being logged</param>
private static void Log(string output, LogLevel loglevel)
{
// If the log level is less than the filter level, we skip it but claim we didn't
if (loglevel < LowestLogLevel)
return;
// USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
Console.WriteLine((loglevel == LogLevel.ERROR && AppendPrefix ? loglevel.FromLogLevel() + " " : string.Empty) + output);
// If we're writing to file, use the existing stream
if (LogToFile)
{
try
{
lock (_lock)
{
_log?.WriteLine((AppendPrefix ? $"{loglevel.FromLogLevel()} - {DateTime.Now} - " : string.Empty) + output);
}
}
catch (Exception ex) when (ThrowOnError)
{
Console.WriteLine(ex);
Console.WriteLine("Could not write to log file!");
return;
}
}
return;
}
#endregion
#region Log Event Triggers
#region Verbose
/// <summary>
/// Write the given string as a verbose message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Verbose(object? instance, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.VERBOSE, output));
/// <summary>
/// Write the given exception as a verbose message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Verbose(object? instance, Exception ex)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.VERBOSE, ex));
/// <summary>
/// Write the given exception and string as a verbose message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Verbose(object? instance, Exception ex, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.VERBOSE, output, ex));
/// <summary>
/// Write the given verbose progress message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public static void Verbose(object? instance, long total, long current, string? output = null)
=> LogEventHandler(instance, new LogEventArgs(total, current, LogLevel.VERBOSE, output));
#endregion
#region User
/// <summary>
/// Write the given string as a user message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void User(object? instance, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.USER, output));
/// <summary>
/// Write the given exception as a user message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void User(object? instance, Exception ex)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.USER, ex));
/// <summary>
/// Write the given exception and string as a user message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void User(object? instance, Exception ex, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.USER, output, ex));
/// <summary>
/// Write the given user progress message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public static void User(object? instance, long total, long current, string? output = null)
=> LogEventHandler(instance, new LogEventArgs(total, current, LogLevel.USER, output));
#endregion
#region Warning
/// <summary>
/// Write the given string as a warning to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Warning(object? instance, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.WARNING, output));
/// <summary>
/// Write the given exception as a warning to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Warning(object? instance, Exception ex)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.WARNING, ex));
//// <summary>
/// Write the given exception and string as a warning to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Warning(object? instance, Exception ex, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.WARNING, output, ex));
/// <summary>
/// Write the given warning progress message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public static void Warning(object? instance, long total, long current, string? output = null)
=> LogEventHandler(instance, new LogEventArgs(total, current, LogLevel.WARNING, output));
#endregion
#region Error
/// <summary>
/// Writes the given string as an error in the log
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Error(object? instance, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.ERROR, output));
/// <summary>
/// Writes the given exception as an error in the log
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Error(object? instance, Exception ex)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.ERROR, ex));
/// <summary>
/// Writes the given exception and string as an error in the log
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public static void Error(object? instance, Exception ex, string output)
=> LogEventHandler(instance, new LogEventArgs(LogLevel.ERROR, output, ex));
/// <summary>
/// Write the given error progress message to the log output
/// </summary>
/// <param name="instance">Instance object that's the source of logging</param>
/// <param name="total">Total count for progress</param>
/// <param name="current">Current count for progres</param>
/// <param name="output">String to be written log</param>
public static void Error(object? instance, long total, long current, string? output = null)
=> LogEventHandler(instance, new LogEventArgs(total, current, LogLevel.ERROR, output));
#endregion
#endregion
}
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Extensions;
using SabreTools.Matching;
using SabreTools.Matching.Compare;
namespace SabreTools.IO
{

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text;
using System.Text.RegularExpressions;
@@ -295,11 +297,22 @@ namespace SabreTools.IO.Readers
s = s.Trim();
// Now we get each string, divided up as cleanly as possible
#if NET20 || NET35
var matchList = Regex.Matches(s, InternalPatternAttributesCMP);
var matchStrings = new List<string>();
foreach (Match m in matchList)
{
matchStrings.Add(m.Groups[0].Value);
}
string[] matches = matchStrings.ToArray();
#else
string[] matches = Regex
.Matches(s, InternalPatternAttributesCMP)
.Cast<Match>()
.Select(m => m.Groups[0].Value)
.ToArray();
#endif
return matches;
}

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text;
namespace SabreTools.IO.Readers
@@ -118,7 +120,13 @@ namespace SabreTools.IO.Readers
// If the value field contains an '=', we need to put them back in
string key = data[0].Trim();
#if NET20 || NET35
var valueArr = new string[data.Length - 1];
Array.Copy(data, 1, valueArr, 0, valueArr.Length);
string value = string.Join("=", valueArr).Trim();
#else
string value = string.Join("=", data.Skip(1).ToArray()).Trim();
#endif
KeyValuePair = new KeyValuePair<string, string>(key, value);
RowType = IniRowType.KeyValue;

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text;
using System.Text.RegularExpressions;
@@ -124,7 +126,11 @@ namespace SabreTools.IO.Readers
// https://stackoverflow.com/questions/3776458/split-a-comma-separated-string-with-both-quoted-and-unquoted-strings
var lineSplitRegex = new Regex($"(?:^|{Separator})(\"(?:[^\"]+|\"\")*\"|[^{Separator}]*)");
var temp = new List<string>();
#if NET20 || NET35
foreach (Match? match in lineSplitRegex.Matches(fullLine))
#else
foreach (Match? match in lineSplitRegex.Matches(fullLine).Cast<Match?>())
#endif
{
string? curr = match?.Value;
if (curr == null)
@@ -143,7 +149,17 @@ namespace SabreTools.IO.Readers
// Otherwise, just split on the delimiter
else
{
Line = fullLine.Split(Separator).Select(f => f.Trim()).ToList();
#if NET20 || NET35
Line = new List<string>();
foreach (string f in fullLine.Split(Separator))
{
Line.Add(f.Trim());
}
#else
Line = fullLine.Split(Separator)
.Select(f => f.Trim())
.ToList();
#endif
}
// If we don't have a header yet and are expecting one, read this as the header

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Assembly Properties -->
@@ -7,8 +7,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.4.10</Version>
<WarningsNotAsErrors>CS0618</WarningsNotAsErrors>
<Version>1.4.13</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -23,16 +22,11 @@
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath=""/>
</ItemGroup>
<!-- Support for old .NET versions -->
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))">
<PackageReference Include="Net30.LinqBridge" Version="1.3.0" />
<None Include="../README.md" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Matching" Version="1.3.2" />
</ItemGroup>
</Project>

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
namespace SabreTools.IO.Streams
{
@@ -110,7 +112,11 @@ namespace SabreTools.IO.Streams
/// </summary>
public ReadOnlyCompositeStream(IEnumerable<Stream> streams)
{
#if NET20 || NET35
_streams = new List<Stream>(streams);
#else
_streams = streams.ToList();
#endif
_length = 0;
_position = 0;
@@ -148,7 +154,7 @@ namespace SabreTools.IO.Streams
public override int Read(byte[] buffer, int offset, int count)
{
// Determine which stream we start reading from
(int streamIndex, long streamOffset) = DetermineStreamIndex(_position);
int streamIndex = DetermineStreamIndex(_position, out long streamOffset);
if (streamIndex == -1)
return 0;
@@ -227,12 +233,16 @@ namespace SabreTools.IO.Streams
/// <summary>
/// Determine the index of the stream that contains a particular offset
/// </summary>
/// <returns>Index of the stream containing the offset and the real offset in the stream, (-1, -1) on error</returns>
private (int index, long realOffset) DetermineStreamIndex(long offset)
/// <param name="realOffset">Output parameter representing the real offset in the stream, -1 on error</param>
/// <returns>Index of the stream containing the offset, -1 on error</returns>
private int DetermineStreamIndex(long offset, out long realOffset)
{
// If the offset is out of bounds
if (offset < 0 || offset >= _length)
return (-1, -1);
{
realOffset = -1;
return -1;
}
// Seek through until we hit the correct offset
long currentLength = 0;
@@ -241,13 +251,14 @@ namespace SabreTools.IO.Streams
currentLength += _streams[i].Length;
if (currentLength > offset)
{
long realOffset = offset - (currentLength - _streams[i].Length);
return (i, realOffset);
realOffset = offset - (currentLength - _streams[i].Length);
return i;
}
}
// Should never happen
return (-1, -1);
realOffset = -1;
return -1;
}
/// <summary>