示例#1
0
// This is based on the example code found here: https:// svn.boost.org/trac/boost/ticket/1976
// but changed to not return a trailing ".." when paths only differ in their file name.
// The function still seems to be missing in boost as of 1.54.0.
FS_NAMESPACE::path naiveUncomplete(FS_NAMESPACE::path const path, FS_NAMESPACE::path const base)
{
	if (path.has_root_path())
	{
		if (path.root_path() != base.root_path())
		{
			return path;
		}
		else
		{
			return naiveUncomplete(path.relative_path(), base.relative_path());
		}
	}
	else
	{
		if (base.has_root_path())
		{
			return path;
		}
		else
		{
			auto path_it = path.begin();
			auto base_it = base.begin();
			while (path_it != path.end() && base_it != base.end())
			{
				if (*path_it != *base_it) break;
				++path_it; ++base_it;
			}
			FS_NAMESPACE::path result;
			// check if we're at the filename of the base path already
			if (*base_it != base.filename())
			{
				// add trailing ".." from path to base, but only if we're not already at the filename of the base path
				for (; base_it != base.end() && *base_it != base.filename(); ++base_it)
				{
					result /= "..";
				}
			}
			for (; path_it != path.end(); ++path_it)
			{
				result /= *path_it;
			}
			return result;
		}
	}
	return path;
}