FileURI.st
changeset 4182 4c84b6a60de4
parent 3453 a49af911b6e0
equal deleted inserted replaced
4181:d7d3be3e8f19 4182:4c84b6a60de4
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 "{ Package: 'stx:libbasic2' }"
    12 "{ Package: 'stx:libbasic2' }"
    13 
    13 
       
    14 "{ NameSpace: Smalltalk }"
       
    15 
    14 HierarchicalURI subclass:#FileURI
    16 HierarchicalURI subclass:#FileURI
    15 	instanceVariableNames:''
    17 	instanceVariableNames:''
    16 	classVariableNames:''
    18 	classVariableNames:''
    17 	poolDictionaries:''
    19 	poolDictionaries:''
    18 	category:'Net-Resources'
    20 	category:'Net-Resources'
    53 !FileURI methodsFor:'converting'!
    55 !FileURI methodsFor:'converting'!
    54 
    56 
    55 asFilename
    57 asFilename
    56     "answer the receiver represented as filename"
    58     "answer the receiver represented as filename"
    57 
    59 
    58     ^ authority notNil ifTrue:[
    60     ^ authority notEmptyOrNil ifTrue:[
    59         Filename remoteHost:authority rootComponents:pathSegments.
    61         Filename remoteHost:authority rootComponents:pathSegments.
    60     ] ifFalse:[
    62     ] ifFalse:[
    61         "kludge"
    63         isAbsolute ifTrue:[
    62         (pathSegments first startsWith:$~) ifTrue:[
    64             Filename rootComponents:pathSegments.
    63             pathSegments first asFilename construct:(Filename rootComponents:(pathSegments copyFrom:2)).
       
    64         ] ifFalse:[
    65         ] ifFalse:[
    65             Filename rootComponents:pathSegments.
    66             Filename fromComponents:pathSegments.
    66         ].
    67         ].
    67     ].
    68     ].
    68 
    69 
    69     "
    70     "
    70         (URI fromString:'file:~/bla') asFilename
    71         (URI fromString:'file:///dir/file') asFilename
    71         (URI fromString:'file:~root/bla') asFilename 
    72         (URI fromString:'file:///C:dir/file') asFilename
       
    73         (URI fromString:'file:///C:/dir/file') asFilename
       
    74         (URI fromString:'file:///~/bla') asFilename
       
    75         (URI fromString:'file:///~root/bla') asFilename 
       
    76         (URI fromString:'file:////host/dir/file') asFilename 
    72     "
    77     "
    73 ! !
    78 ! !
    74 
    79 
    75 !FileURI methodsFor:'initialization'!
    80 !FileURI methodsFor:'initialization'!
    76 
    81 
    77 fromFilename:aFilename
    82 fromFilename:aFilename
    78     "create an URI based on an a filename"
    83     "create an URI based on an a filename"
    79 
    84 
    80     |components|
    85     |volume|
    81 
    86 
    82     components := aFilename components.
    87     pathSegments := aFilename components.
    83     aFilename isAbsolute ifTrue:[
    88     aFilename isAbsolute ifTrue:[
    84         (components size > 3 and:[(components at:2) size == 0]) ifTrue:[
    89         (pathSegments notEmpty and:[pathSegments first startsWith:'\\']) ifTrue:[
    85             "this is a MS-Windows network path: \\host\path"
    90             "this is a MS-Windows network path: \\host\path"
    86             authority := components at:3.
    91             isAbsolute := false. "there are already enogh / in the first pathComponent"
    87             pathSegments := components copyFrom:4.
    92             pathSegments at:1 put:(pathSegments first replaceAll:$\ with:$/).
    88         ] ifFalse:[
    93         ] ifFalse:[
    89             "this is an absolute path"
    94             "this is an absolute path"
    90             isAbsolute := true.
    95             isAbsolute := true.
    91             pathSegments := components copyFrom:2.
    96             volume := aFilename volume.
       
    97             volume notEmpty ifTrue:[
       
    98                 pathSegments at:1 put:volume.
       
    99             ].
    92         ].
   100         ].
    93     ] ifFalse:[
   101     ] ifFalse:[
    94         "this is a relative path"
   102         "this is a relative path"
    95         isAbsolute := false.
   103         isAbsolute := false.
    96         pathSegments := components.
       
    97     ]
   104     ]
    98 
   105 
    99     "
   106     "
   100       self fromFilename:'/a/b/c'  asFilename   
   107       self fromFilename:'/a/b/c'  asFilename   
   101       self fromFilename:'//a/b/c' asFilename  
   108       self fromFilename:'//a/b/c' asFilename  
   102       self fromFilename:'a/b/c'   asFilename    
   109       self fromFilename:'a/b/c'   asFilename    
       
   110 
       
   111       self fromFilename:'\a\b\c'  asFilename   
       
   112       self fromFilename:'~user\a\b\c'  asFilename   
       
   113       self fromFilename:'C:\a\b\c'  asFilename   
       
   114       self fromFilename:'\\a\b\c'  asFilename 
       
   115       self fromFilename:'a\b\c'   asFilename    
   103     "
   116     "
   104 ! !
   117 ! !
   105 
   118 
   106 !FileURI methodsFor:'queries'!
   119 !FileURI methodsFor:'queries'!
   107 
   120 
   350 ! !
   363 ! !
   351 
   364 
   352 !FileURI class methodsFor:'documentation'!
   365 !FileURI class methodsFor:'documentation'!
   353 
   366 
   354 version
   367 version
   355     ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.18 2014-12-02 14:22:43 stefan Exp $'
   368     ^ '$Header$'
   356 !
   369 !
   357 
   370 
   358 version_CVS
   371 version_CVS
   359     ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.18 2014-12-02 14:22:43 stefan Exp $'
   372     ^ '$Header$'
   360 ! !
   373 ! !
   361 
   374