无限递归当我建立使用boost绝对路径 [英] Infinite recursion when I build an absolute path using boost

查看:124
本文介绍了无限递归当我建立使用boost绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来获取的绝对路径,并为此目的,我在提振看看,但它只有在最近的版本和我使用的是旧的1.44 。正如我不能在近期提振1.55或更高版本更新我的code,我决定重新写功能。
它工作正常的Windows,但我在Linux下无限递归得到,我不明白为什么?这code是基于你能找到的此处

所有的意见,以解决这个问题将受到欢迎!
谢谢

 的#include助推/ filesystem.hpp
命名空间BFS =的boost ::文件系统;内嵌BFS ::绝对路径(常量BFS ::路径和放大器; P,常量BFS ::路径和放大器;基地= BFS的current_path ::())
{
  如果(p.has_root_directory())
  {
    如果(p.has_root_name())回磷;
    否则返回绝对(基).root_name()/ P;
  }
  其他
  {
    如果(p.has_root_name())返回BFS ::路径(p.root_name())/ BFS ::路径(绝对(基地).root_directory())/绝对(基).relative_path()/ p.relative_path() ;
    否则返回绝对(基地)/ P;
  }
}


解决方案

最后,我用的升压v1.55 code的副本来解决这个问题。

 内联BOOL is_absolute(常量BFS ::路径p)
{
#如果定义(WIN32)||定义(WIN64)
  返回p.has_root_name()及&放大器; p.has_root_directory();
#其他
  返回p.has_root_directory();
#万一
}内嵌BFS ::绝对路径(常量BFS ::路径和放大器; P,常量BFS ::路径和放大器;基地= BFS的current_path ::())
{
  //递归调用绝对是次优的,但肯定与简单
  BFS ::路径abs_bas​​e(is_absolute(基地)基地:绝对(基));  //商店昂贵来计算所需要多次值
  BFS ::路径p_root_name(p.root_name());
  BFS ::路径base_root_name(abs_bas​​e.root_name());
  BFS ::路径p_root_directory(p.root_directory());  如果(p.empty())
  {
    返回abs_bas​​e;
  }  如果(!p_root_name.empty())// p.has_root_name()
  {
    如果(p_root_directory.empty())//!p.has_root_directory()
      返回p_root_name / abs_bas​​e.root_directory()
      / abs_bas​​e.relative_path()/ p.relative_path();
    // p是绝对的,因此告吹到段结束处返回p
  }
  否则如果(!p_root_directory.empty())// p.has_root_directory()
  {
    返回base_root_name / p;
  }
  其他
  {
    返回abs_bas​​e / p;
  }  回磷; // p.is_absolute()为真
}

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;
  }  
}

解决方案

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
}

这篇关于无限递归当我建立使用boost绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆