--- a/FileURI.st Wed Jun 18 21:04:56 2003 +0200
+++ b/FileURI.st Tue Jun 24 16:10:53 2003 +0200
@@ -67,6 +67,13 @@
"
! !
+!FileURI methodsFor:'queries'!
+
+pathExists
+
+ ^ self asFilename exists
+! !
+
!FileURI methodsFor:'stream access'!
readStream
@@ -83,13 +90,16 @@
and a dictionary containing attributes as second argument.
The stream is closed after aBlock has been evaluated."
- |stream|
+ |attributes file stream|
- ^ [
- |stream attributes|
+ attributes := self class attributes.
+ file := self asFilename.
+ attributes at:#fileSize put:(file fileSize).
+ attributes at:#baseName put:file baseName.
+ attributes at:#uriInfo put:self printString.
- stream := self asFilename readStream.
- attributes := Dictionary new at:#MIME put:'text/plain'.
+ ^ [
+ stream := file readStream.
aBlock value:stream value:attributes
] ensure:[
stream notNil ifTrue:[stream close]
@@ -104,13 +114,81 @@
"
!
-writeStream
+readStreamsDo:aBlock
+ "evaluate the block with a Collection of streams as first argument
+ and a dictionary containing attributes as second argument,
+ - a collection with a stream on a single file,
+ - or a collection with streams on a directorie's files, but not recursive"
+
+ |attributes fn files|
+
+ fn := self asFilename.
+ files := OrderedCollection new.
+ attributes := self class attributes.
+ fn isDirectory ifTrue:[
+ attributes at:#requestDirectory put:true.
+ (DirectoryContents directoryNamed:fn pathName) filesDo:[:aFile|
+ files add:aFile
+ ].
+ ] ifFalse:[
+ attributes at:#requestDirectory put:false.
+ files add:fn.
+ ].
- ^ self asFilename writeStream
+ files do:[:aFile| |baseName stream|
+ [
+ baseName := aFile baseName.
+ attributes at:#fileSize put:(aFile fileSize).
+ attributes at:#baseName put:baseName.
+ (self pathSegements includes:baseName) ifTrue:[
+ attributes at:#uriInfo put: self printString.
+ ] ifFalse:[
+ attributes at:#uriInfo put: ((self copy) addComponent:baseName) printString.
+ ].
+ stream := aFile readStream.
+ aBlock value:stream value:attributes
+ ] ensure:[
+ stream notNil ifTrue:[stream close]
+ ].
+ ].
+
+ "
+ (URI fromString:'file:/home/tm/tmp')
+ readStreamsDo:[:stream :attributes |
+ Transcript showCR:(attributes at:#baseName).
+ Transcript showCR:(attributes at:#fileSize).
+ Transcript showCR:(attributes at:#requestDirectory).
+ Transcript showCR:(attributes at:#uriInfo).
+ ].
+ "
+!
+
+writeStreamDo:aBlock
+ "evaluate a block with the write stream as first argument
+ and a dictionary containing attributes as second argument.
+ The stream is closed after aBlock has been evaluated.
+ Attributes may be the mime type (key #MIME)"
+
+ |stream|
+
+ ^ [
+ stream := self asFilename writeStream.
+ aBlock value:stream value:self class attributes
+ ] ensure:[
+ stream notNil ifTrue:[stream close]
+ ].
+
+ "
+ (URI fromString:'file:/home/tm/tmp')
+ readStreamsDo:[:stream :attributes|
+ Transcript showCR:(attributes at:#MIME).
+ Transcript showCR:(stream isWritable).
+ ].
+ "
! !
!FileURI class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.1 2002-01-17 14:24:59 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.2 2003-06-24 14:10:53 tm Exp $'
! !