From 916f139df06ba05fbc7cf6709293eac1dde98d2b Mon Sep 17 00:00:00 2001 From: niksedk Date: Fri, 26 Nov 2021 18:53:15 +0100 Subject: [PATCH] Some fixes to improve compability with dot net 6 Will probably remove gzip some more placeces --- src/libse/Common/FileUtil.cs | 30 +++++++++++++++++++++++ src/ui/Controls/AdvancedTextBox.cs | 1 - src/ui/Forms/SpellCheck.cs | 1 - src/ui/Logic/Ocr/NOcrDb.cs | 38 +++++++++++++++++++++++------- src/ui/Logic/Ocr/OcrFixEngine.cs | 2 +- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index 69bdd8048..3dbbca037 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -50,6 +50,35 @@ namespace Nikse.SubtitleEdit.Core.Common } } + public static byte[] ReadBytesShared(string path, int bytesToRead) + { + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + var index = 0; + var fileLength = fs.Length; + if (fileLength > int.MaxValue) + { + throw new IOException("File too long"); + } + + var count = Math.Min(bytesToRead, (int)fileLength); + var bytes = new byte[count]; + while (count > 0) + { + var n = fs.Read(bytes, index, count); + if (n == 0) + { + throw new InvalidOperationException("End of file reached before expected"); + } + + index += n; + count -= n; + } + + return bytes; + } + } + public static List ReadAllLinesShared(string path, Encoding encoding) { return encoding.GetString(ReadAllBytesShared(path)).SplitToLines(); @@ -77,6 +106,7 @@ namespace Nikse.SubtitleEdit.Core.Common && buffer[3] == 0x04; // (EOT) } } + public static bool Is7Zip(string fileName) { using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) diff --git a/src/ui/Controls/AdvancedTextBox.cs b/src/ui/Controls/AdvancedTextBox.cs index cd3db225f..01f5a1e9d 100644 --- a/src/ui/Controls/AdvancedTextBox.cs +++ b/src/ui/Controls/AdvancedTextBox.cs @@ -919,7 +919,6 @@ namespace Nikse.SubtitleEdit.Controls var suggestThread = new System.Threading.Thread(DoWork); suggestThread.Start(parameter); suggestThread.Join(3000); // wait max 3 seconds - suggestThread.Abort(); if (!parameter.Success) { LoadHunspell(_currentDictionary); diff --git a/src/ui/Forms/SpellCheck.cs b/src/ui/Forms/SpellCheck.cs index 15a283a45..3d18f4c86 100644 --- a/src/ui/Forms/SpellCheck.cs +++ b/src/ui/Forms/SpellCheck.cs @@ -286,7 +286,6 @@ namespace Nikse.SubtitleEdit.Forms var suggestThread = new System.Threading.Thread(DoWork); suggestThread.Start(parameter); suggestThread.Join(3000); // wait max 3 seconds - suggestThread.Abort(); if (!parameter.Success) { LoadHunspell(_currentDictionary); diff --git a/src/ui/Logic/Ocr/NOcrDb.cs b/src/ui/Logic/Ocr/NOcrDb.cs index 7f9bd7710..443a7ae87 100644 --- a/src/ui/Logic/Ocr/NOcrDb.cs +++ b/src/ui/Logic/Ocr/NOcrDb.cs @@ -33,19 +33,24 @@ namespace Nikse.SubtitleEdit.Logic.Ocr public void Save() { - using (Stream gz = new GZipStream(File.OpenWrite(FileName), CompressionMode.Compress)) + if (File.Exists(FileName)) + { + File.Delete(FileName); + } + + using (Stream stream = new FileStream(FileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) { var versionBuffer = Encoding.ASCII.GetBytes(Version); - gz.Write(versionBuffer, 0, versionBuffer.Length); + stream.Write(versionBuffer, 0, versionBuffer.Length); foreach (var ocrChar in OcrCharacters) { - ocrChar.Save(gz); + ocrChar.Save(stream); } foreach (var ocrChar in OcrCharactersExpanded) { - ocrChar.Save(gz); + ocrChar.Save(stream); } } } @@ -63,24 +68,24 @@ namespace Nikse.SubtitleEdit.Logic.Ocr } bool isVersion2; - using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress)) + using (Stream stream = GetOpenStream()) { var versionBuffer = new byte[Version.Length]; - gz.Read(versionBuffer, 0, versionBuffer.Length); + stream.Read(versionBuffer, 0, versionBuffer.Length); isVersion2 = Encoding.ASCII.GetString(versionBuffer) == Version; } - using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress)) + using (Stream stream = GetOpenStream()) { bool done = false; if (isVersion2) { var versionBuffer = new byte[Version.Length]; - gz.Read(versionBuffer, 0, versionBuffer.Length); + stream.Read(versionBuffer, 0, versionBuffer.Length); } while (!done) { - var ocrChar = new NOcrChar(gz, isVersion2); + var ocrChar = new NOcrChar(stream, isVersion2); if (ocrChar.LoadedOk) { if (ocrChar.ExpandCount > 0) @@ -98,10 +103,25 @@ namespace Nikse.SubtitleEdit.Logic.Ocr } } } + OcrCharacters = list; OcrCharactersExpanded = listExpanded; } + private Stream GetOpenStream() + { + var bytes = FileUtil.ReadBytesShared(FileName, 2); + var isVersion2Plain = Encoding.ASCII.GetString(bytes) == Version; + if (isVersion2Plain) + { + return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + } + else + { + return new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress); + } + } + public void Add(NOcrChar ocrChar) { if (ocrChar.ExpandCount > 0) diff --git a/src/ui/Logic/Ocr/OcrFixEngine.cs b/src/ui/Logic/Ocr/OcrFixEngine.cs index 023564326..96b46c9bb 100644 --- a/src/ui/Logic/Ocr/OcrFixEngine.cs +++ b/src/ui/Logic/Ocr/OcrFixEngine.cs @@ -646,7 +646,7 @@ namespace Nikse.SubtitleEdit.Logic.Ocr return word; } - internal static string FixFrenchLApostrophe(string input, string tag, string lastLine) + public static string FixFrenchLApostrophe(string input, string tag, string lastLine) { var text = input; bool endingBeforeThis = string.IsNullOrEmpty(lastLine) || lastLine.EndsWith('.') || lastLine.EndsWith('!') || lastLine.EndsWith('?') ||