Question

I'm using the boost filesystem library, and having two paths, I need to know if there is an elegant way of checking if path1 is a child of path2 (e.g. path1 = /usr/local, path2 = /usr). I can do this by using string functions, but I was wondering if there is a way using boost filesystem functions. I could do this with path iterators, is that the only way? Is there some helper function that does this check? I searched on the documentation but could not find anything. Thanks

Was it helpful?

Solution

*path1.begin() == *path2.begin()

This will however mean that "c:/foo" shares a base with "c:/bar", which might be unintended.

for( boost::filesystem::path::iterator itrLeft( path1.begin() ), itrRight( path2.begin() ); *itrLeft == *itrRight && itrLeft != path1.end() && itrRight != path2.end(); ++itrLeft, ++itrRight ) 

This way you can see how many atoms are matching, I suggest using boost::filesystem::absolute first if you want to make it robust.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top