[v2.0.4] IniRead fails to read UTF-8-BOM files. Topic is solved

Report problems with documented functionality
User avatar
RaptorX
Posts: 386
Joined: 06 Dec 2014, 14:27
Contact:

[v2.0.4] IniRead fails to read UTF-8-BOM files.

15 Jul 2023, 11:46

Hello guys.

please try the following code:

Code: Select all

hFile := FileOpen("temp.ini", "w-", "utf-8")
hFile.Write("[config]`ntemp=this is a test`n")
hFile.Close()

Msgbox FileExist("temp.ini")
Msgbox IniRead("temp.ini", "config", "temp")

You will get an error on the last line.

Now change the first line to this:

Code: Select all

hFile := FileOpen("temp.ini", "w-", "utf-8-raw")
And if you try again, everything will work fine.
IniRead only has problems with UTF-8-BOM files.
Projects:
AHK-ToolKit
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: [v2.0.4] IniRead fails to read UTF-8-BOM files.

15 Jul 2023, 13:49

I've observed that Initializing it as utf-8 and then filling it with IniWrite works, but contains an empty line at the beginning (the BOM is alone in it's own line).
Adding the new line at the start of your example makes it work, but I don't know about the underlying issues that can be causing this.
Descolada
Posts: 1143
Joined: 23 Dec 2021, 02:30

Re: [v2.0.4] IniRead fails to read UTF-8-BOM files.

15 Jul 2023, 13:51

Might this be related to the comment in IniRead/IniWrite docs?
Unicode: IniRead and IniWrite rely on the external functions GetPrivateProfileString and WritePrivateProfileString to read and write values. These functions support Unicode only in UTF-16 files; all other files are assumed to use the system's default ANSI code page.
User avatar
RaptorX
Posts: 386
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2.0.4] IniRead fails to read UTF-8-BOM files.

15 Jul 2023, 16:19

Descolada wrote:
15 Jul 2023, 13:51
Might this be related to the comment in IniRead/IniWrite docs?
Unicode: IniRead and IniWrite rely on the external functions GetPrivateProfileString and WritePrivateProfileString to read and write values. These functions support Unicode only in UTF-16 files; all other files are assumed to use the system's default ANSI code page.
Yes its related to that but in V1 IniRead was handling that under the hood I guess, but v2 doesnt seem to be doing the same.
Also UTF-8-RAW is being read, so AHK is hadling that under the hood without issues, is just the UTF-8-BOM that fails.
Projects:
AHK-ToolKit
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2.0.4] IniRead fails to read UTF-8-BOM files.  Topic is solved

15 Jul 2023, 19:27

IniRead never handled this in any version.

These functions support Unicode only in UTF-16 files.

UTF-8 is not supported, period.

In your example, the section header is on the same "line" as the UTF-8 BOM, so is not recognized as a section header. If you omit the BOM or put a line break before the section header, the functions will locate the value and read it incorrectly, unless it is purely ASCII. If you are using an ANSI version of AutoHotkey, no translation between code pages is performed, because the INI file is assumed to be ANSI, therefore what you get is the unaltered UTF-8 data.

IniWrite will never write a section header on the same "line" as the BOM, nor will it ever encode data as UTF-8.
User avatar
RaptorX
Posts: 386
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2.0.4] IniRead fails to read UTF-8-BOM files.

16 Jul 2023, 08:55

lexikos wrote:
15 Jul 2023, 19:27
IniRead never handled this in any version.

These functions support Unicode only in UTF-16 files.

UTF-8 is not supported, period.

In your example, the section header is on the same "line" as the UTF-8 BOM, so is not recognized as a section header. If you omit the BOM or put a line break before the section header, the functions will locate the value and read it incorrectly, unless it is purely ASCII. If you are using an ANSI version of AutoHotkey, no translation between code pages is performed, because the INI file is assumed to be ANSI, therefore what you get is the unaltered UTF-8 data.

IniWrite will never write a section header on the same "line" as the BOM, nor will it ever encode data as UTF-8.
For some reason I had the idea that in v1 we didnt get the above errors, but after trying it again I get the same behavior.
Weird I swear I had created utf8 files (by mistake) before and used IniRead on them on v1 and didnt seem to have issues but I guess Im mistaken.
Projects:
AHK-ToolKit

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 19 guests