source: tol/tolbase/lib/toltk/ini.tcl @ 3844cff

1941-docker-image-to-testdevel
Last change on this file since 3844cff was 6d7779d, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

initial import

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#############################################
2
3proc setini {file chap name val} {
4if {![file exists $file]} {
5   puts "INI::setini no such file \"$file\""
6   retun
7   }
8set ifile [open $file]
9set ofile [open $file.tmp w]
10set chapfound [set found 0]
11while {![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      }
16while {![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      }
24if {[eof $ifile] && !$found} {
25   if {!$chapfound} {puts $ofile \[$chap\]}
26   puts -nonewline $ofile $name=$val
27   }
28close $ifile
29close $ofile
30file rename -force ${file}.tmp $file
31}
32
33proc getini {file chap name} {
34if {![file exists $file]} {
35   puts "INI::getini no such file \"$file\""
36   retun
37   }
38set ifile [open $file]
39set chapfound 0
40set res ""
41while {![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      }
52close $ifile
53return $res
54}
55
56proc getchaps {file {chap ""}} {
57if {![file exists $file]} {
58   puts "INI::getini no such file \"$file\""
59   retun
60   }
61set ifile [open $file]
62set res ""
63while {![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      }
71return $res
72}
Note: See TracBrowser for help on using the repository browser.