mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-02-08 13:49:55 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c482ab98b | ||
|
|
8aaac551eb | ||
|
|
026e2ee052 | ||
|
|
9b3553e43f | ||
|
|
5221546af9 | ||
|
|
658df0e91c | ||
|
|
fd75f122d1 |
@@ -15,11 +15,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static byte ReadByte(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 1);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 1);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -29,11 +25,7 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Read a UInt8[] and increment the pointer to an array
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static byte[] ReadBytes(this byte[] content, ref int offset, int count)
|
||||
#else
|
||||
public static byte[]? ReadBytes(this byte[]? content, ref int offset, int count)
|
||||
#endif
|
||||
{
|
||||
// If the byte array is invalid, don't do anything
|
||||
if (content == null)
|
||||
@@ -64,11 +56,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static sbyte ReadSByte(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 1);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 1);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -80,11 +68,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static char ReadChar(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 1);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 1);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -96,11 +80,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static short ReadInt16(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 2);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 2);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -112,11 +92,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static short ReadInt16BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 2);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 2);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -129,11 +105,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static ushort ReadUInt16(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 2);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 2);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -145,11 +117,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static ushort ReadUInt16BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 2);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 2);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -162,11 +130,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static int ReadInt32(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 4);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 4);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -178,11 +142,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static int ReadInt32BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 4);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 4);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -195,11 +155,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static uint ReadUInt32(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 4);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 4);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -211,11 +167,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static uint ReadUInt32BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 4);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 4);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -228,11 +180,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static long ReadInt64(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 8);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 8);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -244,11 +192,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static long ReadInt64BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 8);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 8);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -261,11 +205,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static ulong ReadUInt64(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 8);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 8);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -277,11 +217,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static ulong ReadUInt64BigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 8);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 8);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -294,11 +230,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static Guid ReadGuid(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 16);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 16);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -310,11 +242,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
public static Guid ReadGuidBigEndian(this byte[] content, ref int offset)
|
||||
{
|
||||
#if NET48
|
||||
byte[] buffer = content.ReadBytes(ref offset, 16);
|
||||
#else
|
||||
byte[]? buffer = content.ReadBytes(ref offset, 16);
|
||||
#endif
|
||||
if (buffer == null)
|
||||
return default;
|
||||
|
||||
@@ -325,20 +253,12 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Read a null-terminated string from the stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static string ReadString(this byte[] content, ref int offset) => content.ReadString(ref offset, Encoding.Default);
|
||||
#else
|
||||
public static string? ReadString(this byte[] content, ref int offset) => content.ReadString(ref offset, Encoding.Default);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Read a null-terminated string from the stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static string ReadString(this byte[] content, ref int offset, Encoding encoding)
|
||||
#else
|
||||
public static string? ReadString(this byte[] content, ref int offset, Encoding encoding)
|
||||
#endif
|
||||
{
|
||||
if (offset >= content.Length)
|
||||
return null;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace SabreTools.IO
|
||||
public static string Ensure(this string dir, bool create = false)
|
||||
{
|
||||
// If the output directory is invalid
|
||||
if (string.IsNullOrWhiteSpace(dir))
|
||||
if (string.IsNullOrEmpty(dir))
|
||||
dir = PathTool.GetRuntimeDirectory();
|
||||
|
||||
// Get the full path for the output directory
|
||||
@@ -60,7 +60,7 @@ namespace SabreTools.IO
|
||||
file.Dispose();
|
||||
|
||||
// Disable warning about UTF7 usage
|
||||
#pragma warning disable SYSLIB0001
|
||||
#pragma warning disable SYSLIB0001
|
||||
|
||||
// Analyze the BOM
|
||||
if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7;
|
||||
@@ -70,7 +70,7 @@ namespace SabreTools.IO
|
||||
if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
|
||||
return Encoding.Default;
|
||||
|
||||
#pragma warning restore SYSLIB0001
|
||||
#pragma warning restore SYSLIB0001
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -83,29 +83,21 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
/// <param name="path">Path to get extension from</param>
|
||||
/// <returns>Extension, if possible</returns>
|
||||
#if NET48
|
||||
public static string GetNormalizedExtension(this string path)
|
||||
#else
|
||||
public static string? GetNormalizedExtension(this string? path)
|
||||
#endif
|
||||
{
|
||||
// Check null or empty first
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
if (string.IsNullOrEmpty(path))
|
||||
return null;
|
||||
|
||||
// Get the extension from the path, if possible
|
||||
#if NET48
|
||||
string ext = Path.GetExtension(path)?.ToLowerInvariant();
|
||||
#else
|
||||
string? ext = Path.GetExtension(path)?.ToLowerInvariant();
|
||||
#endif
|
||||
|
||||
// Check if the extension is null or empty
|
||||
if (string.IsNullOrWhiteSpace(ext))
|
||||
if (string.IsNullOrEmpty(ext))
|
||||
return null;
|
||||
|
||||
// Make sure that extensions are valid
|
||||
ext = ext.TrimStart('.');
|
||||
ext = ext!.TrimStart('.');
|
||||
|
||||
return ext;
|
||||
}
|
||||
@@ -115,11 +107,7 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
/// <param name="root">Root directory to parse</param>
|
||||
/// <returns>IEumerable containing all directories that are empty, an empty enumerable if the root is empty, null otherwise</returns>
|
||||
#if NET48
|
||||
public static List<string> ListEmpty(this string root)
|
||||
#else
|
||||
public static List<string>? ListEmpty(this string? root)
|
||||
#endif
|
||||
{
|
||||
// Check null or empty first
|
||||
if (string.IsNullOrEmpty(root))
|
||||
|
||||
100
IniFile.cs
100
IniFile.cs
@@ -12,32 +12,16 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Key-value pair INI file
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public class IniFile : IDictionary<string, string>
|
||||
#else
|
||||
public class IniFile : IDictionary<string, string?>
|
||||
#endif
|
||||
{
|
||||
#if NET48
|
||||
private Dictionary<string, string> _keyValuePairs = new Dictionary<string, string>();
|
||||
#else
|
||||
private Dictionary<string, string?>? _keyValuePairs = new Dictionary<string, string?>();
|
||||
#endif
|
||||
|
||||
#if NET48
|
||||
public string this[string key]
|
||||
#else
|
||||
public string? this[string? key]
|
||||
#endif
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_keyValuePairs == null)
|
||||
#if NET48
|
||||
_keyValuePairs = new Dictionary<string, string>();
|
||||
#else
|
||||
_keyValuePairs = new Dictionary<string, string?>();
|
||||
#endif
|
||||
|
||||
key = key?.ToLowerInvariant() ?? string.Empty;
|
||||
if (_keyValuePairs.ContainsKey(key))
|
||||
@@ -48,11 +32,7 @@ namespace SabreTools.IO
|
||||
set
|
||||
{
|
||||
if (_keyValuePairs == null)
|
||||
#if NET48
|
||||
_keyValuePairs = new Dictionary<string, string>();
|
||||
#else
|
||||
_keyValuePairs = new Dictionary<string, string?>();
|
||||
#endif
|
||||
|
||||
key = key?.ToLowerInvariant() ?? string.Empty;
|
||||
_keyValuePairs[key] = value;
|
||||
@@ -122,11 +102,7 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Read an INI file from a stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public bool Parse(Stream stream)
|
||||
#else
|
||||
public bool Parse(Stream? stream)
|
||||
#endif
|
||||
{
|
||||
// If the stream is invalid or unreadable, we can't process it
|
||||
if (stream == null || !stream.CanRead || stream.Position >= stream.Length - 1)
|
||||
@@ -138,11 +114,7 @@ namespace SabreTools.IO
|
||||
using (var reader = new IniReader(stream, Encoding.UTF8))
|
||||
{
|
||||
// TODO: Can we use the section header in the reader?
|
||||
#if NET48
|
||||
string section = string.Empty;
|
||||
#else
|
||||
string? section = string.Empty;
|
||||
#endif
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
// If we dont have a next line
|
||||
@@ -157,11 +129,7 @@ namespace SabreTools.IO
|
||||
break;
|
||||
|
||||
case IniRowType.KeyValue:
|
||||
#if NET48
|
||||
string key = reader.KeyValuePair?.Key;
|
||||
#else
|
||||
string? key = reader.KeyValuePair?.Key;
|
||||
#endif
|
||||
|
||||
// Section names are prepended to the key with a '.' separating
|
||||
if (!string.IsNullOrEmpty(section))
|
||||
@@ -227,21 +195,17 @@ namespace SabreTools.IO
|
||||
{
|
||||
// Extract the key and value
|
||||
string key = keyValuePair.Key;
|
||||
#if NET48
|
||||
string value = keyValuePair.Value;
|
||||
#else
|
||||
string? value = keyValuePair.Value;
|
||||
#endif
|
||||
|
||||
// We assume '.' is a section name separator
|
||||
if (key.Contains('.'))
|
||||
if (key.Contains("."))
|
||||
{
|
||||
// Split the key by '.'
|
||||
string[] data = keyValuePair.Key.Split('.');
|
||||
|
||||
// If the key contains an '.', we need to put them back in
|
||||
string newSection = data[0].Trim();
|
||||
key = string.Join(".", data.Skip(1)).Trim();
|
||||
key = string.Join(".", data.Skip(1).ToArray()).Trim();
|
||||
|
||||
// If we have a new section, write it out
|
||||
if (!string.Equals(newSection, section, StringComparison.OrdinalIgnoreCase))
|
||||
@@ -267,61 +231,18 @@ namespace SabreTools.IO
|
||||
|
||||
#region IDictionary Impelementations
|
||||
|
||||
#if NET48
|
||||
public ICollection<string> Keys => _keyValuePairs?.Keys;
|
||||
|
||||
public ICollection<string> Values => _keyValuePairs?.Values;
|
||||
|
||||
public int Count => _keyValuePairs?.Count ?? 0;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public void Add(string key, string value) => this[key] = value;
|
||||
|
||||
bool IDictionary<string, string>.Remove(string key) => Remove(key);
|
||||
|
||||
public bool TryGetValue(string key, out string value)
|
||||
{
|
||||
value = null;
|
||||
return _keyValuePairs?.TryGetValue(key.ToLowerInvariant(), out value) ?? false;
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<string, string> item) => this[item.Key] = item.Value;
|
||||
|
||||
public void Clear() => _keyValuePairs?.Clear();
|
||||
|
||||
public bool Contains(KeyValuePair<string, string> item)
|
||||
{
|
||||
var newItem = new KeyValuePair<string, string>(item.Key.ToLowerInvariant(), item.Value);
|
||||
return (_keyValuePairs as ICollection<KeyValuePair<string, string>>)?.Contains(newItem) ?? false;
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key) => _keyValuePairs?.ContainsKey(key?.ToLowerInvariant() ?? string.Empty) ?? false;
|
||||
|
||||
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
|
||||
{
|
||||
(_keyValuePairs as ICollection<KeyValuePair<string, string>>)?.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<string, string> item)
|
||||
{
|
||||
var newItem = new KeyValuePair<string, string>(item.Key.ToLowerInvariant(), item.Value);
|
||||
return (_keyValuePairs as ICollection<KeyValuePair<string, string>>)?.Remove(newItem) ?? false;
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
|
||||
{
|
||||
return (_keyValuePairs as IEnumerable<KeyValuePair<string, string>>)?.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return (_keyValuePairs as IEnumerable)?.GetEnumerator();
|
||||
}
|
||||
#if NET40 || NET452
|
||||
public ICollection<string> Keys => _keyValuePairs?.Keys?.ToArray() ?? new string[0];
|
||||
#else
|
||||
public ICollection<string> Keys => _keyValuePairs?.Keys?.ToArray() ?? Array.Empty<string>();
|
||||
#endif
|
||||
|
||||
|
||||
#if NET40 || NET452
|
||||
public ICollection<string?> Values => _keyValuePairs?.Values?.ToArray() ?? new string[0];
|
||||
#else
|
||||
public ICollection<string?> Values => _keyValuePairs?.Values?.ToArray() ?? Array.Empty<string?>();
|
||||
#endif
|
||||
|
||||
public int Count => (_keyValuePairs as ICollection<KeyValuePair<string, string>>)?.Count ?? 0;
|
||||
|
||||
@@ -369,7 +290,6 @@ namespace SabreTools.IO
|
||||
{
|
||||
return (_keyValuePairs as IEnumerable)!.GetEnumerator();
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -31,11 +31,7 @@ namespace NaturalSort
|
||||
table.Clear();
|
||||
}
|
||||
|
||||
#if NET48
|
||||
public override int Compare(string x, string y)
|
||||
#else
|
||||
public override int Compare(string? x, string? y)
|
||||
#endif
|
||||
{
|
||||
if (x == null || y == null)
|
||||
{
|
||||
@@ -50,24 +46,16 @@ namespace NaturalSort
|
||||
{
|
||||
return x.CompareTo(y);
|
||||
}
|
||||
#if NET48
|
||||
if (!table.TryGetValue(x, out string[] x1))
|
||||
#else
|
||||
if (!table.TryGetValue(x, out string[]? x1))
|
||||
#endif
|
||||
{
|
||||
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
|
||||
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
|
||||
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
table.Add(x, x1);
|
||||
}
|
||||
#if NET48
|
||||
if (!table.TryGetValue(y, out string[] y1))
|
||||
#else
|
||||
if (!table.TryGetValue(y, out string[]? y1))
|
||||
#endif
|
||||
{
|
||||
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
table.Add(y, y1);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,11 +31,7 @@ namespace NaturalSort
|
||||
table.Clear();
|
||||
}
|
||||
|
||||
#if NET48
|
||||
public override int Compare(string x, string y)
|
||||
#else
|
||||
public override int Compare(string? x, string? y)
|
||||
#endif
|
||||
{
|
||||
if (x == null || y == null)
|
||||
{
|
||||
@@ -50,24 +46,16 @@ namespace NaturalSort
|
||||
{
|
||||
return y.CompareTo(x);
|
||||
}
|
||||
#if NET48
|
||||
if (!table.TryGetValue(x, out string[] x1))
|
||||
#else
|
||||
if (!table.TryGetValue(x, out string[]? x1))
|
||||
#endif
|
||||
{
|
||||
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
|
||||
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
|
||||
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
table.Add(x, x1);
|
||||
}
|
||||
#if NET48
|
||||
if (!table.TryGetValue(y, out string[] y1))
|
||||
#else
|
||||
if (!table.TryGetValue(y, out string[]? y1))
|
||||
#endif
|
||||
{
|
||||
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
table.Add(y, y1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
20
OldDotNet.cs
Normal file
20
OldDotNet.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#if NET40
|
||||
|
||||
namespace SabreTools.IO
|
||||
{
|
||||
internal delegate U LinqOrderByDelegate<T, U>(T str);
|
||||
|
||||
internal static partial class EnumerationExtensions
|
||||
{
|
||||
public static IEnumerable<T> OrderBy<T, U>(this IEnumerable<T> arr, LinqOrderByDelegate<T, U> func)
|
||||
{
|
||||
// TODO: Implement ordering
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,26 +11,14 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Current full path represented
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string CurrentPath { get; private set; }
|
||||
#else
|
||||
public string CurrentPath { get; init; }
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Possible parent path represented (may be null or empty)
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string ParentPath { get; private set; }
|
||||
#else
|
||||
public string? ParentPath { get; init; }
|
||||
#endif
|
||||
public string? ParentPath { get; private set; }
|
||||
|
||||
#if NET48
|
||||
public ParentablePath(string currentPath, string parentPath = null)
|
||||
#else
|
||||
public ParentablePath(string currentPath, string? parentPath = null)
|
||||
#endif
|
||||
{
|
||||
CurrentPath = currentPath;
|
||||
ParentPath = parentPath;
|
||||
@@ -41,22 +29,18 @@ namespace SabreTools.IO
|
||||
/// </summary>
|
||||
/// <param name="sanitize">True if path separators should be converted to '-', false otherwise</param>
|
||||
/// <returns>Subpath for the file</returns>
|
||||
#if NET48
|
||||
public string GetNormalizedFileName(bool sanitize)
|
||||
#else
|
||||
public string? GetNormalizedFileName(bool sanitize)
|
||||
#endif
|
||||
{
|
||||
// If the current path is empty, we can't do anything
|
||||
if (string.IsNullOrWhiteSpace(CurrentPath))
|
||||
if (string.IsNullOrEmpty(CurrentPath))
|
||||
return null;
|
||||
|
||||
// Assume the current path is the filename
|
||||
string filename = Path.GetFileName(CurrentPath);
|
||||
|
||||
// If we have a true ParentPath, remove it from CurrentPath and return the remainder
|
||||
if (!string.IsNullOrWhiteSpace(ParentPath) && !string.Equals(CurrentPath, ParentPath, StringComparison.Ordinal))
|
||||
filename = CurrentPath.Remove(0, ParentPath.Length + 1);
|
||||
if (string.IsNullOrEmpty(ParentPath) && !string.Equals(CurrentPath, ParentPath, StringComparison.Ordinal))
|
||||
filename = CurrentPath.Remove(0, ParentPath!.Length + 1);
|
||||
|
||||
// If we're sanitizing the path after, do so
|
||||
if (sanitize)
|
||||
@@ -71,22 +55,18 @@ namespace SabreTools.IO
|
||||
/// <param name="outDir">Output directory to use</param>
|
||||
/// <param name="inplace">True if the output file should go to the same input folder, false otherwise</param>
|
||||
/// <returns>Complete output path</returns>
|
||||
#if NET48
|
||||
public string GetOutputPath(string outDir, bool inplace)
|
||||
#else
|
||||
public string? GetOutputPath(string outDir, bool inplace)
|
||||
#endif
|
||||
{
|
||||
// If the current path is empty, we can't do anything
|
||||
if (string.IsNullOrWhiteSpace(CurrentPath))
|
||||
if (string.IsNullOrEmpty(CurrentPath))
|
||||
return null;
|
||||
|
||||
// If the output dir is empty (and we're not inplace), we can't do anything
|
||||
if (string.IsNullOrWhiteSpace(outDir) && !inplace)
|
||||
if (string.IsNullOrEmpty(outDir) && !inplace)
|
||||
return null;
|
||||
|
||||
// Check if we have a split path or not
|
||||
bool splitpath = !string.IsNullOrWhiteSpace(ParentPath);
|
||||
bool splitpath = !string.IsNullOrEmpty(ParentPath);
|
||||
|
||||
// If we have an inplace output, use the directory name from the input path
|
||||
if (inplace)
|
||||
@@ -105,15 +85,9 @@ namespace SabreTools.IO
|
||||
workingParent = Path.GetDirectoryName(ParentPath ?? string.Empty) ?? string.Empty;
|
||||
|
||||
// Determine the correct subfolder based on the working parent directory
|
||||
#if NET48
|
||||
int extraLength = workingParent.EndsWith(":")
|
||||
|| workingParent.EndsWith(Path.DirectorySeparatorChar.ToString())
|
||||
|| workingParent.EndsWith(Path.AltDirectorySeparatorChar.ToString()) ? 0 : 1;
|
||||
#else
|
||||
int extraLength = workingParent.EndsWith(':')
|
||||
|| workingParent.EndsWith(Path.DirectorySeparatorChar)
|
||||
|| workingParent.EndsWith(Path.AltDirectorySeparatorChar) ? 0 : 1;
|
||||
#endif
|
||||
|
||||
return Path.GetDirectoryName(Path.Combine(outDir, CurrentPath.Remove(0, workingParent.Length + extraLength)));
|
||||
}
|
||||
|
||||
14
PathTool.cs
14
PathTool.cs
@@ -29,14 +29,10 @@ namespace SabreTools.IO
|
||||
|
||||
// If we have a wildcard
|
||||
string pattern = "*";
|
||||
if (input.Contains('*') || input.Contains('?'))
|
||||
if (input.Contains("*") || input.Contains("?"))
|
||||
{
|
||||
pattern = Path.GetFileName(input);
|
||||
#if NET48
|
||||
input = input.Substring(0, input.Length - pattern.Length);
|
||||
#else
|
||||
input = input[..^pattern.Length];
|
||||
#endif
|
||||
}
|
||||
|
||||
// Get the parent path in case of appending
|
||||
@@ -108,14 +104,10 @@ namespace SabreTools.IO
|
||||
|
||||
// If we have a wildcard
|
||||
string pattern = "*";
|
||||
if (input.Contains('*') || input.Contains('?'))
|
||||
if (input.Contains("*") || input.Contains("?"))
|
||||
{
|
||||
pattern = Path.GetFileName(input);
|
||||
#if NET48
|
||||
input = input.Substring(0, input.Length - pattern.Length);
|
||||
#else
|
||||
input = input[..^pattern.Length];
|
||||
#endif
|
||||
}
|
||||
|
||||
// Get the parent path in case of appending
|
||||
@@ -173,7 +165,7 @@ namespace SabreTools.IO
|
||||
// Return the new list
|
||||
return infiles;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get the current runtime directory
|
||||
/// </summary>
|
||||
|
||||
@@ -23,20 +23,12 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Internal stream reader for inputting
|
||||
/// </summary>
|
||||
#if NET48
|
||||
private readonly StreamReader sr;
|
||||
#else
|
||||
private readonly StreamReader? sr;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Contents of the current line, unprocessed
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string CurrentLine { get; private set; } = string.Empty;
|
||||
#else
|
||||
public string? CurrentLine { get; private set; } = string.Empty;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get the current line number
|
||||
@@ -57,20 +49,12 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Contents of the currently read line as an internal item
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public Dictionary<string, string> Internal { get; private set; } = new Dictionary<string, string>();
|
||||
#else
|
||||
public Dictionary<string, string>? Internal { get; private set; } = new Dictionary<string, string>();
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Current internal item name
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string InternalName { get; private set; }
|
||||
#else
|
||||
public string? InternalName { get; private set; }
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get if we should be making DosCenter exceptions
|
||||
@@ -101,11 +85,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Current top-level being read
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string TopLevel { get; private set; } = string.Empty;
|
||||
#else
|
||||
public string? TopLevel { get; private set; } = string.Empty;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for opening a write from a file
|
||||
@@ -151,11 +131,7 @@ namespace SabreTools.IO.Readers
|
||||
// Standalone (special case for DC dats)
|
||||
if (CurrentLine.StartsWith("Name:"))
|
||||
{
|
||||
#if NET48
|
||||
string temp = CurrentLine.Substring("Name:".Length).Trim();
|
||||
#else
|
||||
string temp = CurrentLine["Name:".Length..].Trim();
|
||||
#endif
|
||||
CurrentLine = $"Name: {temp}";
|
||||
}
|
||||
|
||||
@@ -192,7 +168,7 @@ namespace SabreTools.IO.Readers
|
||||
for (int i = 0; i < linegc.Length; i++)
|
||||
{
|
||||
string key = linegc[i].Replace("\"", string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
if (string.IsNullOrEmpty(key))
|
||||
continue;
|
||||
|
||||
string value = string.Empty;
|
||||
|
||||
@@ -11,11 +11,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Internal stream reader for inputting
|
||||
/// </summary>
|
||||
#if NET48
|
||||
private readonly StreamReader sr;
|
||||
#else
|
||||
private readonly StreamReader? sr;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get if at end of stream
|
||||
@@ -36,11 +32,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Contents of the current line, unprocessed
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string CurrentLine { get; private set; } = string.Empty;
|
||||
#else
|
||||
public string? CurrentLine { get; private set; } = string.Empty;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get the current line number
|
||||
@@ -55,11 +47,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Current section being read
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string Section { get; private set; } = string.Empty;
|
||||
#else
|
||||
public string? Section { get; private set; } = string.Empty;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Validate that rows are in key=value format
|
||||
@@ -123,14 +111,14 @@ namespace SabreTools.IO.Readers
|
||||
}
|
||||
|
||||
// KeyValuePair
|
||||
else if (CurrentLine.Contains('='))
|
||||
else if (CurrentLine.Contains("="))
|
||||
{
|
||||
// Split the line by '=' for key-value pairs
|
||||
string[] data = CurrentLine.Split('=');
|
||||
|
||||
// If the value field contains an '=', we need to put them back in
|
||||
string key = data[0].Trim();
|
||||
string value = string.Join("=", data.Skip(1)).Trim();
|
||||
string value = string.Join("=", data.Skip(1).ToArray()).Trim();
|
||||
|
||||
KeyValuePair = new KeyValuePair<string, string>(key, value);
|
||||
RowType = IniRowType.KeyValue;
|
||||
|
||||
@@ -12,11 +12,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Internal stream reader for inputting
|
||||
/// </summary>
|
||||
#if NET48
|
||||
private readonly StreamReader sr;
|
||||
#else
|
||||
private readonly StreamReader? sr;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Internal value to say how many fields should be written
|
||||
@@ -37,11 +33,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Contents of the current line, unprocessed
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string CurrentLine { get; private set; } = string.Empty;
|
||||
#else
|
||||
public string? CurrentLine { get; private set; } = string.Empty;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get the current line number
|
||||
@@ -56,20 +48,12 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Header row values
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public List<string> HeaderValues { get; set; } = null;
|
||||
#else
|
||||
public List<string>? HeaderValues { get; set; } = null;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Get the current line values
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public List<string> Line { get; private set; } = null;
|
||||
#else
|
||||
public List<string>? Line { get; private set; } = null;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Assume that values are wrapped in quotes
|
||||
@@ -127,11 +111,7 @@ namespace SabreTools.IO.Readers
|
||||
if (!sr.BaseStream.CanRead || sr.EndOfStream)
|
||||
return false;
|
||||
|
||||
#if NET48
|
||||
string fullLine = sr.ReadLine();
|
||||
#else
|
||||
string? fullLine = sr.ReadLine();
|
||||
#endif
|
||||
CurrentLine = fullLine;
|
||||
LineNumber++;
|
||||
|
||||
@@ -144,9 +124,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>();
|
||||
foreach (Match match in lineSplitRegex.Matches(fullLine))
|
||||
foreach (Match? match in lineSplitRegex.Matches(fullLine))
|
||||
{
|
||||
string curr = match.Value;
|
||||
string? curr = match?.Value;
|
||||
if (curr == null)
|
||||
continue;
|
||||
if (curr.Length == 0)
|
||||
temp.Add("");
|
||||
|
||||
@@ -157,7 +139,7 @@ namespace SabreTools.IO.Readers
|
||||
|
||||
Line = temp;
|
||||
}
|
||||
|
||||
|
||||
// Otherwise, just split on the delimiter
|
||||
else
|
||||
{
|
||||
@@ -181,11 +163,7 @@ namespace SabreTools.IO.Readers
|
||||
/// <summary>
|
||||
/// Get the value for the current line for the current key
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public string GetValue(string key)
|
||||
#else
|
||||
public string? GetValue(string key)
|
||||
#endif
|
||||
{
|
||||
// No header means no key-based indexing
|
||||
if (!Header)
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<!-- Assembly Properties -->
|
||||
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||
<Version>1.1.1</Version>
|
||||
<TargetFrameworks>net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.2.0</Version>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
@@ -18,10 +21,6 @@
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)'!='net48'">
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="README.md" Pack="true" PackagePath=""/>
|
||||
</ItemGroup>
|
||||
|
||||
@@ -25,11 +25,7 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Read a UInt8[] from the stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static byte[] ReadBytes(this Stream stream, int count)
|
||||
#else
|
||||
public static byte[]? ReadBytes(this Stream stream, int count)
|
||||
#endif
|
||||
{
|
||||
// If there's an invalid byte count, don't do anything
|
||||
if (count <= 0)
|
||||
@@ -210,20 +206,12 @@ namespace SabreTools.IO
|
||||
/// <summary>
|
||||
/// Read a null-terminated string from the stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static string ReadString(this Stream stream) => stream.ReadString(Encoding.Default);
|
||||
#else
|
||||
public static string? ReadString(this Stream stream) => stream.ReadString(Encoding.Default);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Read a null-terminated string from the stream
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static string ReadString(this Stream stream, Encoding encoding)
|
||||
#else
|
||||
public static string? ReadString(this Stream stream, Encoding encoding)
|
||||
#endif
|
||||
{
|
||||
if (stream.Position >= stream.Length)
|
||||
return null;
|
||||
|
||||
@@ -44,20 +44,6 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Tag information for the stack
|
||||
/// </summary>
|
||||
#if NET48
|
||||
private class TagInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public bool Mixed { get; set; }
|
||||
|
||||
public void Init()
|
||||
{
|
||||
Name = null;
|
||||
Mixed = false;
|
||||
}
|
||||
}
|
||||
#else
|
||||
private record struct TagInfo(string? Name, bool Mixed)
|
||||
{
|
||||
public void Init()
|
||||
@@ -66,7 +52,6 @@ namespace SabreTools.IO.Writers
|
||||
Mixed = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Internal stream writer
|
||||
@@ -185,11 +170,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Write a complete element with content
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteElementString(string name, string value)
|
||||
#else
|
||||
public void WriteElementString(string name, string? value)
|
||||
#endif
|
||||
{
|
||||
WriteStartElement(name);
|
||||
WriteString(value);
|
||||
@@ -202,11 +183,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="name">Name of the element</param>
|
||||
/// <param name="value">Value to write in the element</param>
|
||||
/// <param name="throwOnError">Indicates if an error should be thrown on a missing required value</param>
|
||||
#if NET48
|
||||
public void WriteRequiredElementString(string name, string value, bool throwOnError = false)
|
||||
#else
|
||||
public void WriteRequiredElementString(string name, string? value, bool throwOnError = false)
|
||||
#endif
|
||||
{
|
||||
// Throw an exception if we are configured to
|
||||
if (value == null && throwOnError)
|
||||
@@ -220,11 +197,7 @@ namespace SabreTools.IO.Writers
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the element</param>
|
||||
/// <param name="value">Value to write in the element</param>
|
||||
#if NET48
|
||||
public void WriteOptionalElementString(string name, string value)
|
||||
#else
|
||||
public void WriteOptionalElementString(string name, string? value)
|
||||
#endif
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
WriteElementString(name, value);
|
||||
@@ -277,11 +250,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="name">Name of the attribute</param>
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
#if NET48
|
||||
public void WriteAttributeString(string name, string value, bool? quoteOverride = null)
|
||||
#else
|
||||
public void WriteAttributeString(string name, string? value, bool? quoteOverride = null)
|
||||
#endif
|
||||
{
|
||||
WriteStartAttribute(name, quoteOverride);
|
||||
WriteString(value);
|
||||
@@ -295,11 +264,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
/// <param name="throwOnError">Indicates if an error should be thrown on a missing required value</param>
|
||||
#if NET48
|
||||
public void WriteRequiredAttributeString(string name, string value, bool? quoteOverride = null, bool throwOnError = false)
|
||||
#else
|
||||
public void WriteRequiredAttributeString(string name, string? value, bool? quoteOverride = null, bool throwOnError = false)
|
||||
#endif
|
||||
{
|
||||
// Throw an exception if we are configured to
|
||||
if (value == null && throwOnError)
|
||||
@@ -314,11 +279,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="name">Name of the attribute</param>
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
#if NET48
|
||||
public void WriteOptionalAttributeString(string name, string value, bool? quoteOverride = null)
|
||||
#else
|
||||
public void WriteOptionalAttributeString(string name, string? value, bool? quoteOverride = null)
|
||||
#endif
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
WriteAttributeString(name, value, quoteOverride);
|
||||
@@ -330,11 +291,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="name">Name of the attribute</param>
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
#if NET48
|
||||
public void WriteStandalone(string name, string value, bool? quoteOverride = null)
|
||||
#else
|
||||
public void WriteStandalone(string name, string? value, bool? quoteOverride = null)
|
||||
#endif
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -378,11 +335,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
/// <param name="throwOnError">Indicates if an error should be thrown on a missing required value</param>
|
||||
#if NET48
|
||||
public void WriteRequiredStandalone(string name, string value, bool? quoteOverride = null, bool throwOnError = false)
|
||||
#else
|
||||
public void WriteRequiredStandalone(string name, string? value, bool? quoteOverride = null, bool throwOnError = false)
|
||||
#endif
|
||||
{
|
||||
// Throw an exception if we are configured to
|
||||
if (value == null && throwOnError)
|
||||
@@ -397,11 +350,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <param name="name">Name of the attribute</param>
|
||||
/// <param name="value">Value to write in the attribute</param>
|
||||
/// <param name="quoteOverride">Non-null to overwrite the writer setting, null otherwise</param>
|
||||
#if NET48
|
||||
public void WriteOptionalStandalone(string name, string value, bool? quoteOverride = null)
|
||||
#else
|
||||
public void WriteOptionalStandalone(string name, string? value, bool? quoteOverride = null)
|
||||
#endif
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
WriteStandalone(name, value, quoteOverride);
|
||||
@@ -410,11 +359,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Write a string content value
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteString(string value)
|
||||
#else
|
||||
public void WriteString(string? value)
|
||||
#endif
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -424,7 +369,7 @@ namespace SabreTools.IO.Writers
|
||||
|
||||
// If we're writing quotes, don't write out quote characters internally
|
||||
if (Quotes)
|
||||
value = value.Replace("\"", "''");
|
||||
value = value!.Replace("\"", "''");
|
||||
|
||||
sw.Write(value);
|
||||
}
|
||||
|
||||
@@ -9,11 +9,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Internal stream writer for outputting
|
||||
/// </summary>
|
||||
#if NET48
|
||||
private readonly StreamWriter sw;
|
||||
#else
|
||||
private readonly StreamWriter? sw;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for writing to a file
|
||||
@@ -34,81 +30,53 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Write a section tag
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteSection(string value)
|
||||
#else
|
||||
public void WriteSection(string? value)
|
||||
#endif
|
||||
{
|
||||
if (sw?.BaseStream == null)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
throw new ArgumentException("Section tag cannot be null or empty", nameof(value));
|
||||
|
||||
sw.WriteLine($"[{value.TrimStart('[').TrimEnd(']')}]");
|
||||
sw.WriteLine($"[{value!.TrimStart('[').TrimEnd(']')}]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a key value pair
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteKeyValuePair(string key, string value)
|
||||
#else
|
||||
public void WriteKeyValuePair(string key, string? value)
|
||||
#endif
|
||||
{
|
||||
if (sw?.BaseStream == null)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentException("Key cannot be null or empty", nameof(key));
|
||||
|
||||
#if NET48
|
||||
value = value != null ? value : string.Empty;
|
||||
#else
|
||||
value ??= string.Empty;
|
||||
#endif
|
||||
sw.WriteLine($"{key}={value}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a comment
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteComment(string value)
|
||||
#else
|
||||
public void WriteComment(string? value)
|
||||
#endif
|
||||
{
|
||||
if (sw?.BaseStream == null)
|
||||
return;
|
||||
|
||||
#if NET48
|
||||
value = value != null ? value : string.Empty;
|
||||
#else
|
||||
|
||||
value ??= string.Empty;
|
||||
#endif
|
||||
sw.WriteLine($";{value}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a generic string
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteString(string value)
|
||||
#else
|
||||
public void WriteString(string? value)
|
||||
#endif
|
||||
{
|
||||
if (sw?.BaseStream == null)
|
||||
return;
|
||||
|
||||
#if NET48
|
||||
value = value != null ? value : string.Empty;
|
||||
#else
|
||||
|
||||
value ??= string.Empty;
|
||||
#endif
|
||||
sw.Write(value);
|
||||
}
|
||||
|
||||
@@ -119,7 +87,7 @@ namespace SabreTools.IO.Writers
|
||||
{
|
||||
if (sw?.BaseStream == null)
|
||||
return;
|
||||
|
||||
|
||||
sw.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -60,11 +60,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Write a header row
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteHeader(string[] headers)
|
||||
#else
|
||||
public void WriteHeader(string?[] headers)
|
||||
#endif
|
||||
{
|
||||
// If we haven't written anything out, we can write headers
|
||||
if (!header && !firstRow)
|
||||
@@ -76,11 +72,7 @@ namespace SabreTools.IO.Writers
|
||||
/// <summary>
|
||||
/// Write a value row
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public void WriteValues(object[] values, bool newline = true)
|
||||
#else
|
||||
public void WriteValues(object?[] values, bool newline = true)
|
||||
#endif
|
||||
{
|
||||
// If the writer can't be used, we error
|
||||
if (sw == null || !sw.BaseStream.CanWrite)
|
||||
|
||||
Reference in New Issue
Block a user