Skip to content

Commit e167d1e

Browse files
committed
Add project files.
1 parent e8e60fc commit e167d1e

27 files changed

+6243
-0
lines changed

ConsoleTest/ConsoleTest.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp1.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\SubLib.NET\SubLib.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

ConsoleTest/Program.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace ConsoleTest
2+
{
3+
using System;
4+
using System.Reflection;
5+
using SubLib.Formats;
6+
using System.IO;
7+
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
Console.WriteLine("Test running...");
13+
string file = string.empty;
14+
if (!File.Exists(file))
15+
{
16+
throw new FileNotFoundException(file);
17+
}
18+
var sub = new Subrip(file);
19+
Console.WriteLine($"Total paragraphs: {sub.Paragraphs.Count}");
20+
foreach (var p in sub.Paragraphs)
21+
{
22+
Console.WriteLine(p.Text);
23+
}
24+
Console.ReadLine();
25+
}
26+
}
27+
}

SubLib.NET/Configs/Configs.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SubLib.NET.Models
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
public static class Configs
8+
{
9+
static Configs()
10+
{
11+
// todo: init timeConfigs;
12+
TimeConfigs = new TimeConfigs() { FrameRate = 25 };
13+
}
14+
15+
public static TimeConfigs TimeConfigs { get; }
16+
}
17+
}

SubLib.NET/Configs/TimeConfigs.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SubLib.NET.Models
6+
{
7+
public class TimeConfigs
8+
{
9+
public double FrameRate { get; set; }
10+
}
11+
}

SubLib.NET/Enums/ExpectingLine.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SubLib.Formats.Enums
2+
{
3+
public enum ExpectingLineSubrip
4+
{
5+
Number,
6+
TimeCodes,
7+
Text
8+
}
9+
}

SubLib.NET/Formats/MacSub.cs

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
namespace SubLib.Formats
2+
{
3+
using SubLib.Models;
4+
using SubLib.NET.Utils;
5+
using SubLib.Utils;
6+
using System;
7+
using System.Text;
8+
9+
public class MacSub : Subtitle
10+
{
11+
private enum Expecting
12+
{
13+
StartFrame,
14+
Text,
15+
EndFrame
16+
}
17+
18+
public MacSub(string file) : base(file)
19+
{
20+
}
21+
22+
public override string Name => "MacSub";
23+
24+
public override string Extension => ".txt";
25+
26+
public override bool IsFrameBased => true;
27+
28+
public override void Save(string file, Encoding encoding)
29+
{
30+
throw new NotImplementedException();
31+
}
32+
33+
public string ToText()
34+
{
35+
// Startframe
36+
// Text
37+
// Endframe.
38+
const string writeFormat = "/{0}{3}{1}{3}/{2}{3}";
39+
var sb = new StringBuilder();
40+
foreach (var p in Paragraphs)
41+
{
42+
sb.AppendFormat(writeFormat, TimeUtils.MillisecondsToFrames(p.StartTime.TotalMilliseconds), HtmlUtils.RemoveHtmlTags(p.Text, true),
43+
TimeUtils.MillisecondsToFrames(p.EndTime.TotalMilliseconds), Environment.NewLine);
44+
}
45+
return sb.ToString();
46+
}
47+
48+
public override void Load(string[] lines)
49+
{
50+
var expecting = Expecting.StartFrame;
51+
Paragraphs.Clear();
52+
_errorCount = 0;
53+
char[] trimChar = { '/' };
54+
var p = new Paragraph();
55+
for (int i = 0, lineNumber = 1; i < lines.Length; i++)
56+
{
57+
string line = lines[i].Trim();
58+
string nextLine = null;
59+
if (i + 1 < lines.Length)
60+
{
61+
nextLine = lines[i + 1].Trim();
62+
}
63+
try
64+
{
65+
switch (expecting)
66+
{
67+
case Expecting.StartFrame:
68+
if (ContainsOnlyNumber(line))
69+
{
70+
double ms = TimeUtils.FramesToMilliseconds(int.Parse(line.TrimStart(trimChar)));
71+
p.StartTime = new TimeCode(ms);
72+
expecting = Expecting.Text;
73+
}
74+
else
75+
{
76+
_errorCount++;
77+
}
78+
break;
79+
80+
case Expecting.Text:
81+
line = HtmlUtils.RemoveHtmlTags(line, true);
82+
p.Text += string.IsNullOrEmpty(p.Text) ? line : Environment.NewLine + line;
83+
// Next reading is going to be endframe if next line starts with (/) delimeter which indicates frame start.
84+
if ((nextLine == null) || (nextLine.Length > 0 && nextLine[0] == '/'))
85+
{
86+
expecting = Expecting.EndFrame;
87+
p.Number = lineNumber++;
88+
}
89+
break;
90+
91+
case Expecting.EndFrame:
92+
if (ContainsOnlyNumber(line))
93+
{
94+
double ms = TimeUtils.FramesToMilliseconds(int.Parse(line.TrimStart(trimChar)));
95+
p.EndTime = new TimeCode(ms);
96+
Paragraphs.Add(p);
97+
// Prepare for next reading.
98+
p = new Paragraph();
99+
expecting = Expecting.StartFrame;
100+
}
101+
else
102+
{
103+
_errorCount++;
104+
}
105+
break;
106+
}
107+
}
108+
catch
109+
{
110+
_errorCount++;
111+
}
112+
}
113+
}
114+
115+
private static bool ContainsOnlyNumber(string input)
116+
{
117+
int len = input.Length;
118+
// 10 = length of int.MaxValue (2147483647); +1 if starts with '/'
119+
if (len == 0 || len > 11 || input[0] != '/')
120+
{
121+
return false;
122+
}
123+
int halfLen = len / 2;
124+
for (int i = 1; i <= halfLen; i++) // /10.0 (Do not parse double)
125+
{
126+
if (!(CharUtils.IsDigit(input[i]) && CharUtils.IsDigit(input[len - i])))
127+
{
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
134+
}
135+
}

0 commit comments

Comments
 (0)