'lightweight' QDir::isAbsolutePath replacement ?

Jarosław Staniek js at iidea.pl
Thu Mar 6 15:51:09 CET 2008


Ralf Habacker said the following, On 2008-03-06 13:42:
> 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)

I remember some stuff like that in KPath for KDE3.1/win32 :)
No idea why I cannot find one now :)

Anyway to avoid assers, the function should also have path.length() >= 2 check.

For QString I would even use code like this:

<code>
if ( path.length() < 1 )
	return false;

ushort first = path.constData()[0].unicode();

if ( first == ushort('/') )
	return true;

return path.length() >= 2
	&& path.constData()[1].unicode() == ushort(':')
	&&
	( ( first >= ushort('a') && first <= ushort('z') )
	|| ( first >= ushort('A') && first <= ushort('Z') )
	);

</code>

Re naming, the KDE:: functions look OK.

-- 
regards / pozdrawiam, Jaroslaw Staniek
  Sponsored by OpenOffice Polska (http://www.openoffice.com.pl/en) to work on
  Kexi & KOffice (http://www.kexi.pl/en, http://www.koffice.org/kexi)
  KDE Libraries for MS Windows (http://windows.kde.org)



More information about the Kde-windows mailing list