Stream.st
changeset 2894 344aec8ba014
parent 2714 cee1fce8c9de
child 3084 bf7811414895
equal deleted inserted replaced
2893:8ba406da6b22 2894:344aec8ba014
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
       
    12 
       
    13 'From Smalltalk/X, Version:3.1.9 on 30-aug-1997 at 3:02:55 pm'                  !
    12 
    14 
    13 Object subclass:#Stream
    15 Object subclass:#Stream
    14 	instanceVariableNames:'signalAtEnd'
    16 	instanceVariableNames:'signalAtEnd'
    15 	classVariableNames:'StreamErrorSignal PositionErrorSignal ReadErrorSignal
    17 	classVariableNames:'StreamErrorSignal PositionErrorSignal ReadErrorSignal
    16 		WriteErrorSignal EndOfStreamSignal'
    18 		WriteErrorSignal EndOfStreamSignal'
  1479     "
  1481     "
  1480 
  1482 
  1481     "Modified: 17.5.1996 / 08:51:56 / cg"
  1483     "Modified: 17.5.1996 / 08:51:56 / cg"
  1482 !
  1484 !
  1483 
  1485 
  1484 upToAll:aCollectionOfObjects
  1486 upToAll:aCollection
  1485     "read a collection of all objects up-to a element which is contained in
  1487     "read a collection of all objects up-to a element which is contained in
  1486      aCollectionOfObjects and return these elements, but excluding the matching one. 
  1488      aCollectionOfObjects and return these elements, but excluding the matching one. 
  1487      The next read operation will return the element after anObject.
  1489      The next read operation will return the element after anObject.
  1488      If no such element is encountered, all elements up to the end are read
  1490      If no such element is encountered, all elements up to the end are read
  1489      and returned.
  1491      and returned.
  1490      Compare this with #throughAll: which also reads up to some object
  1492      Compare this with #throughAll: which also reads up to some object
  1491      and also positions behind it, but DOES include it in the returned
  1493      and also positions behind it, but DOES include it in the returned
  1492      value."
  1494      value."
  1493 
  1495 
       
  1496     <resource: #tobevalidated>
       
  1497 
       
  1498     |answerStream element last rslt|
       
  1499 
       
  1500     last := aCollection last.
       
  1501     answerStream := WriteStream on:(self contentsSpecies new).
       
  1502     [self atEnd] whileFalse:[
       
  1503         element := self next.
       
  1504         answerStream nextPut:element.
       
  1505         element == last ifTrue:[
       
  1506             ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
       
  1507                 self position:(self position - aCollection size).
       
  1508                 ^ rslt copyWithoutLast:aCollection size
       
  1509             ]
       
  1510         ].
       
  1511     ].
       
  1512     ^ answerStream contents
       
  1513 
       
  1514     "
       
  1515      |s|
       
  1516      s := ReadStream on:'hello world world'.
       
  1517      Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
       
  1518      Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
       
  1519      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
       
  1520     "
       
  1521     "
       
  1522      |s|
       
  1523      s := ReadStream on:'hello world'.
       
  1524      Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. 
       
  1525      Transcript showCR:s atEnd.
       
  1526      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
       
  1527     "
       
  1528 
       
  1529     "Modified: 30.8.1997 / 03:02:11 / cg"
       
  1530 !
       
  1531 
       
  1532 upToAny:aCollectionOfObjects
       
  1533     "read a collection of all objects up-to a element which is contained in
       
  1534      aCollectionOfObjects and return these elements, but excluding the matching one. 
       
  1535      The next read operation will return the element after anObject.
       
  1536      If no such element is encountered, all elements up to the end are read
       
  1537      and returned.
       
  1538      Compare this with #throughAll: which also reads up to some object
       
  1539      and also positions behind it, but DOES include it in the returned
       
  1540      value."
       
  1541 
  1494     |answerStream element|
  1542     |answerStream element|
  1495 
  1543 
  1496     answerStream := WriteStream on:(self contentsSpecies new).
  1544     answerStream := WriteStream on:(self contentsSpecies new).
  1497     [self atEnd] whileFalse:[
  1545     [self atEnd] whileFalse:[
  1498         element := self next.
  1546         element := self next.
  1511 
  1559 
  1512      'Makefile' asFilename readStream upToAll:($A to:$Z)  
  1560      'Makefile' asFilename readStream upToAll:($A to:$Z)  
  1513 "
  1561 "
  1514 
  1562 
  1515     "Modified: 26.2.1997 / 12:24:09 / cg"
  1563     "Modified: 26.2.1997 / 12:24:09 / cg"
       
  1564     "Created: 30.8.1997 / 03:02:05 / cg"
  1516 !
  1565 !
  1517 
  1566 
  1518 upToEnd
  1567 upToEnd
  1519     "return a collection of the elements up-to the end.
  1568     "return a collection of the elements up-to the end.
  1520      Return an empty collection, if the stream-end is already at the end."
  1569      Return an empty collection, if the stream-end is already at the end."
  1836 ! !
  1885 ! !
  1837 
  1886 
  1838 !Stream class methodsFor:'documentation'!
  1887 !Stream class methodsFor:'documentation'!
  1839 
  1888 
  1840 version
  1889 version
  1841     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.60 1997-06-23 13:45:55 cg Exp $'
  1890     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.61 1997-09-02 20:54:02 cg Exp $'
  1842 ! !
  1891 ! !
  1843 Stream initialize!
  1892 Stream initialize!