QGIS API Documentation  3.0.2-Girona (307d082)
qgsfileutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfileutils.cpp
3  ---------------------
4  begin : November 2017
5  copyright : (C) 2017 by Etienne Trimaille
6  email : etienne.trimaille at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsfileutils.h"
16 #include <QObject>
17 #include <QRegularExpression>
18 
19 QString QgsFileUtils::representFileSize( qint64 bytes )
20 {
21  QStringList list;
22  list << QObject::tr( "KB" ) << QObject::tr( "MB" ) << QObject::tr( "GB" ) << QObject::tr( "TB" );
23 
24  QStringListIterator i( list );
25  QString unit = QObject::tr( "bytes" );
26 
27  while ( bytes >= 1024.0 && i.hasNext() )
28  {
29  unit = i.next();
30  bytes /= 1024.0;
31  }
32  return QString( "%1 %2" ).arg( QString::number( bytes ), unit );
33 }
34 
35 QStringList QgsFileUtils::extensionsFromFilter( const QString &filter )
36 {
37  const QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]+)" ) );
38  QStringList extensions;
39  QRegularExpressionMatchIterator matches = rx.globalMatch( filter );
40 
41  while ( matches.hasNext() )
42  {
43  const QRegularExpressionMatch match = matches.next();
44  if ( match.hasMatch() )
45  {
46  QStringList newExtensions = match.capturedTexts();
47  newExtensions.pop_front(); // remove whole match
48  extensions.append( newExtensions );
49  }
50  }
51  return extensions;
52 }
53 
54 QString QgsFileUtils::ensureFileNameHasExtension( const QString &f, const QStringList &extensions )
55 {
56  if ( extensions.empty() || f.isEmpty() )
57  return f;
58 
59  QString fileName = f;
60  bool hasExt = false;
61  for ( const QString &extension : qgis::as_const( extensions ) )
62  {
63  const QString extWithDot = extension.startsWith( '.' ) ? extension : '.' + extension;
64  if ( fileName.endsWith( extWithDot, Qt::CaseInsensitive ) )
65  {
66  hasExt = true;
67  break;
68  }
69  }
70 
71  if ( !hasExt )
72  {
73  const QString extension = extensions.at( 0 );
74  const QString extWithDot = extension.startsWith( '.' ) ? extension : '.' + extension;
75  fileName += extWithDot;
76  }
77 
78  return fileName;
79 }
80 
81 QString QgsFileUtils::addExtensionFromFilter( const QString &fileName, const QString &filter )
82 {
83  const QStringList extensions = extensionsFromFilter( filter );
84  return ensureFileNameHasExtension( fileName, extensions );
85 }
86 
87 QString QgsFileUtils::stringToSafeFilename( const QString &string )
88 {
89  QRegularExpression rx( "[/\\\\\\?%\\*\\:\\|\"<>]" );
90  QString s = string;
91  s.replace( rx, QStringLiteral( "_" ) );
92  return s;
93 }
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QString representFileSize(qint64 bytes)
Return the human size from bytes.
static QString stringToSafeFilename(const QString &string)
Converts a string to a safe filename, replacing characters which are not safe for filenames with an &#39;...
static QStringList extensionsFromFilter(const QString &filter)
Returns a list of the extensions contained within a file filter string.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.