Re: [Interest] Handling of ~ paths
On Thursday, 11 June 2020 11:03:00 PDT Scott Bloom wrote: > If you are working with a path, for use in QDir, QFileInfo, QFile etc etc, > and the path string is using a ~, either of the form ~/foo.txt or > ~user/foo.txt, Qt seems to be treating it as a relative path of the current > user, and prepends “/home/scott” in my case to the path, and of course, the > canonicalPath returns an empty string because that file path is invalid and > doesn’t exist. > What is the Qt way to handle this? It cant be to tell your users not to use > ~. Can it? Does everyone write their own little “analyze the string before > actually using it, and handle ~”? There isn't a Qt way to handle that. The tilde is not a special character in any way, any more than the dollar sign. The only special character in paths on Unix systems is the slash. You're mistaking them with the shell parsing of a command line. There are a lot of special characters there. Among others: - # starts a comment - $ is used to denote environment variable expansion - backslashes escape the next character - backticks start a process substitutios - angle brackets denote redirects - the vertical bar is a pipe - exclamation mark does history expansion - * and ? are wildcards - tilde are used in filename expansion to mean a user's home dir If you want to handle some or all of that, you need to interpret the input yourself (or use one of the KDE Frameworks 5 libraries). There are probably existing implementations that do the filename expansion part but not the rest of the shell interpretation. Note that interpreting any of them implies handling backslashes too, at a minimum. -- Thiago Macieira - thiago.macieira (AT) intel.com Software Architect - Intel System Software Products ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Handling of ~ paths
-Original Message- From: Matthew Woehlke Sent: Thursday, June 11, 2020 11:43 AM To: Scott Bloom ; interest@qt-project.org Subject: Re: [Interest] Handling of ~ paths On 11/06/2020 14.33, Scott Bloom wrote: > On 11/06/2020 14.24, Matthew Woehlke wrote: >> On 11/06/2020 14.03, > Scott Bloom wrote:>>> If you are working with a path, for use in QDir, QFileInfo, QFile etc>>> etc, and the path string is using a ~, either of the form ~/foo.txt or>>> ~user/foo.txt, Qt seems to be treating it as a relative path of the>>> current user, and prepends “/home/scott” in my case to the path,>> >> Uh... yeah? A path starting with "~/" or "~/" has been "shorthand">> for that user's home directory ("~/" → current user) for decades.> > Maybe I wasn’t clear, that is 100% exactly what I would expect. > > But instead what is returned is "/home/scott/~/foo.txt" Ah, I see, what you mean is that *the current working directory* is appended. (Which in your example is $HOME, which confused me, since replacing ~ with $HOME is expected.) Yeah... well, the bad news is that tilde expansion (not unlike variable expansion) is usually a shell function and/or needs to be invoked manually, rather than an OS function. It appears that QFileDialog also does the expansion, but if your path comes from another source, I guess you need to do it yourself. So you are basically asking the same as https://stackoverflow.com/questions/1833261/qt-expand-to-home-directory. Unfortunately, the answer there does not appear relevant. -- Yes. I had found that, and the answer was at "best" irrelevant. Sorry to the confusion for the prefix, yes, I should have said /current/working/dir/~/foo.txt Scott ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Handling of ~ paths
On 11/06/2020 14.33, Scott Bloom wrote: On 11/06/2020 14.24, Matthew Woehlke wrote: >> On 11/06/2020 14.03, Scott Bloom wrote:>>> If you are working with a path, for use in QDir, QFileInfo, QFile etc>>> etc, and the path string is using a ~, either of the form ~/foo.txt or>>> ~user/foo.txt, Qt seems to be treating it as a relative path of the>>> current user, and prepends “/home/scott” in my case to the path,>> >> Uh... yeah? A path starting with "~/" or "~/" has been "shorthand">> for that user's home directory ("~/" → current user) for decades.> > Maybe I wasn’t clear, that is 100% exactly what I would expect. But instead what is returned is "/home/scott/~/foo.txt" Ah, I see, what you mean is that *the current working directory* is appended. (Which in your example is $HOME, which confused me, since replacing ~ with $HOME is expected.) Yeah... well, the bad news is that tilde expansion (not unlike variable expansion) is usually a shell function and/or needs to be invoked manually, rather than an OS function. It appears that QFileDialog also does the expansion, but if your path comes from another source, I guess you need to do it yourself. So you are basically asking the same as https://stackoverflow.com/questions/1833261/qt-expand-to-home-directory. Unfortunately, the answer there does not appear relevant. -- Matthew ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Handling of ~ paths
On 11/06/2020 14.03, Scott Bloom wrote: > If you are working with a path, for use in QDir, QFileInfo, QFile etc > etc, and the path string is using a ~, either of the form ~/foo.txt or > ~user/foo.txt, Qt seems to be treating it as a relative path of the > current user, and prepends “/home/scott” in my case to the path, Uh... yeah? A path starting with "~/" or "~/" has been "shorthand" for that user's home directory ("~/" → current user) for decades. I'm not sure what you were expecting? $ cd / $ ls -d ~ /home/matthew === Maybe I wasn’t clear, that is 100% exactly what I would expect. User entry, ~/foo.txt, I would 100% expect QFileInfo( "~/foo.txt" ).absoluteFilePath() to return "/home/scott/foo.txt" But instead what is returned is "/home/scott/~/foo.txt" > and of course, the canonicalPath returns an empty string because that > file path is invalid and doesn’t exist. Why is it invalid? Did you somehow manage to have your home directory not exist? = See above, the returned path is /home/scott/~/foo.txt > What is the Qt way to handle this? It cant be to tell your users not > to use ~. Can it? If your intention is to treat "~/foo" as a relative path, the first component of which is literally "~", then... yeah, don't do that; that isn't the "traditional" interpretation of such a path. If that's what you want, use "./~/foo" instead. (If you just need to suppress tilde expansion, you may be able to check for paths that start with "~" and always add "./" to the beginning of them. Beware, however, that users that expect tilde expansion to work may be confused or annoyed.) -- == My problem is QFileInfo, QDir and QFile do not appear to be doing the ~ expansion Scott ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Handling of ~ paths
On 11/06/2020 14.03, Scott Bloom wrote: If you are working with a path, for use in QDir, QFileInfo, QFile etc etc, and the path string is using a ~, either of the form ~/foo.txt or ~user/foo.txt, Qt seems to be treating it as a relative path of the current user, and prepends “/home/scott” in my case to the path, Uh... yeah? A path starting with "~/" or "~/" has been "shorthand" for that user's home directory ("~/" → current user) for decades. I'm not sure what you were expecting? $ cd / $ ls -d ~ /home/matthew and of course, the canonicalPath returns an empty string because that file path is invalid and doesn’t exist. Why is it invalid? Did you somehow manage to have your home directory not exist? What is the Qt way to handle this? It cant be to tell your users not to use ~. Can it? If your intention is to treat "~/foo" as a relative path, the first component of which is literally "~", then... yeah, don't do that; that isn't the "traditional" interpretation of such a path. If that's what you want, use "./~/foo" instead. (If you just need to suppress tilde expansion, you may be able to check for paths that start with "~" and always add "./" to the beginning of them. Beware, however, that users that expect tilde expansion to work may be confused or annoyed.) -- Matthew ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] Handling of ~ paths
On 6/11/20 8:03 PM, Scott Bloom wrote: What is the Qt way to handle this? It cant be to tell your users not to use ~. Can it? Does everyone write their own little “analyze the string before actually using it, and handle ~”? How are users entering these paths to start with? Cheers, -- Giuseppe D'Angelo | giuseppe.dang...@kdab.com | Senior Software Engineer KDAB (France) S.A.S., a KDAB Group company Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com KDAB - The Qt, C++ and OpenGL Experts smime.p7s Description: S/MIME Cryptographic Signature ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
[Interest] Handling of ~ paths
I’m feeling really stupid right now Ive been a Qt user for almost 20 years, and I cant believe I have never had to deal with this before. If you are working with a path, for use in QDir, QFileInfo, QFile etc etc, and the path string is using a ~, either of the form ~/foo.txt or ~user/foo.txt, Qt seems to be treating it as a relative path of the current user, and prepends “/home/scott” in my case to the path, and of course, the canonicalPath returns an empty string because that file path is invalid and doesn’t exist. What is the Qt way to handle this? It cant be to tell your users not to use ~. Can it? Does everyone write their own little “analyze the string before actually using it, and handle ~”? Thanks Scott ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest