new-fileio.sl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % New-FileIO.SL
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 30 July 1982
  8. %
  9. % Revised File I/O for EMODE.
  10. %
  11. % The combination of buffered file input and string-oriented reading of the
  12. % file into the buffer makes for a 5X improvement in the speed of reading a
  13. % nontrivial file (or more, since it no longer does unnecessary consing).
  14. % In addition, the ^Z EOF bug has been fixed.
  15. %
  16. % A similar speedup has been made to file output. In addition, an extra
  17. % blank line is no longer written at the end of each file.
  18. %
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. (CompileTime (load objects))
  21. (load input-stream output-stream fast-vector)
  22. (de readfile (file-name)
  23. (write-prompt "")
  24. (let* ((p (ErrorSet (List 'open-input file-name) NIL NIL))
  25. )
  26. (if (PairP p)
  27. (read-file-into-buffer (car p))
  28. (write-prompt (BldMsg "Unable to read file: %w" file-name))
  29. (Ding)
  30. )))
  31. (de read-file-into-buffer (s)
  32. (write-prompt (BldMsg "Reading file: %w" (=> s file-name)))
  33. (setf CurrentBufferText (MkVect 1))
  34. (setf CurrentBufferSize 1)
  35. (append-file-to-buffer s)
  36. (=> s close)
  37. (write-prompt (BldMsg "File read: %w (%d lines)"
  38. (=> s file-name)
  39. (current-buffer-visible-size)))
  40. )
  41. (de append-file-to-buffer (s)
  42. (prog (line-buffer line-size ch)
  43. (setf line-buffer (MkString 200 0))
  44. (while T
  45. (setf line-size 0)
  46. (setf ch (input-stream$getc s))
  47. (while (not (or (null ch) (WEq ch (char EOL))))
  48. (if (WGreaterP line-size (ISizeS line-buffer))
  49. (setf line-buffer (concat line-buffer (Mkstring 200 0)))
  50. )
  51. (iputs line-buffer line-size ch)
  52. (setf line-size (WPlus2 line-size 1))
  53. (setf ch (input-stream$getc s))
  54. )
  55. (if (not (and (null ch) (WEq line-size 0)))
  56. (append-line-to-buffer (sub line-buffer 0 (WDifference line-size 1)))
  57. )
  58. (cond ((null ch)
  59. (if (> line-size 0)
  60. (setf CurrentBufferSize (- CurrentBufferSize 1))
  61. )
  62. (exit)))
  63. )
  64. (GetLine (setf CurrentLineIndex 0))
  65. ))
  66. (de append-line-to-buffer (contents)
  67. % Note: GETLINE must be done after a sequence of appends
  68. (let ((indx CurrentBufferSize))
  69. (setf CurrentBufferSize (+ CurrentBufferSize 1))
  70. (if (> CurrentBufferSize (size CurrentBufferText))
  71. (setf CurrentBufferText (concat CurrentBufferText (MkVect 63))))
  72. (SetBufferText (- indx 1) contents)
  73. (SetBufferText indx "")
  74. ))
  75. (de WriteFile (file-name)
  76. % Write whole of current EMODE buffer to file.
  77. (write-prompt "")
  78. (let* ((p (ErrorSet (list 'open-output file-name) NIL NIL))
  79. )
  80. (if (PairP p)
  81. (let ((s (car p)))
  82. (write-prompt (BldMsg "Writing file: %w" (=> s file-name)))
  83. (write-buffer-to-stream s)
  84. (=> s close)
  85. (write-prompt (BldMsg "File written: %w (%d lines)"
  86. (=> s file-name)
  87. (current-buffer-visible-size)))
  88. )
  89. (write-prompt (BldMsg "Unable to write file: %w" file-name))
  90. (Ding)
  91. )))
  92. (de write-buffer-to-stream (s)
  93. (PutLine CurrentLineIndex)
  94. (for (from i 0 (- CurrentBufferSize 2) 1)
  95. (do (output-stream$putl s (GetBufferText i)))
  96. )
  97. (output-stream$puts s (GetBufferText (- CurrentBufferSize 1)))
  98. )