mirror of
https://github.com/nsnail/dot.git
synced 2025-06-19 22:08:16 +08:00
<refactor>
This commit is contained in:
46
src/Rbom/Main.cs
Normal file
46
src/Rbom/Main.cs
Normal file
@ -0,0 +1,46 @@
|
||||
namespace Dot.Rbom;
|
||||
|
||||
[Description(nameof(Str.TrimUtf8Bom))]
|
||||
[Localization(typeof(Str))]
|
||||
public sealed class Main : FilesTool<Option>
|
||||
{
|
||||
private readonly byte[] _utf8Bom = { 0xef, 0xbb, 0xbf };
|
||||
|
||||
|
||||
private bool CloneFileWithoutBom(Stream fsr, ref string tempFile)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[_utf8Bom.Length];
|
||||
var readLen = fsr.Read(buffer);
|
||||
if (readLen != _utf8Bom.Length || !buffer.SequenceEqual(_utf8Bom)) return false;
|
||||
|
||||
using var fsw = CreateTempFile(out tempFile);
|
||||
int data;
|
||||
while ((data = fsr.ReadByte()) != -1) fsw.WriteByte((byte)data);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected override async ValueTask FileHandle(string file, CancellationToken _)
|
||||
{
|
||||
ShowMessage(1, 0, 0);
|
||||
|
||||
string tmpFile = default;
|
||||
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||
if (fsr is null) {
|
||||
ShowMessage(0, 0, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (CloneFileWithoutBom(fsr, ref tmpFile)) {
|
||||
if (Opt.WriteMode) File.Copy(tmpFile, file, true);
|
||||
ShowMessage(0, 1, 0);
|
||||
UpdateStats(Path.GetExtension(file));
|
||||
}
|
||||
else {
|
||||
ShowMessage(0, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmpFile != default) File.Delete(tmpFile);
|
||||
}
|
||||
}
|
3
src/Rbom/Option.cs
Normal file
3
src/Rbom/Option.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace Dot.Rbom;
|
||||
|
||||
public class Option : DirOption { }
|
Reference in New Issue
Block a user