home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 11 / CDACTUAL11.iso / cdactual / demobin / share / os2 / MST / KERNEL / POSSTRM.ST < prev    next >
Encoding:
Text File  |  1995-06-22  |  5.4 KB  |  201 lines

  1. "======================================================================
  2. |
  3. |   PositionableStream Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 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. |
  19. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  20. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  21. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  22. | details.
  23. |
  24. | You should have received a copy of the GNU General Public License along with
  25. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  26. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27. |
  28.  ======================================================================"
  29.  
  30.  
  31. "
  32. |     Change Log
  33. | ============================================================================
  34. | Author       Date       Change
  35. | sbyrne     19 Sep 89    Converted to use real method categories
  36. |
  37. | sbyrne     25 Apr 89    created.
  38. |
  39. "
  40.  
  41. Stream subclass: #PositionableStream
  42.        instanceVariableNames: 'collection ptr endPtr access'
  43.        classVariableNames: ''
  44.        poolDictionaries: ''
  45.        category: nil.
  46.  
  47. PositionableStream comment:
  48. 'My instances represent streams where explicit positioning is permitted.
  49. Thus, my streams act in a manner to normal disk files: you can read
  50. or write sequentially, but also position the file to a particular place
  51. whenever you choose.  Generally, you''ll want to use ReadStream, WriteStream
  52. or ReadWriteStream instead of me to create and use streams.' !
  53.  
  54.  
  55. !PositionableStream class methodsFor: 'instance creation'!
  56.  
  57. on: aCollection
  58.     self subclassResponsiblity
  59. !
  60.  
  61. on: aCollection from: firstIndex to: lastIndex
  62.     ^self on: (aCollection copyFrom: firstIndex to: lastIndex)
  63.  
  64. !!
  65.  
  66.  
  67.  
  68. !PositionableStream methodsFor: 'accessing-reading'!
  69.  
  70. next
  71.     | element |
  72.     (access bitAnd: 1) = 0
  73.         ifTrue: [ ^self shouldNotImplement ].
  74.     self atEnd ifTrue: [ ^self error: 'end of stream reached' ].
  75.     element _ collection at: ptr.
  76.     ptr _ ptr + 1.
  77.     ^element
  78. !
  79.  
  80. next: anInteger
  81.     "Returns a collection of the same type that the stream accesses, that has
  82.     the next anInteger elements from the stream."
  83.     | newStream |
  84.     newStream _ WriteStream on: (collection species new: 0).
  85.     anInteger timesRepeat: [ newStream nextPut: (self next) ].
  86.     ^newStream contents
  87. !
  88.  
  89. peek
  90.     "Returns the next element of the stream without moving the pointer.
  91.     Returns nil when at end of stream."
  92.     | peekValue |
  93.     self atEnd ifTrue: [ ^nil ].
  94.     peekValue _ self next.
  95.     self skip: -1.
  96.     ^peekValue
  97. !
  98.  
  99. peekFor: anObject
  100.     "Returns true and gobbles the next element from the stream of it is
  101.     equal to anObject, returns false and doesn't gobble the next element
  102.     if the next element is not equal to anObject."
  103.     (self peek) = anObject
  104.         ifTrue: [ self next.
  105.                   ^true ]
  106.         ifFalse: [ ^false ]
  107. !
  108.  
  109. upTo: anObject
  110.     "Returns a collection of the same type that the stream accesses, up to
  111.     but not including the object anObject.  Returns the entire rest of the
  112.     stream's contents if anObject is not present.  ### Should this use = or
  113.     ==."
  114.     | newStream |
  115.     newStream _ WriteStream on: (collection species new: 0).
  116.     [ self atEnd or: [ self peek == anObject ] ] whileFalse:
  117.         [ newStream nextPut: (self next) ].
  118.     ^newStream contents
  119. !
  120.  
  121. contents
  122.     "Returns a collection of the same type that the stream accesses, up to
  123.     and including the final element."
  124.     ^collection copyFrom: 1 to: endPtr
  125. !
  126.  
  127. reverseContents
  128.     "Returns a collection of the same type that the stream accesses, up to
  129.     and including the final element, but in reverse order."
  130.     | newCollection |
  131.     newCollection _ collection species new: endPtr.
  132.     1 to: endPtr do:
  133.         [ :i | newCollection at: i put: (collection at: endPtr - i + 1) ].
  134.     ^newCollection
  135. !!
  136.  
  137.  
  138.  
  139. !PositionableStream methodsFor: 'testing'!
  140.  
  141. atEnd
  142.     ^ptr > endPtr
  143. !
  144.  
  145. isEmpty
  146.     ^endPtr = 0
  147. !!
  148.  
  149.  
  150.  
  151. !PositionableStream methodsFor: 'enumerating'!
  152.  
  153. !
  154.  
  155.  
  156.  
  157. !PositionableStream methodsFor: 'positioning'!
  158.  
  159. position
  160.     ^ptr
  161. !
  162.  
  163. position: anInteger
  164.     (anInteger between: 1 and: endPtr)
  165.         ifTrue: [ ptr _ anInteger ]
  166.         ifFalse: [ ^self error: 'position out of range' ]
  167. !
  168.  
  169. reset
  170.     ptr _ 1
  171. !
  172.  
  173. setToEnd
  174.     ptr _ endPtr + 1
  175. !
  176.  
  177. skip: anInteger
  178.     ptr _ (ptr + anInteger max: 1) min: endPtr
  179. !
  180.  
  181. skipTo: anObject
  182.     "Moves the current position to after the next occurrence of anObject
  183.     and returns true if anObject was found.  If anObject doesn't exist, the
  184.     position is unchanged, and false is returned."
  185.     | curPos |
  186.     curPos _ self position.
  187.     [ self atEnd ] whileFalse:
  188.         [ (self nextMatchFor: anObject)
  189.             ifTrue: [ ^true ] ].
  190.     self position: curPos.
  191.     ^false
  192.  
  193. !!
  194.  
  195.  
  196. !PositionableStream methodsFor: 'private'!
  197.  
  198. access: anInteger
  199.     access _ anInteger
  200. !!
  201.