/* Subtitle.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ namespace Handbrake.Parsing { using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; /// /// An object that represents a subtitle associated with a Title, in a DVD /// public class Subtitle { /// /// The Language /// private string language; /// /// The Track Number /// private int trackNumber; /// /// The type of subtitle /// private string type; /// /// The typecode /// private string typecode; /// /// Gets the track number of this Subtitle /// public int TrackNumber { get { return trackNumber; } } /// /// Gets the The language (if detected) of this Subtitle /// public string Language { get { return language; } } /// /// Gets the Langauage Code /// public string LanguageCode { get { return typecode; } } /// /// Gets the Subtitle Type /// public string Type { get { return type; } } /// /// Parse the input strings related to subtitles /// /// /// The output. /// /// /// A Subitle object /// public static Subtitle Parse(StringReader output) { string curLine = output.ReadLine(); Match m = Regex.Match(curLine, @"^ \+ ([0-9]*), ([A-Za-z, ]*) \((.*)\) \(([a-zA-Z]*)\)"); if (m.Success && !curLine.Contains("HandBrake has exited.")) { var thisSubtitle = new Subtitle { trackNumber = int.Parse(m.Groups[1].Value.Trim()), language = m.Groups[2].Value, typecode = m.Groups[3].Value, type = m.Groups[4].Value }; return thisSubtitle; } return null; } /// /// Parse a list of Subtitle tracks from an input string. /// /// /// The output. /// /// /// An array of Subtitle objects /// public static IEnumerable ParseList(StringReader output) { var subtitles = new List(); while ((char) output.Peek() != '+') { Subtitle thisSubtitle = Parse(output); if (thisSubtitle != null) subtitles.Add(thisSubtitle); else break; } return subtitles.ToArray(); } /// /// Override of the ToString method to make this object easier to use in the UI /// /// A string formatted as: {track #} {language} public override string ToString() { return string.Format("{0} {1} ({2})", trackNumber, language, type); } } }