Question

I needed a function to get the absolute path and for that purpose I had a look in boost but it only has that in recent version and I'm using an old 1.44. As I cannot update my code on recent boost 1.55 or higher, I decided to re-write that function. It worked fine on Windows but I'm getting in an infinite recursion under Linux and I cannot understand why ? That code is based on the description you can find here

All advice to fix that issue will be welcome ! Thanks

#include "boost/filesystem.hpp"
namespace bfs = boost::filesystem;

inline bfs::path absolute(const bfs::path& p, const bfs::path& base=bfs::current_path())
{
  if(p.has_root_directory())
  {
    if(p.has_root_name()) return p;
    else return absolute(base).root_name() / p;
  }
  else
  {
    if(p.has_root_name()) return bfs::path(p.root_name()) / bfs::path(absolute(base).root_directory()) / absolute(base).relative_path() / p.relative_path();
    else return absolute(base) / p;
  }  
}

No correct solution

OTHER TIPS

Finally I used a copy of the boost v1.55 code to solve that issue.

inline bool is_absolute(const bfs::path p) 
{
#if defined(WIN32) || defined(WIN64)
  return p.has_root_name() && p.has_root_directory();
#else
  return p.has_root_directory();
#endif
}

inline bfs::path absolute(const bfs::path& p, const bfs::path& base=bfs::current_path())
{
  //  recursively calling absolute is sub-optimal, but is sure and simple
  bfs::path abs_base(is_absolute(base) ? base : absolute(base));

  //  store expensive to compute values that are needed multiple times
  bfs::path p_root_name (p.root_name());
  bfs::path base_root_name (abs_base.root_name());
  bfs::path p_root_directory (p.root_directory());

  if (p.empty()) 
  {
    return abs_base;
  }

  if (!p_root_name.empty())  // p.has_root_name()
  {
    if (p_root_directory.empty())  // !p.has_root_directory()
      return p_root_name / abs_base.root_directory()
      / abs_base.relative_path() / p.relative_path();
    // p is absolute, so fall through to return p at end of block
  } 
  else if (!p_root_directory.empty())  // p.has_root_directory()
  {
    return base_root_name / p;
  }
  else
  {
    return abs_base / p;
  }

  return p;  // p.is_absolute() is true
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top