Results 1 to 11 of 11

Thread: file name comparison

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default file name comparison

    What is the best way to compare file names in Qt? Files may not exist at that moment.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: file name comparison

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: file name comparison

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: file name comparison

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: file name comparison

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: file name comparison

    Quote Originally Posted by high_flyer View Post
    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 NTFS supports case sensitive file names.

  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: file name comparison

    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.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: file name comparison

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: file name comparison

    Quote Originally Posted by high_flyer View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: file name comparison

    Sure you can if you attach a file system that is case sensitive.
    Yeap. I spoke to soon.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: file name comparison

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. QHash add comparison function, how?
    By holst in forum Qt Programming
    Replies: 9
    Last Post: 30th March 2010, 19:49
  2. SQL Select statement with binary comparison
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2009, 02:33
  3. comparison of two pixmap images
    By sar_van81 in forum Qt Programming
    Replies: 5
    Last Post: 18th September 2007, 11:35
  4. String comparison
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2006, 12:31
  5. case insensitive comparison.
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th May 2006, 16:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.