路径简化/减少
我正在编写一些代码来管理自定义磁盘文件结构并将其同步到未连接的系统。我的要求之一是能够在实际生成同步内容之前估计同步的大小。作为一个简单的解决方案,我将带有完整路径文件名的地图放在一起,作为有效查找已扫描内容的关键。
当我的文件结构中有多个文件以不同方式从不同位置引用时,我会遇到问题。例如:
C:\DataSource\files\samplefile.txt
C:\DataSource\data\samples\..\..\files\samplefile.txt
C:\DataSource\etc\..\files\samplefile.txt
这 3 个路径字符串都引用磁盘上的同一文件,但它们的字符串表示形式不同。如果我将它们逐字放入地图中,我将计算 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 或其他库中?
第二,我并不完全相信我编写的函数在所有情况下都能工作,并且我希望更多人关注它。它在我的测试中有效。有人看到它会崩溃的情况吗?
I'm writing some code to manage a custom on disk file structure and syncronize it to unconnected systems. One of my requirements is to be able to estimate the size of a sync prior to actually generating the sync content. As a simple solution, I've put together a map with full path filenames as the key for effecient lookup of already scanned content.
I run into problems with this when I have multiple files in my file structure referenced from different places in different ways. For example:
C:\DataSource\files\samplefile.txt
C:\DataSource\data\samples\..\..\files\samplefile.txt
C:\DataSource\etc\..\files\samplefile.txt
These 3 path strings all reference the same file on-disk, however their string representation is different. If I drop these into a map verbatim, I'll count the size of samplefile.txt 3 times, and my estimate will be wrong.
In an attempt to find a way around this, I was hoping boost::filesystem::path provided a function to reduce or simplify a path, but I didn't see anything of the sort. Using the path decomposition table and path iterators, I wrote up the following function (for use in a Windows environment):
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( );
}
I have two questions.
One, is there a standard function that provides this sort of functionality, or does this already exist in boost or another library somewhere?
Two, I'm not entirely confident that the function I wrote will work in all cases, and I'd like some more eyes on it. It works in my tests. Does anyone see a case where it'll break down?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost中有一个函数
可以检查两条路径是否相等。这将是理想的,除非没有等效的 <运算符(也许不能)。
也许;如果您输入了“../test.txt”之类的内容,父路径可能不会执行您想要的操作。我建议首先完成路径。
请参阅文件系统库中的“完整”。
祝你好运
——罗伯特·纳尔逊
There is a function in boost
That checks if two paths are equal. That would be ideal except that there is no equivalent < operator(and perhaps cannot be).
Maybe; if you have input like "../test.txt", parent path might not do what you want. I would recommend completing the path first.
See "complete" in the filesystem library.
Good luck
--Robert Nelson
虽然不是一个完全的骗局,但这个问题会有所帮助:
确定两个是否存在的最佳方法Windows 中同一文件的路径引用?
While not an exact dupe, this question will help:
Best way to determine if two path reference to same file in Windows?