home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / filestream.st < prev    next >
Text File  |  1992-02-15  |  5KB  |  234 lines

  1. "======================================================================
  2. |
  3. |   FileStream Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         22 Sep 91      Added #popen:dir:ifFail: and #open:mode:ifFail: so
  34. |              that open failures can be explicitly handled.  Thanks
  35. |              to Michael Richardson for the brilliant idea!
  36. |
  37. | sbb          5 Jul 91      Added fileIn:line:from:at: so that when Emacs sends
  38. |              out an expression or a method definition to Smalltalk
  39. |              the error messages accurately report the line number.
  40. |
  41. | sbb          6 Jun 91      Fixed open and popen to use self new instead of
  42. |              FileStream explicitly.
  43. |
  44. | sbb          4 Mar 91      Added verbose flag.
  45. |
  46. | sbyrne     19 May 90      Rewrite contents to take advantage of the new
  47. |              FileStream>>size method.
  48. |
  49. | sbyrne     19 Dec 89      added fileIn: and primitive file in.
  50. |
  51. | sbyrne     21 May 89      created.
  52. |
  53. "
  54.  
  55. ReadWriteStream subclass: #FileStream
  56.         instanceVariableNames: 'file name buffer'
  57.         classVariableNames: 'verbose'
  58.         poolDictionaries: ''
  59.         category: nil
  60. !
  61.  
  62. FileStream comment: 
  63. 'My instances are what conventional programmers think of as files.
  64. My instance creation methods accept the name of a disk file (or any named
  65. file object, such as /dev/rmt0 on UNIX or MTA0: on VMS).'
  66. !
  67.  
  68.  
  69. !FileStream class methodsFor: 'basic'!
  70.  
  71. open: fileName mode: fileMode
  72.     ^self open: fileName mode: fileMode 
  73.       ifFail: [ ^self error: 'Failed to open ''', fileName, '''' ]
  74. !
  75.  
  76. open: fileName mode: fileMode ifFail: aBlock
  77.     | file |
  78.     file _ self new.
  79.     "Does it make sense to have the block be invoked with the file name as 
  80.      an argument?"
  81.     (file fileOp: 0 with: fileName with: fileMode)
  82.     isNil ifTrue: [ ^aBlock value ].
  83.     ^file
  84. !
  85.  
  86. popen: commandName dir: direction
  87.     ^self popen: commandName dir: direction
  88.       ifFail: [ ^self error: 'popen failed for command ''',
  89.             commandName, '''' ]
  90. !
  91.  
  92. popen: commandName dir: direction ifFail: aBlock
  93.     | file |
  94.     file _ self new.
  95.     (file fileOp: 7 with: commandName with: direction)
  96.     isNil ifTrue: [ ^aBlock value ].
  97.     ^file
  98. !
  99.  
  100. fileIn: aFileName
  101.     | fileStream |
  102.     verbose ifTrue: [ stdout nextPutAll: 'Loading ';
  103.               nextPutAll: aFileName; nl ].
  104.     fileStream _ self open: aFileName mode: 'r'.
  105.     fileStream fileIn.
  106.     fileStream close.
  107. !
  108.  
  109. fileIn: aFileName line: lineInteger from: realFileName at: aCharPos
  110.     | fileStream |
  111.     verbose ifTrue: [ stdout nextPutAll: 'Loading ';
  112.               nextPutAll: aFileName; nl ].
  113.     fileStream _ self open: aFileName mode: 'r'.
  114.     fileStream fileInLine: lineInteger fileName: realFileName at: aCharPos.
  115.     fileStream close.
  116. !
  117.  
  118. verbose: verboseFlag
  119.     | oldVerbose |
  120.     oldVerbose _ verbose.
  121.     verbose _ verboseFlag.
  122.     ^oldVerbose
  123. !
  124.  
  125. require: aSymbol
  126.     "If there is a class defined with the name 'aSymbol', do nothing, otherwise
  127.      fileIn 'aSymbol'.st"
  128.     Smalltalk at: aSymbol
  129.           ifAbsent:
  130.           [ self fileIn: aSymbol , '.st' ]
  131. ! !
  132.  
  133.  
  134.  
  135. !FileStream class methodsFor: 'initialization'!
  136.  
  137. initialize
  138.     verbose _ false.
  139. ! !
  140.  
  141.  
  142.  
  143. !FileStream methodsFor: 'basic'!
  144.  
  145. close
  146.     self fileOp: 1
  147. !
  148.  
  149. next
  150.     | ch |
  151.     "Returns nil at eof"
  152.     buffer isNil
  153.     ifTrue: [ ^self fileOp: 2 ]
  154.     ifFalse: [ ch _ buffer.
  155.            buffer _ nil.
  156.            ^ch ]
  157. !
  158.  
  159. nextPut: aChar
  160.     self fileOp: 3 with: aChar
  161. !
  162.  
  163. peek
  164.     buffer
  165.     isNil ifTrue: [ buffer _ self next ].
  166.     ^buffer
  167. !
  168.  
  169. position: bytePosition
  170.     self fileOp: 4 with: bytePosition
  171.  
  172. position
  173.     ^self fileOp: 5
  174. !
  175.  
  176. contents
  177.     ^self next: self size
  178. !
  179.  
  180. size
  181.     "Return the current size of the file, in bytes"
  182.     ^self fileOp: 8
  183.  
  184. !!
  185.  
  186.  
  187.  
  188. !FileStream methodsFor: 'overriding inherited methods'!
  189.  
  190. reset
  191.     self position: 0
  192. !
  193.  
  194. setToEnd
  195.     self position: self size
  196. !
  197.  
  198. skip: anInteger
  199.     | pos |
  200.     pos _ ((self position + anInteger) max: 0) min: self size - 1.
  201.     self position: pos
  202. !
  203.  
  204. reverseContents
  205.     ^(ReadStream on: self contents) reverseContents
  206. !
  207.  
  208. isEmpty
  209.     ^self size == 0
  210. !
  211.  
  212. nextPutAll: aString        "only works for strings (species String)"
  213.     ^self fileOp: 9 with: aString
  214. !
  215.  
  216. next: anInteger
  217.     "return the next 'anInteger' characters from the stream, as a String."
  218.     ^self fileOp: 10 with: anInteger
  219. !!
  220.  
  221.  
  222.  
  223. !FileStream methodsFor: 'testing'!
  224.  
  225. atEnd
  226.     ^self fileOp: 6
  227.  
  228. !!
  229.  
  230. FileStream initialize!
  231.