Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inno Setup Reading file in Ansi and Unicode encoding

I have a function called GetServerName. I need to pass the file name (say for example 'test.txt') as well as a needed section string (say for example 'server')

The test.txt file is contains something like this

data1 | abcd
data2 | efgh
server| 'serverName1'
data3 | ijkl

I need to extract server name so in my function I will pass something like GetServerName('test.txt', 'server') and it should return serverName1.

My problem is that the test.txt was an ANSI-encoded file earlier. Now it can be an ANSI-encoded file or Unicode-encoded file. Below function worked correctly for ANSI-encoded file, but giving problem, if file is encoded in UNICODE. I suspect something with LoadStringsFromFile function. Because when, I debug I could see it returns Unicode characters instead of human readable characters. How to solve my issue simply? (or how to find the type of encoding of my file and how to convert UNICODE string to ANSI for comparison, then I can do it myself)

function GetServerName(const FileName, Section: string): string;
//Get Smartlink server name
var
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;   
  Lines: TArrayOfString;
  //Lines: String;
  AHA: TArrayOfString;
begin
  Result := '';
  if LoadStringsFromFile(FileName, Lines) then
  begin
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do
    begin
      AHA := StrSplit(Trim(Lines[SectionLine]), '|')
      if AHA[0] = Section then
      begin
       Result := AHA[1];
       Exit;
      end
    end
  end;
end;

Edit

In Windows, when I Save As text files. I get 4 options as I attached in the image. I found it, Windows mention unicode as UTF-16LE encoding (Bit confusing) enter image description here

like image 348
RobinAtTech Avatar asked Aug 16 '16 06:08

RobinAtTech


1 Answers

First, note that the Unicode is not an encoding. The Unicode is a character set. Encoding is UTF-8, UTF-16, UTF-32 etc. So we do not know which encoding you actually use.


In the Unicode version of Inno Setup, the LoadStringsFromFile function uses the current Windows Ansi encoding by default.

But, if the file has the UTF-8 BOM, it will treat the contents accordingly. The BOM is a common way to autodetect the UTF-8 (and other UTF-*) encoding. You can create a file in the UTF-8 encoding with BOM using Windows Notepad.

UTF-16 or other encodings are not supported natively.

For implementation of reading UTF-16 file, see Inno Setup Pascal Script - Reading UTF-16 file.

For working with files in any encoding, including UTF-8 without BOM, see Inno Setup - Convert array of string to Unicode and back to ANSI or Inno Setup replace a string in a UTF-8 file without BOM.

like image 129
Martin Prikryl Avatar answered Oct 27 '22 22:10

Martin Prikryl