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
8 using System.Collections.Generic;
\r
10 using System.Text.RegularExpressions;
\r
11 using System.Windows.Forms;
\r
13 namespace Handbrake.Parsing
\r
16 /// A delegate to handle custom events regarding data being parsed from the buffer
\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
23 /// A delegate to handle events regarding progress during DVD scanning
\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
31 /// A simple wrapper around a StreamReader to keep track of the entire output from a cli process
\r
33 internal class Parser : StreamReader
\r
35 private string m_buffer;
\r
37 /// The output from the CLI process
\r
39 public string Buffer
\r
43 return this.m_buffer;
\r
48 /// Raised upon a new line being read from stdout/stderr
\r
50 public event DataReadEventHandler OnReadLine;
\r
53 /// Raised upon the entire stdout/stderr stream being read in a single call
\r
55 public event DataReadEventHandler OnReadToEnd;
\r
58 /// Raised upon the catching of a "Scanning title # of #..." in the stream
\r
60 public event ScanProgressEventHandler OnScanProgress;
\r
64 /// Default constructor for this object
\r
66 /// <param name="baseStream">The stream to parse from</param>
\r
67 public Parser(Stream baseStream) : base(baseStream)
\r
69 this.m_buffer = string.Empty;
\r
72 public override string ReadLine()
\r
74 string tmp = base.ReadLine();
\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
83 if (m.Success && OnScanProgress != null)
\r
84 OnScanProgress(this, int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
\r
86 catch (Exception exc)
\r
88 MessageBox.Show("Parser.cs - ReadLine " + exc.ToString());
\r
93 public override string ReadToEnd()
\r
95 string tmp = base.ReadToEnd();
\r
98 this.m_buffer += tmp;
\r
99 if (OnReadToEnd != null)
\r
100 OnReadToEnd(this, tmp);
\r
103 catch (Exception exc)
\r
105 MessageBox.Show("Parser.cs - ReadToEnd " + exc.ToString());
\r