Purpose: to collect pointers and observations regarding techniques for reading and writing configuration files by an application


  • What is a configuration file?
  • What are the issues one needs to keep in mind?
  • What internet resources are available for exploring tcl techniques in writing and reading configuration files?

bach: lvirden: what I like to do is using arrays. Writing: puts array get. Reading read eval array set

lvirden: That's a pretty common technique. That's why I was surprised not to find a page describing that as well as some of the other techniques (like option, etc.)

lvirden: I think that perhaps there are just so many different techniques that we haven't gotten even a small percent of them documented.

RS: But, as I found out, source also works on pure data, so you can write:

 array set x [source x.dmp] 

Only you cannot have comments in the data then - or you can, if you accept an array element by the name of #, the , then you can dump

 # {Saved by ... on ...}

rmax: It's downside is, that this kind of configuration file isn't very readable or editable.

suchenwi: Oh, you could format it like this:

 foreach i [lsort [array names a]] {
        puts [list $i $a($i)]
 } 

That's actually what I do, so the file still is nice to browse.

aku AK: config files - There was a page about tcl patterns somewhere. I believe this contained something. also look for the pages by Koen Van Damme on the wiki.

aku: His homepage refers to a paper by him about parsing data files

rmax: Hmm, reading and writing configuration files of various flavour seems me worth spending a tcllib module for. I could add a package that parses windows-style .ini files, I once wrote.


See Metakit


US: I always use the following code snippet as a configuration preamble [to ensure that one has a name for the current program running]

 if {[string length [info script]]} {
    # It's just a script
    set here [file dirname [info script]]
    } else {
    # Wrapped into an executable
    set here [file dirname [info nameofexecutable]]
    }
 set owd [pwd]
 cd $here
 cd ..
 set CFGDIR [file join [pwd] cfg]
 cd $owd

See Config File Parser too.