18 #include <QRegularExpression>
22 #include <QDirIterator>
27 list << QObject::tr(
"KB" ) << QObject::tr(
"MB" ) << QObject::tr(
"GB" ) << QObject::tr(
"TB" );
29 QStringListIterator i( list );
30 QString unit = QObject::tr(
"bytes" );
32 while ( bytes >= 1024.0 && i.hasNext() )
37 return QStringLiteral(
"%1 %2" ).arg( QString::number( bytes ), unit );
42 const QRegularExpression rx( QStringLiteral(
"\\*\\.([a-zA-Z0-9]+)" ) );
43 QStringList extensions;
44 QRegularExpressionMatchIterator matches = rx.globalMatch( filter );
46 while ( matches.hasNext() )
48 const QRegularExpressionMatch match = matches.next();
49 if ( match.hasMatch() )
51 QStringList newExtensions = match.capturedTexts();
52 newExtensions.pop_front();
53 extensions.append( newExtensions );
61 if ( extensions.empty() || f.isEmpty() )
66 for (
const QString &extension : qgis::as_const( extensions ) )
68 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
69 if ( fileName.endsWith( extWithDot, Qt::CaseInsensitive ) )
78 const QString extension = extensions.at( 0 );
79 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
80 fileName += extWithDot;
94 QRegularExpression rx( QStringLiteral(
"[/\\\\\\?%\\*\\:\\|\"<>]" ) );
96 s.replace( rx, QStringLiteral(
"_" ) );
102 if ( path.isEmpty() )
106 QFileInfo fi( path );
108 currentPath = fi.dir();
110 currentPath = QDir( path );
112 QSet< QString > visited;
113 while ( !currentPath.exists() )
115 const QString parentPath = QDir::cleanPath( currentPath.absolutePath() + QStringLiteral(
"/.." ) );
116 if ( visited.contains( parentPath ) )
119 if ( parentPath.isEmpty() || parentPath == QStringLiteral(
"." ) )
121 currentPath = QDir( parentPath );
122 visited << parentPath;
125 const QString res = QDir::cleanPath( currentPath.absolutePath() );
127 if ( res == QDir::currentPath() )
130 return res == QStringLiteral(
"." ) ? QString() : res;
133 QStringList
QgsFileUtils::findFile(
const QString &file,
const QString &basePath,
int maxClimbs,
int searchCeilling,
const QString ¤tDir )
136 QString originalFolder;
138 const QString fileName( basePath.isEmpty() ? QFileInfo( file ).fileName() : file );
139 const QString baseFolder( basePath.isEmpty() ? QFileInfo( file ).path() : basePath );
141 if ( QFileInfo( baseFolder ).isDir() )
143 folder = QDir( baseFolder ) ;
144 originalFolder = folder.absolutePath();
148 folder = QDir( QFileInfo( baseFolder ).absolutePath() );
149 originalFolder = folder.absolutePath();
152 QStringList searchedFolder = QStringList();
153 QString existingBase;
154 QString backupDirectory = QDir::currentPath();
155 QStringList foundFiles;
157 if ( !currentDir.isEmpty() && backupDirectory != currentDir && QDir( currentDir ).exists() )
158 QDir::setCurrent( currentDir );
161 while ( !folder.exists() && folder.absolutePath().count(
'/' ) > searchCeilling )
164 existingBase = folder.path();
165 if ( !folder.cdUp() )
166 folder = QFileInfo( existingBase ).absoluteDir();
170 if ( depth > ( maxClimbs + 4 ) )
173 bool folderExists = folder.exists();
175 if ( depth > maxClimbs )
178 if ( folder.absolutePath().count(
'/' ) < searchCeilling )
179 searchCeilling = folder.absolutePath().count(
'/' ) - 1;
181 while ( depth <= maxClimbs && folderExists && folder.absolutePath().count(
'/' ) >= searchCeilling )
184 QDirIterator localFinder( folder.path(), QStringList() << fileName, QDir::Files, QDirIterator::NoIteratorFlags );
185 searchedFolder.append( folder.absolutePath() );
186 if ( localFinder.hasNext() )
188 foundFiles << localFinder.next();
193 const QFileInfoList subdirs = folder.entryInfoList( QDir::AllDirs );
194 for (
const QFileInfo subdir : subdirs )
196 if ( ! searchedFolder.contains( subdir.absolutePath() ) )
198 QDirIterator subDirFinder( subdir.path(), QStringList() << fileName, QDir::Files, QDirIterator::Subdirectories );
199 if ( subDirFinder.hasNext() )
201 QString possibleFile = subDirFinder.next();
202 if ( !subDirFinder.hasNext() )
204 foundFiles << possibleFile;
208 foundFiles << possibleFile;
209 while ( subDirFinder.hasNext() )
211 foundFiles << subDirFinder.next();
219 if ( depth > maxClimbs )
222 folderExists = folder.cdUp();
225 if ( QDir::currentPath() == currentDir && currentDir != backupDirectory )
226 QDir::setCurrent( backupDirectory );