Simplifying a File reference

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Simplifying a File reference

Prof. Andrew P. Black
I have a file reference containing multiple occurrences of .., like this

File @ /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace

How do I simplify this to show the simpler path without the .. ?  There should be a method on FileReference, no?  Something like #normalize ?  Or maybe FileReference should do this automatically when it is created?

Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Simplifying a File reference

Alistair Grant
On 1 December 2017 at 17:32, Prof. Andrew P. Black <[hidden email]> wrote:
> I have a file reference containing multiple occurrences of .., like this
>
> File @
> /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace
>
> How do I simplify this to show the simpler path without the .. ?  There
> should be a method on FileReference, no?  Something like #normalize ?  Or
> maybe FileReference should do this automatically when it is created?

The answer will depend on which version of Pharo you are using, but for Pharo7:

  '/Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace'
asFileReference canonicalize
  "File @ /Users/black/Development/mg/gracelang/albums_junk.grace"


The reason the '..' segments are left in is because while the result
is probably what you are looking for, if the original reference
contains symbolic links you may end up with a different result to what
the file system will return.

Cheers,
Alistair

Reply | Threaded
Open this post in threaded view
|

Re: Simplifying a File reference

Andreas Brodbeck-3
In reply to this post by Prof. Andrew P. Black
Am 01.12.17 um 17:32 schrieb Prof. Andrew P. Black:
> I have a file reference containing multiple occurrences of .., like this
>
> File @ /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace
>
> How do I simplify this to show the simpler path without the .. ?  There should be a method on FileReference, no?  Something like #normalize ?  Or maybe FileReference should do this automatically when it is created?

On Pharo 6 use "asAbsolute" on a FileReference, like this:

'/Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace'
asFileReference asAbsolute

"File @ /Users/black/Development/mg/gracelang/albums_junk.grace"


Cheers, Andreas

--
Andreas Brodbeck
www.mindclue.ch


Reply | Threaded
Open this post in threaded view
|

Re: Simplifying a File reference

Ben Coman
In reply to this post by Alistair Grant


On 2 December 2017 at 00:39, Alistair Grant <[hidden email]> wrote:
On 1 December 2017 at 17:32, Prof. Andrew P. Black <[hidden email]> wrote:
> I have a file reference containing multiple occurrences of .., like this
>
> File @
> /Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace
>
> How do I simplify this to show the simpler path without the .. ?  There
> should be a method on FileReference, no?  Something like #normalize ?  Or
> maybe FileReference should do this automatically when it is created?

The answer will depend on which version of Pharo you are using, but for Pharo7:

  '/Users/black/graceProgs/lists/../../Development/mg/gracelang/albums_junk.grace'
asFileReference canonicalize
  "File @ /Users/black/Development/mg/gracelang/albums_junk.grace"


While 'canonicalize' obviously fits, it seems a bit exotic and a tongue twist at five syllables.
I wonder if 'flatten' might also be suitable.  Is there some concept this wouldn't cover?

(just a random first impression.  happy to be shot down on it)
cheers -ben