Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFileHelpers.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxFileHelpers.h"
34 #include <QFile>
35 #include <QDir>
36 #include <QFileInfo>
37 
38 namespace cx
39 {
40 
41 bool copyRecursively(const QString &srcFilePath,
42  const QString &tgtFilePath)
43 {
44  QFileInfo srcFileInfo(srcFilePath);
45  if (srcFileInfo.isDir())
46  {
47  QDir targetDir(tgtFilePath);
48  if (!targetDir.mkpath("."))
49  return false;
50 // targetDir.cdUp();
51 // if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
52 // return false;
53  QDir sourceDir(srcFilePath);
54  QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
55  foreach (const QString &fileName, fileNames)
56  {
57  const QString newSrcFilePath = srcFilePath + QLatin1Char('/') + fileName;
58  const QString newTgtFilePath = tgtFilePath + QLatin1Char('/') + fileName;
59  if (!copyRecursively(newSrcFilePath, newTgtFilePath))
60  return false;
61  }
62  } else
63  {
64  if (!QFile::copy(srcFilePath, tgtFilePath))
65  return false;
66  }
67  return true;
68 }
69 
70 bool removeNonemptyDirRecursively(const QString & dirName)
71 {
72  bool result = false;
73  QDir dir(dirName);
74 
75  if (dir.exists(dirName)) {
76  Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
77  if (info.isDir()) {
78  result = removeNonemptyDirRecursively(info.absoluteFilePath());
79  }
80  else {
81  result = QFile::remove(info.absoluteFilePath());
82  }
83 
84  if (!result) {
85  return result;
86  }
87  }
88  result = dir.rmdir(dirName);
89  }
90  return result;
91 }
92 
93 QFileInfoList getDirs(QString path)
94 {
95  QDir dir(path);
96  dir.setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
97  QFileInfoList retval = dir.entryInfoList();
98  return retval;
99 }
100 
101 QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
102 {
103  QStringList retval;
104  QDir dir(path);
105  dir.setFilter(QDir::Files);
106  dir.setNameFilters(nameFilters);
107 
108  foreach(QFileInfo file, dir.entryInfoList())
109  retval << file.absoluteFilePath();
110 
111  if (includeSubDirs)
112  foreach(QFileInfo directory, getDirs(path))
113  retval << getAbsolutePathToFiles(directory.absoluteFilePath(), nameFilters, includeSubDirs);
114 
115  return retval;
116 }
117 
118 QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
119 {
120  return getAbsolutePathToFiles(path, QStringList("*.xml"), includeSubDirs);
121 }
122 
123 } // namespace cx
bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
bool removeNonemptyDirRecursively(const QString &dirName)
QFileInfoList getDirs(QString path)
QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)