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;\r
8 using System.Collections.Generic;\r
9 using System.IO;\r
10 using System.Text.RegularExpressions;\r
11 using System.Windows.Forms;\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 simple wrapper around a StreamReader to keep track of the entire output from a cli process\r
32     /// </summary>\r
33     internal class Parser : StreamReader\r
34     {\r
35         private string m_buffer;\r
36         /// <summary>\r
37         /// The output from the CLI process\r
38         /// </summary>\r
39         public string Buffer\r
40         {\r
41             get\r
42             {\r
43                 return this.m_buffer;\r
44             }\r
45         }\r
46 \r
47         /// <summary>\r
48         /// Raised upon a new line being read from stdout/stderr\r
49         /// </summary>\r
50         public event DataReadEventHandler OnReadLine;\r
51 \r
52         /// <summary>\r
53         /// Raised upon the entire stdout/stderr stream being read in a single call\r
54         /// </summary>\r
55         public event DataReadEventHandler OnReadToEnd;\r
56 \r
57         /// <summary>\r
58         /// Raised upon the catching of a "Scanning title # of #..." in the stream\r
59         /// </summary>\r
60         public event ScanProgressEventHandler OnScanProgress;\r
61 \r
62 \r
63         /// <summary>\r
64         /// Default constructor for this object\r
65         /// </summary>\r
66         /// <param name="baseStream">The stream to parse from</param>\r
67         public Parser(Stream baseStream)\r
68             : base(baseStream)\r
69         {\r
70             this.m_buffer = string.Empty;\r
71         }\r
72 \r
73         public override string ReadLine()\r
74         {\r
75             string tmp = base.ReadLine();\r
76 \r
77             this.m_buffer += tmp;\r
78             Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
79             if (OnReadLine != null)\r
80                 OnReadLine(this, tmp);\r
81 \r
82             if (m.Success && OnScanProgress != null)\r
83                 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
84 \r
85             return tmp;\r
86         }\r
87 \r
88         public override string ReadToEnd()\r
89         {\r
90             string tmp = base.ReadToEnd();\r
91 \r
92             this.m_buffer += tmp;\r
93             if (OnReadToEnd != null)\r
94                 OnReadToEnd(this, tmp);\r
95 \r
96             return tmp;\r
97         }\r
98     }\r
99 }\r