路径简化/减少

发布于 2024-08-05 12:15:03 字数 1303 浏览 4 评论 0原文

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

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

婴鹅 2024-08-12 12:15:03

boost中有一个函数

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

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

有没有人看到这会打破的情况......

也许;如果您输入了“../test.txt”之类的内容,父路径可能不会执行您想要的操作。我建议首先完成路径。

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

祝你好运
——罗伯特·纳尔逊

There is a function in boost

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

That checks if two paths are equal. That would be ideal except that there is no equivalent < operator(and perhaps cannot be).

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

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

酷到爆炸 2024-08-12 12:15:03

虽然不是一个完全的骗局,但这个问题会有所帮助:
确定两个是否存在的最佳方法Windows 中同一文件的路径引用?

While not an exact dupe, this question will help:
Best way to determine if two path reference to same file in Windows?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文