ADDED modules/inifile/ini.man Index: modules/inifile/ini.man ================================================================== --- /dev/null +++ modules/inifile/ini.man @@ -0,0 +1,63 @@ +[comment {-*- tcl -*- doctools manpage}] +[manpage_begin inifile n 0.1] +[moddesc {Parsing of Windows INI files}] +[titledesc {Parsing of Windows INI files}] +[require Tcl] +[require inifile [opt 0.1]] +[description] + +This package provides an interface for easy manipulation of Windows INI files. + +[para] + +[list_begin definitions] + +[call [cmd ::ini::open] [arg file] [opt mode]] + +Opens an INI file and returns a handle that is used by other commands. +Mode has the same types as the [const open] command. The default mode is [const r+]. + +[call [cmd ::ini::close] [arg ini]] + +Close the specified handle. If any changes were made and not written by +[const commit] they are lost. + +[call [cmd ::ini::commit] [arg ini]] + +Writes the file and all changes to disk. + +[call [cmd ::ini::sections] [arg ini]] + +Returns a list of all the names of the existing sections in the file handle +specified. + +[call [cmd ::ini::keys] [arg ini] [arg section]] + +Returns a list of all they key names in the section and file specified. + +[call [cmd ::ini::get] [arg ini] [arg section]] + +Returns a list of key value pairs that exist in the section and file specified. + +[call [cmd ::ini::exists] [arg ini] [arg section] [opt key]] + +Returns a boolean value indicating the existance of the specified section as a +whole or the specified key within that section. + +[call [cmd ::ini::value] [arg ini] [arg section] [arg key]] + +Returns the value of the named key from the specified section. + +[call [cmd ::ini::set] [arg ini] [arg section] [arg key] [arg value]] + +Sets the value of the key in the specified section. if the section does not +exist then a new one is created. + +[call [cmd ::ini::delete] [arg ini] [arg section] [opt key]] + +Removes the key or the entire section and all its keys. + + +[list_end] + +[manpage_end] ADDED modules/inifile/ini.tcl Index: modules/inifile/ini.tcl ================================================================== --- /dev/null +++ modules/inifile/ini.tcl @@ -0,0 +1,114 @@ +package provide inifile 0.1 + +namespace eval ini {} + +proc ::ini::open {ini {mode r+}} { + ::set fh [::open $ini $mode] + fconfigure $fh -translation crlf + array set $fh {} + _loadfile $fh + return $fh +} + +proc ::ini::close {fh} { + variable $fh + commit $fh + unset $fh + close $fh +} + +# write all changes to disk + +proc ::ini::commit {fh} { + variable $fh + seek $fh 0 start + array set s {} + foreach x [array names $fh] { + ::set s([lindex [split $x \000] 0]) 1 + } + foreach sec [array names s] { + puts $fh "\[$sec\]" + foreach key [lsort -dictionary [array names $fh $sec\000*]] { + puts $fh "[lindex [split $key \000] 1]=[::set ${fh}($key)]" + } + puts $fh "" + } + _loadfile $fh +} + +proc ::ini::_loadfile {fh} { + variable $fh + ::set cur {} + seek $fh 0 start + ::set data [read $fh] + foreach line [split $data "\n"] { + if {[string match {\[*\]} $line]} { + ::set cur [string range $line 1 end-1] + } elseif {[string match {*=*} $line]} { + ::set line [split $line =] + ::set key [lindex $line 0] + ::set value [join [lrange $line 1 end] =] + ::set ${fh}($cur\000$key) $value + } + } +} + +# return all section names + +proc ::ini::sections {fh} { + variable $fh + array set r {} + foreach x [array names $fh] { + ::set r([lindex [split $x \000] 0]) 1 + } + return [array names r] +} + +#return all key names of section + +proc ::ini::keys {fh sec} { + variable $fh + ::set r {} + foreach x [array names $fh $sec\000*] { + lappend r [lindex [split $x \000] 1] + } + return $r +} + +#return all key value pairs of section + +proc ::ini::get {fh sec} { + variable $fh + ::set r {} + foreach x [array names $fh $sec\000*] { + lappend r [lindex [split $x \000] 1] [::set ${fh}($x)] + } + return $r +} + +proc ::ini::exists {fh sec {key {}}} { + variable $fh + if {$key == ""} { + if {[array names $fh $sec\000*] == ""} {return 0} + return 1 + } + return [info exists ${fh}($sec\000$key)] +} + +proc ::ini::value {fh sec key} { + variable $fh + return [::set ${fh}($sec\000$key)] +} + +proc ::ini::set {fh sec key value} { + variable $fh + ::set ${fh}($sec\000$key) $value +} + +proc ::ini::delete {fh sec {key {}}} { + variable $fh + if {$key == ""} { + array unset $fh $sec\000$key + } + array unset $fh $sec\000* +}