Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::filesystem::path as a key in an std::map

So std::map wants the key type to be ordered, but I ran into problems:

"a/b" < "a/c" < "a//b" but fs::equivalent("a/b", "a//b")

"a/b" < "a/c" < "a\b" but fs::equivalent("a/b", "a\b")

How I do I normalize a path enough to create an ordering? Is that even possible? I tried using path::generic_string() in a custom comparison operator, but it didn't work. fs::equivalent() doesn't help either, because implementing equality isn't enough for std::map, it needs an ordering. That's also the reason why this question isn't a duplicate of How do I "normalize" a pathname using boost::filesystem?.

Context: Windows only, Boost 1.49, filesystem3.

like image 877
Andreas Haferburg Avatar asked May 14 '26 05:05

Andreas Haferburg


1 Answers

To make paths comparable you need to convert them to the canonical representation and then compare via string. A canonical path is absolute, normalized and has symbolic links removed. Boost offers canonical AFAIK. Because symbolic links need to be resolved, calling canonical requires access to the filesystem.

like image 79
Christopher Oezbek Avatar answered May 19 '26 04:05

Christopher Oezbek