diff options
author | dgp <dgp@users.sourceforge.net> | 2023-11-22 19:20:12 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2023-11-22 19:20:12 (GMT) |
commit | ca0654d3b984b0cb57abf74e7eef420a6d6d04c9 (patch) | |
tree | 362394bea3c02ca87ea7ea1e536bc65f2f9fa8a9 /library | |
parent | cd515c4321f079a9a2e4dd72e769da7a88a45b14 (diff) | |
parent | 9977a1183593d954bcb6c63c24502b8eb5220e65 (diff) | |
download | tcl-ca0654d3b984b0cb57abf74e7eef420a6d6d04c9.zip tcl-ca0654d3b984b0cb57abf74e7eef420a6d6d04c9.tar.gz tcl-ca0654d3b984b0cb57abf74e7eef420a6d6d04c9.tar.bz2 |
Merge many improvements from trunk that look good for next rc.
Diffstat (limited to 'library')
-rw-r--r-- | library/foreachline.tcl | 25 | ||||
-rw-r--r-- | library/readfile.tcl | 23 | ||||
-rw-r--r-- | library/tclIndex | 3 | ||||
-rw-r--r-- | library/writefile.tcl | 37 |
4 files changed, 88 insertions, 0 deletions
diff --git a/library/foreachline.tcl b/library/foreachline.tcl new file mode 100644 index 0000000..aacbd5b --- /dev/null +++ b/library/foreachline.tcl @@ -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 + } +} diff --git a/library/readfile.tcl b/library/readfile.tcl new file mode 100644 index 0000000..c1d5b84 --- /dev/null +++ b/library/readfile.tcl @@ -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 + } +} diff --git a/library/tclIndex b/library/tclIndex index a8db3cb..8fd5a89 100644 --- a/library/tclIndex +++ b/library/tclIndex @@ -19,6 +19,7 @@ set auto_index(::auto_mkindex_parser::childhook) [list ::tcl::Pkg::source [file set auto_index(::auto_mkindex_parser::command) [list ::tcl::Pkg::source [file join $dir auto.tcl]] set auto_index(::auto_mkindex_parser::commandInit) [list ::tcl::Pkg::source [file join $dir auto.tcl]] set auto_index(::auto_mkindex_parser::fullname) [list ::tcl::Pkg::source [file join $dir auto.tcl]] +set auto_index(foreachLine) [list ::tcl::Pkg::source [file join $dir foreachline.tcl]] set auto_index(history) [list ::tcl::Pkg::source [file join $dir history.tcl]] set auto_index(::tcl::HistAdd) [list ::tcl::Pkg::source [file join $dir history.tcl]] set auto_index(::tcl::HistKeep) [list ::tcl::Pkg::source [file join $dir history.tcl]] @@ -34,6 +35,7 @@ set auto_index(tclPkgUnknown) [list ::tcl::Pkg::source [file join $dir package.t set auto_index(::tcl::MacOSXPkgUnknown) [list ::tcl::Pkg::source [file join $dir package.tcl]] set auto_index(::pkg::create) [list ::tcl::Pkg::source [file join $dir package.tcl]] set auto_index(parray) [list ::tcl::Pkg::source [file join $dir parray.tcl]] +set auto_index(readFile) [list ::tcl::Pkg::source [file join $dir readfile.tcl]] set auto_index(::safe::InterpStatics) [list ::tcl::Pkg::source [file join $dir safe.tcl]] set auto_index(::safe::InterpNested) [list ::tcl::Pkg::source [file join $dir safe.tcl]] set auto_index(::safe::interpCreate) [list ::tcl::Pkg::source [file join $dir safe.tcl]] @@ -67,6 +69,7 @@ set auto_index(tcl_wordBreakBefore) [list ::tcl::Pkg::source [file join $dir wor set auto_index(tcl_endOfWord) [list ::tcl::Pkg::source [file join $dir word.tcl]] set auto_index(tcl_startOfNextWord) [list ::tcl::Pkg::source [file join $dir word.tcl]] set auto_index(tcl_startOfPreviousWord) [list ::tcl::Pkg::source [file join $dir word.tcl]] +set auto_index(writeFile) [list ::tcl::Pkg::source [file join $dir writefile.tcl]] set auto_index(::tcl::tm::add) [list ::tcl::Pkg::source [file join $dir tm.tcl]] set auto_index(::tcl::tm::remove) [list ::tcl::Pkg::source [file join $dir tm.tcl]] set auto_index(::tcl::tm::list) [list ::tcl::Pkg::source [file join $dir tm.tcl]] diff --git a/library/writefile.tcl b/library/writefile.tcl new file mode 100644 index 0000000..fbd9138 --- /dev/null +++ b/library/writefile.tcl @@ -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 + } +} |