Stream.st
changeset 3154 fe3af716a704
parent 3112 ccf7121ddae8
child 3169 cef3b9a6de2c
equal deleted inserted replaced
3153:d8eed4e5c60f 3154:fe3af716a704
   313      If signalAtEnd == true, raise a signal. If its false, return nil.
   313      If signalAtEnd == true, raise a signal. If its false, return nil.
   314      Otherwise raise the signal, but only if handled; otherwise return nil."
   314      Otherwise raise the signal, but only if handled; otherwise return nil."
   315 
   315 
   316     signalAtEnd == true ifTrue:[
   316     signalAtEnd == true ifTrue:[
   317 	"/ always raise ...
   317 	"/ always raise ...
   318         ^ EndOfStreamSignal raiseIn:thisContext sender
   318 	^ EndOfStreamSignal raiseIn:thisContext sender
   319     ].
   319     ].
   320     signalAtEnd == false ifTrue:[
   320     signalAtEnd == false ifTrue:[
   321 	"/ never raise ...
   321 	"/ never raise ...
   322         ^ nil
   322 	^ nil
   323     ].
   323     ].
   324 
   324 
   325     ^ EndOfStreamQuerySignal raiseRequestFrom:self
   325     ^ EndOfStreamQuerySignal raiseRequestFrom:self
   326 
   326 
   327     "Modified: / 11.11.1997 / 16:14:41 / cg"
   327     "Modified: / 11.11.1997 / 16:14:41 / cg"
   358      nil is returned."
   358      nil is returned."
   359 
   359 
   360     |data n|
   360     |data n|
   361 
   361 
   362     self binary ifTrue:[
   362     self binary ifTrue:[
   363         data := ByteArray new:count
   363 	data := ByteArray new:count
   364     ] ifFalse:[
   364     ] ifFalse:[
   365         data := String new:count
   365 	data := String new:count
   366     ].
   366     ].
   367     n := self nextBytes:count into:data startingAt:1.
   367     n := self nextBytes:count into:data startingAt:1.
   368     n ~~ count ifTrue:[
   368     n ~~ count ifTrue:[
   369         n == 0 ifTrue:[
   369 	n == 0 ifTrue:[
   370             ^ nil
   370 	    ^ nil
   371         ].
   371 	].
   372         data := data copyTo:n
   372 	data := data copyTo:n
   373     ].
   373     ].
   374 
   374 
   375     ^ data
   375     ^ data
   376 
   376 
   377     "Created: 21.6.1997 / 11:18:57 / cg"
   377     "Created: 21.6.1997 / 11:18:57 / cg"
   403      collection.
   403      collection.
   404      Returns the number of bytes that have been actually read.
   404      Returns the number of bytes that have been actually read.
   405      The receiver must support reading of binary bytes.
   405      The receiver must support reading of binary bytes.
   406 
   406 
   407      Notice: this method is privided here for protocol completeness
   407      Notice: this method is privided here for protocol completeness
   408              with externalStreams - it is normally not used with other
   408 	     with externalStreams - it is normally not used with other
   409              streams."
   409 	     streams."
   410 
   410 
   411     |n "{Class: SmallInteger }"
   411     |n "{Class: SmallInteger }"
   412      dstIndex|
   412      dstIndex|
   413 
   413 
   414     dstIndex := initialIndex.
   414     dstIndex := initialIndex.
   415     n := 0.
   415     n := 0.
   416     [self atEnd] whileFalse:[
   416     [self atEnd] whileFalse:[
   417         n == numBytes ifTrue:[
   417 	n == numBytes ifTrue:[
   418             ^ n
   418 	    ^ n
   419         ].
   419 	].
   420 
   420 
   421         aCollection byteAt:dstIndex put:(self nextByte).
   421 	aCollection byteAt:dstIndex put:(self nextByte).
   422         dstIndex := dstIndex + 1.
   422 	dstIndex := dstIndex + 1.
   423         n := n + 1.
   423 	n := n + 1.
   424     ].
   424     ].
   425     ^ n
   425     ^ n
   426 
   426 
   427     "
   427     "
   428      |s n buffer|
   428      |s n buffer|
   446     |nR oR n|
   446     |nR oR n|
   447 
   447 
   448     nR := numBytes.
   448     nR := numBytes.
   449     oR := initialIndex.
   449     oR := initialIndex.
   450     [nR > 0] whileTrue:[
   450     [nR > 0] whileTrue:[
   451         n := nR.
   451 	n := nR.
   452         n > blockSize ifTrue:[n := blockSize].
   452 	n > blockSize ifTrue:[n := blockSize].
   453         n := self nextBytes:n into:aCollection startingAt:oR.
   453 	n := self nextBytes:n into:aCollection startingAt:oR.
   454         n == 0 ifTrue:[
   454 	n == 0 ifTrue:[
   455             ^ numBytes - nR
   455 	    ^ numBytes - nR
   456         ].
   456 	].
   457         oR := oR + n.
   457 	oR := oR + n.
   458         nR := nR - n
   458 	nR := nR - n
   459     ].
   459     ].
   460     ^ numBytes
   460     ^ numBytes
   461 
   461 
   462     "Created: 24.4.1997 / 21:09:34 / cg"
   462     "Created: 24.4.1997 / 21:09:34 / cg"
   463     "Modified: 24.4.1997 / 21:19:50 / cg"
   463     "Modified: 24.4.1997 / 21:19:50 / cg"
   838 
   838 
   839     |idx|
   839     |idx|
   840 
   840 
   841     idx := start.
   841     idx := start.
   842     1 to:count do:[:i |
   842     1 to:count do:[:i |
   843         self nextPutByte:(anObject byteAt:idx).
   843 	self nextPutByte:(anObject byteAt:idx).
   844         idx := idx + 1
   844 	idx := idx + 1
   845     ].
   845     ].
   846     ^ count
   846     ^ count
   847 
   847 
   848     "Created: 22.4.1997 / 10:44:09 / cg"
   848     "Created: 22.4.1997 / 10:44:09 / cg"
   849 !
   849 !
   880 
   880 
   881     hi := (aNumber // 16r10000).
   881     hi := (aNumber // 16r10000).
   882     lo := (aNumber \\ 16r10000).
   882     lo := (aNumber \\ 16r10000).
   883 
   883 
   884     msbFlag ifTrue:[
   884     msbFlag ifTrue:[
   885         "high word first"
   885 	"high word first"
   886         w1 := hi.
   886 	w1 := hi.
   887         w2 := lo.
   887 	w2 := lo.
   888     ] ifFalse:[
   888     ] ifFalse:[
   889         "low word first"
   889 	"low word first"
   890         w1 := lo.
   890 	w1 := lo.
   891         w2 := hi.
   891 	w2 := hi.
   892     ].
   892     ].
   893     self nextPutShort:w1 MSB:false; nextPutShort:w2 MSB:false.
   893     self nextPutShort:w1 MSB:false; nextPutShort:w2 MSB:false.
   894 
   894 
   895     "Modified: / 1.11.1997 / 18:30:52 / cg"
   895     "Modified: / 1.11.1997 / 18:30:52 / cg"
   896 !
   896 !
  1336     "
  1336     "
  1337 
  1337 
  1338     "Modified: 17.5.1996 / 08:51:40 / cg"
  1338     "Modified: 17.5.1996 / 08:51:40 / cg"
  1339 !
  1339 !
  1340 
  1340 
       
  1341 throughAny:aCollection
       
  1342     "read & return a collection of all objects up-to and including 
       
  1343      an element contained in aCollection.
       
  1344      (i.e. read until any from aCollection is encountered.)
       
  1345      If no such character is encountered, all elements up to the end are read
       
  1346      and returned."
       
  1347 
       
  1348     |answerStream element last|
       
  1349 
       
  1350     last := aCollection last.
       
  1351     answerStream := WriteStream on:(self contentsSpecies new).
       
  1352     [self atEnd] whileFalse:[
       
  1353 	element := self next.
       
  1354 	answerStream nextPut:element.
       
  1355 	(aCollection includes:element) ifTrue:[
       
  1356 	    ^ answerStream contents
       
  1357 	].
       
  1358     ].
       
  1359     ^ answerStream contents
       
  1360 
       
  1361     "
       
  1362      |s|
       
  1363      s := ReadStream on:#(1 2 3 4 5 6 7 8).
       
  1364      Transcript showCR:(s throughAny:#(3 4 5)).  
       
  1365      Transcript showCR:s next
       
  1366 
       
  1367      |s|
       
  1368      s := ReadStream on:'hello world, this is some text'.
       
  1369      Transcript showCR:(s throughAny:'wt').  
       
  1370      Transcript showCR:(s throughAny:'wt').  
       
  1371      Transcript showCR:s upToEnd.
       
  1372     "
       
  1373 
       
  1374     "Modified: / 11.1.1998 / 15:28:04 / cg"
       
  1375 !
       
  1376 
  1341 throughAll:aCollection
  1377 throughAll:aCollection
  1342     "read & return a collection of all objects up-to and including 
  1378     "read & return a collection of all objects up-to and including 
  1343      a subcollection given by aCollection.
  1379      a subcollection given by aCollection.
  1344      (i.e. read until a ``substring'' is encountered.)
  1380      (i.e. read until a ``substring'' is encountered.)
  1345      The next read operation will return the element after aCollection.
  1381      The next read operation will return the element after aCollection.
  1450     |answerStream element last rslt|
  1486     |answerStream element last rslt|
  1451 
  1487 
  1452     last := aCollection last.
  1488     last := aCollection last.
  1453     answerStream := WriteStream on:(self contentsSpecies new).
  1489     answerStream := WriteStream on:(self contentsSpecies new).
  1454     [self atEnd] whileFalse:[
  1490     [self atEnd] whileFalse:[
  1455         element := self next.
  1491 	element := self next.
  1456         answerStream nextPut:element.
  1492 	answerStream nextPut:element.
  1457         element == last ifTrue:[
  1493 	element == last ifTrue:[
  1458             ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
  1494 	    ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
  1459                 self position:(self position - aCollection size).
  1495 		self position:(self position - aCollection size).
  1460                 ^ rslt copyWithoutLast:aCollection size
  1496 		^ rslt copyWithoutLast:aCollection size
  1461             ]
  1497 	    ]
  1462         ].
  1498 	].
  1463     ].
  1499     ].
  1464     ^ answerStream contents
  1500     ^ answerStream contents
  1465 
  1501 
  1466     "
  1502     "
  1467      |s|
  1503      |s|
  1493 
  1529 
  1494     |answerStream element|
  1530     |answerStream element|
  1495 
  1531 
  1496     answerStream := WriteStream on:(self contentsSpecies new).
  1532     answerStream := WriteStream on:(self contentsSpecies new).
  1497     [self atEnd] whileFalse:[
  1533     [self atEnd] whileFalse:[
  1498         element := self next.
  1534 	element := self next.
  1499         (aCollectionOfObjects includes:element) ifTrue: [
  1535 	(aCollectionOfObjects includes:element) ifTrue: [
  1500             ^ answerStream contents
  1536 	    ^ answerStream contents
  1501         ].
  1537 	].
  1502         answerStream nextPut:element.
  1538 	answerStream nextPut:element.
  1503     ].
  1539     ].
  1504     ^ answerStream contents
  1540     ^ answerStream contents
  1505 
  1541 
  1506     "
  1542     "
  1507      |s|
  1543      |s|
  1508      s := ReadStream on:'hello world'.
  1544      s := ReadStream on:'hello world'.
  1509      Transcript showCR:(s upToAll:(Array with:Character space)).
  1545      Transcript showCR:(s upToAny:(Array with:Character space)).
  1510      Transcript showCR:(s upToEnd)
  1546      Transcript showCR:(s upToEnd)
  1511 
  1547 
  1512      'Makefile' asFilename readStream upToAll:($A to:$Z)  
  1548      'Makefile' asFilename readStream upToAny:($A to:$Z)  
  1513 "
  1549     "
  1514 
  1550 
  1515     "Modified: 26.2.1997 / 12:24:09 / cg"
  1551     "Created: / 30.8.1997 / 03:02:05 / cg"
  1516     "Created: 30.8.1997 / 03:02:05 / cg"
  1552     "Modified: / 11.1.1998 / 15:19:18 / cg"
  1517 !
  1553 !
  1518 
  1554 
  1519 upToEnd
  1555 upToEnd
  1520     "return a collection of the elements up-to the end.
  1556     "return a collection of the elements up-to the end.
  1521      Return an empty collection, if the stream-end is already at the end."
  1557      Return an empty collection, if the stream-end is already at the end."
  1905 ! !
  1941 ! !
  1906 
  1942 
  1907 !Stream class methodsFor:'documentation'!
  1943 !Stream class methodsFor:'documentation'!
  1908 
  1944 
  1909 version
  1945 version
  1910     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.66 1997-11-15 14:59:17 cg Exp $'
  1946     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.67 1998-01-12 13:13:13 cg Exp $'
  1911 ! !
  1947 ! !
  1912 Stream initialize!
  1948 Stream initialize!