FileDirectory.st
changeset 172 52750f9c44de
parent 159 514c749165c3
child 202 40ca7cc6fb9c
--- a/FileDirectory.st	Fri Oct 28 02:22:16 1994 +0100
+++ b/FileDirectory.st	Fri Oct 28 02:22:49 1994 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -19,9 +19,9 @@
 
 FileDirectory comment:'
 COPYRIGHT (c) 1989 by Claus Gittinger
-             All Rights Reserved
+	     All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.12 1994-10-10 00:26:00 claus Exp $
+$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.13 1994-10-28 01:22:49 claus Exp $
 '!
 
 !FileDirectory class methodsFor:'documentation'!
@@ -29,7 +29,7 @@
 copyright
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.12 1994-10-10 00:26:00 claus Exp $
+$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.13 1994-10-28 01:22:49 claus Exp $
 "
 !
 
@@ -51,6 +51,11 @@
     FileDirectories represent directories in the underlying host system.
     They provide various methods to create/delete and query for files and/or
     directories.
+    Notice:
+	This class is not available in other ST-systems, while for example,
+	ST-80 provides a Filename class.
+	Therefore, Filename will take over that functionality in the near future.
+	Use instances of Filename if possible.
 "
 ! !
 
@@ -63,7 +68,7 @@
 
 update:something
     something == #restarted ifTrue:[
-        PathOfCurrentDirectory := nil
+	PathOfCurrentDirectory := nil
     ]
 ! !
 
@@ -114,24 +119,24 @@
 
     sep := OperatingSystem fileSeparator.
     ((name at:1) == sep) ifTrue:[
-        ^ self directoryNamed:name
+	^ self directoryNamed:name
     ].
     (aFileDirectory isKindOf:FileDirectory) ifTrue:[
-        baseName := aFileDirectory pathName
+	baseName := aFileDirectory pathName
     ] ifFalse:[
-        baseName := aFileDirectory
+	baseName := aFileDirectory
     ].
 "
     (name = '..') ifTrue:[
-        ^ (self basicNew) pathName:(OperatingSystem directoryNameOf:baseName)
+	^ (self basicNew) pathName:(OperatingSystem directoryNameOf:baseName)
     ].
 "
     (name = '.') ifTrue:[^ aFileDirectory].
 
     (baseName = sep asString) ifFalse:[
-        (baseName endsWith:sep) ifFalse:[
-            baseName := baseName copyWith:sep
-        ]
+	(baseName endsWith:sep) ifFalse:[
+	    baseName := baseName copyWith:sep
+	]
     ].
     ^ (self basicNew) pathName:(baseName , name)
 ! !
@@ -166,11 +171,11 @@
 
     pathName := dirName.
     (dirName startsWith:OperatingSystem fileSeparator) ifFalse:[
-        lazy := true
+	lazy := true
     ] ifTrue:[
-        (dirName includes:$.) ifTrue:[
-            lazy := true
-        ]
+	(dirName includes:$.) ifTrue:[
+	    lazy := true
+	]
     ].
     ^ self
 "
@@ -185,10 +190,10 @@
 
     coll := OrderedCollection new.
     self do:[:name |
-        coll add:name
+	coll add:name
     ].
     (coll size ~~ 0) ifTrue:[
-        coll sort
+	coll sort
     ].
     ^ coll
 !
@@ -200,10 +205,10 @@
 
     coll := OrderedCollection new.
     self directoriesDo:[:name |
-        coll add:name
+	coll add:name
     ].
     (coll size ~~ 0) ifTrue:[
-        coll sort
+	coll sort
     ].
     ^ coll
 !
@@ -215,11 +220,25 @@
 
     coll := OrderedCollection new.
     self filesDo:[:name |
-        coll add:name
+	coll add:name
     ].
     ^ coll sort
 ! !
 
+!FileDirectory methodsFor:'converting'!
+
+asFilename
+    "return myself as a filename"
+
+    ^ self pathName asFilename
+!
+
+asFilename:someFile
+    "return a filename for a file named someFile in myself"
+
+    ^ self pathName asFilename construct:someFile
+! !
+
 !FileDirectory methodsFor:'private'!
 
 getFullPathName
@@ -228,18 +247,18 @@
     |aStream command shortPathName fullPathName|
 
     (pathName = OperatingSystem fileSeparator asString) ifTrue:[
-        lazy := false.
-        ^ self
+	lazy := false.
+	^ self
     ].
 
     "since currentDirectory is used very often, cache its path here"
 
     (pathName = '.') ifTrue:[
-        PathOfCurrentDirectory notNil ifTrue:[
-            pathName := PathOfCurrentDirectory.
-            lazy := false.
-            ^ self
-        ]
+	PathOfCurrentDirectory notNil ifTrue:[
+	    pathName := PathOfCurrentDirectory.
+	    lazy := false.
+	    ^ self
+	]
     ].
 
     shortPathName := pathName.
@@ -253,40 +272,40 @@
     char nameBuffer[MAXPATHLEN + 1];
 
     if (realpath(_stringVal(_INST(pathName)), nameBuffer)) {
-        fullPathName = _MKSTRING(nameBuffer COMMA_CON);
+	fullPathName = _MKSTRING(nameBuffer COMMA_CON);
     }
 #endif
 %}
 .
     fullPathName notNil ifTrue:[
-        pathName := fullPathName.
-        lazy := false
+	pathName := fullPathName.
+	lazy := false
     ] ifFalse:[
-        "since there might be symbolic links and other stuff involved,
-         better trust pwd than removing '..' by ourself
-         - although this is very slow"
+	"since there might be symbolic links and other stuff involved,
+	 better trust pwd than removing '..' by ourself
+	 - although this is very slow"
 
-        command := 'cd ' , pathName , '; pwd'.
-        aStream := PipeStream readingFrom:command.
-        aStream isNil ifFalse:[
-            (aStream atEnd) ifFalse:[
-                fullPathName := aStream nextLine
-            ].
-            aStream close.
-            fullPathName notNil ifTrue:[
-                pathName := fullPathName.
-                lazy := false
-            ]
-        ] ifTrue:[
-            self error:('PipeStream for <' , command , '> failed').
-            "by clearing lazy, we avoid triggering the error again"
-            lazy := false
-        ]
+	command := 'cd ' , pathName , '; pwd'.
+	aStream := PipeStream readingFrom:command.
+	aStream isNil ifFalse:[
+	    (aStream atEnd) ifFalse:[
+		fullPathName := aStream nextLine
+	    ].
+	    aStream close.
+	    fullPathName notNil ifTrue:[
+		pathName := fullPathName.
+		lazy := false
+	    ]
+	] ifTrue:[
+	    self error:('PipeStream for <' , command , '> failed').
+	    "by clearing lazy, we avoid triggering the error again"
+	    lazy := false
+	]
     ].
 
     "if it was the current dir, keep name for next query"
     (shortPathName = '.') ifTrue:[
-        PathOfCurrentDirectory := fullPathName
+	PathOfCurrentDirectory := fullPathName
     ]
 ! !
 
@@ -299,14 +318,14 @@
     |realName|
 
     (newName = '.') ifFalse:[
-        (newName = '..') ifFalse:[
-            ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[
-                realName := newName copyFrom:2
-            ] ifFalse:[
-                realName := newName
-            ].
+	(newName = '..') ifFalse:[
+	    ((newName at:1) == OperatingSystem fileSeparator) ifTrue:[
+		realName := newName copyFrom:2
+	    ] ifFalse:[
+		realName := newName
+	    ].
 	    ^ OperatingSystem createDirectory:(self class fullPathNameOf:realName in:pathName)
-        ]
+	]
     ].
     ^ false
 !
@@ -336,7 +355,7 @@
 
     path := self class fullPathNameOf:aFileOrDirectoryName in:pathName.
     (OperatingSystem isDirectory:path) ifTrue:[
-        ^ OperatingSystem removeDirectory:path
+	^ OperatingSystem removeDirectory:path
     ].
     ^ OperatingSystem removeFile:path
 !
@@ -365,7 +384,7 @@
 
 fullPathNameOf:name in:path
     (name startsWith:OperatingSystem fileSeparator) ifTrue:[
-        ^ name
+	^ name
     ].
     ^ path , OperatingSystem fileSeparator asString , name
 ! !
@@ -394,7 +413,7 @@
      redefined since '.' and '..' do not count as entries here."
 
     self do:[:fName |
-        ((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false].
+	((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false].
     ].
     ^ true
 !
@@ -428,7 +447,7 @@
     "set the access-mode bits (rwxrwxrwx) of a file in myself"
 
     ^ OperatingSystem changeAccessModeOf:(self class fullPathNameOf:name in:pathName)
-                                      to:modeBits
+				      to:modeBits
 !
 
 typeOf:name
@@ -469,12 +488,19 @@
 
 !FileDirectory methodsFor:'printing & storing'!
 
-printString
+printOn:aStream
+    "append a printed representation of the receiver to aStream."
+
     lazy ifTrue:[self getFullPathName].
-    ^ '(a FileDirectory pathName:' , pathName, ')'
+    aStream nextPutAll:'(a FileDirectory pathName:';
+	    nextPutAll:pathName;
+	    nextPutAll:')'
 !
 
 storeOn:aStream
+    "append a printed representation of the receiver to aStream,
+     which allows reconstructing it via readFrom:"
+
     lazy ifTrue:[self getFullPathName].
     aStream nextPutAll:'(FileDirectory directoryNamed:'.
     aStream nextPutAll:pathName.
@@ -498,12 +524,12 @@
     aStream := DirectoryStream directoryNamed:pathName.
     aStream isNil ifTrue:[^ nil].
     [aStream atEnd] whileFalse:[
-        name := aStream nextLine.
-        name notNil ifTrue:[
-            (testBlock value:name) ifTrue:[
-                aBlock value:name
-            ]
-        ]
+	name := aStream nextLine.
+	name notNil ifTrue:[
+	    (testBlock value:name) ifTrue:[
+		aBlock value:name
+	    ]
+	]
     ].
     aStream close
 !
@@ -531,11 +557,11 @@
     "evaluate the argument, aBlock for every subdirectory name in the directory"
 
     self where:[:name | (self isDirectory:name) ifTrue:[
-                            ((name ~= '.') and:[name ~= '..'])
-                        ] ifFalse:[
-                            false
-                        ]
-               ] do:aBlock
+			    ((name ~= '.') and:[name ~= '..'])
+			] ifFalse:[
+			    false
+			]
+	       ] do:aBlock
 !
 
 allFilesDo:aBlock
@@ -549,14 +575,14 @@
     aStream := PipeStream readingFrom:command.
     aStream isNil ifTrue:[^ nil].
     [aStream atEnd] whileFalse:[
-        line := aStream nextLine.
-        line notNil ifTrue:[
-            (line = '.') ifFalse:[
-                "cut off initial ./"
-                line := line copyFrom:3
-            ].
-            aBlock value:line
-        ]
+	line := aStream nextLine.
+	line notNil ifTrue:[
+	    (line = '.') ifFalse:[
+		"cut off initial ./"
+		line := line copyFrom:3
+	    ].
+	    aBlock value:line
+	]
     ].
     aStream close
 !
@@ -572,14 +598,14 @@
     aStream := PipeStream readingFrom:command.
     aStream isNil ifTrue:[^ nil].
     [aStream atEnd] whileFalse:[
-        line := aStream nextLine.
-        line notNil ifTrue:[
-            (line = '.') ifFalse:[
-                "cut off initial ./"
-                line := line copyFrom:3
-            ].
-            aBlock value:line
-        ]
+	line := aStream nextLine.
+	line notNil ifTrue:[
+	    (line = '.') ifFalse:[
+		"cut off initial ./"
+		line := line copyFrom:3
+	    ].
+	    aBlock value:line
+	]
     ].
     aStream close
 ! !