PDA

View Full Version : file name comparison



mentalmushroom
15th March 2011, 15:05
What is the best way to compare file names in Qt? Files may not exist at that moment.

high_flyer
15th March 2011, 15:27
Can you explain a bit more?
Without explanation, it sounds you have to filenames, which are two strings.
QString has a comparison operator which you can use.
But I guess you mean something else, but I can't guess what.

mentalmushroom
15th March 2011, 16:30
Yes, I have two strings that need to be compared. Depending on what file system is used file names may be case sensitive or case insensitive, so I want to find a platform-independent way to check whether two strings represent the same file name.

wysota
15th March 2011, 16:37
It is not possible to determine if a particular path is case-sensitive or not in a reliable way. You can try a heuristic that takes an existing file name, changes its case and uses QFile::exists() to check if the filesystem treats the two names as the same file. Then you can use QString::compare() variant that takes a second parameter determining whether the check should be case-sensitive or not.

high_flyer
15th March 2011, 17:14
Depending on what file system is used file names may be case sensitive or case insensitive
AFIK, this is not true.
Let me explain:
Can you have two files, having the same name just differing in case in the same folder under linux? (I can't test at the moment but I think you can't)
Under windows you can't.
So I don't think a situation can occur where two different files differ only in their name case (one being small the other large capital letters or a combination) and are in the same folder .
Therefore, if my assumptions are correct, it should be safe to compare with out case sensitive comparison.

nightghost
15th March 2011, 17:20
AFIK, this is not true.
Let me explain:
Can you have two files, having the same name just differing in case in the same folder under linux? (I can't test at the moment but I think you can't)
Under windows you can't.


According to Microsoft (http://support.microsoft.com/kb/100625) NTFS supports case sensitive file names.

mentalmushroom
15th March 2011, 19:04
You can try a heuristic that takes an existing file name, changes its case and uses QFile::exists() to check if the filesystem treats the two names as the same file.
But as I said before file doesn't exist at the moment of comparison, therefore QFile::exists will not work, I suppose.

squidge
15th March 2011, 20:14
You can not do it easily because you do not know the underlying file system. Regardless of OS, it is the file system you need to be worried about. On both Windows and Linux, you can have file systems which are case sensitive and case-insensitive. An application can not easily detect the file system.

wysota
15th March 2011, 21:27
Can you have two files, having the same name just differing in case in the same folder under linux? (I can't test at the moment but I think you can't)
Yes, of course. But this is not related to the operating system but rather to the file system.

$ ll
razem 1
drwxr-xr-x 2 wysota wysota 96 2011-03-15 21:25 ./
drwxrwxrwt 24 root root 1336 2011-03-15 21:25 ../
-rw-r--r-- 1 wysota wysota 0 2011-03-15 21:25 file
-rw-r--r-- 1 wysota wysota 0 2011-03-15 21:25 File

Under windows you can't.
Sure you can if you attach a file system that is case sensitive.

I still say the only plausible way of detecting if the file system is case sensitive or not is to try to create (or just reference) two files where the case differs and see if it succeeds or not. This is not foul proof as the user may have access rights to create one file but not the other but this is something to start with.

high_flyer
15th March 2011, 21:40
Sure you can if you attach a file system that is case sensitive.
Yeap. I spoke to soon.

squidge
15th March 2011, 23:54
I still say the only plausible way of detecting if the file system is case sensitive or not is to try to create (or just reference) two files where the case differs and see if it succeeds or not. This is not foul proof as the user may have access rights to create one file but not the other but this is something to start with.Also, it's possible on some file systems (eg. NTFS) to run in two different modes of operation - "case preserving but not case sensitive" and "fully case sensitive". It can even be that the default is the former but the later is possible by passing FILE_FLAG_POSIX_SEMANTICS to a CreateFile API call. By using this flag you can create files which can not be seen by 'normal' applications (they will only be able to open one of the filenames regardless of case). However, you can tell the OS to ignore this flag via registry settings...

exists() would return true based on the default value, which is the expected behavior. Since the file system is unknown, it is the only way of doing an decent (but not 100%) compare.