public override void Read(DataStream strIn) { this.entries = new List<SubtitleEntry>(); // Gets all the lines DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis")); string[] lines = reader.ReadString().Split(ExtraSplit); for (int i = 0; i < lines.Length; i++) { SubtitleEntry entry = new SubtitleEntry(); if (string.IsNullOrEmpty(lines[i]) || lines[i] == "\n") continue; string line = lines[i].Substring(1); if (line[0] == '#') { entry.Type = SubtitleType.Comment; entry.Data = line.Substring(1).ApplyTable("replace", false); } else if (line.StartsWith("/stream", StringComparison.Ordinal)) { entry.Type = SubtitleType.Voice; entry.Data = line.Substring(8); } else if (line.StartsWith("/sync", StringComparison.Ordinal)) { entry.Type = SubtitleType.SyncTime; entry.Data = line.Substring(6); } else if (line.StartsWith("/clear", StringComparison.Ordinal)) { entry.Type = SubtitleType.Clear; } else { entry.Type = SubtitleType.Text; entry.Data = line.ApplyTable("replace", false); } this.entries.Add(entry); } }
public override void Read(DataStream strIn) { this.entries = new List <SubtitleEntry>(); // Gets all the lines DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis")); string[] lines = reader.ReadString().Split(ExtraSplit); for (int i = 0; i < lines.Length; i++) { SubtitleEntry entry = new SubtitleEntry(); if (string.IsNullOrEmpty(lines[i]) || lines[i] == "\n") { continue; } string line = lines[i].Substring(1); if (line[0] == '#') { entry.Type = SubtitleType.Comment; entry.Data = line.Substring(1).ApplyTable("replace", false); } else if (line.StartsWith("/stream", StringComparison.Ordinal)) { entry.Type = SubtitleType.Voice; entry.Data = line.Substring(8); } else if (line.StartsWith("/sync", StringComparison.Ordinal)) { entry.Type = SubtitleType.SyncTime; entry.Data = line.Substring(6); } else if (line.StartsWith("/clear", StringComparison.Ordinal)) { entry.Type = SubtitleType.Clear; } else { entry.Type = SubtitleType.Text; entry.Data = line.ApplyTable("replace", false); } this.entries.Add(entry); } }
protected override void Import(XElement root) { this.entries = new List <SubtitleEntry>(); foreach (XElement el in root.Elements()) { SubtitleEntry entry = new SubtitleEntry(); switch (el.Name.LocalName) { case "Text": entry.Type = SubtitleType.Text; entry.Data = el.Value.FromXmlString('<', '>'); break; case "Comment": entry.Type = SubtitleType.Comment; entry.Data = el.Value.FromXmlString('<', '>'); break; case "Voice": entry.Type = SubtitleType.Voice; entry.Data = el.Attribute("File").Value; break; case "Sync": entry.Type = SubtitleType.SyncTime; entry.Data = el.Attribute("Time").Value; break; case "Clear": entry.Type = SubtitleType.Clear; break; default: throw new FormatException("Invalid XML tag."); } this.entries.Add(entry); } }
protected override void Import(XElement root) { this.entries = new List<SubtitleEntry>(); foreach (XElement el in root.Elements()) { SubtitleEntry entry = new SubtitleEntry(); switch (el.Name.LocalName) { case "Text": entry.Type = SubtitleType.Text; entry.Data = el.Value.FromXmlString('<', '>'); break; case "Comment": entry.Type = SubtitleType.Comment; entry.Data = el.Value.FromXmlString('<', '>'); break; case "Voice": entry.Type = SubtitleType.Voice; entry.Data = el.Attribute("File").Value; break; case "Sync": entry.Type = SubtitleType.SyncTime; entry.Data = el.Attribute("Time").Value; break; case "Clear": entry.Type = SubtitleType.Clear; break; default: throw new FormatException("Invalid XML tag."); } this.entries.Add(entry); } }
private SubtitleList srtToSubtitleList(string fileContent) { //TODO: detect encoding better. Assuming encoding is correct... // assuming file is .srt type. SubtitleList subList = new SubtitleList(); int entryNum = 0; int entryStatus = 0; // 0 = expecting new entry, 1 = expecting time, 2 = reading text until empty line. SubtitleEntry entry = null; using (StringReader reader = new StringReader(fileContent)) { string line; int lineNum = 0; while ((line = reader.ReadLine()) != null) { Debug.WriteLine(lineNum + ": " + line); switch (entryStatus) { case 0: entryNum++; if (int.Parse(line) != entryNum) { throw new Exception("Bad format in line " + lineNum + ". Expected " + entryNum + " got " + line); } entry = new SubtitleEntry(); subList.Add(entry); entry.Index = entryNum; entryStatus = 1; break; case 1: if (!srtTime.IsMatch(line)) { throw new Exception("Bad format in time line " + lineNum); } string startStr = "1/1/0001 " + line.Substring(0, 12).Replace(',', '.'); string endStr = "1/1/0001 " + line.Substring(17, 12).Replace(',', '.'); DateTime st = Convert.ToDateTime(startStr); entry.StartTime = st.Ticks; DateTime et = Convert.ToDateTime(endStr); entry.EndTime = et.Ticks; entryStatus = 2; break; case 2: // all of these lines until empty string is the entry string if (String.IsNullOrEmpty(line)) { entryStatus = 0; } else { var textLine = StripHTML(line); if (String.IsNullOrEmpty(entry.Text)) { entry.Text = textLine; } else { entry.Text += " " + textLine; } } break; default: break; } lineNum++; } } return(subList); }