1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="ChapterImporterTxt.cs" company="HandBrake Project (http://handbrake.fr)">
3 //   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
4 // </copyright>
5 // <summary>
6 //   Imports chapter markers in the ChaptersDb.org TXT format
7 //   More info: http://www.chapterdb.org/docs
8 // </summary>
9 // --------------------------------------------------------------------------------------------------------------------
10 
11 namespace HandBrakeWPF.Utilities.Input
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.IO;
16     using System.Linq;
17     using HandBrakeWPF.Helpers;
18 
19     /// <summary>
20     /// Imports chapter markers in the ChaptersDb.org TXT format
21     /// More info: http://www.chapterdb.org/docs
22     /// </summary>
23     public class ChapterImporterTxt
24     {
25         /// <summary>
26         /// The file filter value for the OpenFileDialog
27         /// </summary>
28         public static string FileFilter => "Text files (*.txt)|*.txt";
29 
30         /// <summary>
31         /// Imports all chapter information from the given <see cref="filename"/> into the <see cref="chapterMap"/> dictionary.
32         /// </summary>
33         /// <param name="filename">The full path and filename of the chapter marker file to import</param>
34         /// <param name="chapterMap">The dictionary that should be populated with parsed chapter markers</param>
Import(string filename, ref Dictionary<int, Tuple<string, TimeSpan>> chapterMap)35         public static void Import(string filename, ref Dictionary<int, Tuple<string, TimeSpan>> chapterMap)
36         {
37             using (var file = new StreamReader(filename))
38             {
39                 // Indexing is 1-based
40                 int chapterMapIdx = 1;
41                 TimeSpan prevChapterStart = TimeSpan.Zero;
42 
43                 while (!file.EndOfStream)
44                 {
45                     // Read the lines in pairs, the duration is always first then the chapter name
46                     var chapterStartRaw = file.ReadLine();
47                     var chapterName = file.ReadLine();
48 
49                     // If either of the values is null then the end of the file has been reached and we need to terminate
50                     if (chapterName == null || chapterStartRaw == null)
51                         break;
52 
53                     // Split the values on '=' and take the left side
54                     chapterName = chapterName.Split(new[] { '=' }, 2).LastOrDefault();
55                     chapterStartRaw = chapterStartRaw.Split(new[] { '=' }, 2).LastOrDefault();
56 
57                     // Parse the time
58                     if (!string.IsNullOrWhiteSpace(chapterStartRaw))
59                     {
60                         var chapterStart = TimeSpanHelper.ParseChapterTimeStart(chapterStartRaw);
61 
62                         // If we're past the first chapter in the file then calculate the duration for the previous chapter
63                         if (chapterMapIdx > 1)
64                         {
65                             var old = chapterMap[chapterMapIdx - 1];
66                             chapterMap[chapterMapIdx - 1] = new Tuple<string, TimeSpan>(old.Item1, chapterStart - prevChapterStart);
67                         }
68 
69                         prevChapterStart = chapterStart;
70                     }
71 
72                     // Save the chapter info, we calculate the duration in the next iteration (look back)
73                     chapterMap[chapterMapIdx++] = new Tuple<string, TimeSpan>(chapterName, TimeSpan.Zero);
74                 }
75             }
76         }
77     }
78 }
79