mercurial/HGChangesetFile.st
branchcvs_MAIN
changeset 806 2b5c72c7b9d9
parent 693 70e47f715bc1
equal deleted inserted replaced
803:73291d6b935a 806:2b5c72c7b9d9
   397      and return the block's value. 
   397      and return the block's value. 
   398      If the file cannot be opened, an exception is raised or
   398      If the file cannot be opened, an exception is raised or
   399      (old behavior, will vanish:)the block is evaluated with a nil argument.
   399      (old behavior, will vanish:)the block is evaluated with a nil argument.
   400      Ensures that the stream is closed."
   400      Ensures that the stream is closed."
   401 
   401 
   402     |stream result|
   402     |stream|
   403 
   403 
   404     stream := self readStream.
   404     stream := self readStream.
   405     [
   405     ^ [
   406         result := aBlock value:stream
   406         aBlock value:stream
   407     ] ensure:[
   407     ] ensure:[
   408         stream notNil ifTrue:[stream close]
   408         stream notNil ifTrue:[stream close]
   409     ].
   409     ].
   410     ^ result
   410 
   411 
   411     "Modified (comment): / 12-01-2018 / 18:23:20 / stefan"
   412     "
       
   413      read the first line from some file:
       
   414 
       
   415      |rslt|
       
   416 
       
   417      rslt := 
       
   418         '/etc/passwd' asFilename 
       
   419             readingFileDo:[:s |
       
   420                 s nextLine
       
   421             ]. 
       
   422      Transcript showCR:rslt.
       
   423     "
       
   424 
       
   425     "
       
   426      find all used shells in /etc/passwd and count their usage:
       
   427 
       
   428      |rslt|
       
   429 
       
   430      rslt :=
       
   431         '/etc/passwd' asFilename 
       
   432             readingFileDo:
       
   433                 [:s |
       
   434                     |shells|
       
   435 
       
   436                     shells := Bag new.
       
   437                     s linesDo:
       
   438                         [:line |
       
   439                             |parts|
       
   440 
       
   441                             parts := line asCollectionOfSubstringsSeparatedBy:$:.
       
   442                             shells add:(parts seventh).
       
   443                         ].
       
   444                     shells contents
       
   445                 ].           
       
   446      Transcript showCR:rslt.
       
   447     "
       
   448 !
   412 !
   449 
   413 
   450 readingLinesDo:aBlock
   414 readingLinesDo:aBlock
   451     "create a read-stream on the receiver file,
   415     "create a read-stream on the receiver file,
   452      evaluate aBlock for each line read from the stream.
   416      evaluate aBlock for each line read from the stream.