+# --- write-file NAME CONTENTS [TRANS] ---
+#
+# Write file NAME, storing CONTENTS translated according to TRANS (default
+# `binary'. The write is safe against errors -- we don't destroy the old
+# data until the file is written.
+
+proc write-file {name contents {trans binary}} {
+ if {[file exists $name]} {
+ if {[set rc [catch { file copy -force $name "$name.old" } err]]} {
+ return -code $rc $err
+ }
+ }
+ if {[set rc [catch {
+ set f [open $name w]
+ fconfigure $f -translation $trans
+ puts -nonewline $f $contents
+ close $f
+ } err]]} {
+ catch { close $f }
+ catch { file rename -force "$name.old" $name }
+ return -code $rc $err
+ }
+ return ""
+}
+
+# --- read-file NAME [TRANS] ---
+#
+# Read the contents of the file NAME, translating it according to TRANS
+# (default `binary').
+
+proc read-file {name {trans binary}} {
+ set f [open $name]
+ fconfigure $f -translation $trans
+ set c [read $f]
+ close $f
+ return $c
+}
+