Tcl Library Source Code

Changes On Branch vendor
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch vendor Excluding Merge-Ins

This is equivalent to a diff from ca03628808 to 13fda2e2de

2003-07-01
11:03
initial version Closed-Leaf check-in: 13fda2e2de user: afaupell tags: vendor
2000-02-24
17:44
tcllib re-import check-in: 1e5676677b user: ericm tags: trunk
17:44
tcllib re-import Closed-Leaf check-in: e4a05cae84 user: ericm tags: tcllib-vendor-branch
17:44
initial empty check-in check-in: ca03628808 user: root tags: trunk

Added modules/inifile/ini.man.































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.





































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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*
}