'lightweight' QDir::isAbsolutePath replacement ?

Ralf Habacker ralf.habacker at freenet.de
Thu Mar 6 21:12:54 CET 2008


Ralf Habacker schrieb:
> Ralf Habacker schrieb:
>   
>> 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.
>>     
> One little correction: I fact QDir::isAbsolutePath() creates a QFileInfo 
> instance instead of QDir but the problems still remains.
>
> qdir.h
>     static bool isRelativePath(const QString &path);
>     inline static bool isAbsolutePath(const QString &path) { return 
> !isRelativePath(path); }
>
> qdir.cpp
> bool QDir::isRelativePath(const QString &path)
> {
>     return QFileInfo(path).isRelative();
> }
>
>   
the real check is done in qt-copy/src/corelib/io/qfsfileengine_win.cpp 
(with the knowledge that isAbsolutePath() is !isRelativePath())

bool QFSFileEngine::isRelativePath() const
{
    Q_D(const QFSFileEngine);
    return !(d->filePath.startsWith(QLatin1Char('/'))
        || (d->filePath.length() >= 2
        && ((d->filePath.at(0).isLetter() && d->filePath.at(1) == 
QLatin1Char(':'))
        || (d->filePath.at(0) == QLatin1Char('/') && d->filePath.at(1) 
== QLatin1Char('/')))));                // drive, e.g. a:
}

which requires a QFSFileEnginePrivate and QFSFileEngine and QFileInfo 
object which explains the performance issue.

In the above mentioned file there is also a static function with the 
same result

static bool isRelativePath(const QString &path)
{
    return !(path.startsWith(QLatin1Char('/'))
           || (path.length() >= 2
           && ((path.at(0).isLetter() && path.at(1) == QLatin1Char(':'))
           || (path.at(0) == QLatin1Char('/') && path.at(1) == 
QLatin1Char('/'))))); // drive, e.g. a:
}

Would it be possible to add something like the following stuff  ?

---- qt-copy/src/corelib/io/qfsfileengine.h ----

static inline bool QFSFileEngine::isAbsolutePath(const QString &path) { 
return !QFSFileEngine::isRelativePath(path); }
static bool QFSFileEngine::isRelativePath(const QString &path);

---- qt-copy/src/corelib/io/qfsfileengine_unix.cpp ----

static bool QFSFileEngine::isRelativePath(const QString &path)
{
    if (path.size() == 0)
       return true;
    return path[0] != QLatin1Char('/');
}

---- in qt-copy/src/corelib/io/qfsfileengine_win.cpp

static bool QFSFileEngine::isRelativePath(const QString &path)
{
    return !(path.startsWith(QLatin1Char('/'))
           || path.startsWith(QLatin1Char('\\'))
           || (path.length() >= 2 &&
            ( (path.at(0).isLetter() && path.at(1) == QLatin1Char(':'))
           || (path.at(0) == QLatin1Char('/') && path.at(1) == 
QLatin1Char('/'))
           || (path.at(0) == QLatin1Char('\\') && path.at(1) == 
QLatin1Char('\\')) )) );
}

---- in qt-copy/src/corelib/io/qfileinfo.h ----
 
static inline bool QFileInfo::isAbsolutePath(const QString &path) { 
return !QFileInfo::isRelativePath(path); }
static bool QFileInfo::isRelativePath(const QString &path);

---- in qt-copy/src/corelib/io/qfileinfo.cpp -----

static bool QFileInfo::isRelativePath(const QString &path)
{
    return QFSFileEngine::isRelativePath(path);
}

Because QFSFileEngine is a public class it may be possible to implement 
only the QFSFileEngine static methods and to skip the QFileInfo stuff.

If this would be possible I can prepare a patch.

Ralf



More information about the Kde-windows mailing list