writefile.tcl 911 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # writeFile:
  2. # Write 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 writeFile {args} {
  10. # Parse the arguments
  11. switch [llength $args] {
  12. 2 {
  13. lassign $args filename data
  14. set mode text
  15. }
  16. 3 {
  17. lassign $args filename mode data
  18. set MODES {binary text}
  19. set ERR [list -level 1 -errorcode [list TCL LOOKUP MODE $mode]]
  20. set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode]
  21. }
  22. default {
  23. set COMMAND [lindex [info level 0] 0]
  24. return -code error -errorcode {TCL WRONGARGS} \
  25. "wrong # args: should be \"$COMMAND filename ?mode? data\""
  26. }
  27. }
  28. # Write the file
  29. set f [open $filename [dict get {text w binary wb} $mode]]
  30. try {
  31. puts -nonewline $f $data
  32. } finally {
  33. close $f
  34. }
  35. }