使用 Qt 检查字符串是否是有效的文件名
Qt 4.6 有没有办法检查给定的 QString 是否是当前操作系统上的有效文件名(或目录名)?我想检查名称是否有效,而不是文件是否存在。
示例:
// Some valid names
test
under_score
.dotted-name
// Some specific names
colon:name // valid under UNIX OSes, but not on Windows
what? // valid under UNIX OSes, but still not on Windows
我将如何实现这一目标?是否有一些Qt
内置函数?
我想避免创建空文件,但如果没有其他可靠的方法,我仍然想看看如何以“干净”的方式做到这一点。
非常感谢。
Is there a way with Qt 4.6
to check if a given QString
is a valid filename (or directory name) on the current operating system ? I want to check for the name to be valid, not for the file to exist.
Examples:
// Some valid names
test
under_score
.dotted-name
// Some specific names
colon:name // valid under UNIX OSes, but not on Windows
what? // valid under UNIX OSes, but still not on Windows
How would I achieve this ? Is there some Qt
built-in function ?
I'd like to avoid creating an empty file, but if there is no other reliable way, I would still like to see how to do it in a "clean" way.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我从 Trolltech ASA 支持工程师 Silje Johansen 那里得到的答案(不过是在 2008 年 3 月)
(阅读:他们不会实现它)
Boost 也没有解决问题,他们只给出了路径最大长度的一些模糊概念,特别是如果你想跨平台的话。据我所知,很多人都尝试过解决这个问题,但都失败了(至少在理论上,在实践中,绝对有可能编写一个在大多数情况下创建有效文件名的程序。
如果你想自己实现这一点,可能值得考虑一些不是立即显而易见的事情,例如:
无效字符的并发症
文件系统限制与操作系统和软件限制之间的区别,我认为这是文件系统限制的一部分。例如,Windows 操作系统不完全支持 NTFS。包含“:”和“?”等的文件可以愉快地驻留在 ntfs 分区上,但资源管理器只会卡住它们。除此之外,您可以安全地使用它们。来自 Boost 文件系统的建议
。路径长度
升压页面没有完全解决的第二个问题是完整路径的长度,目前唯一确定的是没有操作系统/文件系统组合支持不确定的路径长度。但是,诸如“Windows 最大路径限制为 260 个字符”之类的说法是错误的。 Windows 中的 unicode API 允许您创建最长 32,767 个 utf-16 字符的路径。我没有检查过,但我想象资源管理器同样会窒息,这将使该功能对于除您之外的任何用户的软件完全无用(另一方面,您可能不希望让您的软件齐声窒息)。
存在一个名为 PATH_MAX 的旧变量,这听起来很有希望,但问题是 PATH_MAX 根本就不是。
最后,以建设性的意见结束,这里有一些关于编写解决方案的可能方法的想法。
希望这能提供一些见解。
This is the answer I got from Silje Johansen - Support Engineer - Trolltech ASA (in March 2008 though)
(read: they aren't going to implement it)
Boost doesn't solve the problem either, they give only some vague notion of the maximum length of paths, especially if you want to be cross platform. As far as I know many have tried and failed to crack this problem (at least in theory, in practice it is most definitely possible to write a program that creates valid filenames in most cases.
If you want to implement this yourself, it might be worth considering a few not immediately obvious things such as:
Complications with invalid characters
The difference between file system limitations and OS and software limitations. Windows Explorer, which I consider part of the Windows OS does not fully support NTFS for example. Files containing ':' and '?', etc... can happily reside on an ntfs partition, but Explorer just chokes on them. Other than that, you can play safe and use the recommendations from Boost Filesystem.
Complications with path length
The second problem not fully tackled by the boost page is length of the full path. Probably the only thing that is certain at this moment is that no OS/filesystem combination supports indefinite path lengths. However, statements like "Windows maximum paths are limited to 260 chars" are wrong. The unicode API from Windows does allow you to create paths up to 32,767 utf-16 characters long. I haven't checked, but I imagine Explorer choking equally devoted, which would make this feature utterly useless for software having any users other than yourself (on the other hand you might prefer not to have your software choke in chorus).
There exists an old variable that goes by the name of PATH_MAX, which sounds promising, but the problem is that PATH_MAX simply isn't.
To end with a constructive note, here are some ideas on possible ways to code a solution.
Hope this has given some insights.
基于 User7116 的回答:
How do我检查给定的字符串在 Windows 下是否是合法/有效的文件名?
我不再偷懒了 - 寻找优雅的解决方案,然后对其进行编码。我得到:
功能:
注意,它不会检查以 \\? 开头的文件名的长度,因为这不是一个硬性规定。另请注意,正如此处指出的,名称包含多个反斜杠win32 API 不会拒绝正斜杠。
Based on User7116's answer here:
How do I check if a given string is a legal/valid file name under Windows?
I quit being lazy - looking for elegant solutions, and just coded it. I got:
Features:
Note that it does not check the length of filenames starting with \\?, since that is not a hard and fast rule. Also note, as pointed out here, names containing multiple backslashes and forward slashes are NOT rejected by the win32 API.
我不认为Qt有内置函数,但是如果Boost是一个选项,你可以使用Boost.Filesystem的 name_check 函数。
如果 Boost 不是一个选项,它的 name_check 函数页面仍然很好地概述了在各种平台上检查的内容。
I don't think that Qt has a built-in function, but if Boost is an option, you can use Boost.Filesystem's name_check functions.
If Boost isn't an option, its page on name_check functions is still a good overview of what to check for on various platforms.
在 Windows 上很难可靠地完成(一些奇怪的事情,例如名为“com”的文件仍然无效),并且您是否想要处理 unicode,或者使用 subst 技巧来允许大于 260 个字符的文件名。
这里已经有一个很好的答案 如何如何检查给定的字符串在 Windows 下是否是合法/有效的文件名?
Difficult to do reliably on windows (some odd things such as a file named "com" still being invalid) and do you want to handle unicode, or subst tricks to allow a >260 char filename.
There is already a good answer here How do I check if a given string is a legal / valid file name under Windows?
请参阅示例(来自 Digia Qt Creator 源): https://qt.gitorious.org/qt-creator/qt-creator/source/4df7656394bc63088f67a0bae8733f400671d1b6:src/libs/utils/filenamevalidatinglineedit.cpp
see example (from Digia Qt Creator sources) in: https://qt.gitorious.org/qt-creator/qt-creator/source/4df7656394bc63088f67a0bae8733f400671d1b6:src/libs/utils/filenamevalidatinglineedit.cpp
我只是创建一个简单的函数来验证平台的文件名,该函数只是在字符串中搜索任何无效字符。不要以为Qt中有内置函数。您可以在函数内使用#ifdefs 来确定您所在的平台。我想说足够干净。
I'd just create a simple function to validate the filename for the platform, which just searches through the string for any invalid characters. Don't think there's a built-in function in Qt. You could use #ifdefs inside the function to determine what platform you're on. Clean enough I'd say.