-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TIP #670: Simple Extra Procedures for File Access
- Loading branch information
Showing
6 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# foreachLine: | ||
# Iterate over the contents of a file, a line at a time. | ||
# The body script is run for each, with variable varName set to the line | ||
# contents. | ||
# | ||
# Copyright © 2023 Donal K Fellows. | ||
# | ||
# See the file "license.terms" for information on usage and redistribution | ||
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
# | ||
|
||
proc foreachLine {varName filename body} { | ||
upvar 1 $varName line | ||
set f [open $filename "r"] | ||
try { | ||
while {[gets $f line] >= 0} { | ||
uplevel 1 $body | ||
} | ||
} on return {msg opt} { | ||
dict incr opt -level | ||
return -options $opt $msg | ||
} finally { | ||
close $f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# readFile: | ||
# Read the contents of a file. | ||
# | ||
# Copyright © 2023 Donal K Fellows. | ||
# | ||
# See the file "license.terms" for information on usage and redistribution | ||
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
# | ||
|
||
proc readFile {filename {mode text}} { | ||
# Parse the arguments | ||
set MODES {binary text} | ||
set ERR [list -level 1 -errorcode [list TCL LOOKUP MODE $mode]] | ||
set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode] | ||
|
||
# Read the file | ||
set f [open $filename [dict get {text r binary rb} $mode]] | ||
try { | ||
return [read $f] | ||
} finally { | ||
close $f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# writeFile: | ||
# Write the contents of a file. | ||
# | ||
# Copyright © 2023 Donal K Fellows. | ||
# | ||
# See the file "license.terms" for information on usage and redistribution | ||
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
# | ||
|
||
proc writeFile {args} { | ||
# Parse the arguments | ||
switch [llength $args] { | ||
2 { | ||
lassign $args filename data | ||
set mode text | ||
} | ||
3 { | ||
lassign $args filename mode data | ||
set MODES {binary text} | ||
set ERR [list -level 1 -errorcode [list TCL LOOKUP MODE $mode]] | ||
set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode] | ||
} | ||
default { | ||
set COMMAND [lindex [info level 0] 0] | ||
return -code error -errorcode {TCL WRONGARGS} \ | ||
"wrong # args: should be \"$COMMAND filename ?mode? data\"" | ||
} | ||
} | ||
|
||
# Write the file | ||
set f [open $filename [dict get {text w binary wb} $mode]] | ||
try { | ||
puts -nonewline $f $data | ||
} finally { | ||
close $f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters