|
Boost Users : |
From: Simon Buchan (simon_at_[hidden])
Date: 2005-11-03 03:03:29
Hermann Rodrigues wrote:
> Hi,
>
> I am running into the following problem these days:
>
> I need to translate an absolute path to a path relative to the current
> directory on Windows. The translation process need to include all
> required ".." directory specifiers in the path to make sure it can be
> accessed from the current directory.
>
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 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