OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Parser.cs
1 /*  Parser.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.Globalization;\r
10     using System.IO;\r
11     using System.Text;\r
12     using System.Text.RegularExpressions;\r
13 \r
14     /// <summary>\r
15     /// A delegate to handle custom events regarding data being parsed from the buffer\r
16     /// </summary>\r
17     /// <param name="sender">The object which raised this delegate</param>\r
18     /// <param name="data">The data parsed from the stream</param>\r
19     public delegate void DataReadEventHandler(object sender, string data);\r
20 \r
21     /// <summary>\r
22     /// A delegate to handle events regarding progress during DVD scanning\r
23     /// </summary>\r
24     /// <param name="sender">The object who's raising the event</param>\r
25     /// <param name="currentTitle">The title number currently being processed</param>\r
26     /// <param name="titleCount">The total number of titiles to be processed</param>\r
27     public delegate void ScanProgressEventHandler(object sender, int currentTitle, int titleCount);\r
28 \r
29     /// <summary>\r
30     /// A delegate to handle encode progress updates // EXPERIMENTAL\r
31     /// </summary>\r
32     /// <param name="sender">The object which raised the event</param>\r
33     /// <param name="currentTask">The current task being processed from the queue</param>\r
34     /// <param name="taskCount">The total number of tasks in queue</param>\r
35     /// <param name="percentComplete">The percentage this task is complete</param>\r
36     /// <param name="currentFps">The current encoding fps</param>\r
37     /// <param name="averageFps">The average encoding fps for this task</param>\r
38     /// <param name="timeRemaining">The estimated time remaining for this task to complete</param>\r
39     public delegate void EncodeProgressEventHandler(object sender, int currentTask, int taskCount, float percentComplete, float currentFps, float averageFps, TimeSpan timeRemaining);\r
40 \r
41 \r
42     /// <summary>\r
43     /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process\r
44     /// </summary>\r
45     internal class Parser : StreamReader\r
46     {\r
47         /// <summary>\r
48         /// The Buffer StringBuilder\r
49         /// </summary>\r
50         private StringBuilder buffer = new StringBuilder(string.Empty);\r
51 \r
52         /// <summary>\r
53         /// Initializes a new instance of the <see cref="Parser"/> class. \r
54         /// Default constructor for this object\r
55         /// </summary>\r
56         /// <param name="baseStream">\r
57         /// The stream to parse from\r
58         /// </param>\r
59         public Parser(Stream baseStream) : base(baseStream)\r
60         {\r
61         }\r
62 \r
63         /// <summary>\r
64         /// Raised upon a new line being read from stdout/stderr\r
65         /// </summary>\r
66         public event DataReadEventHandler OnReadLine;\r
67 \r
68         /// <summary>\r
69         /// Raised upon the entire stdout/stderr stream being read in a single call\r
70         /// </summary>\r
71         public event DataReadEventHandler OnReadToEnd;\r
72 \r
73         /// <summary>\r
74         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
75         /// </summary>\r
76         public event ScanProgressEventHandler OnScanProgress;\r
77 \r
78         /// <summary>\r
79         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
80         /// </summary>\r
81         public event EncodeProgressEventHandler OnEncodeProgress;\r
82 \r
83         /// <summary>\r
84         /// Gets the buffer of data that came from the CLI standard input/error\r
85         /// </summary>\r
86         public string Buffer\r
87         {\r
88             get { return buffer.ToString(); }\r
89         }\r
90 \r
91         /// <summary>\r
92         /// Read a line from standard in/err\r
93         /// </summary>\r
94         /// <returns>\r
95         /// The read line\r
96         /// </returns>\r
97         public override string ReadLine()\r
98         {\r
99             string tmp = base.ReadLine();\r
100 \r
101             buffer.Append(tmp + Environment.NewLine);\r
102 \r
103             Match m = null;\r
104             if (tmp.Contains("Scanning title"))\r
105                 m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
106 \r
107             if (OnReadLine != null)\r
108                 OnReadLine(this, tmp);\r
109 \r
110             if (m != null)\r
111                 if (m.Success && OnScanProgress != null)\r
112                     OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
113 \r
114             return tmp;\r
115         }\r
116 \r
117         /// <summary>\r
118         /// Read to the end of the input stream\r
119         /// </summary>\r
120         /// <returns>\r
121         /// A string of the input data\r
122         /// </returns>\r
123         public override string ReadToEnd()\r
124         {\r
125             string tmp = base.ReadToEnd();\r
126 \r
127             buffer.Append(tmp + Environment.NewLine);\r
128 \r
129             if (OnReadToEnd != null)\r
130                 OnReadToEnd(this, tmp);\r
131 \r
132             return tmp;\r
133         }\r
134 \r
135         /// <summary>\r
136         /// Pase the CLI status output (from standard output)\r
137         /// </summary>\r
138         public void ReadEncodeStatus()\r
139         {\r
140             CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");\r
141             string tmp = base.ReadLine();\r
142 \r
143             Match m = Regex.Match(tmp, @"^Encoding: task ([0-9]*) of ([0-9]*), ([0-9]*\.[0-9]*) %( \(([0-9]*\.[0-9]*) fps, avg ([0-9]*\.[0-9]*) fps, ETA ([0-9]{2})h([0-9]{2})m([0-9]{2})s\))?");\r
144             if (m.Success && OnEncodeProgress != null)\r
145             {\r
146                 int currentTask = int.Parse(m.Groups[1].Value);\r
147                 int totalTasks = int.Parse(m.Groups[2].Value);\r
148                 float percent = float.Parse(m.Groups[3].Value, culture);\r
149                 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);\r
150                 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);\r
151                 TimeSpan remaining = TimeSpan.Zero;\r
152                 if (m.Groups[7].Value != string.Empty)\r
153                 {\r
154                     remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);\r
155                 }\r
156                 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);\r
157             }\r
158         }\r
159     }\r
160 }