Switch-Toolbox/Switch_Toolbox_Library/IO/IOExtensions.cs

70 lines
2.4 KiB
C#
Raw Normal View History

2019-05-28 20:11:40 +00:00
using System;
using System.Collections.Generic;
2019-06-11 00:54:23 +00:00
using System.Text;
using System.IO;
2019-05-28 20:11:40 +00:00
using System.Threading.Tasks;
using System.Text.RegularExpressions;
2019-06-11 00:54:23 +00:00
using System.Security.Cryptography;
2019-05-28 20:11:40 +00:00
namespace Switch_Toolbox.Library.IO
{
public static class IOExtensions
{
2019-06-01 13:49:33 +00:00
//https://github.com/exelix11/EditorCore/blob/872d210f85ec0409f8a6ac3a12fc162aaf4cd90c/EditorCoreCommon/CustomClasses.cs#L367
public static bool Matches(this byte[] arr, string magic) =>
arr.Matches(0, magic.ToCharArray());
2019-06-01 14:23:06 +00:00
public static bool Matches(this byte[] arr, uint magic) =>
arr.Matches(0, BitConverter.GetBytes(magic));
public static bool Matches(this byte[] arr, uint startIndex, params byte[] magic)
{
if (arr.Length < magic.Length + startIndex) return false;
for (uint i = 0; i < magic.Length; i++)
{
if (arr[i + startIndex] != magic[i]) return false;
}
return true;
}
2019-06-01 13:49:33 +00:00
2019-06-11 00:54:23 +00:00
public static byte[] ToByteArray(this string str, int length)
{
return Encoding.ASCII.GetBytes(str.PadRight(length, ' '));
}
public static uint EncodeCrc32(this string str)
{
return Crc32.Compute(str);
}
2019-06-01 13:49:33 +00:00
public static bool Matches(this byte[] arr, uint startIndex, params char[] magic)
{
if (arr.Length < magic.Length + startIndex) return false;
for (uint i = 0; i < magic.Length; i++)
{
if (arr[i + startIndex] != magic[i]) return false;
}
return true;
}
2019-05-28 20:11:40 +00:00
public static uint Reverse(this uint x)
{
// swap adjacent 16-bit blocks
x = (x >> 16) | (x << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
}
2019-05-28 20:55:17 +00:00
//https://stackoverflow.com/questions/2230826/remove-invalid-disallowed-bad-characters-from-filename-or-directory-folder/12800424#12800424
public static string RemoveIllegaleFileNameCharacters(this string str)
{
2019-05-28 20:55:17 +00:00
return string.Join("_", str.Split(Path.GetInvalidFileNameChars()));
}
2019-05-28 20:55:17 +00:00
public static string RemoveIllegaleFolderNameCharacters(this string str)
{
return string.Join("_", str.Split(Path.GetInvalidPathChars()));
}
2019-05-28 20:11:40 +00:00
}
}