Hi
In order to write a program which can be parameter easily it can be
useful to have a plain text config file.
I tried to build a config file reader knowing that :
- values are easy to ready
- default vals and type checking are already implemented in tuples
- i'm not very at ease with "let" structures
- I didn't understood how to use bind in with-slots word (Slava 30
nov 08)
A naive config file can be :
foo 2
state bad
#bar 4.6
I defined the loadable-values vocab like this :
====================================
USING: kernel namespaces words sequences vocabs parser compiler.units
arrays splitting
math.parser mirrors accessors io.encodings.utf8 values.private values
classes.tuple.parser
classes.tuple assocs combinators io.files slots strings ;
IN: loadable-values
: create-value ( str -- word )
create-in
{ [ set-word ]
[ reset-generic ] ! rewrite of CREATE-WORD but not as a
parsing word
[ t "no-def-strip" set-word-prop ]
[ T{ value-holder } clone [ obj>> ] curry
(( -- value )) define-declared ]
[ ]
} cleave ;
: (create-values) ( seq -- ) first2 swap create-value set-value ;
: create-values ( word -- )
[ <mirror> >alist [ (create-values) ] each ] with-
compilation-unit ;
: activate-values ( loadable -- ) ! to factorize with create-values
<mirror> >alist
[ first2 swap words-named first set-value ] each ;
: LOADABLE-VALUES:
parse-tuple-definition
[ dup ] 2dip define-tuple-class
new create-values ; parsing
: file>loadable ( loadable file -- )
utf8 file-lines
[ " \t" split [ "" = not ] filter
first2
over 1 head "#" = [ 2drop ] [
{ { [ dup string>number ] [ string>number ] }
[ ]
} cond
swap setter-word execute ] if
] each ;
================================
In my program I define sets of values and can activate them when
needed, switching from a configuration to an other
================================
USING: loadable-values accessors kernel values math prettyprint
tools.walker ;
IN: testvar
VALUE: conf1
LOADABLE-VALUES: configuration-values
{ foo initial: 3 }
{ state initial: "good" }
{ val initial: 4.5 }
;
: test1 ( -- ) foo val + . ;
================================
>> configuration-values new
>> "resource:work/loadable-values/config.ini" file>loadable
>> to: conf1
>> foo
3
>> conf1 activate-values
>> foo
2
I'm quite sure I reinvented the wheel, I feel that namespaces were
made for that but it seems that with "bind" symbols are to be
declared first.
Could you tell me if there is a simplest way to manage a config file
and a set of values ?
thanks a lot
Jeff
------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk