| [6d7779d] | 1 | ############################################# |
|---|
| 2 | |
|---|
| 3 | proc setini {file chap name val} { |
|---|
| 4 | if {![file exists $file]} { |
|---|
| 5 | puts "INI::setini no such file \"$file\"" |
|---|
| 6 | retun |
|---|
| 7 | } |
|---|
| 8 | set ifile [open $file] |
|---|
| 9 | set ofile [open $file.tmp w] |
|---|
| 10 | set chapfound [set found 0] |
|---|
| 11 | while {![eof $ifile]} { |
|---|
| 12 | gets $ifile str |
|---|
| 13 | if {$str != "" } {puts "w1 ofile <$str>"} |
|---|
| 14 | if [regexp -- \\\[$chap\\\] [string trim $str]] {set chapfound 1; break} |
|---|
| 15 | } |
|---|
| 16 | while {![eof $ifile]} { |
|---|
| 17 | gets $ifile str |
|---|
| 18 | if {[string trim [lindex [split $str =] 0]] == "$name"} {set found 1; puts $ofile $name=$val |
|---|
| 19 | } else { |
|---|
| 20 | if [regexp -- {(\[)([[:alnum:]])*(\])} [string trim $str]] {set found 1; puts $ofile $name=$val} |
|---|
| 21 | if {$str != "" } {puts $ofile $str} |
|---|
| 22 | } |
|---|
| 23 | } |
|---|
| 24 | if {[eof $ifile] && !$found} { |
|---|
| 25 | if {!$chapfound} {puts $ofile \[$chap\]} |
|---|
| 26 | puts -nonewline $ofile $name=$val |
|---|
| 27 | } |
|---|
| 28 | close $ifile |
|---|
| 29 | close $ofile |
|---|
| 30 | file rename -force ${file}.tmp $file |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | proc getini {file chap name} { |
|---|
| 34 | if {![file exists $file]} { |
|---|
| 35 | puts "INI::getini no such file \"$file\"" |
|---|
| 36 | retun |
|---|
| 37 | } |
|---|
| 38 | set ifile [open $file] |
|---|
| 39 | set chapfound 0 |
|---|
| 40 | set res "" |
|---|
| 41 | while {![eof $ifile]} { |
|---|
| 42 | gets $ifile str |
|---|
| 43 | if [regexp -- \\\[$chap\\\] [string trim $str]] {set chapfound 1 |
|---|
| 44 | } else { |
|---|
| 45 | if [regexp -- {(\[)([[:alnum:]])*(\])} [string trim $str]] {set chapfound 0} |
|---|
| 46 | } |
|---|
| 47 | if {($chapfound) && ([string trim [lindex [split $str =] 0]] == "$name")} { |
|---|
| 48 | set res [string trim [lindex [split $str =] 1]] |
|---|
| 49 | break |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | close $ifile |
|---|
| 53 | return $res |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | proc getchaps {file {chap ""}} { |
|---|
| 57 | if {![file exists $file]} { |
|---|
| 58 | puts "INI::getini no such file \"$file\"" |
|---|
| 59 | retun |
|---|
| 60 | } |
|---|
| 61 | set ifile [open $file] |
|---|
| 62 | set res "" |
|---|
| 63 | while {![eof $ifile]} { |
|---|
| 64 | gets $ifile str |
|---|
| 65 | if {$chap == ""} { |
|---|
| 66 | if [regexp -- {(\[)([[:alnum:]])*(\])} [string trim $str]] {lappend res "[lindex [split $str \] ] 0]\]"} |
|---|
| 67 | } else { |
|---|
| 68 | if [regexp -- \\\[$chap\\\] [string trim $str]] {lappend res "[lindex [split $str \] ] 0]\]"} |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | return $res |
|---|
| 72 | } |
|---|