gpt4 book ai didi

boost - 路径简化/减少

转载 作者:行者123 更新时间:2023-12-05 01:35:13 26 4
gpt4 key购买 nike

我正在编写一些代码来管理自定义磁盘文件结构并将其同步到未连接的系统。我的要求之一是能够在实际生成同步内容之前估计同步的大小。作为一个简单的解决方案,我整理了一个包含完整路径文件名的 map ,作为高效查找已扫描内容的关键。

当我的文件结构中有多个文件以不同方式从不同位置引用时,我遇到了这个问题。例如:

C:\DataSource\files\samplefile.txt
C:\DataSource\data\samples\..\..\files\samplefile.txt
C:\DataSource\etc\..\files\samplefile.txt

这 3 个路径字符串都引用磁盘上的同一个文件,但是它们的字符串表示形式不同。如果我将它们逐字放入 map 中,我将计算 samplefile.txt 的大小 3 次,我的估计将是错误的。

为了找到解决这个问题的方法,我希望 boost::filesystem::path 提供一个函数来减少或简化路径,但我没有看到任何类似的东西。使用路径分解表和路径迭代器,我编写了以下函数(用于 Windows 环境):

std::string ReducePath( std::string Path )
{
bfs::path input( Path );
bfs::path result( "" );
bfs::path::iterator it, endIt;
for( it = input.begin( ), endIt = input.end( ); it != endIt; it ++ )
{
if( (*it) == ".." )
{
// Remove the leaf directory.
result = result.parent_path( );
}
else if( (*it) == "." )
{
// Just ignore.
}
else
{
// Append the element to the end of the current result.
result /= (*it);
}
}

return result.string( ).c_str( );
}

我有两个问题。

其一,是否有提供此类功能的标准函数,或者它是否已经存在于 boost 或其他库中?

第二,我不完全相信我编写的函数在所有情况下都能正常工作,我希望有更多人关注它。它在我的测试中有效。有没有人看到它会发生故障的情况?

最佳答案

boost中有一个函数

bool equivalent(const Path1& p1, const Path2& p2);

检查两条路径是否相等。这将是理想的,除了没有等效的 < 运算符(也许不能)。

Does anyone see a case where this will break ...

也许;如果你有像“../test.txt”这样的输入,父路径可能不会做你想要的。我建议先完成路径。

请参阅文件系统库中的“完整”。

祝你好运--罗伯特·尼尔森

关于boost - 路径简化/减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1467274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com