WritePreferenceString and UTF8

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
simkot
User
User
Posts: 31
Joined: Sat Oct 26, 2024 8:25 am

WritePreferenceString and UTF8

Post by simkot »

The default WritePreferenceString writes to UTF BOM. How to make it writes to UTF8 without BOM?
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: WritePreferenceString and UTF8

Post by Axolotl »

Edit: This is not true: As far as I know that is not possible. .....
Some hints (anyway):
You can use CreateFile or OpenFile with WriteXxxx or the xml or json file functions.
You can also use any of the (kind of) INI file modules (on the forum) or on github......
A small collection:
Sicro's PB-CodeArchiv-Rebirth
Kenmo's Includes
RSBasic's WinAPI Library
Last edited by Axolotl on Wed Mar 05, 2025 12:19 pm, edited 1 time in total.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: WritePreferenceString and UTF8

Post by AZJIO »

CreatePreferences + #PB_Preference_NoBOM
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: WritePreferenceString and UTF8

Post by Axolotl »

Upps, I missed that new Flag. So AZJIO is right the flag #PB_Preference_NoBOM worked.

I use the OpenPreferences() for reading and writing and with it you always get the BOM, because the flag is not possible here.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: WritePreferenceString and UTF8

Post by AZJIO »

Axolotl wrote: Wed Mar 05, 2025 12:15 pm I use the OpenPreferences() for reading and writing and with it you always get the BOM, because the flag is not possible here.
Check by creating ini in UTF8 format without BOM. Theoretically, Openpreferences () should not add BOM.
Look in the reference file:
#PB_Ascii: The file will be read as Ascii, unless there an UTF-8 BOM. If the preferences are written back to file, it will be using UTF-8.
simkot
User
User
Posts: 31
Joined: Sat Oct 26, 2024 8:25 am

Re: WritePreferenceString and UTF8

Post by simkot »

AZJIO wrote: Wed Mar 05, 2025 12:03 pm CreatePreferences + #PB_Preference_NoBOM
I need to write to an existing file. And if I use CreatePreferences + #PB_Preference_NoBOM then the file will be overwritten and its contents will disappear.
simkot
User
User
Posts: 31
Joined: Sat Oct 26, 2024 8:25 am

Re: WritePreferenceString and UTF8

Post by simkot »

AZJIO wrote: Wed Mar 05, 2025 12:30 pm Check by creating ini in UTF8 format without BOM. Theoretically, Openpreferences () should not add BOM.
Look in the reference file:
I tried this method. The file still converts to UTF8 BOM if I use WritePreferenceString.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: WritePreferenceString and UTF8

Post by AZJIO »

Then you need to contact Fred to add #PB_Preference_NoBOM for OpenPreferences.

I had a file with Russian text (UTF-8 without BOM) opened apparently as Ascii, but when writing a new parameter UTF-8 + BOM was added and I got broken text.

Use WinAPI for now. But the ANSI or UTF16 file format

Code: Select all

WritePrivateProfileString_(Section, Key, Value, Path)
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: WritePreferenceString and UTF8

Post by Axolotl »

Well, ......
Here Fred wrote that the Preference library is only for PB applications.
PB 6.10 b1 - OpenPreference()

So, I follow the "rules" and use the Preferences Library for PB stuff only.
The advantage of OpenPreferences() compared to CreatePreferences() for me is, that you can do partial update.
In all other cases I try my best with functions I mentioned in my first post above.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: WritePreferenceString and UTF8

Post by AZJIO »

Axolotl wrote: Wed Mar 05, 2025 1:22 pm Here Fred wrote that the Preference library is only for PB applications.
Yes, I am in the know
Linux uses UTF8 files without BOM because the system does not support Ascii. And it's not that hard to add, all you have to do is disable BOM writing to the file. I don't think it requires complex code.

Code: Select all

If Not #PB_Preference_NoBOM
	WriteStringFormat(#File, #PB_UTF8)
EndIf
You can read the previous topics about this
https://www.purebasic.fr/english/viewtopic.php?t=78105 (There is a solution from mk-soft)
https://www.purebasic.fr/english/viewtopic.php?t=83196
Post Reply