home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winlisp.zip / OOPL.LZH / STREAM.WL < prev    next >
Text File  |  1989-03-12  |  5KB  |  110 lines

  1. ;===============================================================================
  2. ;
  3. ;                               S T R E A M
  4. ;
  5. ; Provide a sequential view of any ordered collection of objects (text files
  6. ; may be considered as an ordered collection of characters).
  7. ;===============================================================================
  8.  
  9. [{Class} new 'name 'MetaStream
  10.     'superClass        {Class}
  11.         'methods        '(
  12. on ((aCollection)
  13.     [super new 'collection         aCollection 
  14.                'position          -1
  15.                'limitPosition          (1- [aCollection size])])
  16. )]
  17.  
  18. [{MetaStream} new 'name 'Stream
  19.     'superClass        {Object}
  20.     'instanceVariables    '(collection position limitPosition)
  21.         'methods        '(
  22. contents (()    
  23.           ;; -----------------------------------------------------------
  24.           ;; Answer the collection over which the receiver is streaming.
  25.           ;; -----------------------------------------------------------
  26.         #Icollection)
  27.  
  28. position ( arg
  29.            ;; ----------------------------------------------------------
  30.            ;; If no argument is provided, answer the current receiver
  31.            ;; position else set its position to <arg> (between -1 and
  32.            ;; #IlimitPosition).
  33.            ;; ----------------------------------------------------------
  34.            (if arg
  35.                (let ( (newPosition   (car arg)) 
  36.                       (limitPosition #IlimitPosition) )
  37.                     (cond ( (> newPosition limitPosition)
  38.                             (setf #Iposition limitPosition) )
  39.                           ( (< newPosition -1)
  40.                             (setf #Iposition -1) )
  41.                           ( t
  42.                             (setf #Iposition newPosition) )))
  43.                ;;ELSE
  44.                #Iposition))
  45.  
  46. atEnd (()    
  47.        ;; -------------------------------------------------------------
  48.        ;; Answer something true if the receiver is positioned at its
  49.        ;; end else answer ().
  50.        ;; -------------------------------------------------------------
  51.     (equal #Iposition #IlimitPosition))
  52.  
  53. reset (()    
  54.        ;; -------------------------------------------------------------
  55.        ;; Position the receiver to its beginning.
  56.        ;; -------------------------------------------------------------
  57.     (setf #Iposition -1))
  58.  
  59. peek (()
  60.       ;; -------------------------------------------------------------
  61.       ;; Answer the next object in the receiver without advancing the
  62.       ;; stream position. If the receiver is positioned at its end,
  63.       ;; answer STREAMATEND.
  64.       ;; -------------------------------------------------------------
  65.     (let  ( (nextPosition (1+ #Iposition)) )
  66.               (if (> nextPosition #IlimitPosition)
  67.                   'STREAMATEND
  68.                   ;;ELSE
  69.                   [#Icollection at nextPosition])))
  70.  
  71. skipTo ((anObject)
  72.         ;; -----------------------------------------------------------
  73.         ;; Advance the receiver position to the next occurrence of
  74.         ;; <anObject>, or if none to the end of stream. Answer t if
  75.         ;; <anObject> occurred, else answer ().
  76.         ;; -----------------------------------------------------------
  77.     (let ( (collection    #Icollection)
  78.                (nextPosition  #Iposition)
  79.                (limitPosition #IlimitPosition) )
  80.              (while (and (<= (incr nextPosition) limitPosition)
  81.                          (nequal [collection at nextPosition] anObject)))
  82.              (cond ( (> nextPosition limitPosition)
  83.                      (setf #Iposition limitPosition)
  84.                      () )
  85.                    ( t
  86.                      (setf #Iposition nextPosition)
  87.                      t ))))
  88.  
  89. next ((aNumber)
  90.       ;; -------------------------------------------------------------
  91.       ;; Answer the next <aNumber> (or up to the end of stream) items
  92.       ;; from the receiver, returned in a collection of the same 
  93.       ;; species as the collection being streamed over.
  94.       ;; -------------------------------------------------------------
  95.     (let ( (collection    #Icollection)
  96.                (nextPosition  #Iposition)
  97.                (limitPosition #IlimitPosition)
  98.                (items) )
  99.              (while (and (> aNumber 0) (<= (incr nextPosition) limitPosition))
  100.                     (newl items [collection at nextPosition])
  101.                     (decr aNumber))
  102.              (ifn items
  103.                   'STREAMATEND
  104.                   ;;ELSE
  105.                   (setf #Iposition nextPosition)
  106.                   [[collection class] new (nreverse items)])))
  107. )]
  108.  
  109.  
  110.