From: drkm
Subject: About ENOUGH-NAMESTRING
Date: 
Message-ID: <wkoea3msz6.fsf@fgeorges.org>
  Hi

  Given a "base directory", I'd like to reduce the name of files
relatively to this base directory.  For example, given the base
"dir/", I'd like to reduce from "dir/file" to "file".

  I tried (naively?) this:

    (enough-namestring #p"dir/file" #p"dir/")

but I get "dir/file" as result.  So it doesn't seem to be the
right way.

  Do you know how to do what I want?

  Thanks,

--drkm

From: Kent M Pitman
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <u7jgr4hcw.fsf@nhplace.com>
drkm <······@fgeorges.org> writes:

>   Given a "base directory", I'd like to reduce the name of files
> relatively to this base directory.  For example, given the base
> "dir/", I'd like to reduce from "dir/file" to "file".
> 
>   I tried (naively?) this:
> 
>     (enough-namestring #p"dir/file" #p"dir/")
> 
> but I get "dir/file" as result.  So it doesn't seem to be the
> right way.

You might try reporting this as an implementation bug and seeing if your
vendor will just fix it.  I don't know why this shouldn't work.

>   Do you know how to do what I want?

It's probably some issue with relative pathnames.  But the result should
be the same if you use absolute ones.  So if the files you are naming 
exist (i.e., wouldn't return NIL from TRUENAME), try:

 (enough-namestring (truename #P"dir/file") (truename "dir/"))

In general:

 (defun enuf-namestring (x y)
   (enough-namestring (or (truename x) x)
                      (or (truename y) y)))

If that doesn't work, it's a relatively (pardon pun) simple programming
problem to see that they're both relative, do as many :up's as are needed
to get to their least common superior dir, then go back down into the 
target pathnames subdirs that are not part of the common subtail.
You can write this yourself--it's not something the system has to provide.
It just gets provided because it's handy.
From: drkm
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <wkk6kqr3en.fsf@fgeorges.org>
Kent M Pitman <······@nhplace.com> writes:

> drkm <······@fgeorges.org> writes:

>>     (enough-namestring #p"dir/file" #p"dir/")

>> but I get "dir/file" as result.  So it doesn't seem to be the
>> right way.

> You might try reporting this as an implementation bug and seeing if your
> vendor will just fix it.  I don't know why this shouldn't work.

  Ok, I'll do.  For info:

    ~> lisp --version
    SBCL 0.9.0

> It's probably some issue with relative pathnames.  But the result should
> be the same if you use absolute ones.

  Nop:

    CL-USER> (enough-namestring #p"dir/file" #p"dir/")
    "dir/subdir"
    CL-USER> (enough-namestring #p"/dir/file" #p"/dir/")
    "subdir"

>                                       So if the files you are naming 
> exist (i.e., wouldn't return NIL from TRUENAME), try:

>  (enough-namestring (truename #P"dir/file") (truename "dir/"))

  Ok.  It works fine.

> You can write this yourself--it's not something the system has to provide.
> It just gets provided because it's handy.

  Indeed.  But the goal is to learn Common Lisp, not to have the
final program.

  Thanks.

--drkm
From: Peter Seibel
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <m27jgrihl4.fsf@gigamonkeys.com>
drkm <······@fgeorges.org> writes:

>   Hi
>
> Given a "base directory", I'd like to reduce the name of files
> relatively to this base directory.  For example, given the base
> "dir/", I'd like to reduce from "dir/file" to "file".
>
>   I tried (naively?) this:
>
>     (enough-namestring #p"dir/file" #p"dir/")
>
> but I get "dir/file" as result.  So it doesn't seem to be the
> right way.
>
>   Do you know how to do what I want?

Hmmm. What Lisp are you using? And what's the value of *DEFAULT-PATHNAME-DEFAULTS*?

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: drkm
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <wkekayr39w.fsf@fgeorges.org>
Peter Seibel <·····@gigamonkeys.com> writes:

> Hmmm. What Lisp are you using? And what's the value of
> *DEFAULT-PATHNAME-DEFAULTS*?

  I don't know if it's important here.  Here is the value:

    CL-USER> *default-pathname-defaults*
    #P"/home/users/f/fl/florentgeorges/"

  It's my home directory.  And the working directory of SBCL
while evaluating the variable.

--drkm
From: Harald Hanche-Olsen
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <pcohdfvqsuy.fsf@shuttle.math.ntnu.no>
+ drkm <······@fgeorges.org>:

|   Given a "base directory", I'd like to reduce the name of files
| relatively to this base directory.  For example, given the base
| "dir/", I'd like to reduce from "dir/file" to "file".
| 
|   I tried (naively?) this:
| 
|     (enough-namestring #p"dir/file" #p"dir/")

(Actually, the CLHS requires the first argument to enough-namestring
to be a string or NIL, so your example should be
(enough-namestring "dir/file" #p"dir/").  But this is less important.)

| but I get "dir/file" as result.  So it doesn't seem to be the
| right way.

This is because the file name "dir/file" seen relative to "dir/"
becomes "dir/dir/file".  Note that the standard requires

 (merge-pathnames (enough-namestring pathname defaults) defaults)
==  (merge-pathnames (parse-namestring pathname nil defaults) defaults)

and the latter certainly will become #P"dir/dir/file" in your case.

It seems to me that enough-namestring cannot do other than return its
first argument, unless the second argument is an absolute pathname,
and the first argument names an absolute pathname as well.

|   Do you know how to do what I want?

You may need to take apart the PATHNAME-DIRECTORY of each argument and
peel off common parts at the beginning, build a new pathname and apply
NAMESTRING to it.

But maybe you also need to consider again if this is what you want:
The "dir/dir/file" example shows that dragons lurk along this path.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Peter Seibel
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <m2psujgyj6.fsf@gigamonkeys.com>
Harald Hanche-Olsen <······@math.ntnu.no> writes:

> + drkm <······@fgeorges.org>:
>
> |   Given a "base directory", I'd like to reduce the name of files
> | relatively to this base directory.  For example, given the base
> | "dir/", I'd like to reduce from "dir/file" to "file".
> | 
> |   I tried (naively?) this:
> | 
> |     (enough-namestring #p"dir/file" #p"dir/")
>
> (Actually, the CLHS requires the first argument to enough-namestring
> to be a string or NIL, so your example should be
> (enough-namestring "dir/file" #p"dir/").  But this is less important.)

Eh, my CLHS says both argument are pathname designators.

> | but I get "dir/file" as result.  So it doesn't seem to be the
> | right way.
>
> This is because the file name "dir/file" seen relative to "dir/"
> becomes "dir/dir/file".  Note that the standard requires
>
>  (merge-pathnames (enough-namestring pathname defaults) defaults)
> ==  (merge-pathnames (parse-namestring pathname nil defaults) defaults)
>
> and the latter certainly will become #P"dir/dir/file" in your case.
>
> It seems to me that enough-namestring cannot do other than return its
> first argument, unless the second argument is an absolute pathname,
> and the first argument names an absolute pathname as well.

So, unless the OP's *default-pathname-defaults* is set to some odd
value, chances are he can get what he wants with this:

  (enough-namestring (merge-pathnames #p"dir/file") (merge-pathnames #p"dir/"))

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Harald Hanche-Olsen
Subject: Re: About ENOUGH-NAMESTRING
Date: 
Message-ID: <pcod5qjqrm8.fsf@shuttle.math.ntnu.no>
+ Peter Seibel <·····@gigamonkeys.com>:

| Eh, my CLHS says both argument are pathname designators.

Oh.  So does mine.  I looked at the wrong line.  I must have been
confused by the multiple functions being defined simultaneously.

| So, unless the OP's *default-pathname-defaults* is set to some odd
| value, chances are he can get what he wants with this:
| 
|   (enough-namestring (merge-pathnames #p"dir/file")
|                      (merge-pathnames #p"dir/"))

Clever.  Of course he doesn't have to rely on
*default-pathname-defaults*:

  (enough-namestring (merge-pathnames #p"dir/file" #p"/")
                     (merge-pathnames #p"dir/" #p"/"))

But my final comment stands:  Here be dragons.  Another example:

  (enough-namestring (merge-pathnames #p"dir/file" #p"/")
                     (merge-pathnames #p"barf/" #p"/"))
-> "/dir/file"

which is neither here nor there.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow