用于操作 Windows 路径的跨平台库?

发布于 2024-08-14 07:34:58 字数 720 浏览 2 评论 0原文

我正在编写一个跨平台应用程序,需要检查和操作 Windows 路径。

具体来说,对于我现在遇到的特定问题,我需要知道路径是绝对路径还是相对路径。

当前的代码使用 boost::filesystem::path 这当然在 Windows 上工作起来就像一个魅力:

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

这种方法的问题在于 boost::filesystem::path 仅有两种模式:本机模式和便携式模式。这意味着当我在Linux下编译时,Windows路径语法不可用(它在源代码中被#ifdef删除)。因此,路径“C:\path”在 Windows 中被视为绝对路径,但在 Linux 中被视为相对路径。


你们能推荐一个可以检查和操作 Windows 路径的跨平台 C++ 库吗?


目前,我唯一要做的 Windows 路径操作是检查路径是否是绝对路径。

我对绝对路径使用的标准是它都包含驱动器号,并且路径以 \ 开头。在此条件下的绝对路径的一个示例是C:\path。这些都是此标准下相对路径的示例:C:path\path

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

云仙小弟 2024-08-21 07:34:58

似乎很难为此找到一个图书馆。一种可能性是 Winelib 中的 PathIsRelative,但我不想使用 Winelib。

我最终做了一个非常具体的解决方案,只是为了决定这个小事情。假设路径是正确的(在我的情况下是一个合理的假设),绝对路径将包含 :\,而相对路径则不会。

因此,糟糕但可行的解决方案是:没有合适的库。检查 :\ 是否存在。

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

晨光如昨 2024-08-21 07:34:58

QT 与 QFileInfo 怎么样?

What about QT with QFileInfo ?

横笛休吹塞上声 2024-08-21 07:34:58

您能否详细说明该程序对 Linux 上的 Windows 路径有何作用?

也许对 Windows 路径字符串应用一些简单的转换就足够了,在语法上将其转换为 Unix 路径,然后使用 boost::filesystem 来操作它。

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.

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