[Pharo-project] Creating a file with FS
Hi FS users I'm puzzled by FS api. I want to create a file in a directory so I did | wk | wk := FSFilesystem disk workingDirectory. (wk / 'CSS2') ensureDirectory worked Now I looked at ensureFile and I do not understand the code ensureFile "Create if necessary a file for the receiver." self writeStream close. Because I would like to be able to write in the file and now I do not see why closing it is good. Would be good to add comments to FSReference>>writeStreamDo: doBlock ifPresent: presentBlock "Create a file if not present and execute doBlock (with a stream) as argument, if the file is present execute the presentBlock." ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStreamDo: doBlock ] FSReference>>writeStreamIfPresent: presentBlock "Return a writestream on the receiver if it does not already exist or execute the presentBlock with no argument" ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStream ] I do not get why we do not get as argument to the presentBlock the file itself? I added method commentsâ¦. as usual. :( Camillo could you have a look at such comments and commit them? I published the package to the fs package. Would be good to cross check and that I add it in 1.4 Stef
On 2012-02-05, at 16:16, stephane ducasse wrote:
Hi FS users
I'm puzzled by FS api. I want to create a file in a directory so I did
| wk | wk := FSFilesystem disk workingDirectory. (wk / 'CSS2') ensureDirectory
worked
Now I looked at ensureFile and I do not understand the code
ensureFile "Create if necessary a file for the receiver." self writeStream close.
Because I would like to be able to write in the file and now I do not see why closing it is good.
ensureFile makes sense: - it creates an _empty_ file - it still returns the FSReference... what you want is the following I guess: (wk / 'foo') writeStreamDo: [ :s| s << 'bar' ]. since that will - create also create a new file. - ensure closing the stream
FSReference>>writeStreamDo: doBlock ifPresent: presentBlock "Create a file if not present and execute doBlock (with a stream) as argument, if the file is present execute the presentBlock." ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStreamDo: doBlock ]
FSReference>>writeStreamIfPresent: presentBlock "Return a writestream on the receiver if it does not already exist or execute the presentBlock with no argument" ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStream ]
I do not get why we do not get as argument to the presentBlock the file itself?
indeed, a [... cull: self] might make sense here :)
I added method commentsâ¦. as usual. :(
Camillo could you have a look at such comments and commit them? I published the package to the fs package. Would be good to cross check and that I add it in 1.4
will do
<FSReference-streams.st>
Stef
- it still returns the FSReference...
what you want is the following I guess:
(wk / 'foo') writeStreamDo: [ :s| s << 'bar' ].
since that will - create also create a new file. - ensure closing the stream
Yes I did the same.
FSReference>>writeStreamDo: doBlock ifPresent: presentBlock "Create a file if not present and execute doBlock (with a stream) as argument, if the file is present execute the presentBlock." ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStreamDo: doBlock ]
FSReference>>writeStreamIfPresent: presentBlock "Return a writestream on the receiver if it does not already exist or execute the presentBlock with no argument" ^ self isFile ifTrue: [ presentBlock value ] ifFalse: [ self writeStream ]
I do not get why we do not get as argument to the presentBlock the file itself?
indeed, a [... cull: self] might make sense here :)
participants (4)
-
Camillo Bruni -
stephane ducasse -
Stéphane Ducasse -
Sven Van Caekenberghe