readfile.tcl 599 B

1234567891011121314151617181920212223
  1. # readFile:
  2. # Read the contents of a file.
  3. #
  4. # Copyright © 2023 Donal K Fellows.
  5. #
  6. # See the file "license.terms" for information on usage and redistribution
  7. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  8. #
  9. proc readFile {filename {mode text}} {
  10. # Parse the arguments
  11. set MODES {binary text}
  12. set ERR [list -level 1 -errorcode [list TCL LOOKUP MODE $mode]]
  13. set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode]
  14. # Read the file
  15. set f [open $filename [dict get {text r binary rb} $mode]]
  16. try {
  17. return [read $f]
  18. } finally {
  19. close $f
  20. }
  21. }