quick and dirty settings
#1
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
Reply
#2
(07-05-2015, 06:55 PM)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
#3
(07-07-2015, 01:48 PM)lelldorin Wrote: 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]
or better:

Code:
sub GetSettings()
writefile = open(SettingsFile$, "a")
close(writefile)
local tempval$
tempval$ = attribute get$ "", SettingsFile$
if  not instr(tempval$,"WindowX") then
    return
else
    xorigin= val(attribute get$ "WindowX",SettingsFile$)
    yorigin = val(attribute get$ "WindowY",SettingsFile$)
    xwidth = val(attribute get$ "WindowWidth",SettingsFile$)
    yheight = val(attribute get$ "WindowHeight",SettingsFile$)
    WordWrap =  val(attribute get$ "WordWrap",SettingsFile$)
    Backup =  val(attribute get$ "Backup",SettingsFile$)
    Toolbar = val(attribute get$ "Toolbar",SettingsFile$)
end if




sub SaveSettings()
writefile = open(SettingsFile$, "a")
close(writefile)

        attribute set "string", "WindowX", str$(window get "MainWindow", "position-x"),SettingsFile$
        attribute set "string", "WindowY", str$(window get "MainWindow", "position-y"),SettingsFile$
        attribute set "string", "WindowWidth", str$( window get "MainWindow", "width"),SettingsFile$
        attribute set "string", "WindowHeight", str$( window get "MainWindow", "height"),SettingsFile$
        attribute set "string", "WordWrap", str$(WordWrap),SettingsFile$
        attribute set "string", "Backup", str$(Backup),SettingsFile$
        attribute set "string", "Toolbar", str$(Toolbar),SettingsFile$

end sub
Reply
#4
(07-07-2015, 01:48 PM)lelldorin Wrote: Why you des not use attributes?

That's a good question.

First, of all let me say that while I love yab, I have a love/hate relationship with Haiku. When I wrote Trope some years back, I still had this faint hope that someone might revive the old flyab project so I could write cross-platform apps in yab. Silly, I know. Since we are not likely to see BeFS installed on Windows or MacOSX machines, ever, I wanted to keep things portable.

Also, I actually started to design this app before Jan__64 added attribute handling to yab (somewhere around Haiku alpha2 IIRC). It could be done, but with a lot of SYSTEM$ calls.

But anyway, what are the advantages of doing this particular task with attributes? One that I can see is that the user is less likely to mess with them, but I try to build in defaults when reading my settings file yields gibberish, and you have to do that regardless of how you store the settings. I don't see a huge speed advantage on any vaguely modern machine.

Attributes turn the file system into a flat-file database. But when do you need a database? When you have a lot of data. You don't load Microsoft Access to store six lines of text.

In my next major project, I intend to use attributes extensively. I'm even creating a new filetype for them. Because they must be able to store thousands of data records.

But it's great that we have three different ways of doing this on record here now. People can try all three and see which one works for them.

I remember seeing a BASIC routine somewhere that allowed the parsing of MS-style .ini files. I'll see if I can get my hands on it again and post it here for reference purposes.
Reply
#5
(07-08-2015, 04:59 AM)clasqm Wrote:
(07-07-2015, 01:48 PM)lelldorin Wrote: Why you des not use attributes?

That's a good question.

First, of all let me say that while I love yab, I have a love/hate relationship with Haiku. When I wrote Trope some years back, I still had this faint hope that someone might revive the old flyab project so I could write cross-platform apps in yab. Silly, I know. Since we are not likely to see BeFS installed on Windows or MacOSX machines, ever, I wanted to keep things portable.

Also, I actually started to design this app before Jan__64 added attribute handling to yab (somewhere around Haiku alpha2 IIRC). It could be done, but with a lot of SYSTEM$ calls.

But anyway, what are the advantages of doing this particular task with attributes? One that I can see is that the user is less likely to mess with them, but I try to build in defaults when reading my settings file yields gibberish, and you have to do that regardless of how you store the settings. I don't see a huge speed advantage on any vaguely modern machine.

Attributes turn the file system into a flat-file database. But when do you need a database? When you have a lot of data. You don't load Microsoft Access to store six lines of text.

In my next major project, I intend to use attributes extensively. I'm even creating a new filetype for them. Because they must be able to store thousands of data records.

But it's great that we have three different ways of doing this on record here now. People can try all three and see which one works for them.

I remember seeing a BASIC routine somewhere that allowed the parsing of MS-style .ini files. I'll see if I can get my hands on it again and post it here for reference purposes.

yes jan add attribute Support past my asking about it, but he include a limitation of signs to store into it (can be that bbjimmy solve this already) so i use the system attribut tool. this one stores without limutation? i dint know but more then 4 signs.

my project buildLOG uses attributes too and there i store Informations out of textcontrols and textedits
Reply
#6
(07-08-2015, 06:12 AM)lelldorin Wrote: yes jan add attribute Support past my asking about it, but he include a limitation of signs to store into it (can be that bbjimmy solve this already) so i use the system attribut tool. this one stores without limutation? i dint know but more then 4 signs.

my project buildLOG uses attributes too and there i store Informations out of textcontrols and textedits

I made the attribute commands more user friendly:

Name:

attribute set -- sets ( adds or overwrites ) an attribute of a file.


Synopsis:

ATTRIBUTE SET Type$, Name$, Value$, Filename$
Set the attribute Name$ of type Type$ for the file Filename$ with the string Value$.
These are valid types:
Type$ = "String|Int|Long|Double|Float|Mime|Bool"
For Bool type attributes:
Value$ = "true|false"

Description:

Attribute set sets, adds or overwrites, file attributes. The attributes can be strings, integer, long.double, or float numbers, or boolian true/false.

Name:

attribute get$ -- Get the string value of an attribute.

Synopsis:

Value$ = ATTRIBUTE GET$ Name$, Filename$
Get the string value of the attribute Name$ for file Filename$.
For "Bool" type attributes:
Value$ ="true|false"
To get a list of attribute names and their types separated by " | ":
Name$ = ""
To get the current program directory:
Name$ = "" and Filename$= ""

Description:

Gets the string value of an attribute, or the list of attributes attatched to a file, or the directory that the program was executed from.

If the attribute is a bool type, the string returned is "true" or "false"

If the attribute name is "" a list of attributes attatched to the file is returned.

if both the attribute name and filename are "", the current program directory is returned.

Example:

dir$ = attribute get$ "",""
dir$=dir$+"/"

attribute set "string","attribute1", "This is the first attribute,", dir$+"foobar"
attribute set "int","attribute2", "127", dir$+"foobar"
attribute set "bool", "attribute3", "false", dir$+"foobar"
a$=attribute get$ "", dir$+"foobar"
print a$
a$=attribute get$ "attribute1", dir$+"foobar"
print "attribute1 \""+a$+"\""


Output:

attribute1 | String | attribute2 | Int | attribute3 | Bool |
attribute1 "This is the first attribute."


Explanation:

This program uses attrribute get$ to find the program directory, then saves three attributes to the file foobar located in the program directory. Next it gets and prints a list of the attribute names and types associated with the file, then it prints the contents of attribute1.



Name:
attribute get -- Get the number value of the attribute.

Synopsis:

Value = ATTRIBUTE GET Name$, Filename$

Description:

Value = ATTRIBUTE GET Name$, Filename$
Get the number value of the attribute Name$ for file Filename$.
For "Bool" type attributes:
Value = 1|0

Example:

attribute set "bool","Saved", "false", "foobar"

if (attribute get "Saved","foobar") then
print "foobar is saved"
else
print "foobar is not saved"
end if

Explanation:

This example saves a false (0) attribute named Saved on the file "foobar" then checks to see if the Saved attribute is true(1) or false(0) and prints the result. in this case "foobar is not saved."


Name:
attribute clear -- Deletes an attrinbute from a file.

Synopsis:
attribute clear Name$, Filename$

Description:

Delete the attribute Name$ from file Filename$.
Reply


Forum Jump:


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