Filename.st
author claus
Thu, 02 Jun 1994 13:21:56 +0200
changeset 85 1343af456e28
parent 77 6c38ca59927f
child 88 81dacba7a63a
permissions -rw-r--r--
(none)

"
 COPYRIGHT (c) 1992 by Claus Gittinger
              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
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Object subclass:#Filename
         instanceVariableNames:'nameString'
         classVariableNames:''
         poolDictionaries:''
         category:'ST-80 compatibility'!

Filename comment:'

COPYRIGHT (c) 1992 by Claus Gittinger
             All Rights Reserved

Filenames; originally added for ST-80 compatibility, is
taking over functionality from other classes (FileDirectory).

$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.9 1994-06-02 11:20:23 claus Exp $
'!

!Filename class methodsFor:'instance creation'!

currentDirectory
    "return a filename for the current directory"

    ^ (self basicNew) setName:(FileDirectory currentDirectory pathName)

    "Filename currentDirectory"
!

named:aString
    "return a filename for a directory named aString."

    ^ (self basicNew) setName:aString

    "Filename named:'/tmp/fooBar'"
! !

!Filename class methodsFor:'queries'!

separator
    "return the file/directory separator."

     ^ OperatingSystem fileSeparator

     "Filename separator"
! !

!Filename methodsFor:'instance creation'!

construct:subname
    "taking the receiver as a directory name, construct a new
     filename for an entry within this directory (i.e. for a file
     or a subdirectory in that directory)."

    nameString = self class separator asString ifTrue:[
        "I am the root"
        ^ (self class basicNew) setName:(nameString  , subname)
    ].
    ^ (self class basicNew) setName:(nameString , self class separator asString , subname)

    "
     ('/tmp' asFilename construct:'foo') asString    
     ('/' asFilename construct:'foo') asString         
     ('/usr/tmp' asFilename construct:'foo') asString
    "
! !

!Filename methodsFor:'converting'!

asString
    "return the receiver converted to a string"

    ^ nameString
!

asFilename
    "return the receiver converted to a filename."

    "Thats pretty easy here :-)"
    ^ self
! !

!Filename privateMethodsFor:'private accessing'!

setName:aString
    "set the filename"

    nameString := aString
! !

!Filename methodsFor:'queries'!

directoryName
    "return the directoryName of the argument, aPath
     - thats the name of the directory where aPath is"

    ^ OperatingSystem directoryNameOf:nameString

    "/foo/bar' asFilename directoryName"
!

baseName
    "return my baseName
     - thats the file/directory name without leading parent-dirs"

    ^ OperatingSystem baseNameOf:nameString

    "
     /foo/bar' asFilename baseName
    "
!

isAbsolute
    "return true, if the receiver represents an absolute pathname
     (in contrast to one relative to the current directory)"

    ^ (nameString startsWith:self class separator)

    "
     /foo/bar' asFilename isAbsolute
     '..' asFilename isAbsolute
     'source/SBrowser.st' asFilename isAbsolute
    "
!

isDirectory
    "return true, if the receiver represents an existing,
     readable directories pathname."

    ^ OperatingSystem isDirectory:nameString

    "
     '/foo/bar' asFilename isDirectory
     '/tmp' asFilename isDirectory
    "
!

exists
    "return true, if such a file exists"

    ^ OperatingSystem isValidPath:nameString

    "
     '/foo/bar' asFilename exists
     '/tmp' asFilename exists
    "
!

isReadable
    "return true, if such a file exists and is readable"

    ^ OperatingSystem isReadable:nameString

    "
     '/foo/bar' asFilename isReadable
     '/tmp' asFilename isReadable
    "
!

isWritable
    "return true, if such a file exists and is writable"

    ^ OperatingSystem isWritable:nameString

    "
     '/foo/bar' asFilename isWritable
     '/tmp' asFilename isWritable
    "
! !

!Filename methodsFor:'file access'!

renameTo:newName
    "rename the file - the argument must be convertable to a String"

    ^ OperatingSystem renameFile:nameString to:(newName asString)

    "'/tmp/foo asFileName renameTo:'/tmp/bar'"
!

copyTo:newName
    "copy the file - the argument must be convertable to a String"

    ^ OperatingSystem copyFile:nameString to:(newName asString)
!

fileIn
    "load smalltalk code from the file"

    ^ self readStream fileIn
!

newReadWriteStream
    "return a stream for read/write"

    ^ FileStream newFileNamed:nameString
!

readWriteStream
    "return a stream for read/write"

    ^ FileStream oldFileNamed:nameString
!

readStream
    "return a stream for reading"

    ^ FileStream readonlyFileNamed:nameString
!

writeStream
    "return a stream for writing"

    ^ FileStream newFileNamed:nameString
! !