fileutils.cpp
1 //------------------------------------------------------------------------------
2 // fileutils.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "fileutils.h"
24 
25 #include <QCryptographicHash>
26 #include <QDirIterator>
27 #include <QFileInfo>
28 #include "log.h"
29 
30 QByteArray FileUtils::md5(const QString &path)
31 {
32  QFile f(path);
33  if (f.open(QIODevice::ReadOnly))
34  {
35  QCryptographicHash hash(QCryptographicHash::Md5);
36  QByteArray chunk = f.read(1024 * 1024);
37  for (; !chunk.isEmpty(); chunk = f.read(1024 * 1024))
38  {
39  hash.addData(chunk);
40  }
41  f.close();
42  return hash.result();
43  }
44  return QByteArray();
45 }
46 
47 Qt::CaseSensitivity FileUtils::comparisonSensitivity()
48 {
49 #if defined(Q_OS_WIN32)
50  return Qt::CaseInsensitive;
51 #else
52  return Qt::CaseSensitive;
53 #endif
54 }
55 
56 bool FileUtils::containsPath(const QStringList &candidates, const QString &path)
57 {
58  foreach (const QString &candidate, candidates)
59  {
60  if (QFileInfo(candidate) == QFileInfo(path))
61  {
62  return true;
63  }
64  }
65  return false;
66 }
67 
68 bool FileUtils::rmAllFiles(const QString& dirPath,
69  const QStringList & nameFilters)
70 {
71  QDirIterator it(dirPath, nameFilters, QDir::Files);
72  bool bAllSuccess = true;
73  while (it.hasNext())
74  {
75  QString path = it.next();
76  QFile f(path);
77  if (!f.remove())
78  {
79  bAllSuccess = false;
80  gLog << Log::tr("Failed to remove: %1").arg(path);
81  }
82  }
83  return bAllSuccess;
84 }
static bool containsPath(const QStringList &candidates, const QString &path)
Uses QFileInfo::operator== to see if &#39;path&#39; is on &#39;candidates&#39; list.
Definition: fileutils.cpp:56
static bool rmAllFiles(const QString &dirPath, const QStringList &nameFilters=QStringList())
Deletes all files in specified directory.
Definition: fileutils.cpp:68