OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Chapter.cs
1 /*  Chapter.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake.Parsing\r
7 {\r
8     using System;\r
9     using System.Collections.Generic;\r
10     using System.IO;\r
11     using System.Text.RegularExpressions;\r
12 \r
13     /// <summary>\r
14     /// An object representing a Chapter aosciated with a Title, in a DVD\r
15     /// </summary>\r
16     public class Chapter\r
17     {\r
18         /// <summary>\r
19         /// Gets The number of this Chapter, in regards to it's parent Title\r
20         /// </summary>\r
21         public int ChapterNumber { get; private set; }\r
22 \r
23         /// <summary>\r
24         /// Gets The length in time this Chapter spans\r
25         /// </summary>\r
26         public TimeSpan Duration { get; private set; }\r
27 \r
28         /// <summary>\r
29         /// Parse a CLI string to a Chapter object\r
30         /// </summary>\r
31         /// <param name="output">\r
32         /// The output.\r
33         /// </param>\r
34         /// <returns>\r
35         /// A chapter Object\r
36         /// </returns>\r
37         public static Chapter Parse(StringReader output)\r
38         {\r
39             Match m = Regex.Match(\r
40                                   output.ReadLine(),\r
41                                   @"^    \+ ([0-9]*): cells ([0-9]*)->([0-9]*), ([0-9]*) blocks, duration ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
42             if (m.Success)\r
43             {\r
44                 var thisChapter = new Chapter\r
45                                       {\r
46                                           ChapterNumber = int.Parse(m.Groups[1].Value.Trim()), \r
47                                           Duration = TimeSpan.Parse(m.Groups[5].Value)\r
48                                       };\r
49                 return thisChapter;\r
50             }\r
51             return null;\r
52         }\r
53 \r
54         /// <summary>\r
55         /// Prase a list of strings / chatpers\r
56         /// </summary>\r
57         /// <param name="output">\r
58         /// The output.\r
59         /// </param>\r
60         /// <returns>\r
61         /// An array of chapter objects\r
62         /// </returns>\r
63         public static Chapter[] ParseList(StringReader output)\r
64         {\r
65             var chapters = new List<Chapter>();\r
66 \r
67             // this is to read the "  + chapters:" line from the buffer\r
68             // so we can start reading the chapters themselvs\r
69             output.ReadLine();\r
70 \r
71             while (true)\r
72             {\r
73                 // Start of the chapter list for this Title\r
74                 Chapter thisChapter = Parse(output);\r
75 \r
76                 if (thisChapter != null)\r
77                     chapters.Add(thisChapter);\r
78                 else\r
79                     break;\r
80             }\r
81             return chapters.ToArray();\r
82         }\r
83 \r
84         /// <summary>\r
85         /// Override of the ToString method to make this object easier to use in the UI\r
86         /// </summary>\r
87         /// <returns>A string formatted as: {chapter #}</returns>\r
88         public override string ToString()\r
89         {\r
90             return ChapterNumber.ToString();\r
91         }\r
92     }\r
93 }