Filename.st
changeset 1120 94c803be0210
parent 1081 c2f1f8441663
child 1225 b7def4d21cb8
equal deleted inserted replaced
1119:956d62a5656c 1120:94c803be0210
   224     |sep s|
   224     |sep s|
   225 
   225 
   226     sep := self separator asString.
   226     sep := self separator asString.
   227     s := ''.
   227     s := ''.
   228     aCollectionOfDirectoryNames keysAndValuesDo:[:index :component |
   228     aCollectionOfDirectoryNames keysAndValuesDo:[:index :component |
   229         index == 1 ifTrue:[
   229 	index == 1 ifTrue:[
   230             (component ~= sep 
   230 	    (component ~= sep 
   231             or:[aCollectionOfDirectoryNames size == 1]) ifTrue:[
   231 	    or:[aCollectionOfDirectoryNames size == 1]) ifTrue:[
   232                 s := s , component
   232 		s := s , component
   233             ]
   233 	    ]
   234         ] ifFalse:[
   234 	] ifFalse:[
   235             s := s , '/' , component
   235 	    s := s , '/' , component
   236         ].
   236 	].
   237     ].
   237     ].
   238     ^ self named:s
   238     ^ self named:s
   239 
   239 
   240     "
   240     "
   241      Filename fromComponents:#('/' 'foo' 'bar' 'baz')  
   241      Filename fromComponents:#('/' 'foo' 'bar' 'baz')  
   242      Filename fromComponents:#('foo' 'bar' 'baz')  
   242      Filename fromComponents:#('foo' 'bar' 'baz')  
   243      Filename fromComponents:#('/')  
   243      Filename fromComponents:#('/')  
   244 
   244 
   245      Filename fromComponents:
   245      Filename fromComponents:
   246          (Filename components:('.' asFilename pathName))
   246 	 (Filename components:('.' asFilename pathName))
   247 
   247 
   248      Filename fromComponents:
   248      Filename fromComponents:
   249          (Filename components:('.' asFilename name)) 
   249 	 (Filename components:('.' asFilename name)) 
   250     "
   250     "
   251 
   251 
   252     "Modified: 29.2.1996 / 20:18:34 / cg"
   252     "Modified: 29.2.1996 / 20:18:34 / cg"
   253 !
   253 !
   254 
   254 
   274 
   274 
   275     |s|
   275     |s|
   276 
   276 
   277     s := OperatingSystem getHomeDirectory.
   277     s := OperatingSystem getHomeDirectory.
   278     s isNil ifTrue:[
   278     s isNil ifTrue:[
   279         ^ self defaultDirectory
   279 	^ self defaultDirectory
   280     ].
   280     ].
   281     ^ s asFilename
   281     ^ s asFilename
   282 
   282 
   283     "
   283     "
   284      Filename homeDirectory        
   284      Filename homeDirectory        
   328      See also: #newTemporary which looks for a good temp directory."
   328      See also: #newTemporary which looks for a good temp directory."
   329 
   329 
   330     |pid nm|
   330     |pid nm|
   331 
   331 
   332     NextTempFilenameIndex isNil ifTrue:[
   332     NextTempFilenameIndex isNil ifTrue:[
   333         NextTempFilenameIndex := 1.
   333 	NextTempFilenameIndex := 1.
   334     ].
   334     ].
   335 
   335 
   336     "
   336     "
   337      the following has to be made OS independent ...
   337      the following has to be made OS independent ...
   338     "
   338     "
   339     pid := OperatingSystem getProcessId printString.
   339     pid := OperatingSystem getProcessId printString.
   340     nm := 'stxtmp_' , pid , '_' , NextTempFilenameIndex printString.
   340     nm := 'stxtmp_' , pid , '_' , NextTempFilenameIndex printString.
   341     NextTempFilenameIndex := NextTempFilenameIndex + 1.
   341     NextTempFilenameIndex := NextTempFilenameIndex + 1.
   342 
   342 
   343     (aDirectoryPrefix isNil or:[aDirectoryPrefix asString isEmpty]) ifFalse:[
   343     (aDirectoryPrefix isNil or:[aDirectoryPrefix asString isEmpty]) ifFalse:[
   344         nm := aDirectoryPrefix asFilename construct:nm
   344 	nm := aDirectoryPrefix asFilename construct:nm
   345     ] ifTrue:[
   345     ] ifTrue:[
   346         nm := nm asFilename
   346 	nm := nm asFilename
   347     ].
   347     ].
   348     ^ nm
   348     ^ nm
   349 
   349 
   350     "temp files in '/tmp':
   350     "temp files in '/tmp':
   351 
   351 
   378      If any of the environment variables ST_TMPDIR or TMPDIR is set, 
   378      If any of the environment variables ST_TMPDIR or TMPDIR is set, 
   379      its value defines the name, otherwise, '/tmp' is used.
   379      its value defines the name, otherwise, '/tmp' is used.
   380      (at least on unix ...).
   380      (at least on unix ...).
   381 
   381 
   382      Notice: do not hardcode '/tmp' into your programs - things may be
   382      Notice: do not hardcode '/tmp' into your programs - things may be
   383              different on other OS's. Also, the user may want to set the
   383 	     different on other OS's. Also, the user may want to set the
   384              TMPDIR environment variable to have her temp files somewhere else.
   384 	     TMPDIR environment variable to have her temp files somewhere else.
   385              (especially on SUNOS, the root partition is ALWAYS too small ..."
   385 	     (especially on SUNOS, the root partition is ALWAYS too small ..."
   386 
   386 
   387     |tempDir|
   387     |tempDir|
   388 
   388 
   389     tempDir := OperatingSystem getEnvironment:'ST_TMPDIR'.
   389     tempDir := OperatingSystem getEnvironment:'ST_TMPDIR'.
   390     tempDir isNil ifTrue:[
   390     tempDir isNil ifTrue:[
   391         tempDir := OperatingSystem getEnvironment:'TMPDIR'.
   391 	tempDir := OperatingSystem getEnvironment:'TMPDIR'.
   392         tempDir isNil ifTrue:[
   392 	tempDir isNil ifTrue:[
   393             tempDir := '/tmp'
   393 	    tempDir := '/tmp'
   394         ].
   394 	].
   395     ].
   395     ].
   396     ^ self named:tempDir
   396     ^ self named:tempDir
   397 
   397 
   398     "
   398     "
   399      Filename tempDirectory           
   399      Filename tempDirectory           
   416 
   416 
   417     |components|
   417     |components|
   418 
   418 
   419     components := aString asCollectionOfSubstringsSeparatedBy:(self separator).
   419     components := aString asCollectionOfSubstringsSeparatedBy:(self separator).
   420     components first isEmpty ifTrue:[
   420     components first isEmpty ifTrue:[
   421         components at:1 put:(self separator asString)
   421 	components at:1 put:(self separator asString)
   422     ].
   422     ].
   423     ^ components
   423     ^ components
   424 
   424 
   425 
   425 
   426     "
   426     "
   674 ! !
   674 ! !
   675 
   675 
   676 !Filename methodsFor:'error handling'!
   676 !Filename methodsFor:'error handling'!
   677 
   677 
   678 accessDeniedError:filename
   678 accessDeniedError:filename
       
   679     "{ Pragma: +optSpace }"
       
   680 
       
   681     "report an error that access to some file was denied"
       
   682 
   679     ^ OperatingSystem accessDeniedErrorSignal
   683     ^ OperatingSystem accessDeniedErrorSignal
   680 	raiseRequestWith:filename
   684 	raiseRequestWith:filename
   681 	errorString:('access denied: ' , filename asString)
   685 	errorString:('access denied: ' , filename asString)
   682 !
   686 !
   683 
   687 
   684 fileCreationError:filename
   688 fileCreationError:filename
       
   689     "{ Pragma: +optSpace }"
       
   690 
       
   691     "report an error that some file could not be created"
       
   692 
   685     ^ OperatingSystem accessDeniedErrorSignal
   693     ^ OperatingSystem accessDeniedErrorSignal
   686 	raiseRequestWith:filename
   694 	raiseRequestWith:filename
   687 	errorString:('cannot create/write file: ' , filename asString)
   695 	errorString:('cannot create/write file: ' , filename asString)
   688 !
   696 !
   689 
   697 
   690 fileNotFoundError:filename 
   698 fileNotFoundError:filename 
       
   699     "{ Pragma: +optSpace }"
       
   700 
       
   701     "report an error that some file was not found"
       
   702 
   691     ^ OperatingSystem fileNotFoundErrorSignal
   703     ^ OperatingSystem fileNotFoundErrorSignal
   692 	raiseRequestWith:filename
   704 	raiseRequestWith:filename
   693 	errorString:('file not found: ' , filename asString)
   705 	errorString:('file not found: ' , filename asString)
   694 !
   706 !
   695 
   707 
   696 removeError:filename
   708 removeError:filename
       
   709     "{ Pragma: +optSpace }"
       
   710 
       
   711     "report an error that some file could not be removed"
       
   712 
   697     ^ OperatingSystem accessDeniedErrorSignal
   713     ^ OperatingSystem accessDeniedErrorSignal
   698 	raiseRequestWith:filename
   714 	raiseRequestWith:filename
   699 	errorString:('cannot remove: ' , filename asString)
   715 	errorString:('cannot remove: ' , filename asString)
   700 !
   716 !
   701 
   717 
   702 reportError:string with:filename
   718 reportError:string with:filename
       
   719     "{ Pragma: +optSpace }"
       
   720 
       
   721     "report an error"
       
   722 
   703     ^ OperatingSystem errorSignal
   723     ^ OperatingSystem errorSignal
   704 	raiseRequestWith:filename
   724 	raiseRequestWith:filename
   705 	errorString:string
   725 	errorString:string
   706 ! !
   726 ! !
   707 
   727 
  1000 truncateTo:newSize
  1020 truncateTo:newSize
  1001     "change the files size.
  1021     "change the files size.
  1002      This may not be supported on all operating systems"
  1022      This may not be supported on all operating systems"
  1003 
  1023 
  1004     (OperatingSystem truncateFile:nameString to:newSize) ifFalse:[
  1024     (OperatingSystem truncateFile:nameString to:newSize) ifFalse:[
  1005         ^ self reportError:'unsupported operation' with:self
  1025 	^ self reportError:'unsupported operation' with:self
  1006     ]
  1026     ]
  1007 
  1027 
  1008     "
  1028     "
  1009      |s|
  1029      |s|
  1010 
  1030 
  1203 
  1223 
  1204     |sepString|
  1224     |sepString|
  1205 
  1225 
  1206     sepString := self class separator asString.
  1226     sepString := self class separator asString.
  1207     nameString = sepString ifTrue:[
  1227     nameString = sepString ifTrue:[
  1208         "I am the root"
  1228 	"I am the root"
  1209         ^ sepString  , subname
  1229 	^ sepString  , subname
  1210     ].
  1230     ].
  1211     ^ nameString , sepString , subname asString
  1231     ^ nameString , sepString , subname asString
  1212 
  1232 
  1213     "
  1233     "
  1214      '/tmp' asFilename constructString:'foo'   
  1234      '/tmp' asFilename constructString:'foo'   
  1575     |dir baseName matching matchLen try allMatching 
  1595     |dir baseName matching matchLen try allMatching 
  1576      sepString parentString prefix nMatch|
  1596      sepString parentString prefix nMatch|
  1577 
  1597 
  1578     sepString := self class separator asString.
  1598     sepString := self class separator asString.
  1579     (nameString endsWith:sepString) ifTrue:[
  1599     (nameString endsWith:sepString) ifTrue:[
  1580         ^ #()
  1600 	^ #()
  1581     ].
  1601     ].
  1582 
  1602 
  1583     parentString := self class parentDirectoryName.
  1603     parentString := self class parentDirectoryName.
  1584     baseName := self baseName.
  1604     baseName := self baseName.
  1585     baseName ~= nameString ifTrue:[
  1605     baseName ~= nameString ifTrue:[
  1586         prefix := self directoryName.
  1606 	prefix := self directoryName.
  1587     ].
  1607     ].
  1588 
  1608 
  1589     self isAbsolute ifTrue:[
  1609     self isAbsolute ifTrue:[
  1590         dir := self directory
  1610 	dir := self directory
  1591     ] ifFalse:[
  1611     ] ifFalse:[
  1592         aDirectory isNil ifTrue:[
  1612 	aDirectory isNil ifTrue:[
  1593             dir := self directory
  1613 	    dir := self directory
  1594         ] ifFalse:[
  1614 	] ifFalse:[
  1595             dir := (aDirectory construct:nameString) directory
  1615 	    dir := (aDirectory construct:nameString) directory
  1596         ]
  1616 	]
  1597     ].
  1617     ].
  1598 
  1618 
  1599     matching := OrderedCollection new.
  1619     matching := OrderedCollection new.
  1600     dir directoryContents do:[:fileName |
  1620     dir directoryContents do:[:fileName |
  1601         ((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
  1621 	((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
  1602             (fileName startsWith:baseName) ifTrue:[
  1622 	    (fileName startsWith:baseName) ifTrue:[
  1603                 matching add:fileName
  1623 		matching add:fileName
  1604             ]
  1624 	    ]
  1605         ]
  1625 	]
  1606     ].
  1626     ].
  1607     (nMatch := matching size) > 1 ifTrue:[
  1627     (nMatch := matching size) > 1 ifTrue:[
  1608         "
  1628 	"
  1609          find the longest common prefix
  1629 	 find the longest common prefix
  1610         "
  1630 	"
  1611         matchLen := baseName size.
  1631 	matchLen := baseName size.
  1612         matchLen > matching first size ifTrue:[
  1632 	matchLen > matching first size ifTrue:[
  1613             try := baseName.
  1633 	    try := baseName.
  1614             allMatching := false
  1634 	    allMatching := false
  1615         ] ifFalse:[
  1635 	] ifFalse:[
  1616             try := matching first copyTo:matchLen.
  1636 	    try := matching first copyTo:matchLen.
  1617             allMatching := true.
  1637 	    allMatching := true.
  1618         ].
  1638 	].
  1619 
  1639 
  1620         [allMatching] whileTrue:[
  1640 	[allMatching] whileTrue:[
  1621             matching do:[:aName |
  1641 	    matching do:[:aName |
  1622                 (aName startsWith:try) ifFalse:[
  1642 		(aName startsWith:try) ifFalse:[
  1623                     allMatching := false
  1643 		    allMatching := false
  1624                 ]
  1644 		]
  1625             ].
  1645 	    ].
  1626             allMatching ifTrue:[
  1646 	    allMatching ifTrue:[
  1627                 matchLen <  matching first size ifTrue:[
  1647 		matchLen <  matching first size ifTrue:[
  1628                     matchLen := matchLen + 1.
  1648 		    matchLen := matchLen + 1.
  1629                     try := matching first copyTo:matchLen.
  1649 		    try := matching first copyTo:matchLen.
  1630                 ] ifFalse:[
  1650 		] ifFalse:[
  1631                     allMatching := false
  1651 		    allMatching := false
  1632                 ]
  1652 		]
  1633             ] ifFalse:[
  1653 	    ] ifFalse:[
  1634                 try := matching first copyTo:matchLen - 1.
  1654 		try := matching first copyTo:matchLen - 1.
  1635             ]
  1655 	    ]
  1636         ].
  1656 	].
  1637         "
  1657 	"
  1638          and set my name to the last full match
  1658 	 and set my name to the last full match
  1639         "
  1659 	"
  1640         nameString := try
  1660 	nameString := try
  1641     ].
  1661     ].
  1642 
  1662 
  1643     "
  1663     "
  1644      if I had a directory-prefix, change names in collection ...
  1664      if I had a directory-prefix, change names in collection ...
  1645     "
  1665     "
  1646     prefix notNil ifTrue:[
  1666     prefix notNil ifTrue:[
  1647         prefix = '/' ifTrue:[
  1667 	prefix = '/' ifTrue:[
  1648             "/ avoid introducing double slashes
  1668 	    "/ avoid introducing double slashes
  1649             prefix := ''
  1669 	    prefix := ''
  1650         ].
  1670 	].
  1651         matching := matching collect:[:n | prefix , '/' , n].
  1671 	matching := matching collect:[:n | prefix , '/' , n].
  1652         nMatch == 1 ifTrue:[
  1672 	nMatch == 1 ifTrue:[
  1653             nameString := matching first
  1673 	    nameString := matching first
  1654         ] ifFalse:[
  1674 	] ifFalse:[
  1655             nMatch > 1 ifTrue:[
  1675 	    nMatch > 1 ifTrue:[
  1656                 nameString := prefix , '/' , nameString
  1676 		nameString := prefix , '/' , nameString
  1657             ]
  1677 	    ]
  1658         ]
  1678 	]
  1659     ] ifFalse:[
  1679     ] ifFalse:[
  1660         nMatch == 1 ifTrue:[
  1680 	nMatch == 1 ifTrue:[
  1661             nameString := matching first
  1681 	    nameString := matching first
  1662         ]
  1682 	]
  1663     ].
  1683     ].
  1664 
  1684 
  1665     "
  1685     "
  1666      return the match-set, so caller can decide what to do
  1686      return the match-set, so caller can decide what to do
  1667      (i.e. show the matches, output a warning etc ...)
  1687      (i.e. show the matches, output a warning etc ...)
  1801     |nm idx|
  1821     |nm idx|
  1802 
  1822 
  1803     nm := self baseName.
  1823     nm := self baseName.
  1804     idx := nm lastIndexOf:(self class suffixSeparator).
  1824     idx := nm lastIndexOf:(self class suffixSeparator).
  1805     idx == 0 ifTrue:[
  1825     idx == 0 ifTrue:[
  1806         ^ Array with:nm with:''
  1826 	^ Array with:nm with:''
  1807     ].
  1827     ].
  1808     ^ Array 
  1828     ^ Array 
  1809         with:(nm copyTo:idx-1)
  1829 	with:(nm copyTo:idx-1)
  1810         with:(nm copyFrom:idx+1)
  1830 	with:(nm copyFrom:idx+1)
  1811 
  1831 
  1812     "
  1832     "
  1813      'abc.st' asFilename prefixAndSuffix  
  1833      'abc.st' asFilename prefixAndSuffix  
  1814      'abc' asFilename prefixAndSuffix  
  1834      'abc' asFilename prefixAndSuffix  
  1815      'a.b.c' asFilename prefixAndSuffix 
  1835      'a.b.c' asFilename prefixAndSuffix 
  1826     "return a new filename for the receivers name with a suffix.
  1846     "return a new filename for the receivers name with a suffix.
  1827      If the name already has a sufix, the new suffix replacaes it;
  1847      If the name already has a sufix, the new suffix replacaes it;
  1828      otherwise, the new suffix simply appended to the name."
  1848      otherwise, the new suffix simply appended to the name."
  1829 
  1849 
  1830     ^ self class named:
  1850     ^ self class named:
  1831         (self withoutSuffix name 
  1851 	(self withoutSuffix name 
  1832          , self class suffixSeparator asString 
  1852 	 , self class suffixSeparator asString 
  1833          , aSuffix asString)
  1853 	 , aSuffix asString)
  1834 
  1854 
  1835     "
  1855     "
  1836      'abc.st' asFilename withSuffix:'o'         
  1856      'abc.st' asFilename withSuffix:'o'         
  1837      'abc' asFilename withSuffix:'o'             
  1857      'abc' asFilename withSuffix:'o'             
  1838      'a.b.c' asFilename withSuffix:'o'            
  1858      'a.b.c' asFilename withSuffix:'o'            
  1867 ! !
  1887 ! !
  1868 
  1888 
  1869 !Filename class methodsFor:'documentation'!
  1889 !Filename class methodsFor:'documentation'!
  1870 
  1890 
  1871 version
  1891 version
  1872     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.53 1996-03-07 13:53:37 cg Exp $'
  1892     ^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.54 1996-03-23 17:55:20 cg Exp $'
  1873 ! !
  1893 ! !