Skip to content

Commit

Permalink
Some Fixes From PVS-Studio #2
Browse files Browse the repository at this point in the history
  • Loading branch information
LinqManJr committed Mar 13, 2018
1 parent 6518c0d commit 39cb777
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 123 deletions.
30 changes: 5 additions & 25 deletions libse/SubtitleFormats/LambdaCap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,22 @@ private static string DecodeStyle(string line)
else if (ch == '@')
{
var part = line.Substring(i);
if (part.StartsWith("@ルビ上[", StringComparison.Ordinal)) // Bouten on first line - horizontal positioning
if (part.StartsWith("@ルビ上[", StringComparison.Ordinal)) // Bouten and Ruby on first line - horizontal positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ下[", StringComparison.Ordinal)) // Bouten on second line - horizontal positioning
else if (part.StartsWith("@ルビ下[", StringComparison.Ordinal)) // Bouten and Ruby on second line - horizontal positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ右[", StringComparison.Ordinal)) // Bouten on first line - vertical positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ左[", StringComparison.Ordinal)) // Bouten on second line - vertical positioning
else if (part.StartsWith("@ルビ左[", StringComparison.Ordinal)) // Bouten and Ruby on second line - vertical positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ上[", StringComparison.Ordinal)) // Ruby on first line - horizontal positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ下[", StringComparison.Ordinal)) // Ruby on second line - horizontal positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ右[", StringComparison.Ordinal)) // Ruby on first line - vertical positioning
{
readUntilEndCode = true;
}
else if (part.StartsWith("@ルビ左[", StringComparison.Ordinal)) // Ruby on second line - vertical positioning
{
readUntilEndCode = true;
}
}
else if (part.StartsWith("@組[", StringComparison.Ordinal)) // Kumi-moji / Tatechuyoko (up to 3 half width digits including numerical punctuation)
{
readUntilEndCode = true;
Expand Down Expand Up @@ -286,11 +270,7 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
subtitle.Paragraphs.Add(p);
}
}
}
else if (string.IsNullOrWhiteSpace(line))
{
// skip these lines
}
}
else if (p != null)
{
if (string.IsNullOrEmpty(p.Text))
Expand Down
4 changes: 2 additions & 2 deletions libse/SubtitleFormats/MacCaption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
string s = line.Trim();

if (s.StartsWith("//", StringComparison.Ordinal) || s.StartsWith("File Format=MacCaption_MCC", StringComparison.Ordinal) || s.StartsWith("UUID=", StringComparison.Ordinal) ||
if (string.IsNullOrEmpty(s) || s.StartsWith("//", StringComparison.Ordinal) || s.StartsWith("File Format=MacCaption_MCC", StringComparison.Ordinal) || s.StartsWith("UUID=", StringComparison.Ordinal) ||
s.StartsWith("Creation Program=") || s.StartsWith("Creation Date=") || s.StartsWith("Creation Time=") ||
s.StartsWith("Code Rate=", StringComparison.Ordinal) || s.StartsWith("Time Code Rate=", StringComparison.Ordinal) || string.IsNullOrEmpty(s))
s.StartsWith("Code Rate=", StringComparison.Ordinal) || s.StartsWith("Time Code Rate=", StringComparison.Ordinal))
{
header.AppendLine(line);
}
Expand Down
21 changes: 10 additions & 11 deletions libse/SubtitleFormats/NetflixTimedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ public override string ToText(Subtitle subtitle, string title)
try
{
var ssaStyle = AdvancedSubStationAlpha.GetSsaStyle(styleName, subtitle.Header);
if (ssaStyle != null)
{
string fontStyle = "normal";
if (ssaStyle.Italic)
fontStyle = "italic";
string fontWeight = "normal";
if (ssaStyle.Bold)
fontWeight = "bold";
AddStyleToXml(x, styleHead, xnsmgr, ssaStyle.Name, ssaStyle.FontName, fontWeight, fontStyle, Utilities.ColorToHex(ssaStyle.Primary), ssaStyle.FontSize.ToString());
convertedFromSubStationAlpha = true;
}

string fontStyle = "normal";
if (ssaStyle.Italic)
fontStyle = "italic";
string fontWeight = "normal";
if (ssaStyle.Bold)
fontWeight = "bold";
AddStyleToXml(x, styleHead, xnsmgr, ssaStyle.Name, ssaStyle.FontName, fontWeight, fontStyle, Utilities.ColorToHex(ssaStyle.Primary), ssaStyle.FontSize.ToString());
convertedFromSubStationAlpha = true;

}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion libse/SubtitleFormats/ScenaristClosedCaptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ public static string GetSccText(string s, ref int errorCount)
var cp = GetColorAndPosition(part);
if (cp != null)
{
if (cp.Y > 0 && y >= 0 && cp.Y > y && !sb.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(sb.ToString()))
if (!string.IsNullOrWhiteSpace(sb.ToString()) && cp.Y > 0 && y >= 0 && cp.Y > y && !sb.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
sb.AppendLine();
if (cp.Y > 0)
y = cp.Y;
Expand Down
20 changes: 9 additions & 11 deletions libse/SubtitleFormats/SmpteTt2052.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ public override string ToText(Subtitle subtitle, string title)
try
{
var ssaStyle = AdvancedSubStationAlpha.GetSsaStyle(styleName, subtitle.Header);
if (ssaStyle != null)
{
string fontStyle = "normal";
if (ssaStyle.Italic)
fontStyle = "italic";
string fontWeight = "normal";
if (ssaStyle.Bold)
fontWeight = "bold";
AddStyleToXml(x, styleHead, xnsmgr, ssaStyle.Name, ssaStyle.FontName, fontWeight, fontStyle, Utilities.ColorToHex(ssaStyle.Primary), ssaStyle.FontSize.ToString());
convertedFromSubStationAlpha = true;
}

string fontStyle = "normal";
if (ssaStyle.Italic)
fontStyle = "italic";
string fontWeight = "normal";
if (ssaStyle.Bold)
fontWeight = "bold";
AddStyleToXml(x, styleHead, xnsmgr, ssaStyle.Name, ssaStyle.FontName, fontWeight, fontStyle, Utilities.ColorToHex(ssaStyle.Primary), ssaStyle.FontSize.ToString());
convertedFromSubStationAlpha = true;
}
catch
{
Expand Down
95 changes: 47 additions & 48 deletions libse/SubtitleFormats/SubStationAlpha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,53 +148,52 @@ private static void LoadStylesFromAdvancedSubstationAlpha(Subtitle subtitle, str
try
{
var ssaStyle = AdvancedSubStationAlpha.GetSsaStyle(styleName, subtitle.Header);
if (ssaStyle != null)

string bold = "0";
if (ssaStyle.Bold)
bold = "-1";
string italic = "0";
if (ssaStyle.Italic)
italic = "-1";

string newAlignment = "2";
switch (ssaStyle.Alignment)
{
string bold = "0";
if (ssaStyle.Bold)
bold = "-1";
string italic = "0";
if (ssaStyle.Italic)
italic = "-1";

string newAlignment = "2";
switch (ssaStyle.Alignment)
{
case "1":
newAlignment = "1";
break;
case "3":
newAlignment = "3";
break;
case "4":
newAlignment = "9";
break;
case "5":
newAlignment = "10";
break;
case "6":
newAlignment = "11";
break;
case "7":
newAlignment = "5";
break;
case "8":
newAlignment = "6";
break;
case "9":
newAlignment = "7";
break;
}
case "1":
newAlignment = "1";
break;
case "3":
newAlignment = "3";
break;
case "4":
newAlignment = "9";
break;
case "5":
newAlignment = "10";
break;
case "6":
newAlignment = "11";
break;
case "7":
newAlignment = "5";
break;
case "8":
newAlignment = "6";
break;
case "9":
newAlignment = "7";
break;
}

//Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
const string styleFormat = "Style: {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},0,1";
// N FN FS PC SC TC BC Bo It BS O Sh Ali ML MR MV A Encoding
//Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
const string styleFormat = "Style: {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},0,1";
// N FN FS PC SC TC BC Bo It BS O Sh Ali ML MR MV A Encoding

ttStyles.AppendLine(string.Format(styleFormat, ssaStyle.Name, ssaStyle.FontName, ssaStyle.FontSize, ssaStyle.Primary.ToArgb(), ssaStyle.Secondary.ToArgb(),
ssaStyle.Outline.ToArgb(), ssaStyle.Background.ToArgb(), bold, italic, ssaStyle.BorderStyle, ssaStyle.OutlineWidth, ssaStyle.ShadowWidth,
newAlignment, ssaStyle.MarginLeft, ssaStyle.MarginRight, ssaStyle.MarginVertical));
styleFound = true;
}
ttStyles.AppendLine(string.Format(styleFormat, ssaStyle.Name, ssaStyle.FontName, ssaStyle.FontSize, ssaStyle.Primary.ToArgb(), ssaStyle.Secondary.ToArgb(),
ssaStyle.Outline.ToArgb(), ssaStyle.Background.ToArgb(), bold, italic, ssaStyle.BorderStyle, ssaStyle.OutlineWidth, ssaStyle.ShadowWidth,
newAlignment, ssaStyle.MarginLeft, ssaStyle.MarginRight, ssaStyle.MarginVertical));
styleFound = true;

}
catch
{
Expand Down Expand Up @@ -333,13 +332,13 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
if (!eventsStarted)
header.AppendLine(line);

if (line.Trim().Equals("[events]", StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrEmpty(line) && line.TrimStart().StartsWith(';'))
{
eventsStarted = true;
// skip comment lines
}
else if (!string.IsNullOrEmpty(line) && line.TrimStart().StartsWith(';'))
else if (line.Trim().Equals("[events]", StringComparison.OrdinalIgnoreCase))
{
// skip comment lines
eventsStarted = true;
}
else if (eventsStarted && !string.IsNullOrWhiteSpace(line))
{
Expand Down
6 changes: 2 additions & 4 deletions libse/SubtitleFormats/Ted20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
buffer[i + TextBufferSize] == 0xff)
{
var text = ReadText(buffer, i);
if (text != null)
{
texts.Add(text);
}
texts.Add(text);

i += TextBufferSize;
}
else
Expand Down
4 changes: 2 additions & 2 deletions libse/SubtitleFormats/UnknownSubtitle6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public override string ToText(Subtitle subtitle, string title)
{
lines = Utilities.AutoBreakLine(p.Text).SplitToLines();
}
if (lines.Count > 0)
firstLine = lines[0];
firstLine = lines[0];

if (lines.Count > 1)
secondLine = lines[1];

Expand Down
33 changes: 16 additions & 17 deletions libse/TransportStream/TransportStreamParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,26 @@ public void Parse(Stream ms, LoadTransportStreamCallback callback)
var subtitles = new List<TransportStreamSubtitle>();
var list = ParseAndRemoveEmpty(GetSubtitlePesPackets(pid));

if (list != null)

for (int i = 0; i < list.Count; i++)
{
for (int i = 0; i < list.Count; i++)
var pes = list[i];
pes.ParseSegments();
if (pes.ObjectDataList.Count > 0)
{
var pes = list[i];
pes.ParseSegments();
if (pes.ObjectDataList.Count > 0)
{
var sub = new TransportStreamSubtitle();
sub.StartMilliseconds = pes.PresentationTimestampToMilliseconds();
sub.Pes = pes;
if (i + 1 < list.Count && list[i + 1].PresentationTimestampToMilliseconds() > 25)
sub.EndMilliseconds = list[i + 1].PresentationTimestampToMilliseconds() - 25;
if (sub.EndMilliseconds < sub.StartMilliseconds || sub.EndMilliseconds - sub.StartMilliseconds > (ulong)Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
sub.EndMilliseconds = sub.StartMilliseconds + 3500;
subtitles.Add(sub);
if (sub.StartMilliseconds < firstVideoMs)
firstVideoMs = sub.StartMilliseconds;
}
var sub = new TransportStreamSubtitle();
sub.StartMilliseconds = pes.PresentationTimestampToMilliseconds();
sub.Pes = pes;
if (i + 1 < list.Count && list[i + 1].PresentationTimestampToMilliseconds() > 25)
sub.EndMilliseconds = list[i + 1].PresentationTimestampToMilliseconds() - 25;
if (sub.EndMilliseconds < sub.StartMilliseconds || sub.EndMilliseconds - sub.StartMilliseconds > (ulong)Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
sub.EndMilliseconds = sub.StartMilliseconds + 3500;
subtitles.Add(sub);
if (sub.StartMilliseconds < firstVideoMs)
firstVideoMs = sub.StartMilliseconds;
}
}

foreach (var s in subtitles)
{
s.OffsetMilliseconds = firstVideoMs;
Expand Down
5 changes: 4 additions & 1 deletion libse/VobSub/VobSubParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public void OpenSubIdx(string vobSubFileName, string idxFileName)
else
position += 0x800;

int currentSubPictureStreamId = vsp.PacketizedElementaryStream.SubPictureStreamId.Value;
int currentSubPictureStreamId = 0;
if(vsp.PacketizedElementaryStream.SubPictureStreamId != null)
currentSubPictureStreamId = vsp.PacketizedElementaryStream.SubPictureStreamId.Value;

while (vsp.PacketizedElementaryStream != null &&
vsp.PacketizedElementaryStream.SubPictureStreamId.HasValue &&
(vsp.PacketizedElementaryStream.Length == PacketizedElementaryStreamMaximumLength ||
Expand Down
2 changes: 1 addition & 1 deletion libse/WaveToVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ internal WavePeakData LoadPeaks()
peaks[peakIndex++] = new WavePeak(max, min);
}
}
else if (_header.NumberOfChannels == 1)
else
{
// single sample value (for backwards compatibility)
int byteIndex = 0;
Expand Down

0 comments on commit 39cb777

Please sign in to comment.