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