filelib.tcl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ###########################################################
  2. # Name: filelib.tcl
  3. # Author: Daniele Bonini (posta@elettronica.lol)
  4. # Date: 01/12/2023
  5. # Desc: Filelib namespace of RadXIDE.
  6. #
  7. # Filelib namespace and most of the code
  8. # here presented and distributed contains excerpts
  9. # from [alited](https://github.com/aplsimple/alited
  10. # by Alex Plotnikov and contributors to the project.
  11. # The original code of these excerpts could be
  12. # borrowed from other sources which the author
  13. # and the contributors to this RadXIDE have no
  14. # knowledge about.
  15. #
  16. # License: MIT. Copyrights 5 Mode (Last implementation and adaptations.)
  17. # Copyright (c) 2021-2023 Alex Plotnikov https://aplsimple.github.io (original scaffolding and excerpts.)
  18. #
  19. ###########################################################
  20. namespace eval filelib {
  21. # __________________________ createFile _________________________ #
  22. proc createFile {{destfolder ""}} {
  23. # Create a file.
  24. # fname - file name
  25. # stxt - text to save
  26. namespace upvar ::radxide dan dan project project
  27. set n "newfile"
  28. #tk_messageBox -title $dan(TITLE) -icon error -message $destfolder
  29. if {$destfolder ne ""} {
  30. set num 1
  31. while {[file exists $destfolder/$n$num]} {
  32. incr num
  33. }
  34. #tk_messageBox -title $dan(TITLE) -icon error -message $destfolder/$n$num
  35. set fname $destfolder/$n$num
  36. set fh [open $fname {WRONLY CREAT}]
  37. set stxt ""
  38. puts $fh $stxt
  39. close $fh
  40. }
  41. }
  42. # __________________________ createFile _________________________ #
  43. proc createFolder {{parentfolder ""}} {
  44. # Create a file.
  45. # fname - file name
  46. # stxt - text to save
  47. namespace upvar ::radxide dan dan project project
  48. set n "newfolder"
  49. #tk_messageBox -title $dan(TITLE) -icon error -message $parentfolder
  50. if {$parentfolder ne ""} {
  51. set num 1
  52. while {[file exists $parentfolder/$n$num]} {
  53. incr num
  54. }
  55. #tk_messageBox -title $dan(TITLE) -icon error -message $parentfolder/$n$num
  56. set fname $parentfolder/$n$num
  57. file mkdir $fname
  58. }
  59. }
  60. # __________________________ delFile _________________________ #
  61. proc delFile {{fname ""}} {
  62. # Delete a file.
  63. # fname - file name
  64. if {($fname ne "")} {
  65. catch {[file delete $fname]}
  66. }
  67. }
  68. # __________________________ openFile _________________________ #
  69. proc openFile {{fname ""} {reload no} {islist no} {Message {}}} {
  70. # Handles "Open file" menu item.
  71. # fnames - file name (if not set, asks for it)
  72. # reload - if yes, loads the file even if it has a "strange" extension
  73. # islist - if yes, *fnames* is a file list
  74. # Message - name of procedure for "open file" message
  75. # Return the content of the file
  76. namespace upvar ::radxide dan dan
  77. set ret ""
  78. if {$fname ne "" && [file exists $fname]} {
  79. set fh [open $fname {RDONLY}]
  80. set ret [set data [read $fh]]
  81. close $fh
  82. }
  83. return $ret
  84. }
  85. # __________________________ saveFile _________________________ #
  86. proc saveFile {{fname ""} {stxt ""}} {
  87. # Saves a file.
  88. # fname - file name
  89. # stxt - text to save
  90. namespace upvar ::radxide dan dan project project
  91. if {($fname ne "") && ($stxt ne "")} {
  92. set fh [open $fname {WRONLY CREAT}]
  93. puts $fh $stxt
  94. chan truncate $fh
  95. close $fh
  96. }
  97. }
  98. #_______________________
  99. }
  100. # _________________________________ EOF _________________________________ #