OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Parser.cs
1 /*  Parser.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.IO;\r
8 using System.Text.RegularExpressions;\r
9 using System;\r
10 using System.Globalization;\r
11 \r
12 namespace Handbrake.Parsing\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         private string m_buffer;\r
48         /// <summary>\r
49         /// The output from the CLI process\r
50         /// </summary>\r
51         public string Buffer\r
52         {\r
53             get\r
54             {\r
55                 return m_buffer;\r
56             }\r
57         }\r
58 \r
59         /// <summary>\r
60         /// Raised upon a new line being read from stdout/stderr\r
61         /// </summary>\r
62         public event DataReadEventHandler OnReadLine;\r
63 \r
64         /// <summary>\r
65         /// Raised upon the entire stdout/stderr stream being read in a single call\r
66         /// </summary>\r
67         public event DataReadEventHandler OnReadToEnd;\r
68 \r
69         /// <summary>\r
70         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
71         /// </summary>\r
72         public event ScanProgressEventHandler OnScanProgress;\r
73 \r
74         #region Experimetnal Code\r
75         /// <summary>\r
76         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
77         /// </summary>\r
78         public event EncodeProgressEventHandler OnEncodeProgress;\r
79         #endregion\r
80 \r
81         /// <summary>\r
82         /// Default constructor for this object\r
83         /// </summary>\r
84         /// <param name="baseStream">The stream to parse from</param>\r
85         public Parser(Stream baseStream) : base(baseStream)\r
86         {\r
87             m_buffer = string.Empty;\r
88         }\r
89 \r
90         public override string ReadLine()\r
91         {\r
92             string tmp = base.ReadLine();\r
93 \r
94             m_buffer += tmp + Environment.NewLine;\r
95             Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
96             if (OnReadLine != null)\r
97                 OnReadLine(this, tmp);\r
98 \r
99             if (m.Success && OnScanProgress != null)\r
100                 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
101 \r
102             return tmp;\r
103         }\r
104 \r
105         public override string ReadToEnd()\r
106         {\r
107             string tmp = base.ReadToEnd();\r
108 \r
109             m_buffer += tmp + Environment.NewLine;\r
110             if (OnReadToEnd != null)\r
111                 OnReadToEnd(this, tmp);\r
112 \r
113             return tmp;\r
114         }\r
115 \r
116         /// <summary>\r
117         /// Pase the CLI status output (from standard output)\r
118         /// </summary>\r
119         public void readEncodeStatus()\r
120         {\r
121             CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");\r
122             string tmp = base.ReadLine();\r
123 \r
124             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
125             if (m.Success && OnEncodeProgress != null)\r
126             {\r
127                 int currentTask = int.Parse(m.Groups[1].Value);\r
128                 int totalTasks = int.Parse(m.Groups[2].Value);\r
129                 float percent = float.Parse(m.Groups[3].Value, culture);\r
130                 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);\r
131                 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);\r
132                 TimeSpan remaining = TimeSpan.Zero;\r
133                 if (m.Groups[7].Value != string.Empty)\r
134                 {\r
135                     remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);\r
136                 }\r
137                 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);\r
138             }\r
139         }\r
140     }\r
141 }\r