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 namespace Handbrake.Parsing\r
8 {\r
9     using System;\r
10     using System.Globalization;\r
11     using System.IO;\r
12     using System.Text;\r
13     using System.Text.RegularExpressions;\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(\r
41         object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, \r
42         TimeSpan TimeRemaining);\r
43 \r
44 \r
45     /// <summary>\r
46     /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process\r
47     /// </summary>\r
48     internal class Parser : StreamReader\r
49     {\r
50         private StringBuilder _buffer = new StringBuilder(string.Empty);\r
51 \r
52         /// <summary>\r
53         /// The output from the CLI process\r
54         /// </summary>\r
55         public string Buffer\r
56         {\r
57             get { return _buffer.ToString(); }\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 \r
77         /// <summary>\r
78         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
79         /// </summary>\r
80         public event EncodeProgressEventHandler OnEncodeProgress;\r
81 \r
82         #endregion\r
83 \r
84         /// <summary>\r
85         /// Initializes a new instance of the <see cref="Parser"/> class. \r
86         /// Default constructor for this object\r
87         /// </summary>\r
88         /// <param name="baseStream">\r
89         /// The stream to parse from\r
90         /// </param>\r
91         public Parser(Stream baseStream)\r
92             : base(baseStream)\r
93         {\r
94         }\r
95 \r
96         public override string ReadLine()\r
97         {\r
98             string tmp = base.ReadLine();\r
99 \r
100             _buffer.Append(tmp + Environment.NewLine);\r
101 \r
102             Match m = null;\r
103             if (tmp.Contains("Scanning title"))\r
104                 m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
105 \r
106             if (OnReadLine != null)\r
107                 OnReadLine(this, tmp);\r
108 \r
109             if (m != null)\r
110                 if (m.Success && OnScanProgress != null)\r
111                     OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
112 \r
113             return tmp;\r
114         }\r
115 \r
116         public override string ReadToEnd()\r
117         {\r
118             string tmp = base.ReadToEnd();\r
119 \r
120             _buffer.Append(tmp + Environment.NewLine);\r
121 \r
122             if (OnReadToEnd != null)\r
123                 OnReadToEnd(this, tmp);\r
124 \r
125             return tmp;\r
126         }\r
127 \r
128         /// <summary>\r
129         /// Pase the CLI status output (from standard output)\r
130         /// </summary>\r
131         public void readEncodeStatus()\r
132         {\r
133             CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");\r
134             string tmp = base.ReadLine();\r
135 \r
136             Match m = Regex.Match(tmp, \r
137                                   @"^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
138             if (m.Success && OnEncodeProgress != null)\r
139             {\r
140                 int currentTask = int.Parse(m.Groups[1].Value);\r
141                 int totalTasks = int.Parse(m.Groups[2].Value);\r
142                 float percent = float.Parse(m.Groups[3].Value, culture);\r
143                 float currentFps = m.Groups[5].Value == string.Empty ? 0.0F : float.Parse(m.Groups[5].Value, culture);\r
144                 float avgFps = m.Groups[6].Value == string.Empty ? 0.0F : float.Parse(m.Groups[6].Value, culture);\r
145                 TimeSpan remaining = TimeSpan.Zero;\r
146                 if (m.Groups[7].Value != string.Empty)\r
147                 {\r
148                     remaining = TimeSpan.Parse(m.Groups[7].Value + ":" + m.Groups[8].Value + ":" + m.Groups[9].Value);\r
149                 }\r
150                 OnEncodeProgress(this, currentTask, totalTasks, percent, currentFps, avgFps, remaining);\r
151             }\r
152         }\r
153     }\r
154 }