OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Parser.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 using System.IO;\r
5 using System.Text.RegularExpressions;\r
6 \r
7 namespace Handbrake.Parsing\r
8 {\r
9     /// <summary>\r
10     /// A delegate to handle custom events regarding data being parsed from the buffer\r
11     /// </summary>\r
12     /// <param name="Sender">The object which raised this delegate</param>\r
13     /// <param name="Data">The data parsed from the stream</param>\r
14     public delegate void DataReadEventHandler(object Sender, string Data);\r
15 \r
16     public delegate void ScanProgressEventHandler(object Sender, int CurrentTitle, int TitleCount);\r
17 \r
18     /// <summary>\r
19     /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process\r
20     /// </summary>\r
21     internal class Parser : StreamReader\r
22     {\r
23         private string m_buffer;\r
24         /// <summary>\r
25         /// The output from the CLI process\r
26         /// </summary>\r
27         public string Buffer\r
28         {\r
29             get\r
30             {\r
31                 return this.m_buffer;\r
32             }\r
33         }\r
34 \r
35         /// <summary>\r
36         /// Raised upon a new line being read from stdout/stderr\r
37         /// </summary>\r
38         public static event DataReadEventHandler OnReadLine;\r
39 \r
40         /// <summary>\r
41         /// Raised upon the entire stdout/stderr stream being read in a single call\r
42         /// </summary>\r
43         public static event DataReadEventHandler OnReadToEnd;\r
44 \r
45         public static event ScanProgressEventHandler OnScanProgress;\r
46 \r
47         /// <summary>\r
48         /// Default constructor for this object\r
49         /// </summary>\r
50         /// <param name="baseStream">The stream to parse from</param>\r
51         public Parser(Stream baseStream) : base(baseStream)\r
52         {\r
53             this.m_buffer = string.Empty;\r
54         }\r
55 \r
56         public override string ReadLine()\r
57         {\r
58             string tmp = base.ReadLine();\r
59             this.m_buffer += tmp;\r
60             Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
61             if (OnReadLine != null)\r
62             {\r
63                 OnReadLine(this, tmp);\r
64             }\r
65             if (m.Success && OnScanProgress != null)\r
66             {\r
67                 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
68             }\r
69             return tmp;\r
70         }\r
71 \r
72         public override string ReadToEnd()\r
73         {\r
74             string tmp = base.ReadToEnd();\r
75             this.m_buffer += tmp;\r
76             if (OnReadToEnd != null)\r
77             {\r
78                 OnReadToEnd(this, tmp);\r
79             }\r
80             return tmp;\r
81         }\r
82     }\r
83 }\r