PB 6.10 b1 - OpenPreference()

Just starting out? Need help? Post your questions and find answers here.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

PB 6.10 b1 - OpenPreference()

Post by User_Russian »

Fred wrote: Fri Dec 22, 2023 6:14 pmAdded: 'Encoding' optional parameter to OpenPreference()
ini file (ascii encoding).

Code: Select all

Ключ = значение
Key = value
pb file

Code: Select all

Debug OpenPreferences("Test.txt", 0, #PB_Ascii)
Debug ReadPreferenceString("Ключ", "")
Debug ReadPreferenceString("Key", "")
Result of executing this code

Code: Select all

280

value
But the result must be

Code: Select all

280
значение
value
There is also no parameter in CreatePreferences() to save in the required encoding.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: PB 6.10 b1 - OpenPreference()

Post by juergenkulow »

Code: Select all

Debug OpenPreferences("/tmp/test.pref", 0, #PB_UTF8)
Debug ReadPreferenceString("Ключ", "")
Debug ReadPreferenceString("Key", "")
; 21241184
; значение
; value
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

Other applications can read/write to an ini file in ascii or UTF16 (UCS2) encoding.
What's the point of adding an encoding parameter that only supports UTF8?
Function OpenPreference() without this parameter supports UTF8.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: PB 6.10 b1 - OpenPreference()

Post by juergenkulow »

My questions about ReadPreferenceString are:
What exactly are the Encoding parameter as of PB6.10?
How does the Linux version differ from the Windows version?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: PB 6.10 b1 - OpenPreference()

Post by Sicro »

These strings "Ключ" and "значение" cannot be saved with the character encoding "ISO-8859-1", which PureBasic uses for "ASCII" as far as I know.

See also:

Code: Select all

If CreateFile(0, GetTemporaryDirectory() + "test.txt", #PB_Ascii)
  WriteStringN(0, "Ключ = значение" + #CRLF$ + "Key = value")
  CloseFile(0)
EndIf

If ReadFile(0, GetTemporaryDirectory() + "test.txt", #PB_Ascii)
  Debug ReadString(0, #PB_File_IgnoreEOL)
  CloseFile(0)
EndIf
Debug wrote: =
Key = value
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

Ascii encoding corresponds to Windows localization.
In this case it is https://en.wikipedia.org/wiki/Windows-1251

Sicro, your code works correctly.

Code: Select all

Ключ = значение
Key = value
In PB 4.30 and previous versions, when selecting the ascii subsystem, preference lib works correctly with ascii ini files.
But after remains to unicode only (PB 4.40), ini files in ascii encoding are unreadable.
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: PB 6.10 b1 - OpenPreference()

Post by AZJIO »

Linux does not support cp1251 encoding natively. This is a Windows thing.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB 6.10 b1 - OpenPreference()

Post by Fred »

Ascii behaves the same way than before (ie: ini file without BOM). The new encoding parameter is only to allow to load ini whithout BOM in UTF-8 mode. Doc will be updated.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

Fred wrote: Tue Jan 16, 2024 8:35 amThe new encoding parameter is only to allow to load ini whithout BOM in UTF-8 mode. Doc will be updated.
Other applications save data not only in UTF-8, but also in ASCII and UTF-16 encodings and in this case, the Preference library is useless, because it cannot read/save in encodings other than UTF-8.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB 6.10 b1 - OpenPreference()

Post by Fred »

Yes, it' not meant to be an universal library but tailored for PB apps.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

But then why do we need flag #PB_Preference_NoBOM if function CreatePreferences() saves with BOM?
In any case, if this is a PB-only library, there is no need for #PB_Preference_NoBOM.
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: PB 6.10 b1 - OpenPreference()

Post by AZJIO »

User_Russian wrote: Tue Jan 16, 2024 11:26 am But then why do we need flag #PB_Preference_NoBOM if function CreatePreferences() saves with BOM?
In any case, if this is a PB-only library, there is no need for #PB_Preference_NoBOM.
https://www.purebasic.fr/english/viewtopic.php?p=577057
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

AZJIO wrote: Tue Jan 16, 2024 2:09 pm
User_Russian wrote: Tue Jan 16, 2024 11:26 am But then why do we need flag #PB_Preference_NoBOM if function CreatePreferences() saves with BOM?
In any case, if this is a PB-only library, there is no need for #PB_Preference_NoBOM.
https://www.purebasic.fr/english/viewtopic.php?p=577057
Fred wrote that the Preference library is only for PB applications.
Fred wrote: Tue Jan 16, 2024 10:48 am Yes, it' not meant to be an universal library but tailored for PB apps.
Therefore it cannot be used for editing *.desktop in Linux. This may sound strange, but this is Fred's answer...
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: PB 6.10 b1 - OpenPreference()

Post by AZJIO »

It is not difficult to embed the flag

Code: Select all

If flag <> #PB_Preference_NoBOM
	WriteStringFormat(0, #PB_UTF8)
EndIf
And it will be supported on any OS.
The #PB_Ascii flag will require all "Windows code pages" to be embedded.
User_Russian wrote: Tue Jan 16, 2024 3:21 pm Therefore it cannot be used for editing *.desktop in Linux.
Let's consider this a random coincidence.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: PB 6.10 b1 - OpenPreference()

Post by User_Russian »

AZJIO wrote: Tue Jan 16, 2024 3:56 pmThe #PB_Ascii flag will require all "Windows code pages" to be embedded.
File functions support required encodings (#PB_Ascii, #PB_UTF8 and #PB_Unicode).
Post Reply