| 1 | #include "boost/filesystem.hpp"
|
|---|
| 2 | #include "boost/version.hpp"
|
|---|
| 3 |
|
|---|
| 4 | namespace boost { namespace filesystem3 {
|
|---|
| 5 | template < >
|
|---|
| 6 | path& path::append< typename path::iterator >( typename path::iterator begin, typename path::iterator end, const codecvt_type& cvt)
|
|---|
| 7 | {
|
|---|
| 8 | for( ; begin != end ; ++begin )
|
|---|
| 9 | *this /= *begin;
|
|---|
| 10 | return *this;
|
|---|
| 11 | }
|
|---|
| 12 | // Return path when appended to a_From will resolve to same as a_To
|
|---|
| 13 | boost::filesystem::path make_relative( boost::filesystem::path a_From, boost::filesystem::path a_To )
|
|---|
| 14 | {
|
|---|
| 15 | a_From = boost::filesystem::absolute( a_From ); a_To = boost::filesystem::absolute( a_To );
|
|---|
| 16 | boost::filesystem::path ret;
|
|---|
| 17 | boost::filesystem::path::const_iterator itrFrom( a_From.begin() ), itrTo( a_To.begin() );
|
|---|
| 18 | // Find common base
|
|---|
| 19 | for( boost::filesystem::path::const_iterator toEnd( a_To.end() ), fromEnd( a_From.end() ) ; itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo );
|
|---|
| 20 | // Navigate backwards in directory to reach previously found base
|
|---|
| 21 | for( boost::filesystem::path::const_iterator fromEnd( a_From.end() ); itrFrom != fromEnd; ++itrFrom )
|
|---|
| 22 | {
|
|---|
| 23 | if( (*itrFrom) != "." )
|
|---|
| 24 | ret /= "..";
|
|---|
| 25 | }
|
|---|
| 26 | // Now navigate down the directory branch
|
|---|
| 27 | ret.append( itrTo, a_To.end() );
|
|---|
| 28 | return ret;
|
|---|
| 29 | }
|
|---|
| 30 | } }
|
|---|
| 31 | namespace boost { namespace filesystem { using filesystem3::make_relative; } }
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | int main()
|
|---|
| 35 | {
|
|---|
| 36 | boost::filesystem::path a("foo/bar"), b("foo/test/korv.txt");
|
|---|
| 37 | std::cout << boost::filesystem::make_relative( a, b ).string() << std::endl;
|
|---|
| 38 | return 0;
|
|---|
| 39 | }
|
|---|