Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform library for manipulating Windows paths?

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.

like image 429
Magnus Hoff Avatar asked Oct 26 '22 23:10

Magnus Hoff


2 Answers

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 :\.

like image 80
Magnus Hoff Avatar answered Oct 28 '22 12:10

Magnus Hoff


What about QT with QFileInfo ?

like image 38
alexkr Avatar answered Oct 28 '22 13:10

alexkr