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