Version 9 of One more inifile parser

Updated 2009-03-17 17:58:17 by AK

See also Tcllib's infile package.


Inspired of some old PowerBASIC/DOS-code I've written 1000 years ago to handle INI-files, and in conjunction with what I said on the inifile page, I've started to write another INI file parser in TCL by myself as a evening fun project. This is the first result; don't expect anything from it.... (Only reading is possible for now! Not much error checking is done.)

 proc iniRead1 {ini args} {
    set h [open $ini r]
    catch {array set i [join $args]}; # optional defaults
    set currentSection {}
    while {[gets $h line] > -1} {
       # maybe sometimes: interpret trailing \, respect {blocks} like Tcl
       set line [string trim $line]; # remove junk
       # ;comments and blank lines are not of interest yet
       if {[string length $line] == 0 || [string range $line 0 0] == ";"} {
          continue; # I think we don't need no regexps for such primitive stuff
       }
       # recognize Sections
       if {[string length $line]        > 2    &&
           [string range $line 0 0]     == {[} &&
           [string range $line end end] == {]}} {
          set currentSection [string range $line 1 end-1]
       }
       # recognize Keywords, but only if a section already exists
       set eq [string first = $line]; # keyword must be at least 1 char long...
       if {$eq > 0 && [string length $currentSection]} {
          incr eq -1; set Key [string range $line 0   $eq]
          incr eq  2; set Val [string range $line $eq end]
          # to be done: (optional) \backslash and %envvar%-substitution
          set i($currentSection,$Key) [string trim $Val {"}]
       }
    }
    close $h
    return [array get i]
 }

I don't use any sophisticated or mysterious techniques in the code above; it should be portable to any language you can think of.... However, in my old PowerBASIC-Units I had implemented a few extra functions to overcome some restrictions of standard INI's, e.g.:

  • reading of "keywordless" sections - the whole text after a [sectionname] is returned (to support large sections of text in inis)
  • optional replacement of %EnvironmentVariables% in the values
  • support of an index appended to the ini file which speeds up access to inis to a level comparable to random files... (remember: in DOS-days it wasn't wise to waste memory; so my program was designed to give back the values of only one section with one call; so - each call was a new file access, no buffering in memory...)

The next step to take is to program the write access. Should not be much more difficult. But as usual, many different ideas of how to pass parameters to and results back from my procedures spook through my brain, I still don't have a general solution for it...

I'm still not sure if it is required to support programmatic access to the comments of an ini file. I think, the usual programmer is mostly interested in getting the values. Ok, if a program writes an ini file it possibly makes sense to write comments, though, and to respect position and order of existing, perhaps manually edited comments.