-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCaptionParser.cs
158 lines (132 loc) · 3.23 KB
/
CaptionParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
namespace Subindex
{
/// <summary>
///
/// </summary>
public class CaptionParser
{
private CaptionParser()
{
}
public static Caption Parse(string FileName)
{
return Parse(Pub.ReadFile(FileName));
}
public static Caption Parse(string[] Buffer)
{
int i=0;
int cnt=10<Buffer.Length?10:Buffer.Length;
for (;i<cnt;i++)
if (Buffer[i].IndexOf(Pub.SrtSymbol)>-1) break;
Caption ct=i<10?ParseSrt(Buffer):ParseSsa(Buffer);
if (ct.Count==0)
{
ct=null;
throw new ExtendException(0);
// MessageBox.Show(Pub.GetSetting("CaptionParseExceptionText"),Pub.GetSetting("CaptionParseExceptionTitle"),MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
//if (ct[0].ToString(-1,0,false).StartsWith(Pub.copyRight)) ct.Remove(0);
//ct.AdjustFirstIndex=ct.BuildFirstIndex=0+Pub.BeginIndex;
//ct.AdjustLastIndex=ct.BuildLastIndex=ct.Count-1+Pub.BeginIndex;
}
return ct;
}
public static Caption ParseSrt(string[] Buffer)
{
Caption cap=new Caption();
Section sec=new Section();
for (int i=0;i<Buffer.Length;i++)
{
if (Buffer[i]=="") continue;
if (Buffer[i].IndexOf(Pub.SrtSymbol)>-1)
{
sec=ExtractSrtSection(Buffer[i]);
cap.Add(sec);
}
else
{
try
{
int.Parse(Buffer[i]);
}
catch
{
if (sec.Items.Count==0) sec.StyleFormat=ExtractStyle(ref Buffer[i]);
sec.Items.Add(Buffer[i]);
}
}
}
return cap;
}
private static Section ExtractSrtSection(string line)
{
Section sec=null;
line=line.Replace(",",".");
int i=line.IndexOf(Pub.SrtSymbol);
if (i>-1)
{
sec=new Section();
try
{
sec.BeginTime = ParseTime(line.Substring(0, i));
sec.EndTime = ParseTime(line.Substring(i + Pub.SrtSymbol.Length));
}
catch(Exception e)
{
int b=1;
b=3;
}
}
return sec;
}
private static DateTime ParseTime(string line)
{
return DateTime.Parse(line.Trim().Replace(" ","0"));
}
public static Caption ParseSsa(string[] Buffer)
{
Caption cap=new Caption();
for (int i=0;i<Buffer.Length;i++)
{
if (Buffer[i].IndexOf(Pub.SsaSymbol)>-1)
cap.Add(ExtractSsaSection(Buffer[i]));
}
return cap;
}
private static Section ExtractSsaSection(string line)
{
Section sec=null;
if (line.IndexOf(Pub.SsaSymbol)>-1)
{
string [] split=line.Split(Pub.SsaLimit.ToCharArray(),10);
sec=new Section();
sec.BeginTime=System.DateTime.Parse(split[1]);
sec.EndTime=System.DateTime.Parse(split[2]);
sec.StyleFormat=ExtractStyle(ref split[9]);
split[9]=split[9].Replace(@"\N",@"\n");
int i;
while ((i=split[9].IndexOf(@"\n"))>-1)
{
sec.Items.Add(split[9].Substring(0,i));
split[9]=split[9].Substring(i+2);
}
sec.Items.Add(split[9]);
}
return sec;
}
private static string ExtractStyle(ref string line)
{
string style=null;
int i=line.IndexOf(Pub.StyleEndChar);
if (line.IndexOf(Pub.StyleBeginChar)==0&&i>0)
{
style=line.Substring(0,i+1);
line=line.Substring(i+1);
}
return style;
}
}
}