Replace a string in a UTF-8 file without BOM in Inno Setup
Asked Answered
T

1

1

I need to change some values in a configuration file. The file is UTF-8 without BOM. I need to save it the same way. How do I do it with Inno Setup Unicode edition?

Note:

Tieback answered 23/1, 2019 at 17:57 Comment(0)
G
3
const
  CP_UTF8 = 65001;

// ...
var
  FileName: string;
  S: string;
begin
  FileName := 'test.txt';
  if not LoadStringFromFileInCP(FileName, S, CP_UTF8) then
  begin
    Log('Error loading the file');
  end
    else
  if StringChangeEx(S, 'žluťoučký kůň', 'ďábelské ódy', True) <= 0 then
  begin
    Log('No value was replaced');
  end
    else
  if not SaveStringToFileInCP(FileName, S, CP_UTF8) then
  begin
    Log('Error writing the file');
  end
    else
  begin
    Log('Replacement successful');
  end;
end;

LoadStringFromFileInCP and SaveStringToFileInCP come from:
Convert array of string to Unicode and back to ANSI in Inno Setup

The code needs Unicode version of Inno Setup (the only version as of Inno Setup 6).
For Unicode string literals, your .iss file must be in UTF-8 encoding with BOM.

Gitagitel answered 23/1, 2019 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.