ReNamer:Scripts:Convert file content from ANSI to UTF-8

From den4b Wiki
Revision as of 15:03, 8 February 2017 by Den4b (talk | contribs) (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Convert the content of processed files from ANSI (default system code page) to UTF-8 encoding.

To prevent files from being processed more than once, we will insert the UTF-8 byte order mark (BOM) "0xEF 0xBB 0xBF" at the beginning of converted files and check for it before processing the file.

Tested

  • ReNamer 5.74.7 Beta

Code

Author: Denis Kozlov. Date: 12 February 2014.

var
  DataAnsi, DataUTF8, UTF8BOM: AnsiString;
  DataWide: WideString;
begin
  UTF8BOM := Chr($EF) + Chr($BB) + Chr($BF);
  DataAnsi := FileReadContent(FilePath);
  if Copy(DataAnsi, 1, Length(UTF8BOM)) <> UTF8BOM then
  begin  
    DataWide := AnsiToWide(DataAnsi);
    DataUTF8 := UTF8BOM + UTF8Encode(DataWide);
    FileWriteContent(FilePath, DataUTF8);
  end;
end.