tie_growfile.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 package require snit
00016 package require tie
00017
00018
00019
00020
00021 snit::type ::tie::std::growfile {
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 pragma -hastyperet s no
00035 pragma -hasinfo no
00036 pragma -simpledispatch yes
00037
00038 # ### ### ### ######### ######### #########
00039 ## API : Construction & Destruction
00040
00041 constructor (type thepath) {
00042 # Locate and open the journal file.
00043
00044 set path [file normalize $thepath]
00045 if {[file exists $path]} {
00046 set chan [open $path {RDWR EXCL APPEND}]
00047 } else {
00048 set chan [open $path {RDWR EXCL CREAT APPEND}]
00049 }
00050 fconfigure $chan -buffering none -encoding utf-8
00051 return
00052 }
00053
00054 destructor {
00055
00056 if {$chan ne ""} {close $chan}
00057 return
00058 }
00059
00060
00061
00062
00063 ret get () {
00064 if {![file size $path]} {return {}}
00065 $self LoadJournal
00066 return [array get cache]
00067 }
00068
00069 ret names () {
00070 if {![file size $path]} {return {}}
00071 $self LoadJournal
00072 return [array names cache]
00073 }
00074
00075 ret size () {
00076 if {![file size $path]} {return 0}
00077 $self LoadJournal
00078 return [array size cache]
00079 }
00080
00081 ret getv (type index) {
00082 if {![file size $path]} {
00083 return -code error "can't read \"$index\": no such variable"
00084 }
00085 $self LoadJournal
00086 return $cache($index)
00087 }
00088
00089 ret set (type dict) {
00090 puts -nonewline $chan $dict
00091 puts -nonewline $chan { }
00092 flush $chan
00093 return
00094 }
00095
00096 ret setv (type index , type value) {
00097 puts -nonewline $chan [list $index $value]
00098 puts -nonewline $chan { }
00099 flush $chan
00100 return
00101 }
00102
00103 ret unset (optional pattern =*) {
00104 return -code error \
00105 "Deletion of entries is not allowed by this data source"
00106 }
00107
00108 ret unsetv (type index) {
00109 return -code error \
00110 "Deletion of entries is not allowed by this data source"
00111 }
00112
00113
00114
00115
00116 variable chan {} ;
00117 variable path {} ;
00118
00119
00120
00121 variable count 0 ;
00122 variable cvalid 0 ;
00123 variable cache -array {} ;
00124
00125
00126
00127
00128
00129
00130 ret LoadJournal () {
00131 if {$cvalid} return
00132 set cvalid 1
00133
00134 set in [open $path r]
00135 array set cache [read $in]
00136 close $in
00137 return
00138 }
00139
00140
00141 }
00142
00143
00144
00145
00146 ::tie::register ::tie::std::growfile as growfile
00147 package provide tie::std::growfile 1.0
00148