Question

I am writing a cross-platform application that needs to inspect and manipulate Windows-paths.

Specifically, for the particular problem I am having now, I need to know if a path is absolute or relative.

The current code uses boost::filesystem::path which of course works like a charm on Windows:

boost::filesystem::path the_path(the_path_as_a_string);
if (!the_path.has_root_path()) { /* do stuff */ }

The problem with this approach is that boost::filesystem::path only has two modes: native and portable. This means that the Windows path grammar is unavailable when I compile under Linux (it is #ifdefed out in the source). Hence, the path "C:\path" is considered absolute in Windows, but relative in Linux.


Can you guys recommend a cross-platform C++ library that can inspect and manipulate Windows-paths?


For now, the only Windows-path operation I will do is to check whether a path is absolute or not.

The criterion I will use for an absolute path is that it both contains a drive letter, and the path starts with \. An example of an absolute path under this criterion is C:\path. These are both examples of relative paths under this criterion: C:path, \path.

Was it helpful?

Solution 2

It seems to be difficult to find a library for this. One possibility is PathIsRelative in Winelib, but I don't want to use Winelib.

I ended up doing a very specific solution just for deciding this small thing. Assuming that the path is correct (a fair assumption in my case), an absolute path will contain :\, while a relative path will not.

So, the bad, but working, solution is: There is no suitable library. Check for existence of :\.

OTHER TIPS

What about QT with QFileInfo ?

Could you elaborate on what the program is meant to do with the Windows paths on Linux?

Perhaps it is sufficient to apply some simple transformation to the Windows path string, syntactically transforming it into a Unix path, and then use boost::filesystem to manipulate it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top