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.m0k.org/>.\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) : base(baseStream)\r
68         {\r
69             this.m_buffer = string.Empty;\r
70         }\r
71 \r
72         public override string ReadLine()\r
73         {\r
74             string tmp = base.ReadLine();\r
75             try\r
76             {\r
77                 \r
78                 this.m_buffer += tmp;\r
79                 Match m = Regex.Match(tmp, "^Scanning title ([0-9]*) of ([0-9]*)");\r
80                 if (OnReadLine != null)\r
81                     OnReadLine(this, tmp);\r
82 \r
83                 if (m.Success && OnScanProgress != null)\r
84                     OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
85             }\r
86             catch (Exception exc)\r
87             {\r
88                 MessageBox.Show("Parser.cs - ReadLine " + exc.ToString());\r
89             }\r
90             return tmp;\r
91         }\r
92 \r
93         public override string ReadToEnd()\r
94         {\r
95             string tmp = base.ReadToEnd();\r
96             try\r
97             {\r
98                 this.m_buffer += tmp;\r
99                 if (OnReadToEnd != null)\r
100                     OnReadToEnd(this, tmp);\r
101 \r
102             }\r
103             catch (Exception exc)\r
104             {\r
105                 MessageBox.Show("Parser.cs - ReadToEnd " + exc.ToString());\r
106             }\r
107             return tmp;\r
108         }\r
109     }\r
110 }\r