OSDN Git Service

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