Skip to content
Permalink
Browse files

Attempt to fully support UTF8 encoding without BOM, as TEncoding.UTF8…

… has a non-empty preamble/bom. See https://forums.embarcadero.com/thread.jspa?threadID=112112 . Probably related to encoding issue #515
  • Loading branch information...
ansgarbecker committed Apr 13, 2019
1 parent a2ef1ae commit 406ab56060f370c2414c5041d766203cd3b75c32
Showing with 28 additions and 10 deletions.
  1. +21 −3 source/apphelpers.pas
  2. +1 −1 source/exportgrid.pas
  3. +6 −6 source/main.pas
@@ -254,7 +254,10 @@ TAppSettings = class(TObject)
// "Static" options, initialized in OnCreate only. For settings which need a restart to take effect.
property RestoreTabsInitValue: Boolean read FRestoreTabsInitValue;
end;

TUTF8NoBOMEncoding = class(TUTF8Encoding)
public
function GetPreamble: TBytes; override;
end;

{$I const.inc}

@@ -367,6 +370,7 @@ TAppSettings = class(TObject)
SystemImageList: TImageList;
mtCriticalConfirmation: TMsgDlgType = mtCustom;
ConfirmIcon: TIcon;
UTF8NoBOM: TUTF8NoBOMEncoding;

implementation

@@ -1266,7 +1270,7 @@ function DetectEncoding(Stream: TStream): TEncoding;
i := 0;
while i < BufferSize do begin
if FoundUTF8Strings = MinimumCountOfUTF8Strings then begin
Result := TEncoding.UTF8;
Result := UTF8NoBOM;
Break;
end;
case Buffer[i] of
@@ -2605,7 +2609,7 @@ function GetHTMLCharsetByEncoding(Encoding: TEncoding): String;
Result := 'utf-16le'
else if Encoding = TEncoding.BigEndianUnicode then
Result := 'utf-16'
else if Encoding = TEncoding.UTF8 then
else if Encoding = UTF8NoBOM then
Result := 'utf-8'
else if Encoding = TEncoding.UTF7 then
Result := 'utf-7';
@@ -4258,6 +4262,20 @@ function TAppSettings.DirnameBackups: String;



{ TUTF8NoBOMEncoding }

function TUTF8NoBOMEncoding.GetPreamble: TBytes;
begin
// Overwriting default UTF8 encoding, which has a preamble which we don't want
// See https://forums.embarcadero.com/thread.jspa?threadID=112112
SetLength(Result, 0);
end;


initialization

UTF8NoBOM := TUTF8NoBOMEncoding.Create;

end.


@@ -549,7 +549,7 @@ procedure TfrmExportGrid.btnOKClick(Sender: TObject);
ExcludeCol := GridData.AutoIncrementColumn;

if radioOutputCopyToClipboard.Checked then
Encoding := TEncoding.UTF8
Encoding := UTF8NoBOM
else begin
Encoding := MainForm.GetEncodingByName(comboEncoding.Text);
// Add selected file to file list, and sort it onto the top of the list
@@ -2226,7 +2226,7 @@ procedure TMainForm.RestoreTabs;
if FileExists(BackupFilename) then begin
Tab := ActiveOrEmptyQueryTab(False);
Tab.Uid := Section;
Tab.LoadContents(BackupFilename, True, TEncoding.UTF8);
Tab.LoadContents(BackupFilename, True, UTF8NoBOM);
Tab.MemoFilename := Filename;
Tab.Memo.Modified := True;
end else begin
@@ -11828,7 +11828,7 @@ function TMainForm.GetEncodingByName(Name: String): TEncoding;
2: Result := TEncoding.GetEncoding(437);
3: Result := TEncoding.Unicode;
4: Result := TEncoding.BigEndianUnicode;
5: Result := TEncoding.UTF8;
5: Result := UTF8NoBOM;
6: Result := TEncoding.UTF7;
end;
end;
@@ -11842,7 +11842,7 @@ function TMainForm.GetEncodingName(Encoding: TEncoding): String;
else if (Encoding <> nil) and (Encoding.CodePage = 437) then idx := 2
else if Encoding = TEncoding.Unicode then idx := 3
else if Encoding = TEncoding.BigEndianUnicode then idx := 4
else if Encoding = TEncoding.UTF8 then idx := 5
else if Encoding = UTF8NoBOM then idx := 5
else if Encoding = TEncoding.UTF7 then idx := 6
else idx := 0;
Result := FileEncodings[idx];
@@ -11910,7 +11910,7 @@ function TMainForm.GetCharsetByEncoding(Encoding: TEncoding): String;
Result := 'utf16le'
else if Encoding = TEncoding.BigEndianUnicode then
Result := 'utf16'
else if Encoding = TEncoding.UTF8 then
else if Encoding = UTF8NoBOM then
Result := 'utf8'
else if Encoding = TEncoding.UTF7 then
Result := 'utf7';
@@ -12881,11 +12881,11 @@ function TQueryTab.LoadContents(Filename: String; ReplaceContent: Boolean; Encod
Memo.SelStart := Memo.SelEnd;
Memo.EndUpdate;
Memo.Modified := False;
MemoFilename := filename;
MemoFilename := Filename;
Result := True;
except on E:Exception do
// File does not exist, is locked or broken
ErrorDialog(E.message);
ErrorDialog(E.message + CRLF + CRLF + Filename);
end;
Screen.Cursor := crDefault;
end;

0 comments on commit 406ab56

Please sign in to comment.
You can’t perform that action at this time.