OSDN Git Service

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