summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr55 <[email protected]>2018-08-09 19:32:33 +0100
committersr55 <[email protected]>2018-08-09 19:33:06 +0100
commit5fddd0b27f16d01efecfe76a92a695245d88cbcc (patch)
tree9c822d9b4192d118d43b8322447e9b42f798251e
parent71af3fc5fbdf00a8a57e0f9047da3b333aa441a1 (diff)
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.
-rw-r--r--win/CS/HandBrakeWPF/Utilities/Input/ChapterImporterCsv.cs83
1 files 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;
-
/// <summary>
/// Handles the importing of Chapter information from CSV files
/// </summary>
@@ -27,51 +26,63 @@ namespace HandBrakeWPF.Utilities.Input
/// </summary>
public static string FileFilter => "CSV files (*.csv;*.tsv)|*.csv;*.tsv";
- /// <summary>
- /// Imports all chapter information from the given <see cref="filename"/> into the <see cref="chapterMap"/> dictionary.
- /// </summary>
- /// <param name="filename">
- /// The full path and filename of the chapter marker file to import
- /// </param>
- /// <param name="importedChapters">
- /// The imported Chapters.
- /// </param>
public static void Import(string filename, ref Dictionary<int, Tuple<string, TimeSpan>> 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<string, TimeSpan>(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<string, TimeSpan>(chapterName, TimeSpan.Zero);
+ }
}
}
}
+ catch (Exception e)
+ {
+ throw new GeneralApplicationException(Resources.ChaptersViewModel_UnableToImportChaptersWarning, string.Format(Resources.ChaptersViewModel_UnableToImportChaptersMalformedLineMsg, lineNumber), e);
+ }
}
}
}