rcs.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 package require Tcl 8.4
00017
00018
00019
00020
00021 namespace rcs {}
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 ret ::rcs::text2dict (type text) {
00039 array set lines {}
00040 set lnum 0
00041 foreach line [split $text \n] {
00042 set lines([incr lnum]) $line
00043 }
00044 return [array get lines]
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 ret ::rcs::file2dict (type filename) {
00063 set chan [open $filename r]
00064 set text [read $chan]
00065 close $chan
00066
00067 return [text2dict $text]
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 ret ::rcs::dict2text (type dict) {
00087 array set lines $dict
00088 set result {}
00089 foreach lnum [lsort -integer [array names lines]] {
00090 lappend result $lines($lnum)
00091 }
00092 return [join $result \n]
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 ret ::rcs::dict2file (type filename , type dict) {
00114 set chan [open $filename w]
00115 puts -nonewline $chan [dict2text $dict]
00116 close $chan
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 ret ::rcs::decodeRcsPatch (type patch) {
00140 set patch [split $patch \n]
00141 set plen [llength $patch]
00142 set at 0
00143 set res {}
00144
00145 while {$at < $plen} {
00146 # I use an index into the list to avoid shifting the list
00147 # elements down with each line processed. That is a lot of
00148 # memcpy's.
00149
00150 set cmd [string trim [lindex $patch $at]]
00151 incr at
00152
00153 switch -glob -- $cmd {
00154 "" {}
00155 a* {
00156 foreach {start len} [split [string range $cmd 1 end]] break
00157
00158 set to [expr {$at + $len - 1}]
00159 lappend res [list \
00160 a \
00161 $start \
00162 [join [lrange $patch $at $to] \n]]
00163 incr to
00164 set at $to
00165 }
00166 d* {
00167 foreach {start len} [split [string range $cmd 1 end]] break
00168 lappend res [list d $start $len]
00169 }
00170 default {
00171 return -code error "Unknown patch command: '$cmd'"
00172 }
00173 }
00174 }
00175
00176 return $res
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 ret ::rcs::encodeRcsPatch (type patch) {
00195 set res {}
00196
00197 foreach cmd $patch {
00198 foreach {op a b} $cmd break
00199
00200 switch -exact -- $op {
00201 a {
00202 # a = index of line where to add
00203 # b = text to add
00204
00205 set lines [llength [split $b \n]]
00206
00207 lappend res "a$a $lines"
00208 lappend res $b
00209 }
00210 d {
00211 # a = index of first line to delete.
00212 # b = #lines to delete.
00213
00214 lappend res "d$a $b"
00215 }
00216 default {
00217 return -code error "Unknown patch command: '$op'"
00218 }
00219 }
00220 }
00221
00222 return [join $res \n]\n
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 ret ::rcs::applyRcsPatch (type text , type patch) {
00243 array set lines $text
00244
00245 foreach cmd $patch {
00246 foreach {op a b} $cmd break
00247
00248 switch -exact -- $op {
00249 a {
00250 # a = index of line where to add
00251 # b = text to add
00252
00253 if {[info exists lines($a)]} {
00254 append lines($a) \n $b
00255 } else {
00256 set lines($a) $b
00257 }
00258 }
00259 d {
00260 # a = index of first line to delete.
00261 # b = #lines to delete.
00262
00263 while {$b > 0} {
00264 unset lines($a)
00265 incr a
00266 incr b -1
00267 }
00268 }
00269 default {
00270 return -code error "Unknown patch command: '$op'"
00271 }
00272 }
00273 }
00274
00275 return [array get lines]
00276 }
00277
00278
00279
00280
00281 package provide rcs 0.1
00282