[REBOL] Re: Newbie questions
From: maximo:meteorstudios at: 2-Oct-2003 17:50
> Make your "ini" file itself a REBOL script, that does nothing except
> define words and their values. Then, as the first step in the
> application, DO the "ini" script. This is based on the idea
> that REBOL
> itself is somewhat readable. For example:
>
> Your application:
>
> REBOL []
> ...
> do %init.ini
> ...
your example is a very common practice amongst rebol coders.
might I just suggest that you conserve the .r extension, in order to make sure windows
and your users, do not expect the content to be an actual windows ini file.
for example rebol has a user.r file which is executed before any rebol script.
you might want to provide the values in your script beforehand with default values, so
that if not all the values where defined in the init file, your application can fallback
on default values.
also, I often use an object to store configuration block and provide a load and save
method to it.
config: make object! [
language: 'francais
bg-image: %polished.png
config-path: %./
config-name: %config.r
word: 'config ;the name of the word which holds the config object
init: does [
print ["using language : " language]
print ["using image : " bg-image]
]
load: has [config-file][
config-file: append copy config-path config-name
either (exists? config-file) [
do system/words/load config-file
][
print rejoin ["There is no file named: %" config-file " ...using default values" ]
]
init
]
save: has [config-file data][
config-file: append copy config-path config-name
data: copy ""
; here we build a rebol script which sets the config for each item
;----------------------------------
append data rejoin [word "/language: '" config/language "^/"]
append data rejoin [word "/bg-image: %" config/bg-image "^/" ]
write config-file data
]
]
if you do:
config/save
a file (%./config.r ) with the following content will be created:
config/language: 'francais
config/bg-image: %polished.png
this is both obvious and easy to edit and has the benefit, that you can load it directly
within rebol.
this simple object can be extended with many new values, which only have to be mirrored
in the save function and has the added benefit that you can load up new configs at run
time
also, because init is separated from load, you can call init anytime to reset your work.
as all the scripts which are posted on this list, edit, use and improve at will.
HTH!
-MAx