From 5fddd0b27f16d01efecfe76a92a695245d88cbcc Mon Sep 17 00:00:00 2001 From: sr55 Date: Thu, 9 Aug 2018 19:32:33 +0100 Subject: [PATCH] WinGui: Preparing the UI for a move to .NET CORE next year when the Desktop Packs get released. Removing use of legacy Microsoft.VisualBasic.FileIO reference. Chapter Importer re-written to accommodate. --- .../Utilities/Input/ChapterImporterCsv.cs | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/win/CS/HandBrakeWPF/Utilities/Input/ChapterImporterCsv.cs b/win/CS/HandBrakeWPF/Utilities/Input/ChapterImporterCsv.cs index 5313e3cf9..52bc8a38a 100644 --- a/win/CS/HandBrakeWPF/Utilities/Input/ChapterImporterCsv.cs +++ b/win/CS/HandBrakeWPF/Utilities/Input/ChapterImporterCsv.cs @@ -11,12 +11,11 @@ namespace HandBrakeWPF.Utilities.Input { using System; using System.Collections.Generic; + using System.IO; using HandBrakeWPF.Exceptions; using HandBrakeWPF.Properties; - using Microsoft.VisualBasic.FileIO; - /// /// Handles the importing of Chapter information from CSV files /// @@ -27,51 +26,63 @@ namespace HandBrakeWPF.Utilities.Input /// public static string FileFilter => "CSV files (*.csv;*.tsv)|*.csv;*.tsv"; - /// - /// Imports all chapter information from the given into the dictionary. - /// - /// - /// The full path and filename of the chapter marker file to import - /// - /// - /// The imported Chapters. - /// public static void Import(string filename, ref Dictionary> importedChapters) { - using (TextFieldParser csv = new TextFieldParser(filename) + if (!File.Exists(filename)) { - CommentTokens = new[] { "#" }, // Comment lines - Delimiters = new[] { ",", "\t", ";", ":" }, // Support all of these common delimiter types - HasFieldsEnclosedInQuotes = true, // Assume that our data will be properly escaped - TextFieldType = FieldType.Delimited, - TrimWhiteSpace = true // Remove excess whitespace from ends of imported values - }) + throw new FileNotFoundException(); + } + + int lineNumber = 0; + try { - while (!csv.EndOfData) + using (StreamReader reader = new StreamReader(filename)) { - try + // Try guess the delimiter. + string contents = reader.ReadToEnd(); + reader.DiscardBufferedData(); + reader.BaseStream.Seek(0, SeekOrigin.Begin); + bool tabDelimited = contents.Split('\t').Length > contents.Split(',').Length; + + // Parse each line. + while (reader.Peek() >= 0) { - // Only read the first two columns, anything else will be ignored but will not raise an error - var row = csv.ReadFields(); - if (row == null || row.Length < 2) // null condition happens if the file is somehow corrupt during reading - throw new MalformedLineException(Resources.ChaptersViewModel_UnableToImportChaptersLineDoesNotHaveAtLeastTwoColumns, csv.LineNumber); + lineNumber = lineNumber + 1; + string line = reader.ReadLine(); + if (!string.IsNullOrEmpty(line)) + { + string[] splitContents = tabDelimited ? line.Split('\t') : line.Split(','); - int chapterNumber; - if (!int.TryParse(row[0], out chapterNumber)) - throw new MalformedLineException(Resources.ChaptersViewModel_UnableToImportChaptersFirstColumnMustContainOnlyIntegerNumber, csv.LineNumber); + if (splitContents.Length < 2) + { + throw new InvalidDataException( + string.Format( + Resources + .ChaptersViewModel_UnableToImportChaptersLineDoesNotHaveAtLeastTwoColumns, + lineNumber)); + } - // Store the chapter name at the correct index - importedChapters[chapterNumber] = new Tuple(row[1]?.Trim(), TimeSpan.Zero); - } - catch (MalformedLineException mlex) - { - throw new GeneralApplicationException( - Resources.ChaptersViewModel_UnableToImportChaptersWarning, - string.Format(Resources.ChaptersViewModel_UnableToImportChaptersMalformedLineMsg, mlex.LineNumber), - mlex); + if (!int.TryParse(splitContents[0], out var chapterNumber)) + { + throw new InvalidDataException( + string.Format( + Resources + .ChaptersViewModel_UnableToImportChaptersFirstColumnMustContainOnlyIntegerNumber, + lineNumber)); + } + + string chapterName = splitContents[1].Trim(); + + // Store the chapter name at the correct index + importedChapters[chapterNumber] = new Tuple(chapterName, TimeSpan.Zero); + } } } } + catch (Exception e) + { + throw new GeneralApplicationException(Resources.ChaptersViewModel_UnableToImportChaptersWarning, string.Format(Resources.ChaptersViewModel_UnableToImportChaptersMalformedLineMsg, lineNumber), e); + } } } } -- 2.40.0