OpenPreferences + #PB_UTF8 without BOM (?)

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

OpenPreferences + #PB_UTF8 without BOM (?)

Post by AZJIO »

When saving a *.desktop file (Linux), "UTF8 without BOM" is required, which is why I can't write to the file directly, but overwrite it by deleting the BOM. To change 1 parameter, I need to read all the parameters so as not to lose them. I need this for the "Creating desktop file" program
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by Marc56us »

Hi,

I guess you already tried to use WriteStringFormat() to add or remove the BOM before and after editing the file to make it compatible with the lib?
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by Tenaja »

AZJIO wrote: Wed Oct 20, 2021 10:43 am When saving a *.desktop file (Linux), "UTF8 without BOM" is required, which is why I can't write to the file directly, but overwrite it by deleting the BOM. To change 1 parameter, I need to read all the parameters so as not to lose them. I need this for the "Creating desktop file" program
I've yet to use pb on Linux, but you cannot edit a file for the Preferences library and expect it to behave the same.

If you need to ask Fred to add a "no bom" flag, that might be a valid request. (If it's not already there.)
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by mk-soft »

Purebasic does not automatically write the BOM.

If you want to write a BOM, you have to use "WriteStringFormat".
This way you can also write a file in UTF-8 format without writing a BOM.

Code: Select all


Structure ArrayOfUByte
  a.a[0]
EndStructure


path.s = GetUserDirectory(#PB_Directory_Documents)

file.s = path + "MyFile.txt"

If CreateFile(0, file)
  WriteStringN(0, "Test ÄÖÜ", #PB_UTF8)
  CloseFile(0)
EndIf

If OpenFile(0, file)
  len = Lof(0)
  *mem.ArrayOfUByte = AllocateMemory(len)
  ReadData(0, *mem, len)
  CloseFile(0)
  len - 1
  For i = 0 To Len
    Debug "Index " + i + ": " + RSet(Hex(*mem\a[i]), 2, "0")
  Next
EndIf

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by mk-soft »

I have to many time ... 8)

Update 2

Code: Select all

;-TOP by mk-soft

Structure udtDesktopFile
  Key.s
  Value.s
EndStructure

Procedure ReadDesktopFile(FileName.s, List Params.udtDesktopFile())
  Protected file, text.s
  
  ClearList(Params())
  
  file = ReadFile(#PB_Any, FileName)
  If file
    While Not Eof(file)
      text = ReadString(file, #PB_UTF8)
      If FindString(text, "[Desktop Entry]", #PB_String_NoCase)
        Break
      EndIf
    Wend
    
    While Not Eof(file)
      text = ReadString(file, #PB_UTF8)
      If FindString(text, "=")
        AddElement(Params())
        Params()\Key = Trim(StringField(text, 1, "="))
        Params()\Value = Trim(StringField(text, 2, "="))
      EndIf
    Wend
    CloseFile(file)
  EndIf
  ProcedureReturn ListSize(Params())
EndProcedure

Procedure WriteDesktopFile(FileName.s, List Params.udtDesktopFile())
  Protected file, text.s
  
  file = OpenFile(#PB_Any, FileName)
  If file
    WriteStringN(file, "[Desktop Entry]", #PB_UTF8)
    ForEach Params()
      text = Params()\Key + "=" + Params()\Value
      WriteStringN(file, text, #PB_UTF8)
    Next
    CloseFile(file)
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure

; ****

Define file.s
Define NewList Params.udtDesktopFile()
file.s = OpenFileRequester("Desktop File", GetUserDirectory(#PB_Directory_Desktop), "Desktop|*.desktop", 1)
If file
  If ReadDesktopFile(file, Params())
    ForEach Params()
      Debug Params()\Key + " -> " + Params()\Value
    Next
  EndIf
EndIf

file.s = SaveFileRequester("Desktop File", GetUserDirectory(#PB_Directory_Desktop), "Desktop|*.desktop", 1)
If file
  WriteDesktopFile(file, Params())
EndIf
Last edited by mk-soft on Fri Nov 19, 2021 7:34 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by AZJIO »

The problem is as follows: I read the 5 main parameters and show them in the GUI. If the function worked correctly, then when saving to the same file, I overwritten these five parameters, while the remaining parameters would remain in place. Instead, I delete the file and write new ones in its place, in which there are 5 parameters. This is good for a custom file with 5-7 parameters. If there are 40 parameters in the original file, then 35 parameters will be lost. I don't need them, but other people do. In order for me not to lose 35 parameters, I have to read them into a virtual array hiding them from the user, and when I save them, I have to save these 35 parameters. At the same time, I save them only to the original, and if this is a new file, then they are not needed there, since the user ordered 5 parameters, then he received them. But if there were 40 of them in the original, then the original should not spoil by losing 35 parameters.

I figured out how to do it without complicating the code. It is necessary to overwrite the file with the BOM, then save the parameters using the native method (Preference), then overwrite without the BOM.
Last edited by AZJIO on Fri Nov 19, 2021 7:13 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by mk-soft »

If the (pref) file contains new or changed parameters, the file must be completely rewritten anyway, as text files are sequential files.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by AZJIO »

The problem is not in the overwrite, but in the fact that I work with the file I have to rewrite it twice so that the Preference functionality can work with it, while this could be just a parameter of the function.
User avatar
mk-soft
Always Here
Always Here
Posts: 5313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: OpenPreferences + #PB_UTF8 without BOM (?)

Post by mk-soft »

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply