CreatePreferences PB 5.51

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trion
New User
New User
Posts: 8
Joined: Tue Dec 27, 2016 11:13 pm

CreatePreferences PB 5.51

Post by Trion »

Hello,

CreatePreferences makes now a blank line in the first line.

PureBasic - Preference example file

Windows 10 64 Bit


<blank line>
[Global]
ApplicationName = MP3 Player
Version = 1.1b
; This is the Window dimension
;
[Window]
WindowX = 123
WindowY = 124
WindowZ = -125.500000

Richard
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CreatePreferences PB 5.51

Post by Demivec »

Trion wrote:CreatePreferences makes now a blank line in the first line.
In this thread Fred describes this as a feature, not a bug and a feature request has been posted to do it differently.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: CreatePreferences PB 5.51

Post by Mistrel »

If files created by this library are expected to be consumed by it then why the workaround? :|
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: CreatePreferences PB 5.51

Post by kenmo »

The blank line is there to separate the UTF-8 BOM from the first preference entry.
See http://www.purebasic.fr/english/viewtop ... =3&t=56113

The UTF-8 BOM has to be there, because OpenPreferences() will read UTF-8 text as ASCII if there's no BOM.
See http://www.purebasic.fr/english/viewtop ... =4&t=67221

Code: Select all

File.s = GetTemporaryDirectory() + "temp.ini"
CreateFile(0, File, #PB_UTF8)
WriteString(0, "Name = Héllo")
CloseFile(0)

OpenPreferences(File) ; Open UTF-8 prefs file WITHOUT B.O.M.
Debug ReadPreferenceString("Name", "")
ClosePreferences()

OpenFile(0, File, #PB_UTF8)
Debug StringField( ReadString(0), 2, " = ")
CloseFile(0)

Ideally, you could avoid the BOM/newline with a #PB_Preferences_NoBOM flag.
But OpenPreferences() would need to accept #PB_UTF8 like OpenFile(), to read UTF-8 text correctly.


I've just accepted the extra blank line. :)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: CreatePreferences PB 5.51

Post by Mistrel »

kenmo wrote:The UTF-8 BOM has to be there, because OpenPreferences() will read UTF-8 text as ASCII if there's no BOM.
If the file is both supplied and consumed by the same library then it already has control of the format being both written to and read from disk. Therefore I don't understand why there is any worry about whether it will be read incorrectly.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: CreatePreferences PB 5.51

Post by kenmo »

I personally agree :wink:

But for now, Unicode PureBasic forces a UTF-8 BOM and blank line, for ASCII program safety.
So we must accept a harmless blank line, or write your own preferences file manually, in your choice of encoding, with or without BOMs.
Trion
New User
New User
Posts: 8
Joined: Tue Dec 27, 2016 11:13 pm

Re: CreatePreferences PB 5.51

Post by Trion »

PB UTF-8

Image

0d and 0a makes blank line

With Notepad++ UTF-8

Image

No blank line in the editor

Richard
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CreatePreferences PB 5.51

Post by Demivec »

Trion wrote:With Notepad++ UTF-8
Why would it matter what Notepad++ does? It is a PureBasic library.

As explained in the thread that was linked to above, the blank line is used as a delimeter. The contents of the file are read until the end of the first line. If there is a BOM for UTF-8 there then the contents of the file are unicode, else it is ASCII.
Post Reply