Re: [Interest] Detecting subdirectories in QDir

2018-07-12 Thread Mårten Nordheim

Does it work properly if 'b' is root?

On 11.07.2018 17:59, Oleg Yadrov wrote:

Ha. I hope I didn't screw up this time.

#include

#include

#include


//returnstrueif'a'isasubdirectoryof'b'

boolisSubdirectory(QDira,QDirb)

{

while(!a.isRoot()){

if(a.absolutePath()==b.absolutePath())

returntrue;

a.cdUp();

}

returnfalse;

}


intmain(intargc,char*argv[])

{

QCoreApplicationapp(argc,argv);


QDira("/Users/olegyadrov/one/two/three");

QDirb("/Users/olegyadrov/one/two");

QDirc("/Users/olegyadrov/one/four");


qDebug()> wrote:


On Wednesday, 11 July 2018 07:44:46 PDT Oleg Yadrov wrote:

   qDebug() << a.absolutePath().startsWith(b.absolutePath());

   qDebug() << a.absolutePath().startsWith(c.absolutePath());


Except that this has a bug (which we had in Qt too): these two 
directories are

considered to be parent-child, when they're not

C:\One\TwoAndTwenty
C:\One\Two

And since we're talking about Windows, you also need to use 
case-insensitive

comparison.
--
Thiago Macieira - thiago.macieira (AT) intel.com 
 Software Architect - Intel Open Source Technology Center



___
Interest mailing list
Interest@qt-project.org 
http://lists.qt-project.org/mailman/listinfo/interest




___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Detecting subdirectories in QDir

2018-07-11 Thread Thiago Macieira
On Wednesday, 11 July 2018 07:59:28 PDT Thiago Macieira wrote:
> On Wednesday, 11 July 2018 07:44:46 PDT Oleg Yadrov wrote:
> > qDebug() << a.absolutePath().startsWith(b.absolutePath());
> > 
> > qDebug() << a.absolutePath().startsWith(c.absolutePath());
> 
> Except that this has a bug (which we had in Qt too): these two directories
> are considered to be parent-child, when they're not
> 
>   C:\One\TwoAndTwenty
>   C:\One\Two
> 
> And since we're talking about Windows, you also need to use case-insensitive
> comparison.

Because of these gotchas, it's probably worth having a QDir::isParentOf() 
function.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center



___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Detecting subdirectories in QDir

2018-07-11 Thread Oleg Yadrov
Ha. I hope I didn't screw up this time.


#include 

#include 

#include 


// returns true if 'a' is a subdirectory of 'b'

bool isSubdirectory(QDir a, QDir b)

{

while (!a.isRoot()) {

if (a.absolutePath() == b.absolutePath())

return true;

a.cdUp();

}

return false;

}


int main(int argc, char *argv[])

{

QCoreApplication app(argc, argv);


QDir a("/Users/olegyadrov/one/two/three");

QDir b("/Users/olegyadrov/one/two");

QDir c("/Users/olegyadrov/one/four");


qDebug() << isSubdirectory(a, b);

qDebug() << isSubdirectory(a, c);


return app.exec();

}

On Jul 11, 2018, at 10:59 AM, Thiago Macieira 
mailto:thiago.macie...@intel.com>> wrote:

On Wednesday, 11 July 2018 07:44:46 PDT Oleg Yadrov wrote:
   qDebug() << a.absolutePath().startsWith(b.absolutePath());

   qDebug() << a.absolutePath().startsWith(c.absolutePath());

Except that this has a bug (which we had in Qt too): these two directories are
considered to be parent-child, when they're not

C:\One\TwoAndTwenty
C:\One\Two

And since we're talking about Windows, you also need to use case-insensitive
comparison.
--
Thiago Macieira - thiago.macieira (AT) intel.com
 Software Architect - Intel Open Source Technology Center



___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Detecting subdirectories in QDir

2018-07-11 Thread Thiago Macieira
On Wednesday, 11 July 2018 07:44:46 PDT Oleg Yadrov wrote:
> qDebug() << a.absolutePath().startsWith(b.absolutePath());
> 
> qDebug() << a.absolutePath().startsWith(c.absolutePath());

Except that this has a bug (which we had in Qt too): these two directories are 
considered to be parent-child, when they're not

C:\One\TwoAndTwenty
C:\One\Two

And since we're talking about Windows, you also need to use case-insensitive 
comparison.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center



___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Detecting subdirectories in QDir

2018-07-11 Thread Oleg Yadrov
Hi there,

You can compare their absolute paths.


#include 

#include 

#include 


int main(int argc, char *argv[])

{

QCoreApplication app(argc, argv);


QDir a("/Users/olegyadrov/one/two/three");

QDir b("/Users/olegyadrov/one/two");

QDir c("/Users/olegyadrov/one/four");


qDebug() << a.absolutePath().startsWith(b.absolutePath());

qDebug() << a.absolutePath().startsWith(c.absolutePath());


return app.exec();

}


outputs

true
false

Best regards,
Oleg Yadrov - oleg.yad...@qt.io

On Jul 11, 2018, at 5:04 AM, Tom Isaacson via Interest 
mailto:interest@qt-project.org>> wrote:

Is there any way of finding if one QDir is a subdirectory of another? So for 
instance:
C:\One\Two\Three
is a subdirectory of:
C:\One\Two
but not of:
C:\One\Four

Thanks.

Tom Isaacson

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest