From 6b3f183a3d35950199fed790389700c378fcdd68 Mon Sep 17 00:00:00 2001 From: Waldi Ravens Date: Thu, 19 May 2016 17:09:15 +0200 Subject: [PATCH] Replaced Encoding.GetEncodings with Configuration.AvailableEncodings --- libse/DetectEncoding/EncodingTools.cs | 63 ++++++++++++--------------- libse/LanguageAutoDetect.cs | 10 ++--- src/Forms/BatchConvert.cs | 14 +++--- src/Forms/ChooseEncoding.cs | 8 ++-- src/Forms/ExportCustomText.cs | 15 ++++--- src/Forms/ExportText.cs | 14 +++--- src/Forms/Main.cs | 19 +++----- src/Forms/Settings.cs | 12 ++--- src/Forms/Split.cs | 14 +++--- 9 files changed, 79 insertions(+), 90 deletions(-) diff --git a/libse/DetectEncoding/EncodingTools.cs b/libse/DetectEncoding/EncodingTools.cs index cc7b9cccc..ab388bb76 100644 --- a/libse/DetectEncoding/EncodingTools.cs +++ b/libse/DetectEncoding/EncodingTools.cs @@ -22,13 +22,13 @@ namespace Nikse.SubtitleEdit.Core.DetectEncoding /// static EncodingTools() { - List streamEcodings = new List(); + List streamEncodings = new List(); List allEncodings = new List(); - List mimeEcodings = new List(); + List mimeEncodings = new List(); // asscii - most simple so put it in first place... - streamEcodings.Add(Encoding.ASCII.CodePage); - mimeEcodings.Add(Encoding.ASCII.CodePage); + streamEncodings.Add(Encoding.ASCII.CodePage); + mimeEncodings.Add(Encoding.ASCII.CodePage); allEncodings.Add(Encoding.ASCII.CodePage); // add default 2nd for all encodings @@ -37,66 +37,61 @@ namespace Nikse.SubtitleEdit.Core.DetectEncoding if (Encoding.Default.IsSingleByte) { // put it in second place - streamEcodings.Add(Encoding.Default.CodePage); - mimeEcodings.Add(Encoding.Default.CodePage); + streamEncodings.Add(Encoding.Default.CodePage); + mimeEncodings.Add(Encoding.Default.CodePage); } // prefer JIS over JIS-SHIFT (JIS is detected better than JIS-SHIFT) // this one does include cyrilic (strange but true) allEncodings.Add(50220); - mimeEcodings.Add(50220); + mimeEncodings.Add(50220); // always allow unicode flavours for streams (they all have a preamble) - streamEcodings.Add(Encoding.Unicode.CodePage); - foreach (EncodingInfo enc in Encoding.GetEncodings()) + streamEncodings.Add(Encoding.Unicode.CodePage); + foreach (var encoding in Configuration.AvailableEncodings) { - if (!streamEcodings.Contains(enc.CodePage)) + if (!streamEncodings.Contains(encoding.CodePage)) { - Encoding encoding = Encoding.GetEncoding(enc.CodePage); if (encoding.GetPreamble().Length > 0) - streamEcodings.Add(enc.CodePage); + streamEncodings.Add(encoding.CodePage); } } // stream is done here - PreferredEncodingsForStream = streamEcodings.ToArray(); + PreferredEncodingsForStream = streamEncodings.ToArray(); // all singlebyte encodings - foreach (EncodingInfo enc in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (!enc.GetEncoding().IsSingleByte) - continue; - - if (!allEncodings.Contains(enc.CodePage)) - allEncodings.Add(enc.CodePage); - - // only add iso and IBM encodings to mime encodings - if (enc.CodePage <= 1258) + if (encoding.IsSingleByte) { - mimeEcodings.Add(enc.CodePage); + if (!allEncodings.Contains(encoding.CodePage)) + allEncodings.Add(encoding.CodePage); + + // only add iso and IBM encodings to mime encodings + if (encoding.CodePage <= 1258) + mimeEncodings.Add(encoding.CodePage); } } // add the rest (multibyte) - foreach (EncodingInfo enc in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (!enc.GetEncoding().IsSingleByte) + if (!encoding.IsSingleByte) { - if (!allEncodings.Contains(enc.CodePage)) - allEncodings.Add(enc.CodePage); + if (!allEncodings.Contains(encoding.CodePage)) + allEncodings.Add(encoding.CodePage); // only add iso and IBM encodings to mime encodings - if (enc.CodePage <= 1258) - { - mimeEcodings.Add(enc.CodePage); - } + if (encoding.CodePage <= 1258) + mimeEncodings.Add(encoding.CodePage); } } // add unicodes - mimeEcodings.Add(Encoding.Unicode.CodePage); + mimeEncodings.Add(Encoding.Unicode.CodePage); - PreferredEncodings = mimeEcodings.ToArray(); + PreferredEncodings = mimeEncodings.ToArray(); } /// @@ -338,7 +333,7 @@ namespace Nikse.SubtitleEdit.Core.DetectEncoding } /// - /// Rerurns up to maxEncodings codpages that are assumed to be apropriate + /// Returns up to maxEncodings codepages that are assumed to be apropriate /// /// array containing the raw data /// maxiumum number of encodings to detect diff --git a/libse/LanguageAutoDetect.cs b/libse/LanguageAutoDetect.cs index 28b4dcc38..0a02a826e 100644 --- a/libse/LanguageAutoDetect.cs +++ b/libse/LanguageAutoDetect.cs @@ -577,13 +577,13 @@ namespace Nikse.SubtitleEdit.Core try { - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var enc in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == Configuration.Settings.General.DefaultEncoding && - ei.Name != Encoding.UTF8.BodyName && - ei.Name != Encoding.Unicode.BodyName) + if (enc.CodePage + ": " + enc.EncodingName == Configuration.Settings.General.DefaultEncoding && + enc.WebName != Encoding.UTF8.WebName && + enc.WebName != Encoding.Unicode.WebName) { - encoding = ei.GetEncoding(); + encoding = enc; break; } } diff --git a/src/Forms/BatchConvert.cs b/src/Forms/BatchConvert.cs index e8523ddf7..d74758c68 100644 --- a/src/Forms/BatchConvert.cs +++ b/src/Forms/BatchConvert.cs @@ -170,12 +170,12 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); int encodingSelectedIndex = 0; comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) { - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - if (ei.Name == Configuration.Settings.General.DefaultEncoding) + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); + if (encoding.WebName == Configuration.Settings.General.DefaultEncoding) encodingSelectedIndex = comboBoxEncoding.Items.Count - 1; } } @@ -510,10 +510,10 @@ namespace Nikse.SubtitleEdit.Forms return Encoding.UTF8; } - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) - return ei.GetEncoding(); + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) + return encoding; } return Encoding.UTF8; diff --git a/src/Forms/ChooseEncoding.cs b/src/Forms/ChooseEncoding.cs index fe587305f..9eff34493 100644 --- a/src/Forms/ChooseEncoding.cs +++ b/src/Forms/ChooseEncoding.cs @@ -54,12 +54,12 @@ namespace Nikse.SubtitleEdit.Forms _fileBuffer = new byte[0]; } - Encoding encoding = LanguageAutoDetect.DetectAnsiEncoding(_fileBuffer); - foreach (EncodingInfo ei in Encoding.GetEncodings()) + var encoding = LanguageAutoDetect.DetectAnsiEncoding(_fileBuffer); + foreach (var enc in Configuration.AvailableEncodings) { - var item = new ListViewItem(new[] { ei.CodePage.ToString(), ei.Name, ei.DisplayName }); + var item = new ListViewItem(new[] { enc.CodePage.ToString(), enc.WebName, enc.EncodingName }); listView1.Items.Add(item); - if (ei.CodePage == encoding.CodePage) + if (enc.CodePage == encoding.CodePage) item.Selected = true; } diff --git a/src/Forms/ExportCustomText.cs b/src/Forms/ExportCustomText.cs index c14e6cd70..c333df468 100644 --- a/src/Forms/ExportCustomText.cs +++ b/src/Forms/ExportCustomText.cs @@ -33,16 +33,17 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); int encodingSelectedIndex = 0; comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) { - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - if (ei.Name == Configuration.Settings.General.DefaultEncoding) + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); + if (encoding.WebName == Configuration.Settings.General.DefaultEncoding) encodingSelectedIndex = comboBoxEncoding.Items.Count - 1; } } comboBoxEncoding.SelectedIndex = encodingSelectedIndex; + if (string.IsNullOrEmpty(Configuration.Settings.Tools.ExportCustomTemplates)) { _templates.Add("SubRipÆÆ{number}\r\n{start} --> {end}\r\n{text}\r\n\r\nÆhh:mm:ss,zzzÆ[Do not modify]Æ"); @@ -187,10 +188,10 @@ namespace Nikse.SubtitleEdit.Forms return Encoding.UTF8; } - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) - return ei.GetEncoding(); + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) + return encoding; } return Encoding.UTF8; diff --git a/src/Forms/ExportText.cs b/src/Forms/ExportText.cs index f9b5af353..eb007d26d 100644 --- a/src/Forms/ExportText.cs +++ b/src/Forms/ExportText.cs @@ -50,12 +50,12 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); int encodingSelectedIndex = 0; comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) { - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - if (ei.Name == Configuration.Settings.General.DefaultEncoding) + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); + if (encoding.WebName == Configuration.Settings.General.DefaultEncoding) encodingSelectedIndex = comboBoxEncoding.Items.Count - 1; } } @@ -142,10 +142,10 @@ namespace Nikse.SubtitleEdit.Forms return Encoding.UTF8; } - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) - return ei.GetEncoding(); + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) + return encoding; } return Encoding.UTF8; diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 73a158da2..23d276df7 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -334,17 +334,10 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (var ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - try - { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) //Configuration.Settings.General.EncodingMinimumCodePage) - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - } - catch - { - // Work-around for issue #1285 - } + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) //Configuration.Settings.General.EncodingMinimumCodePage + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); } SetEncoding(Configuration.Settings.General.DefaultEncoding); @@ -613,10 +606,10 @@ namespace Nikse.SubtitleEdit.Forms return Encoding.UTF8; } - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) - return ei.GetEncoding(); + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) + return encoding; } return Encoding.UTF8; diff --git a/src/Forms/Settings.cs b/src/Forms/Settings.cs index 79d132b6e..ae76d540e 100644 --- a/src/Forms/Settings.cs +++ b/src/Forms/Settings.cs @@ -99,12 +99,12 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); int encodingSelectedIndex = 0; comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (var ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) { - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - if (ei.Name == gs.DefaultEncoding || ei.CodePage + ": " + ei.DisplayName == gs.DefaultEncoding) + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); + if (encoding.WebName == gs.DefaultEncoding || encoding.CodePage + ": " + encoding.EncodingName == gs.DefaultEncoding) encodingSelectedIndex = comboBoxEncoding.Items.Count - 1; } } @@ -1066,9 +1066,9 @@ namespace Nikse.SubtitleEdit.Forms gs.DefaultFrameRate = outFrameRate; gs.DefaultEncoding = Encoding.UTF8.BodyName; - foreach (var ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) gs.DefaultEncoding = comboBoxEncoding.Text; } diff --git a/src/Forms/Split.cs b/src/Forms/Split.cs index cd168c4db..e8c773441 100644 --- a/src/Forms/Split.cs +++ b/src/Forms/Split.cs @@ -89,12 +89,12 @@ namespace Nikse.SubtitleEdit.Forms comboBoxEncoding.Items.Clear(); int encodingSelectedIndex = 0; comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName); - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047) + if (encoding.CodePage >= 949 && encoding.CodePage != 1047 && !encoding.EncodingName.Contains("EBCDIC") && !encoding.WebName.Equals(Encoding.UTF8.WebName)) { - comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName); - if (ei.Name == Configuration.Settings.General.DefaultEncoding) + comboBoxEncoding.Items.Add(encoding.CodePage + ": " + encoding.EncodingName); + if (encoding.WebName == Configuration.Settings.General.DefaultEncoding) encodingSelectedIndex = comboBoxEncoding.Items.Count - 1; } } @@ -282,10 +282,10 @@ namespace Nikse.SubtitleEdit.Forms return Encoding.UTF8; } - foreach (EncodingInfo ei in Encoding.GetEncodings()) + foreach (var encoding in Configuration.AvailableEncodings) { - if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text) - return ei.GetEncoding(); + if (encoding.CodePage + ": " + encoding.EncodingName == comboBoxEncoding.Text) + return encoding; } return Encoding.UTF8;