summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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);
+ }
}
}
}