views:

118

answers:

5

Hi,

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.

A: 

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.

JimDaniel
+1  A: 

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 http://stackoverflow.com/questions/62771/how-check-if-given-string-is-legal-allowed-file-name-under-windows

Martin Beckett
+1 for the `com` special case. I didn't think of that one.
ereOn
A: 

As Jim already wrote, there's no such function - you'll have to write your own.

humbagumba
+1  A: 

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.

Josh Kelley
`boost` is definitely an option and that is just perfect ! Thank you very much !
ereOn
Note boost doesn't check for invalid names (on win) such as com,lpt,aux etc - for those who don't remember DOS days.
Martin Beckett
+1  A: 

This is the answer I got from Silje Johansen - Support Engineer - Trolltech ASA (in March 2008 though)

However. the complexity of including locale settings and finding a unified way to query the filesystems on Linux/Unix about their functionality is close to impossible.

However, to my knowledge, all applications I know of ignore this problem.

(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.

  1. Use defines to make OS specific sections. (Qt can help you with this)
  2. Use the advice given on the boost page and OS and filesystem documentation to decide on your illegal characters
  3. For path length the only workable idea that springs to my mind is a binary tree trial an error approach using the system call's error handling to check on a valid path length. This is quite aloof, but might be the only possibility of getting accurate results on a variety of systems.
  4. Get good at elegant error handling.

Hope this has given some insights.

ufotds
Thanks, very instructive and well written answer. Deserves my +1.
ereOn
Hey, thank you, that makes it my first upvote! By now I even have enough reputation to clean up the links and upvote your question... Let's dream one day a good library will emerge to tackle this very common problem.
ufotds