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,"&","&")
theText = Replace(theText,"<","<")
theText = Replace(theText,">",">")
theText = Replace(theText,"¡","¡")
theText = Replace(theText,"¢","¢")
theText = Replace(theText,"£","£")
theText = Replace(theText,"¤","¤")
theText = Replace(theText,"¥","¥")
theText = Replace(theText,"¦","¦")
theText = Replace(theText,"§","§")
theText = Replace(theText,"¨","¨")
theText = Replace(theText,"©","©")
theText = Replace(theText,"ª","ª")
theText = Replace(theText,"«","«")
theText = Replace(theText,"¬","¬")
theText = Replace(theText,"®","®")
theText = Replace(theText,"¯","¯")
theText = Replace(theText,"°","°")
theText = Replace(theText,"±","±")
theText = Replace(theText,"²","²")
theText = Replace(theText,"³","³")
theText = Replace(theText,"´","´")
theText = Replace(theText,"µ","µ")
theText = Replace(theText,"¶","¶")
theText = Replace(theText,"·","·")
theText = Replace(theText,"¸","¸")
theText = Replace(theText,"¹","¹")
theText = Replace(theText,"º","º")
theText = Replace(theText,"»","»")
theText = Replace(theText,"¼","¼")
theText = Replace(theText,"½","½")
theText = Replace(theText,"¾","¾")
theText = Replace(theText,"¿","¿")
theText = Replace(theText,"=","=")
return theText
End Function
Function TextToHTML(s As String) As String
dim theText As String
theText = Replace(s,"&","&")
theText = Replace(theText,"<","<")
theText = Replace(theText,">",">")
theText = Replace(theText,"¡","¡")
theText = Replace(theText,"¢","¢")
theText = Replace(theText,"£","£")
theText = Replace(theText,"¤","¤")
theText = Replace(theText,"¥","¥")
theText = Replace(theText,"¦","¦")
theText = Replace(theText,"§","§")
theText = Replace(theText,"¨","¨")
theText = Replace(theText,"©","©")
theText = Replace(theText,"ª","ª")
theText = Replace(theText,"«","«")
theText = Replace(theText,"¬","¬")
theText = Replace(theText,"®","®")
theText = Replace(theText,"¯","¯")
theText = Replace(theText,"°","°")
theText = Replace(theText,"±","±")
theText = Replace(theText,"²","²")
theText = Replace(theText,"³","³")
theText = Replace(theText,"´","´")
theText = Replace(theText,"µ","µ")
theText = Replace(theText,"¶","¶")
theText = Replace(theText,"·","·")
theText = Replace(theText,"¸","¸")
theText = Replace(theText,"¹","¹")
theText = Replace(theText,"º","º")
theText = Replace(theText,"»","»")
theText = Replace(theText,"¼","¼")
theText = Replace(theText,"½","½")
theText = Replace(theText,"¾","¾")
theText = Replace(theText,"¿","¿")
theText = Replace(theText,"=","=")
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>
|