WritePreferenceStringSafely()

Share your advanced PureBasic knowledge/code with the community.
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

WritePreferenceStringSafely()

Post by Mohawk70 »

Code: Select all

;
; WritePreferenceStringSafely.pbi
; by David Tupponce <Mohawk70>
; SAT 18-AUG-2012
; PureBasic 4.61 (Win x86)
; No Usage Restrictions ( Public Domain )
;-
;- You can easily adapt this procedure for other Preference File data types !
;-

Procedure.i WritePreferenceStringSafely(pFile.s,pGroup.s,pKey.s,text.s,mode.i)
 If pFile.s = ""
  ProcedureReturn #False
 EndIf
 If pGroup.s = ""
  ProcedureReturn #False
 EndIf
 If pKey.s = ""
  ProcedureReturn #False
 EndIf
 If mode.i <> 0 And mode.i <> 1
  ProcedureReturn #False
 EndIf
 Select mode.i
  Case 0
   OpenPreferences(pFile.s)
   PreferenceGroup(pGroup.s)
   WritePreferenceString(pKey.s,pText.s)
   ClosePreferences()
   ProcedureReturn #True
  Case 1
   Select FileSize(pFile.s)
    Case -2; File Is A Directory !
     ProcedureReturn #False
    Case -1; File Not Found !
     CreatePreferences(pFile.s)
     PreferenceGroup(pGroup.s)
     WritePreferenceString(pKey.s,text.s)
     ClosePreferences()
     ProcedureReturn #True
    Case 0,1,2,3,4; Invalid Preferences File !
     ProcedureReturn #False
   EndSelect
  EndSelect
  If OpenPreferences(pFile.s)
   If ExaminePreferenceGroups() = #True
    While NextPreferenceGroup(); Check if the group already exists within the file
     If PreferenceGroupName() = pGroup.s; The group exists, 
      PreferenceGroup(pGroup.s); Switch to the group
      If ExaminePreferenceKeys() = #True; Now we check if the key exists within the group
       While NextPreferenceKey()
	If PreferenceKeyName() = pKey.s; The key exists, so
	 content.s = ReadPreferenceString(pKey.s,#NULL$); Check if it is empty or not
	 If content.s = ""; It's empty so it's safe to write data to it
	  WritePreferenceString(key.s,text.s); Write the data
	  ClosePreferences(); Close the file
	  ProcedureReturn #True; Exit
	 Else
	  ; Ask user if he/she wants to overwrite the existing data ...
	  r = MessageRequester("Caution !","Overwrite existing data ?",#PB_MessageRequester_YesNo)
	  Select r
	   Case #PB_MessageRequester_No
	    ClosePreferences(); Close the file
	    ProcedureReturn #False; Exit without overwriting old data
	   Case #PB_MessageRequester_Yes
	    WritePreferenceString(pKey.s,text.s); Overwrite old data
	    ClosePreferences(); Close the file
	    ProcedureReturn #True; Exit
	  EndSelect
	 EndIf
        EndIf
       Wend
       ;If we get here the key doesn't exist so it's safe to create it !
       WritePreferenceString(pKey.s,text.s); Write data to new key
       ClosePreferences(); Close the file
       ProcedureReturn #True; Exit
      EndIf
     EndIf
    Wend
    ;If we get here the group doesn't exist so it's safe to create it !
    PreferenceGroup(pGroup.s)
    WritePreferenceString(pKey.s,text.s); Write data to new key
    ClosePreferences(); Close the file
    ProcedureReturn #True; Exit
   Else
    ;If we get here the group doesn't exist so it's safe to create it !
    PreferenceGroup(pGroup.s)
    WritePreferenceString(pKey.s,text.s); Write data to new key
    ClosePreferences(); Close the file
    ProcedureReturn #True; Exit
   EndIf
   ;No groups exist so create the group & write the key data
   PreferenceGroup(pGroup.s)
   WritePreferenceString(pKey.s,text.s)
   ClosePreferences()
   ProcedureReturn #True
  Else
   ProcedureReturn #False
  EndIf
EndProcedure

;-
;- Test Code
;-

r = WritePreferenceStringSafely("abc.txt","Group","UCase","ABCDEFGHIJKLMNOPQRSTUVWXYZ",1)
;You should get a MsgBox Here Only After The Program Has Been Run Once !

r = WritePreferenceStringSafely("abc.txt","Group","LCase","abcdefghijklmnopqrstuvwxyz",1)
;You should get a MsgBox Here Only After The Program Has Been Run Once 


r = WritePreferenceStringSafely("abc.txt","Group","LCase","ABCDEFGHIJKLMNOPQRSTUVWXYZ",1)
;You should get a MsgBox Here Everytime You Run The Program !

HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD