Filename.st
branchjv
changeset 19478 1f5aa87f6170
parent 19406 faccbadf3034
parent 19440 10dcec541744
child 19610 a9a6940944a9
equal deleted inserted replaced
19477:af82888ceb72 19478:1f5aa87f6170
  2034 
  2034 
  2035     ^ URL fromString:self pathName.
  2035     ^ URL fromString:self pathName.
  2036 !
  2036 !
  2037 
  2037 
  2038 components
  2038 components
  2039     "return the receivers filename components - that is the name of each directory
  2039     "return the receiver's filename components - that is the name of each directory
  2040      along the pathName (that DOES include the root directory)"
  2040      along the pathName (that DOES include the root directory)"
  2041 
  2041 
  2042     ^ self species components:self name
  2042     ^ self species components:self name
  2043 
  2043 
  2044     "
  2044     "
  2046      'Makefile' asFilename asAbsoluteFilename components
  2046      'Makefile' asFilename asAbsoluteFilename components
  2047     "
  2047     "
  2048 !
  2048 !
  2049 
  2049 
  2050 makeLegalFilename
  2050 makeLegalFilename
  2051     "convert the receivers name to be a legal filename.
  2051     "convert the receiver's name to be a legal filename.
  2052      This removes/replaces invalid characters and/or compresses
  2052      This removes/replaces invalid characters and/or compresses
  2053      the name as required by the OS.
  2053      the name as required by the OS.
  2054      The implementation may change in the future to be more
  2054      The implementation may change in the future to be more
  2055      OS specific."
  2055      OS specific."
  2056 
  2056 
  2942 
  2942 
  2943     "Modified: / 23.12.1999 / 21:52:36 / cg"
  2943     "Modified: / 23.12.1999 / 21:52:36 / cg"
  2944 !
  2944 !
  2945 
  2945 
  2946 basicMakeDirectory
  2946 basicMakeDirectory
  2947     "create a directory with the receivers name.
  2947     "create a directory with the receiver's name.
  2948      Return true if successful, false if not."
  2948      Return true if successful, false if not."
  2949 
  2949 
  2950     ^ OperatingSystem createDirectory:(self osNameForDirectory)
  2950     ^ OperatingSystem createDirectory:(self osNameForDirectory)
  2951 !
  2951 !
  2952 
  2952 
  3034      out reset; contents
  3034      out reset; contents
  3035     "
  3035     "
  3036 !
  3036 !
  3037 
  3037 
  3038 createAsEmptyFile
  3038 createAsEmptyFile
  3039     "create an empty file with the receivers name.
  3039     "create an empty file with the receiver's name.
  3040      Raises an exception if not successful
  3040      Raises an exception if not successful
  3041     (either already existing or creation not possible)"
  3041     (either already existing or creation not possible)"
  3042 
  3042 
  3043     |writeStream|
  3043     |writeStream|
  3044 
  3044 
  3045     self exists ifTrue:[
  3045     self exists ifTrue:[
  3046 	OperatingSystem accessDeniedErrorSignal
  3046         OperatingSystem accessDeniedErrorSignal
  3047 	    raiseRequestWith:self
  3047             raiseRequestWith:self
  3048 	    errorString:(' - file exists: ' , self asString).
  3048             errorString:(' - file exists: ' , self asString).
  3049 	^ self
  3049         ^ self
  3050     ].
  3050     ].
  3051 
  3051 
  3052     FileStream openErrorSignal handle:[:ex|
  3052     FileStream openErrorSignal handle:[:ex|
  3053 	self fileCreationError:self.
  3053         self fileCreationError:self.
  3054 	^ self
  3054         ^ self
  3055     ] do:[
  3055     ] do:[
  3056 	writeStream := self newReadWriteStream.
  3056         writeStream := self newReadWriteStream.
  3057     ].
  3057     ].
  3058     writeStream close.
  3058     writeStream close.
  3059 !
  3059 !
  3060 
  3060 
  3061 createAsSymbolicLinkTo:linkFilenameString
  3061 createAsSymbolicLinkTo:linkFilenameString
  3062     "create a directory with the receivers name.
  3062     "create a directory with the receiver's name.
  3063      Raises an exception if not successful"
  3063      Raises an exception if not successful"
  3064 
  3064 
  3065     OperatingSystem createSymbolicLinkFrom:linkFilenameString to:self pathName.
  3065     OperatingSystem createSymbolicLinkFrom:linkFilenameString to:self pathName.
  3066 
  3066 
  3067     "
  3067     "
  3068 	'/tmp/link' asFilename makeSymbolicLinkTo:'bla'
  3068         '/tmp/link' asFilename makeSymbolicLinkTo:'bla'
  3069     "
  3069     "
  3070 !
  3070 !
  3071 
  3071 
  3072 delete
  3072 delete
  3073     "remove the file - same as remove, for ST-80 compatibility"
  3073     "remove the file - same as remove, for ST-80 compatibility"
  3074 
  3074 
  3075     self remove
  3075     self remove
  3076 !
  3076 !
  3077 
  3077 
  3078 makeDirectory
  3078 makeDirectory
  3079     "create a directory with the receivers name.
  3079     "create a directory with the receiver's name.
  3080      Raises an exception if not successful"
  3080      Raises an exception if not successful"
  3081 
  3081 
  3082     (self basicMakeDirectory) ifFalse:[
  3082     (self basicMakeDirectory) ifFalse:[
  3083 	"/
  3083         "/
  3084 	"/ could have existed before ...
  3084         "/ could have existed before ...
  3085 	"/
  3085         "/
  3086 	(self exists and:[self isDirectory]) ifFalse:[
  3086         (self exists and:[self isDirectory]) ifFalse:[
  3087 	    self fileCreationError:self.
  3087             self fileCreationError:self.
  3088 	    ^ false
  3088             ^ false
  3089 	]
  3089         ]
  3090     ].
  3090     ].
  3091     ^ true
  3091     ^ true
  3092 
  3092 
  3093     "Modified: / 5.5.1999 / 13:36:33 / cg"
  3093     "Modified: / 5.5.1999 / 13:36:33 / cg"
  3094 !
  3094 !
  4774 filenameCompletion
  4774 filenameCompletion
  4775     "try to complete the receiver filename.
  4775     "try to complete the receiver filename.
  4776      BAD DESIGN: has side effect on the receiver.
  4776      BAD DESIGN: has side effect on the receiver.
  4777      This method has both a return value and a side effect on the receiver:
  4777      This method has both a return value and a side effect on the receiver:
  4778        it returns a collection of matching filename objects,
  4778        it returns a collection of matching filename objects,
  4779        and changes the receivers filename-string to the longest common
  4779        and changes the receiver's filename-string to the longest common
  4780        match.
  4780        match.
  4781      If none matches, the returned collection is empty and the recevier is unchanged.
  4781      If none matches, the returned collection is empty and the receiver is unchanged.
  4782      If there is only one match, the size of the returned collection is exactly 1,
  4782      If there is only one match, the size of the returned collection is exactly 1,
  4783      containing the fully expanded filename and the receivers name is changed to it."
  4783      containing the fully expanded filename and the receivers name is changed to it."
  4784 
  4784 
  4785     ^ self filenameCompletionIn:nil
  4785     ^ self filenameCompletionIn:nil
  4786 
  4786 
  5200 !
  5200 !
  5201 
  5201 
  5202 physicalFilename
  5202 physicalFilename
  5203     "return the fileName representing the physical file as represented by the receiver,
  5203     "return the fileName representing the physical file as represented by the receiver,
  5204      If the receiver represents a symbolic link, thats the fileName of the
  5204      If the receiver represents a symbolic link, thats the fileName of the
  5205      final target. Otherwise, its the receivers pathName itself.
  5205      final target. Otherwise, its the receiver's pathName itself.
  5206      If any file along the symbolic path does not exist (i.e. is a broken link),
  5206      If any file along the symbolic path does not exist (i.e. is a broken link),
  5207      nil is returned."
  5207      nil is returned."
  5208 
  5208 
  5209     |pathOrNil|
  5209     |pathOrNil|
  5210 
  5210 
  5211     pathOrNil := self physicalPathName.
  5211     pathOrNil := self physicalPathName.
  5212     pathOrNil isNil ifTrue:[
  5212     pathOrNil isNil ifTrue:[
  5213 	^ nil
  5213         ^ nil
  5214     ].
  5214     ].
  5215     ^ pathOrNil asFilename
  5215     ^ pathOrNil asFilename
  5216 
  5216 
  5217     "
  5217     "
  5218      '/foo/bar' asFilename physicalFileName
  5218      '/foo/bar' asFilename physicalFileName
  5220 !
  5220 !
  5221 
  5221 
  5222 physicalPathName
  5222 physicalPathName
  5223     "return the full pathname of the physical file represented by the receiver,
  5223     "return the full pathname of the physical file represented by the receiver,
  5224      If the receiver represents a symbolic link, thats the fileName of the
  5224      If the receiver represents a symbolic link, thats the fileName of the
  5225      final target. Otherwise, its the receivers pathName itself.
  5225      final target. Otherwise, its the receiver's pathName itself.
  5226      If any file along the symbolic path does not exist (i.e. is a broken link),
  5226      If any file along the symbolic path does not exist (i.e. is a broken link),
  5227      nil is returned."
  5227      nil is returned."
  5228 
  5228 
  5229     |t path info|
  5229     |t path info|
  5230 
  5230 
  5231     info := self linkInfo.
  5231     info := self linkInfo.
  5232     info isNil ifTrue:[
  5232     info isNil ifTrue:[
  5233 	" I do not exist"
  5233         " I do not exist"
  5234 	^ nil.
  5234         ^ nil.
  5235     ].
  5235     ].
  5236     info isSymbolicLink ifFalse:[
  5236     info isSymbolicLink ifFalse:[
  5237 	^ self pathName
  5237         ^ self pathName
  5238     ].
  5238     ].
  5239 
  5239 
  5240     t := self.
  5240     t := self.
  5241     [
  5241     [
  5242 	path := info path.
  5242         path := info path.
  5243 	path isNil ifTrue:[
  5243         path isNil ifTrue:[
  5244 	    "/ cannot happen
  5244             "/ cannot happen
  5245 	    ^ nil
  5245             ^ nil
  5246 	].
  5246         ].
  5247 	path asFilename isAbsolute ifTrue:[
  5247         path asFilename isAbsolute ifTrue:[
  5248 	    t := path asFilename
  5248             t := path asFilename
  5249 	] ifFalse:[
  5249         ] ifFalse:[
  5250 	    t := (self species named:t directoryName) construct:path.
  5250             t := (self species named:t directoryName) construct:path.
  5251 	].
  5251         ].
  5252 	info := t linkInfo.
  5252         info := t linkInfo.
  5253 	info isNil ifTrue:[
  5253         info isNil ifTrue:[
  5254 	    "t does not exist"
  5254             "t does not exist"
  5255 	     ^ nil
  5255              ^ nil
  5256 	].
  5256         ].
  5257     ] doWhile:[info isSymbolicLink].
  5257     ] doWhile:[info isSymbolicLink].
  5258 
  5258 
  5259     ^ t pathName
  5259     ^ t pathName
  5260 
  5260 
  5261     "
  5261     "
  5806 ! !
  5806 ! !
  5807 
  5807 
  5808 !Filename methodsFor:'suffixes'!
  5808 !Filename methodsFor:'suffixes'!
  5809 
  5809 
  5810 addSuffix:aSuffix
  5810 addSuffix:aSuffix
  5811     "return a new filename for the receivers name with a additional suffix.
  5811     "return a new filename for the receiver's name with a additional suffix.
  5812      The new suffix is simply appended to the name,
  5812      The new suffix is simply appended to the name,
  5813      regardless whether there is already an existing suffix.
  5813      regardless whether there is already an existing suffix.
  5814      See also #withSuffix:"
  5814      See also #withSuffix:"
  5815 
  5815 
  5816     |prefixName|
  5816     |prefixName|
  5817 
  5817 
  5818     prefixName := self name.
  5818     prefixName := self name.
  5819     aSuffix isEmptyOrNil ifTrue:[
  5819     aSuffix isEmptyOrNil ifTrue:[
  5820 	^ self species named:prefixName
  5820         ^ self species named:prefixName
  5821     ].
  5821     ].
  5822 
  5822 
  5823     ^ self species named:
  5823     ^ self species named:
  5824 	(prefixName
  5824         (prefixName
  5825 	 , self species suffixSeparator asString
  5825          , self species suffixSeparator asString
  5826 	 , aSuffix asString)
  5826          , aSuffix asString)
  5827 
  5827 
  5828     "
  5828     "
  5829      'abc.st' asFilename addSuffix:nil
  5829      'abc.st' asFilename addSuffix:nil
  5830      'a.b.c' asFilename addSuffix:nil
  5830      'a.b.c' asFilename addSuffix:nil
  5831      '.b.c.' asFilename addSuffix:nil
  5831      '.b.c.' asFilename addSuffix:nil
  5873 
  5873 
  5874     "Modified: 7.9.1997 / 02:55:25 / cg"
  5874     "Modified: 7.9.1997 / 02:55:25 / cg"
  5875 !
  5875 !
  5876 
  5876 
  5877 nameWithoutSuffix
  5877 nameWithoutSuffix
  5878     "return the receivers name without the suffix.
  5878     "return the receiver's name without the suffix.
  5879      If the name has no suffix, the original name is returned."
  5879      If the name has no suffix, the original name is returned."
  5880 
  5880 
  5881     |nm idx idxFromEnd|
  5881     |nm idx idxFromEnd|
  5882 
  5882 
  5883     nm := self baseName.
  5883     nm := self baseName.
  6000 
  6000 
  6001     "Modified: 7.9.1995 / 11:09:03 / claus"
  6001     "Modified: 7.9.1995 / 11:09:03 / claus"
  6002 !
  6002 !
  6003 
  6003 
  6004 withSuffix:aSuffix
  6004 withSuffix:aSuffix
  6005     "return a new filename for the receivers name with a different suffix.
  6005     "return a new filename for the receiver's name with a different suffix.
  6006      If the name already has a suffix, the new suffix replaces it;
  6006      If the name already has a suffix, the new suffix replaces it;
  6007      otherwise, the new suffix is simply appended to the name."
  6007      otherwise, the new suffix is simply appended to the name."
  6008 
  6008 
  6009     |prefixName|
  6009     |prefixName|
  6010 
  6010 
  6011     prefixName := self nameWithoutSuffix.
  6011     prefixName := self nameWithoutSuffix.
  6012     aSuffix isEmptyOrNil ifTrue:[
  6012     aSuffix isEmptyOrNil ifTrue:[
  6013 	^ self species named:prefixName
  6013         ^ self species named:prefixName
  6014     ].
  6014     ].
  6015 
  6015 
  6016     ^ self species named:
  6016     ^ self species named:
  6017 	(prefixName
  6017         (prefixName
  6018 	 , self class suffixSeparator asString
  6018          , self class suffixSeparator asString
  6019 	 , aSuffix asString)
  6019          , aSuffix asString)
  6020 
  6020 
  6021     "
  6021     "
  6022      'abc.st' asFilename withSuffix:nil
  6022      'abc.st' asFilename withSuffix:nil
  6023      'a.b.c' asFilename withSuffix:nil
  6023      'a.b.c' asFilename withSuffix:nil
  6024      '.b.c.' asFilename withSuffix:nil
  6024      '.b.c.' asFilename withSuffix:nil
  6046     "Modified: / 07-09-1995 / 11:15:42 / claus"
  6046     "Modified: / 07-09-1995 / 11:15:42 / claus"
  6047     "Modified: / 07-11-2006 / 13:58:45 / cg"
  6047     "Modified: / 07-11-2006 / 13:58:45 / cg"
  6048 !
  6048 !
  6049 
  6049 
  6050 withoutSuffix
  6050 withoutSuffix
  6051     "return a new filename for the receivers name without the suffix.
  6051     "return a new filename for the receiver's name without the suffix.
  6052      If the name has no suffix, a filename representing the same file as the receiver is returned."
  6052      If the name has no suffix, a filename representing the same file as the receiver is returned."
  6053 
  6053 
  6054     |n|
  6054     |n|
  6055 
  6055 
  6056     n := self nameWithoutSuffix.
  6056     n := self nameWithoutSuffix.