- Panasonic: channel name editing is now supported when all channels implicitly use valid utf-8 encoding

This commit is contained in:
Horst Beham
2020-12-26 18:16:01 +01:00
parent e7bb88c554
commit eaa09181cf
5 changed files with 55 additions and 25 deletions

View File

@@ -9,6 +9,8 @@ namespace ChanSort.Loader.Panasonic
internal class DbChannel : ChannelInfo
{
internal byte[] RawName;
internal bool NonAscii;
internal bool ValidUtf8 = true;
#region ctor()
internal DbChannel(SQLiteDataReader r, IDictionary<string, int> field, DataRoot dataRoot, Encoding encoding)
@@ -157,19 +159,32 @@ namespace ChanSort.Loader.Panasonic
// single byte code pages might have UTF-8 code mixed in, so we have to parse it manually
StringBuilder sb = new StringBuilder();
this.NonAscii = false;
this.ValidUtf8 = true;
for (int i = startOffset; i < this.RawName.Length; i+=bytesPerChar)
{
byte c = this.RawName[i];
byte c2 = i + 1 < this.RawName.Length ? this.RawName[i + 1] : (byte)0;
if (c < 0xA0)
sb.Append((char)c);
if (c >= 0x80)
NonAscii = true;
if (c < 0x80)
sb.Append((char) c);
else if (c < 0xA0)
{
ValidUtf8 = false;
sb.Append((char) c);
}
else if (bytesPerChar == 1 && c >= 0xC0 && c <= 0xDF && c2 >= 0x80 && c2 <= 0xBF) // 2 byte UTF-8
{
sb.Append((char)(((c & 0x1F) << 6) | (c2 & 0x3F)));
++i;
}
else
{
ValidUtf8 = false;
sb.Append(encoding.GetString(this.RawName, i, bytesPerChar));
}
}
string longName, shortName;
@@ -240,14 +255,19 @@ namespace ChanSort.Loader.Panasonic
#endregion
#region UpdateRawData()
public override void UpdateRawData()
public void UpdateRawData(bool explicitUtf8, bool implicitUtf8)
{
if (IsNameModified)
{
var utf8 = Encoding.UTF8.GetBytes(this.Name);
this.RawName = new byte[utf8.Length + 1];
this.RawName[0] = 0x15; // DVB encoding ID for UTF8
Array.Copy(utf8, 0, this.RawName, 1, utf8.Length);
if (implicitUtf8)
this.RawName = utf8;
else if (explicitUtf8)
{
this.RawName = new byte[utf8.Length + 1];
this.RawName[0] = 0x15; // DVB encoding ID for UTF8
Array.Copy(utf8, 0, this.RawName, 1, utf8.Length);
}
}
}
#endregion

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Text;
using ChanSort.Api;
@@ -26,6 +25,8 @@ namespace ChanSort.Loader.Panasonic
private int dbSizeOffset;
private bool littleEndianByteOrder;
private string charEncoding;
private bool explicitUtf8;
private bool implicitUtf8;
enum CypherMode
{
@@ -250,6 +251,7 @@ order by s.ntype,major_channel
var fields = this.GetFieldMap(fieldNames);
cmd.CommandText = sql;
this.implicitUtf8 = true;
using (var r = cmd.ExecuteReader())
{
while (r.Read())
@@ -258,12 +260,17 @@ order by s.ntype,major_channel
if (!channel.IsDeleted)
{
if (channel.RawName.Length > 0 && channel.RawName[0] == 0x15) // if there is a channel with a 0x15 encoding ID (UTF-8), we can allow editing channels
this.Features.ChannelNameEdit = ChannelNameEditMode.All;
this.explicitUtf8 = true;
if (channel.NonAscii) // if all channels with non-ascii characters are valid UTF-8 strings, we can allow editing too
this.implicitUtf8 &= channel.ValidUtf8;
var channelList = this.DataRoot.GetChannelList(channel.SignalSource);
this.DataRoot.AddChannel(channelList, channel);
}
}
}
if (this.explicitUtf8 || this.implicitUtf8)
this.Features.ChannelNameEdit = ChannelNameEditMode.All;
}
#endregion
@@ -346,7 +353,7 @@ order by s.ntype,major_channel
continue;
if (channel.IsDeleted && channel.OldProgramNr >= 0)
continue;
channel.UpdateRawData();
channel.UpdateRawData(this.explicitUtf8, this.implicitUtf8);
cmd.Parameters["@rowid"].Value = channel.RecordIndex;
cmd.Parameters["@progNr"].Value = channel.NewProgramNr;
cmd.Parameters["@sname"].Value = channel.RawName;