From 92df6b21e348ac52cc41a3c826c7bec7b09fbb26 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 21 Nov 2023 00:12:40 -0500 Subject: [PATCH] Support .NET Framework 2.0 --- IOExtensions.cs | 6 +- IniFile.cs | 15 +--- NaturalSort/NaturalComparer.cs | 3 + NaturalSort/NaturalReversedComparer.cs | 3 + OldDotNet.cs | 110 ++++++++++++++++++++++++- PathTool.cs | 14 ++-- Readers/ClrMameProReader.cs | 2 + Readers/IniReader.cs | 2 + Readers/SeparatedValueReader.cs | 4 +- SabreTools.IO.csproj | 7 +- StreamExtensions.cs | 2 + 11 files changed, 145 insertions(+), 23 deletions(-) diff --git a/IOExtensions.cs b/IOExtensions.cs index 502c65a..754cd20 100644 --- a/IOExtensions.cs +++ b/IOExtensions.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; namespace SabreTools.IO @@ -118,7 +120,7 @@ namespace SabreTools.IO return null; // If it does and it is empty, return a blank enumerable -#if NET35 +#if NET20 || NET35 if (!Directory.GetFiles(root, "*", SearchOption.AllDirectories).Any()) #else if (!Directory.EnumerateFileSystemEntries(root, "*", SearchOption.AllDirectories).Any()) @@ -126,7 +128,7 @@ namespace SabreTools.IO return []; // Otherwise, get the complete list -#if NET35 +#if NET20 || NET35 return Directory.GetDirectories(root, "*", SearchOption.AllDirectories) .Where(dir => !Directory.GetFiles(dir, "*", SearchOption.AllDirectories).Any()) .ToList(); diff --git a/IniFile.cs b/IniFile.cs index 3e12154..1c23604 100644 --- a/IniFile.cs +++ b/IniFile.cs @@ -2,7 +2,9 @@ using System.Collections; using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; using SabreTools.IO.Readers; using SabreTools.IO.Writers; @@ -221,18 +223,9 @@ namespace SabreTools.IO #region IDictionary Impelementations -#if NET35 || NET40 || NET452 - public ICollection Keys => _keyValuePairs?.Keys?.ToArray() ?? new string[0]; -#else - public ICollection Keys => _keyValuePairs?.Keys?.ToArray() ?? Array.Empty(); -#endif + public ICollection Keys => _keyValuePairs?.Keys?.ToArray() ?? []; - -#if NET35 || NET40 || NET452 - public ICollection Values => _keyValuePairs?.Values?.ToArray() ?? new string[0]; -#else - public ICollection Values => _keyValuePairs?.Values?.ToArray() ?? Array.Empty(); -#endif + public ICollection Values => _keyValuePairs?.Values?.ToArray() ?? []; public int Count => (_keyValuePairs as ICollection>)?.Count ?? 0; diff --git a/NaturalSort/NaturalComparer.cs b/NaturalSort/NaturalComparer.cs index 02489d5..5c004c0 100644 --- a/NaturalSort/NaturalComparer.cs +++ b/NaturalSort/NaturalComparer.cs @@ -11,8 +11,11 @@ using System; using System.Collections.Generic; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text.RegularExpressions; +using SabreTools.IO; /// TODO: Make this namespace a separate library namespace NaturalSort diff --git a/NaturalSort/NaturalReversedComparer.cs b/NaturalSort/NaturalReversedComparer.cs index 6d781f3..85f587c 100644 --- a/NaturalSort/NaturalReversedComparer.cs +++ b/NaturalSort/NaturalReversedComparer.cs @@ -11,8 +11,11 @@ using System; using System.Collections.Generic; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text.RegularExpressions; +using SabreTools.IO; /// TODO: Make this namespace a separate library namespace NaturalSort diff --git a/OldDotNet.cs b/OldDotNet.cs index 71bea37..d555f45 100644 --- a/OldDotNet.cs +++ b/OldDotNet.cs @@ -1,7 +1,113 @@ -using System; using System.Collections.Generic; +using System.Text.RegularExpressions; -#if NET35 || NET40 +#if NET20 + +namespace SabreTools.IO +{ + internal static partial class EnumerationExtensions + { + internal delegate U LinqSelectDelegate(T str); + + internal delegate bool LinqWhereDelegate(T str); + + public static bool Any(this IEnumerable arr) + { + return arr.Count() != 0; + } + + public static IEnumerable Cast(this MatchCollection arr) + { + foreach (var val in arr) + { + if (val is not T castVal) + throw new System.ArrayTypeMismatchException(); + + yield return castVal; + } + } + + public static int Count(this IEnumerable arr) + { + int counter = 0; + foreach (var val in arr) + { + counter++; + } + + return counter; + } + public static IEnumerable Select(this IEnumerable arr, LinqSelectDelegate func) + { + foreach (var val in arr) + { + yield return func(val); + } + } + + public static bool SequenceEqual(this IEnumerable arr1, IEnumerable arr2) + { + if (arr1 == null || arr2 == null) + return false; + + if (arr1.Count() != arr2.Count()) + return false; + + var arr1Array = arr1.ToArray(); + var arr2Array = arr2.ToArray(); + + for (int i = 0; i < arr1Array.Length; i++) + { + if (!arr1Array[i]?.Equals(arr2Array[i]) != true) + return false; + } + + return true; + } + + public static IEnumerable Skip(this IEnumerable arr, int skip) + { + int counter = 0; + foreach (var val in arr) + { + if (counter++ < skip) + continue; + + yield return val; + } + } + + public static T[] ToArray(this IEnumerable arr) + { + return [.. arr]; + } + + public static List ToList(this IEnumerable arr) + { + return [.. arr]; + } + public static IEnumerable Where(this T[] arr, LinqWhereDelegate func) + { + foreach (var val in arr) + { + if (func(val)) + yield return val; + } + } + public static IEnumerable Where(this IEnumerable arr, LinqWhereDelegate func) + { + foreach (var val in arr) + { + if (func(val)) + yield return val; + } + } + } +} + +#endif + +#if NET20 || NET35 || NET40 namespace SabreTools.IO { diff --git a/PathTool.cs b/PathTool.cs index be886c1..c73e27e 100644 --- a/PathTool.cs +++ b/PathTool.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using NaturalSort; namespace SabreTools.IO @@ -71,8 +73,8 @@ namespace SabreTools.IO private static List GetDirectoriesOrderedHelper(string dir, List infiles, string pattern) { // Take care of the files in the top directory -#if NET35 - List toadd = Directory.GetDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); +#if NET20 || NET35 + List toadd = [.. Directory.GetDirectories(dir, pattern, SearchOption.TopDirectoryOnly)]; #else List toadd = Directory.EnumerateDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); #endif @@ -154,8 +156,8 @@ namespace SabreTools.IO private static List GetFilesOrderedHelper(string dir, List infiles, string pattern) { // Take care of the files in the top directory -#if NET35 - List toadd = Directory.GetFiles(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); +#if NET20 || NET35 + List toadd = [.. Directory.GetFiles(dir, pattern, SearchOption.TopDirectoryOnly)]; #else List toadd = Directory.EnumerateFiles(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); #endif @@ -163,8 +165,8 @@ namespace SabreTools.IO infiles.AddRange(toadd); // Then recurse through and add from the directories -#if NET35 - List subDirs = Directory.GetDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); +#if NET20 || NET35 + List subDirs = [.. Directory.GetDirectories(dir, pattern, SearchOption.TopDirectoryOnly)]; #else List subDirs = Directory.EnumerateDirectories(dir, pattern, SearchOption.TopDirectoryOnly).ToList(); #endif diff --git a/Readers/ClrMameProReader.cs b/Readers/ClrMameProReader.cs index d1ed54b..c293166 100644 --- a/Readers/ClrMameProReader.cs +++ b/Readers/ClrMameProReader.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; using System.Text.RegularExpressions; diff --git a/Readers/IniReader.cs b/Readers/IniReader.cs index ce24778..7d700b8 100644 --- a/Readers/IniReader.cs +++ b/Readers/IniReader.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; namespace SabreTools.IO.Readers diff --git a/Readers/SeparatedValueReader.cs b/Readers/SeparatedValueReader.cs index 9712cc6..245c5c3 100644 --- a/Readers/SeparatedValueReader.cs +++ b/Readers/SeparatedValueReader.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; using System.Text.RegularExpressions; @@ -124,7 +126,7 @@ 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(); - foreach (Match? match in lineSplitRegex.Matches(fullLine)) + foreach (Match? match in lineSplitRegex.Matches(fullLine).Cast()) { string? curr = match?.Value; if (curr == null) diff --git a/SabreTools.IO.csproj b/SabreTools.IO.csproj index 83110e7..fd50ede 100644 --- a/SabreTools.IO.csproj +++ b/SabreTools.IO.csproj @@ -2,7 +2,7 @@ - net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64 latest enable @@ -25,4 +25,9 @@ + + + + + diff --git a/StreamExtensions.cs b/StreamExtensions.cs index a32617c..f890ac2 100644 --- a/StreamExtensions.cs +++ b/StreamExtensions.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +#if NET35_OR_GREATER || NETCOREAPP using System.Linq; +#endif using System.Text; namespace SabreTools.IO