Boost logo

Boost Users :

From: Hermann Rodrigues (hermann.rodrigues_at_[hidden])
Date: 2005-11-06 10:33:57


Thank you for you reply.

I was currently using PathToRelativePath on Windows, pointed by
Gottlob Frege. However, your solution is portable and also seems to
work a Linux environment.

I will definitely try it.

Regards,

Hermann

On 11/3/05, Simon Buchan <simon_at_[hidden]> wrote:
> Try this...
>
> path relative_to_current(path p)
> {
> path current = current_path();
>
> // Trivial case
> if(equivalent(current, p)) return p;
>
> // Doesn't share a root
> if(!equivalent(current.root_path(), p.root_path()))
> return p;
>
> // We don't care about the root anymore
> // (and makes the rest easier)
> current = current.relative_path();
> p = p.relative_path();
>
> path final(".", native);
>
> path::iterator pit = p.begin(),
> cit = current.begin();
> // Find the shared directory
> for(;pit != p.end() && cit != current.end(); ++pit, ++cit)
> if(*pit != *cit) // May not be right
> break;
>
> // Put needed parent dirs in
> while(cit != current.end())
> {
> final = ".." / final;
> ++cit;
> }
>
> // Add the path from shared
> while(pit != p.end())
> // Gah! Why doesn't *path::iterator return paths?
> final /= path(*pit++, native);
>
> // .normalize()?
> return final;
> }
>
> Haven't tested it too extensively, but it seems to work. Probably better
> as relative_path(path from, path to), but hey.
>
> --
> don't quote this
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net