Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
quick and dirty settings
#2
(07-06-2015, 10:55 AM)clasqm Wrote: In my app Trope I wanted to be able to save and recall the following settings

window position and size
wordwrap on/off
Backup on/off
Toolbar on/off

I opted for a quick and dirty way: print those values to a settings file in a known order, and later read them back in the same order. It works fine, but be aware that if your user monkeys around with the settings file, it can cause considerable chaos.

Code:
WordWrap = 0
Backup = 0
Toolbar = 1
SettingsFile$ = "/boot/home/config/settings/Trope/Trope.settings"

sub SaveSettings()
    open SettingsFile$ for writing as #1
    print #1 window get "MainWindow", "position-x"
    print #1 window get "MainWindow", "position-y"
    print #1 window get "MainWindow", "width"
    print #1 window get "MainWindow", "height"
    print #1 str$(WordWrap)
    print #1 str$(Backup)
    print #1 str$(Toolbar)
    close #1
end sub

sub GetSettings()
    local tempval$
    open SettingsFile$ for reading as #1
        line input #1 tempval$: xorigin = val(tempval$)
        if xorigin = 0 then //ie if the settings file was empty
            xorigin = 50 //reset xorigin, use defaults for others
            close #1
            return
        else
            line input #1 tempval$: yorigin = val(tempval$)
            line input #1 tempval$: xwidth = val(tempval$)
            line input #1 tempval$: yheight = val(tempval$)
            line input #1 tempval$: WordWrap = val(tempval$)
            line input #1 tempval$: Backup = val(tempval$)
            line input #1 tempval$: Toolbar = val(tempval$)
        end if
    close #1
end sub

Why you des not use attributes?

Code:
//Creating Settings file
SettingsFile$ = "/boot/home/config/settings/Trope/Trope.settings"

SettingsWindowX=WINDOW GET "MainWindow", "position-x"
SettingsWindowY=WINDOW GET "MainWindow", "position-y"
SettingsWindowWidth=WINDOW GET "MainWindow", "width"
SettingsWindowHeight=WINDOW GET "MainWindow", "height"

WordWrap = 0
Backup = 0
Toolbar = 1

writefile = open(SettingsFile$, "a")
    Output$=System$("addattr -t string WindowX str$(SettingsWindowX)+" "+SettingsFile$)
        Output$=System$("addattr -t string WindowY str$(SettingsWindowY)+" "+SettingsFile$)
        Output$=System$("addattr -t string WindowWidth str$(SettingsWindowWidth)+" "+SettingsFile$)
        Output$=System$("addattr -t string WindowHeight str$(SettingsWindowHeight)+" "+SettingsFile$)
        Output$=System$("addattr -t string WordWrap str$(WordWrap )+" "+SettingsFile$)
        Output$=System$("addattr -t string Backup str$(Backup )+" "+SettingsFile$)
        Output$=System$("addattr -t string Toolbar str$(Toolbar )+" "+SettingsFile$)
close(writefile)

//Read Settings
SettingsWindowX=val(System$("catattr -d WindowX "+SettingsFile$))
SettingsWindowY =val(System$("catattr -d WindowY "+SettingsFile$))
SettingsWindowWidth=val(System$("catattr -d WindowWidth"+SettingsFile$))
SettingsWindowHeight =val(System$("catattr -d WindowHeight "+SettingsFile$))
WordWrap=val(System$("catattr -d WordWrap"+SettingsFile$))
Backup=val(System$("catattr -d Backup "+SettingsFile$))
Toolbar=val(System$("catattr -d Toolbar "+SettingsFile$))

Its out of my mind, i hope i does not add any bug
[/code]
Reply


Messages In This Thread
quick and dirty settings - by clasqm - 07-06-2015, 10:55 AM
RE: quick and dirty settings - by lelldorin - 07-08-2015, 05:48 AM
RE: quick and dirty settings - by bbjimmy - 07-08-2015, 06:33 AM
RE: quick and dirty settings - by clasqm - 07-08-2015, 08:59 PM
RE: quick and dirty settings - by lelldorin - 07-08-2015, 10:12 PM
RE: quick and dirty settings - by bbjimmy - 07-09-2015, 03:05 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting