Filename.st
changeset 20158 2e4c6347ef0b
parent 20156 3974eeb5115f
child 20205 03e626304d06
child 20252 17b9880f243e
equal deleted inserted replaced
20157:98d5b57a7576 20158:2e4c6347ef0b
  1686 
  1686 
  1687     "Modified: 8.9.1997 / 00:23:16 / cg"
  1687     "Modified: 8.9.1997 / 00:23:16 / cg"
  1688 !
  1688 !
  1689 
  1689 
  1690 readingFile:aPathName do:aBlock
  1690 readingFile:aPathName do:aBlock
  1691     "create a read-stream on a file, evaluate aBlock, passing that stream,
  1691     "Create a read stream on a file, evaluate aBlock, passing that stream,
  1692      and return the blocks value.
  1692      and return the block's value.
  1693      Ensures that the stream is closed."
  1693      Ensures that the stream is closed."
  1694 
  1694 
  1695     ^ aPathName asFilename readingFileDo:aBlock.
  1695     ^ aPathName asFilename readingFileDo:aBlock.
  1696 
  1696 
  1697     "
  1697     "
  1698      read the first line from some file:
  1698      read the first line from some file:
  1699 
  1699 
  1700      |rslt|
  1700      |rslt|
  1701 
  1701 
  1702      rslt :=
  1702      rslt :=
  1703 	Filename
  1703         Filename
  1704 	    readingFile:'/etc/passwd'
  1704             readingFile:'/etc/passwd'
  1705 	    do:[:s |
  1705             do:[:s |
  1706 		s nextLine
  1706                 s nextLine
  1707 	    ].
  1707             ].
  1708      Transcript showCR:rslt.
  1708      Transcript showCR:rslt.
  1709     "
  1709     "
  1710 
  1710 
  1711     "
  1711     "
  1712      find all used shells in /etc/passwd and count their usage:
  1712      find all used shells in /etc/passwd and count their usage:
  1713 
  1713 
  1714      |rslt|
  1714      |rslt|
  1715 
  1715 
  1716      rslt :=
  1716      rslt :=
  1717 	Filename
  1717         Filename
  1718 	    readingFile:'/etc/passwd'
  1718             readingFile:'/etc/passwd'
  1719 	    do:
  1719             do:
  1720 		[:s |
  1720                 [:s |
  1721 		    |shells|
  1721                     |shells|
  1722 
  1722 
  1723 		    shells := Bag new.
  1723                     shells := Bag new.
  1724 		    s linesDo:
  1724                     s linesDo:
  1725 			[:line |
  1725                         [:line |
  1726 			    |parts|
  1726                             |parts|
  1727 
  1727 
  1728 			    parts := line asCollectionOfSubstringsSeparatedBy:$:.
  1728                             parts := line asCollectionOfSubstringsSeparatedBy:$:.
  1729 			    shells add:(parts seventh).
  1729                             shells add:(parts seventh).
  1730 			].
  1730                         ].
  1731 		    shells contents
  1731                     shells contents
  1732 		].
  1732                 ].
  1733      Transcript showCR:rslt.
  1733      Transcript showCR:rslt.
  1734     "
  1734     "
  1735 ! !
  1735 ! !
  1736 
  1736 
  1737 !Filename methodsFor:'Compatibility-VW5.4'!
  1737 !Filename methodsFor:'Compatibility-VW5.4'!
  5999      Transcript showCR:rslt.
  5999      Transcript showCR:rslt.
  6000     "
  6000     "
  6001 !
  6001 !
  6002 
  6002 
  6003 readingLinesDo:aBlock
  6003 readingLinesDo:aBlock
  6004     "create a read-stream on the receiver file,
  6004     "Create a read stream on the receiver file and
  6005      evaluate aBlock for each line read from the stream.
  6005      evaluate aBlock for each line read from the stream.
  6006      If the file cannot be opened, an error is raised.
  6006      If the file cannot be opened, an error is raised.
  6007      Ensures that the stream is closed."
  6007      Ensures that the stream is closed."
  6008 
  6008 
  6009     self readingFileDo:[:stream |
  6009     self readingFileDo:[:stream |
  6010 	stream linesDo:aBlock
  6010         stream linesDo:aBlock
  6011     ].
  6011     ].
  6012 
  6012 
  6013     "
  6013     "
  6014     '/etc/passwd' asFilename
  6014     '/etc/passwd' asFilename
  6015 	readingLinesDo:[:eachLine |
  6015         readingLinesDo:[:eachLine |
  6016 	    Transcript showCR:eachLine.
  6016             Transcript showCR:eachLine.
  6017 	].
  6017         ].
  6018     "
  6018     "
  6019 
  6019 
  6020     "
  6020     "
  6021     '/etc/xxxxx' asFilename
  6021     '/etc/xxxxx' asFilename
  6022 	readingLinesDo:[:eachLine |
  6022         readingLinesDo:[:eachLine |
  6023 	    Transcript showCR:eachLine.
  6023             Transcript showCR:eachLine.
  6024 	].
  6024         ].
  6025     "
  6025     "
  6026 ! !
  6026 ! !
  6027 
  6027 
  6028 !Filename methodsFor:'special accessing'!
  6028 !Filename methodsFor:'special accessing'!
  6029 
  6029