- SatcoDX (.sdx): fixed handling of format version 105, which contains trailing data after the last channel

- SatcoDX: changing the character set in the menu now instantly corrects channel names with non-ASCII characters
This commit is contained in:
Horst Beham
2021-03-27 16:54:53 +01:00
parent 9387fb19c1
commit 7da7768495
7 changed files with 830 additions and 16 deletions

View File

@@ -1,12 +1,14 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
using ChanSort.Api;
namespace ChanSort.Loader.SatcoDX
{
internal class Channel : ChannelInfo
{
private byte[] data;
public int FileOffset { get; }
public int Length { get; }
@@ -14,6 +16,7 @@ namespace ChanSort.Loader.SatcoDX
internal Channel(int pos, string line, byte[] data, int start, int length, DvbStringDecoder decoder)
{
this.data = data;
this.FileOffset = start;
this.Length = length;
this.RecordIndex = pos;
@@ -41,14 +44,8 @@ namespace ChanSort.Loader.SatcoDX
// 42: polarity
this.Polarity = line[42] == '1' ? 'H' : 'V';
// 43-50 + (115-126 or 115-131): channel name
byte[] nameBytes = new byte[8+17];
var nameLen2 = Math.Min(length - 115, 17); // version 103 has 12 extra bytes for channel name, version 105 has 17
Array.Copy(data, start + 43, nameBytes, 0, 8);
Array.Copy(data, start + 115, nameBytes, 8, nameLen2);
decoder.GetChannelNames(nameBytes,0, nameBytes.Length, out var longName, out var shortName);
this.Name = longName.TrimEnd();
this.ShortName = shortName.TrimEnd();
// 43-50 + 115-126 in version 103 or 115-131 in version 105: channel name
this.ParseName(decoder);
// 51-54: sat position
var spos = line.Substring(51, 4).TrimStart('0');
@@ -83,5 +80,33 @@ namespace ChanSort.Loader.SatcoDX
}
#endregion
#region ParseName()
/// <summary>
/// SATCODX103 files can contain channel names with unspecified implicit encoding, so we support reparsing based on a user selected default code page
/// </summary>
/// <param name="decoder"></param>
public void ParseName(DvbStringDecoder decoder)
{
var length = this.Length;
var start = this.FileOffset;
// 43-50 + 115-126 in version 103 or 115-131 in version 105: channel name
byte[] nameBytes = new byte[8 + 17];
var nameLen2 = Math.Min(length - 115, 17); // version 103 has 12 extra bytes for channel name, version 105 has 17
Array.Copy(data, start + 43, nameBytes, 0, 8);
Array.Copy(data, start + 115, nameBytes, 8, nameLen2);
// I have seen format 103 files using only implicit CP1252 encoding for Umlauts, as well as format 105 with implicit UTF-8/explicit DVB-encoding
var oldDefaultEncoding = decoder.DefaultEncoding;
if (nameLen2 > 12)
decoder.DefaultEncoding = Encoding.UTF8;
decoder.GetChannelNames(nameBytes, 0, nameBytes.Length, out var longName, out var shortName);
decoder.DefaultEncoding = oldDefaultEncoding;
this.Name = longName.TrimEnd();
this.ShortName = shortName.TrimEnd();
}
#endregion
}
}

View File

@@ -10,6 +10,7 @@ namespace ChanSort.Loader.SatcoDX
private readonly ChannelList allChannels = new ChannelList(0, "All");
private byte[] content;
private int trailingDataPos;
#region ctor()
@@ -51,7 +52,7 @@ namespace ChanSort.Loader.SatcoDX
var pos = 0;
content = File.ReadAllBytes(this.FileName);
int prevPos = 0, nextPos;
while (prevPos < content.Length && (nextPos = Array.FindIndex(content, prevPos, ch => ch == (byte)'\n')) >= 0)
while (prevPos < content.Length && content[prevPos] != 0 && (nextPos = Array.FindIndex(content, prevPos, ch => ch == (byte)'\n')) >= 0)
{
if (nextPos - prevPos == 0)
continue;
@@ -61,6 +62,9 @@ namespace ChanSort.Loader.SatcoDX
pos++;
prevPos = nextPos + 1;
}
// SATCODX105 files contain a \0 character to mark the end, followed by an arbitrary number or spaces (or whatever data). We'll preserve it as-is.
this.trailingDataPos = prevPos;
}
#endregion
@@ -85,11 +89,37 @@ namespace ChanSort.Loader.SatcoDX
if (channel is Channel realChannel)
file.Write(this.content, realChannel.FileOffset, realChannel.Length + 1);
}
file.Write(this.content, this.trailingDataPos, this.content.Length - this.trailingDataPos);
}
}
#endregion
#region DefaultEncoding
/// SATCODX103 files can contain channel names with unspecified implicit encoding, so we support reparsing based on a user selected default code page
public override Encoding DefaultEncoding
{
get => base.DefaultEncoding;
set
{
if (value == this.DefaultEncoding)
return;
base.DefaultEncoding = value;
var decoder = new DvbStringDecoder(value);
foreach (var chan in this.allChannels.Channels)
{
if (chan is Channel ch)
ch.ParseName(decoder);
}
}
}
#endregion
#region GetFileInformation()
public override string GetFileInformation()