[Interest] Handling of ~ paths

Thiago Macieira thiago.macieira at intel.com
Fri Jun 12 06:28:41 CEST 2020


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





More information about the Interest mailing list