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 |
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 |
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 |
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 ...) |