OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Subtitle.cs
1 /*  Subtitle.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 using System.Collections.Generic;\r
8 using System.IO;\r
9 using System.Text.RegularExpressions;\r
10 \r
11 namespace Handbrake.Parsing\r
12 {\r
13     /// <summary>\r
14     /// An object that represents a subtitle associated with a Title, in a DVD\r
15     /// </summary>\r
16     public class Subtitle\r
17     {\r
18         private string m_language;\r
19         private int m_trackNumber;\r
20 \r
21         /// <summary>\r
22         /// The track number of this Subtitle\r
23         /// </summary>\r
24         public int TrackNumber\r
25         {\r
26             get { return m_trackNumber; }\r
27         }\r
28 \r
29         /// <summary>\r
30         /// The language (if detected) of this Subtitle\r
31         /// </summary>\r
32         public string Language\r
33         {\r
34             get { return m_language; }\r
35         }\r
36 \r
37         /// <summary>\r
38         /// Override of the ToString method to make this object easier to use in the UI\r
39         /// </summary>\r
40         /// <returns>A string formatted as: {track #} {language}</returns>\r
41         public override string ToString()\r
42         {\r
43             return string.Format("{0} {1}", m_trackNumber, m_language);\r
44         }\r
45 \r
46         public static Subtitle Parse(StringReader output)\r
47         {\r
48             string curLine = output.ReadLine();\r
49 \r
50             Match m = Regex.Match(curLine, @"^    \+ ([0-9]*), ([A-Za-z, ]*) \((.*)\)");\r
51             if (m.Success && !curLine.Contains("HandBrake has exited."))\r
52             {\r
53                 var thisSubtitle = new Subtitle();\r
54                 thisSubtitle.m_trackNumber = int.Parse(m.Groups[1].Value.Trim());\r
55                 thisSubtitle.m_language = m.Groups[2].Value;\r
56                 return thisSubtitle;\r
57             }\r
58             else\r
59                 return null;\r
60         }\r
61 \r
62         public static Subtitle[] ParseList(StringReader output)\r
63         {\r
64             var subtitles = new List<Subtitle>();\r
65             while ((char) output.Peek() != '+')\r
66             {\r
67                 Subtitle thisSubtitle = Parse(output);\r
68 \r
69                 if (thisSubtitle != null)\r
70                     subtitles.Add(thisSubtitle);\r
71                 else\r
72                     break;\r
73             }\r
74             return subtitles.ToArray();\r
75         }\r
76     }\r
77 }