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 \r
10 namespace Handbrake.Parsing\r
11 {\r
12     /// <summary>\r
13     /// A delegate to handle custom events regarding data being parsed from the buffer\r
14     /// </summary>\r
15     /// <param name="Sender">The object which raised this delegate</param>\r
16     /// <param name="Data">The data parsed from the stream</param>\r
17     public delegate void DataReadEventHandler(object Sender, string Data);\r
18 \r
19     /// <summary>\r
20     /// A delegate to handle events regarding progress during DVD scanning\r
21     /// </summary>\r
22     /// <param name="Sender">The object who's raising the event</param>\r
23     /// <param name="CurrentTitle">The title number currently being processed</param>\r
24     /// <param name="TitleCount">The total number of titiles to be processed</param>\r
25     public delegate void ScanProgressEventHandler(object Sender, int CurrentTitle, int TitleCount);\r
26 \r
27     /// <summary>\r
28     /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process\r
29     /// </summary>\r
30     internal class Parser : StreamReader\r
31     {\r
32         private string m_buffer;\r
33         /// <summary>\r
34         /// The output from the CLI process\r
35         /// </summary>\r
36         public string Buffer\r
37         {\r
38             get\r
39             {\r
40                 return m_buffer;\r
41             }\r
42         }\r
43 \r
44         /// <summary>\r
45         /// Raised upon a new line being read from stdout/stderr\r
46         /// </summary>\r
47         public event DataReadEventHandler OnReadLine;\r
48 \r
49         /// <summary>\r
50         /// Raised upon the entire stdout/stderr stream being read in a single call\r
51         /// </summary>\r
52         public event DataReadEventHandler OnReadToEnd;\r
53 \r
54         /// <summary>\r
55         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
56         /// </summary>\r
57         public event ScanProgressEventHandler OnScanProgress;\r
58 \r
59 \r
60         /// <summary>\r
61         /// Default constructor for this object\r
62         /// </summary>\r
63         /// <param name="baseStream">The stream to parse from</param>\r
64         public Parser(Stream baseStream)\r
65             : base(baseStream)\r
66         {\r
67             m_buffer = string.Empty;\r
68         }\r
69 \r
70         public override string ReadLine()\r
71         {\r
72             string tmp = base.ReadLine();\r
73 \r
74             m_buffer += tmp;\r
75             Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
76             if (OnReadLine != null)\r
77                 OnReadLine(this, tmp);\r
78 \r
79             if (m.Success && OnScanProgress != null)\r
80                 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
81 \r
82             return tmp;\r
83         }\r
84 \r
85         public override string ReadToEnd()\r
86         {\r
87             string tmp = base.ReadToEnd();\r
88 \r
89             m_buffer += tmp;\r
90             if (OnReadToEnd != null)\r
91                 OnReadToEnd(this, tmp);\r
92 \r
93             return tmp;\r
94         }\r
95     }\r
96 }\r