realbasic-nug
[Top] [All Lists]

Writing / Reading to a Preferences File

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Writing / Reading to a Preferences File
From: Mark O'Neill <real at rbclass dot com>
Date: Sun, 30 Mar 2008 23:58:27 +0100
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
Hi All,

Well, having finally decided upon a text file for storing my  
preferences as opposed to binary, I thought I'd share what I wrote in  
case it's useful to other file-newbies like me, and in case there's a  
better way of doing it! :) This routine was written "pre-encoding", so  
I've used html conversions to avoid the problems I was seeing with the  
copyright symbol. Anyway, here is what I did:

============== SAVE ==============
Sub PrefsSave()
   dim f as folderitem
   dim txtOut as TextOutputStream

   if PreferencesFolder.Child(KAppName).Exists = false then
     PreferencesFolder.Child(KAppName).CreateAsFolder
   end if

   f = PreferencesFolder.Child(KAppName).Child(KAppName + ".prefs")

   if f<> nil then
     txtOut=f.CreateTextFile
     txtOut.WriteLine("BoolState=" + GetTextFromBool(true))
     txtOut.WriteLine("AString=" + TextToHTML("A String"))
     txtOut.WriteLine("ANumber=" + Str(1))
     txtOut.Close
   else
     MsgBox "For some strange reason, your preferences were not saved."
   end if
End Sub

============== LOAD ==============
Sub PrefsLoad()
   dim f as folderitem
   dim txtIn as TextInputStream

   if PreferencesFolder.Child(KAppName).Exists then
     if PreferencesFolder.Child(KAppName).Child(KAppName +  
".prefs").Exists = false then
       return
     end if
   else
     return
   end if

   f = PreferencesFolder.Child(KAppName).Child(KAppName + ".prefs")

   if f<> nil then
     txtIn=f.OpenAsTextFile
     dim ln,field1,field2 As String

     while not txtIn.EOF
       ln = txtIn.ReadLine
       field1=NthField(ln,"=",1)
       field2=NthField(ln,"=",2)

       select case field1
       case "BoolState"
         CheckBox1.Value = GetBoolFromText(field2)
       case "AString"
         EditField1.Text = HTMLToText(field2)
       case "ANumber"
         MyInteger = Val(field2)
       end select
     wend

     txtIn.Close
   else
     MsgBox "For some strange reason, your preferences were not loaded."
   end if
End Sub

============== BOOL CONVERT ==============
Function GetBoolFromText(t As String) As Boolean
   if t = "true" then
     return true
   else
     return false
   end if
End Function

Function GetTextFromBool(b As Boolean) As String
   if b then
     return "true"
   else
     return "false"
   end if
End Function

Now, obviously, the following functions are not required if you assign  
the correct encoding to the file to avoid the problems I previously  
experienced, but I'm including them here anyway in case they're useful  
to others for non preference files and the like...

============== HTML CONVERT ==============
Function HTMLToText(s As String) As String
   dim theText As String

   theText = Replace(s,"&amp;","&")
   theText = Replace(theText,"&lt;","<")
   theText = Replace(theText,"&gt;",">")
   theText = Replace(theText,"&iexcl;","¡")
   theText = Replace(theText,"&cent;","¢")
   theText = Replace(theText,"&pound;","£")
   theText = Replace(theText,"&curren;","¤")
   theText = Replace(theText,"&yen;","¥")
   theText = Replace(theText,"&brvbar;","¦")
   theText = Replace(theText,"&sect;","§")
   theText = Replace(theText,"&uml;","¨")
   theText = Replace(theText,"&copy;","©")
   theText = Replace(theText,"&ordf;","ª")
   theText = Replace(theText,"&laquo;","«")
   theText = Replace(theText,"&not;","¬")
   theText = Replace(theText,"&reg;","®")
   theText = Replace(theText,"&macr;","¯")
   theText = Replace(theText,"&deg;","°")
   theText = Replace(theText,"&plusmn;","±")
   theText = Replace(theText,"&sup2;","²")
   theText = Replace(theText,"&sup3;","³")
   theText = Replace(theText,"&acute;","´")
   theText = Replace(theText,"&micro;","µ")
   theText = Replace(theText,"&para;","¶")
   theText = Replace(theText,"&middot;","·")
   theText = Replace(theText,"&cedil;","¸")
   theText = Replace(theText,"&sup1;","¹")
   theText = Replace(theText,"&ordm;","º")
   theText = Replace(theText,"&raquo;","»")
   theText = Replace(theText,"&frac14;","¼")
   theText = Replace(theText,"&frac12;","½")
   theText = Replace(theText,"&frac34;","¾")
   theText = Replace(theText,"&iquest;","¿")
   theText = Replace(theText,"&#61;","=")

   return theText
End Function

Function TextToHTML(s As String) As String
   dim theText As String

   theText = Replace(s,"&","&amp;")
   theText = Replace(theText,"<","&lt;")
   theText = Replace(theText,">","&gt;")
   theText = Replace(theText,"¡","&iexcl;")
   theText = Replace(theText,"¢","&cent;")
   theText = Replace(theText,"£","&pound;")
   theText = Replace(theText,"¤","&curren;")
   theText = Replace(theText,"¥","&yen;")
   theText = Replace(theText,"¦","&brvbar;")
   theText = Replace(theText,"§","&sect;")
   theText = Replace(theText,"¨","&uml;")
   theText = Replace(theText,"©","&copy;")
   theText = Replace(theText,"ª","&ordf;")
   theText = Replace(theText,"«","&laquo;")
   theText = Replace(theText,"¬","&not;")
   theText = Replace(theText,"®","&reg;")
   theText = Replace(theText,"¯","&macr;")
   theText = Replace(theText,"°","&deg;")
   theText = Replace(theText,"±","&plusmn;")
   theText = Replace(theText,"²","&sup2;")
   theText = Replace(theText,"³","&sup3;")
   theText = Replace(theText,"´","&acute;")
   theText = Replace(theText,"µ","&micro;")
   theText = Replace(theText,"¶","&para;")
   theText = Replace(theText,"·","&middot;")
   theText = Replace(theText,"¸","&cedil;")
   theText = Replace(theText,"¹","&sup1;")
   theText = Replace(theText,"º","&ordm;")
   theText = Replace(theText,"»","&raquo;")
   theText = Replace(theText,"¼","&frac14;")
   theText = Replace(theText,"½","&frac12;")
   theText = Replace(theText,"¾","&frac34;")
   theText = Replace(theText,"¿","&iquest;")
   theText = Replace(theText,"=","&#61;")

   return theText
End Function


All the best,

Mark.
------------------------------------------------------------
RB Class
"Killer Tool Bar" - theme-based x-platform toolbar
www.rbclass.com



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>
  • Writing / Reading to a Preferences File, Mark O'Neill <=