'lightweight' QDir::isAbsolutePath replacement ?
Ralf Habacker
ralf.habacker at freenet.de
Thu Mar 6 12:42:08 GMT 2008
Hi,
there are several places in the kde code which needs a check if a path
is absolute. On unix this is mainly done by using
QString::startsWith('/'), which does not work as expected on windows.
The only static method Qt provides to handle this platform independent
is QDir::isAbsolutePath(). Unfortunally this method seems to be very
time expensive because it creates a temporary QDir instance.
A second Qt provided way is QFileInfo::isAbsolute(), although this
method is non static and requires to create a QFileInfo instance before
which blows up code unnecessarily.
the third option is to check this directly
QString path = .... ;
if ( (path.startsWith(QLatin1Char('/')) || (path[0].isLetter() &&
path[1] == QLatin1Char(':')) )
// handle absolute Path
an additional imaginable way would be to add kde only static functions
for this purpose in the KDE namespace. For example
KDE::isAbsolutePath(const QString &path)
KDE::isAbsolutePath(const QByteArray &path)
KDE::isAbsolutePath(const KUrl &url)
....
with an implementation similar to:
return (path.startsWith(QLatin1Char('/')) || (path.[0].isLetter() &&
path[1] == QLatin1Char(':')) ) ;
The question is which way the prefered is ?
Ralf
More information about the kde-core-devel
mailing list