ReadPreferenceString() has a problem with unicode files

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 833
Joined: Fri Jun 11, 2004 7:07 am

ReadPreferenceString() has a problem with unicode files

Post by Lebostein »

Generate a ini file (unicode):

Code: Select all

[EUR]
symbol = €
[GBP]
symbol = £
[CZK]
symbol = Kč
[PLN]
symbol = Zł
and try to read the values:

Code: Select all

OpenPreferences("test.ini")
ExaminePreferenceGroups()
While NextPreferenceGroup()
  Debug ReadPreferenceString("symbol", "")
Wend
ClosePreferences()
What you get is:
€
£
Kč
Zł

// Moved from "Bugs - Mac OSX" to "Coding Questions" (Kiffi)
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: ReadPreferenceString() has a problem with unicode files

Post by Marc56us »

PureBasic - Preference
[...] The file format is in unicode UTF-8 with a BOM [...]


(save your INI with a BOM) :wink:

Code: Select all

€
£
Kč
Zł
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReadPreferenceString() has a problem with unicode files

Post by STARGÅTE »

Since PB 6.10, OpenPreferences has an additional parameter for the Encoding, try it out:

Code: Select all

OpenPreferences("test.ini", 0, #PB_UTF8)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Lebostein
Addict
Addict
Posts: 833
Joined: Fri Jun 11, 2004 7:07 am

Re: ReadPreferenceString() has a problem with unicode files

Post by Lebostein »

Thanks! Nice to know.

But what happens if I create a preference file with CreatePreferences()? Here the new encoding flag is missing...

Edit: most of the Apple / Mac OS Text editors I use ignore/remove the BOM (i.e. Apples TextEdit or other editors like Smultron), because mac OS is fully based on UTF-8 without BOM. For testing I started windows and use Notepad++ to generate a UTF-8 file with BOM.
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReadPreferenceString() has a problem with unicode files

Post by STARGÅTE »

CreatePreferences creates always a UTF-8 file.
You can use the flag: #PB_Preference_NoBOM, to remove the usually set UTF8 BOM.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Lebostein
Addict
Addict
Posts: 833
Joined: Fri Jun 11, 2004 7:07 am

Re: ReadPreferenceString() has a problem with unicode files

Post by Lebostein »

Thanks. In Beta 6 #PB_Preference_NoBOM for CreatePreferences() is not known by the Debugger (returns an error, missplaced constant), but I hope this is fixed in the final.
Post Reply