I write code to manage the user structure of files on disk and synchronize it with unrelated systems. One of my requirements is the ability to estimate the size of the synchronization before actually creating the synchronization content. As a simple solution, I put together a map with full file names as a key to efficiently search for already scanned content.
I am having problems with this when I have several files in my file structure that are referenced in 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 three lines of the path refer to the same file on disk, but their string representation is different. If I drop them onto the card verbatim, I will count the size of the samplefile.txt file 3 times, and my estimate will be incorrect.
In an attempt to find a way around this, I was hoping that boost :: filesystem :: path provided a function to reduce or simplify the path, but I did not see anything like it. Using the path decomposition table and path iterators, I wrote 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) == ".." )
{
result = result.parent_path( );
}
else if( (*it) == "." )
{
}
else
{
result /= (*it);
}
}
return result.string( ).c_str( );
}
I have two questions.
Firstly, is there a standard function that provides this functionality, or does it already exist in boost or another library somewhere?
Secondly, I'm not quite sure that the function that I wrote will work in all cases, and I would like to take a look at it. It works in my tests. Does anyone see a case when it breaks?
source
share