SCFG - Simple configuration files

A module that can be used for reading and writing simple configuration files. The file format is encapsulated. It is however currently scheme write/read format. Later, maybe it will use XML.

Currently this module doesn't work multi-user, i.e. concurrent use of configuration files will most likely result in problems. If you're going to use scfg configuration files concurrently, you'll have to provide your own locking.


 >(require (planet "scfg.scm" ("oesterholt" "ho-utils.plt" 1 0)))
 >(define cfg (sfg-new "config.scfg"))
 >(scfg-set! a '(a configuration parameter) "a-value")
 >(scfg-set! a '(another config par) (list 'a 'list 'of 'values))
 >(scfg-set! a 'par1 12)
 >(define I 1)
 >(scfg-set! a (list 'par 'value I) (* I (post++ I)))
 >(scfg-set! a (list 'par 'value I) (* I (post++ I)))
 >(scfg-set! a (list 'par 'value I) (* I (post++ I)))

 >(scfg-get a (list 'par 'value 2)) 

 >(scfg-get a (list 'par 'value 8)) 

 >(scfg-get a (list 'par 'value 8) "HI!") 


 > (scfg-map (lambda (key val) (display (format "~a=~a~%" key val)) val) a)

 (par value 3)=9
 (par value 2)=4
 (par value 1)=1
 (another config par)=(a list of values)
 (a configuration parameter)=a-value
 (%scfg version)=$Id: scfg.scm,v 1.2 2006/05/27 17:38:02 hoesterholt Exp $
 (%scfg filename)=config.scfg

 ("config.scfg" "$Id: scfg.scm,v 1.2 2006/05/27 17:38:02 hoesterholt Exp $" "a-value" (a list of values) 12 1 4 9)

 > (scfg-rm! a (list 'par 'value 2))
 > (scfg-get a (list 'par 'value 2))
 > (scfg-rm! a (list 'par 'value 8))

 > (scfg-save a)
 > (scfg-filename a)
 > (scfg-save a "new.scfg")
 > (scfg-filename a)
 > (scfg-save a "d:/")
 open-output-file: "d:/" exists as a directory
 > (scfg-filename a)


Standard use

(scfg-new . <file>) : scfg-handle

Creates or reads file into memory for use as scfg structure. Returns an scfg handle. If no file is provided, only a new scfg-handle is created, but no contents are read from file.

(scfg-save <scfg-handle> . <file>) : <scfg-handle>

Writes the current contents of scfg handle to the filename given with the scfg-new function or the given file parameter. Returns the given scfg-handle.

(scfg-set! <scfg-handle> <key> <value>) : <scfg-handle>

Sets key to value. key can be a symbol or number or a list of symbols and numbers.

(scfg-get <scfg-handle> <key> . <default-value>) : <value>

Returns the value associated with key, or, if key doesn't exist withing scfg-handle, the default-value, if given, or 'undef, if no default value has been given.

(scfg-rm! <scfg-handle> <key>): <scfg-handle>

Removes key from the scfg-handle. If key doesn't exist, it doesn't complain. Returns the given scfg-handle.

(scfg-filename <scfg-handle>) : string

Returns the current associated filename with the scfg-handle. The current associated filename is the last one saved or the one provided with scfg-new.

(scfg-cond-set!-get <scfg-handle> <key> <default> . <value>) : <value>

Sets key in scfg-handle, if value has been given. Returns the current value associated with key, or default, if key does not exist within scfg-handle.

Concurrent use

(scfg-multi-get <scfg-handle> <key> . <default>) : <value>##

Should provide concurrent access to the scfg configuration file. Is currently a call to scfg-get.

(scfg-multi-set! <scfg-handle> <key> <value>) : <scfg-handle>##

Should provide concurrent access to the scfg configuration file. Is currently a call to scfg-set! followed by a call to scfg-save.


(c) 2005 Hans Oesterholt-Dijkema. Distributed undef LGPL. Contact: send email to hans in domain Homepage: