Fraxinus  17.12-rc4
An IGT application
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 #include <QTextStream>
38 #include "cxLogger.h"
39 
40 namespace cx
41 {
42 
43 //From http://stackoverflow.com/questions/2536524/copy-directory-using-qt
44 bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
45 {
46  QDir originDirectory(sourceDir);
47 
48  if (! originDirectory.exists())
49  {
50  return false;
51  }
52 
53  QDir destinationDirectory(destinationDir);
54 
55  if(destinationDirectory.exists() && !overWriteDirectory)
56  {
57  return false;
58  }
59  else if(destinationDirectory.exists() && overWriteDirectory)
60  {
61  destinationDirectory.removeRecursively();
62  }
63 
64  originDirectory.mkpath(destinationDir);
65 
66  foreach (QString directoryName, originDirectory.entryList(QDir::Dirs | \
67  QDir::NoDotAndDotDot))
68  {
69  QString destinationPath = destinationDir + "/" + directoryName;
70  originDirectory.mkpath(destinationPath);
71  copyRecursively(sourceDir + "/" + directoryName, destinationPath, overWriteDirectory);
72  }
73 
74  foreach (QString fileName, originDirectory.entryList(QDir::Files))
75  {
76  QFile::copy(sourceDir + "/" + fileName, destinationDir + "/" + fileName);
77  }
78 
80  QDir finalDestination(destinationDir);
81  finalDestination.refresh();
82 
83  if(finalDestination.exists())
84  {
85  return true;
86  }
87 
88  return false;
89 }
90 
91 bool removeNonemptyDirRecursively(const QString & dirName)
92 {
93  bool result = false;
94  QDir dir(dirName);
95 
96  if (dir.exists(dirName)) {
97  Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
98  if (info.isDir()) {
99  result = removeNonemptyDirRecursively(info.absoluteFilePath());
100  }
101  else {
102  result = QFile::remove(info.absoluteFilePath());
103  }
104 
105  if (!result) {
106  return result;
107  }
108  }
109  result = dir.rmdir(dirName);
110  }
111  return result;
112 }
113 
114 QFileInfoList getDirs(QString path)
115 {
116  QDir dir(path);
117  dir.setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
118  QFileInfoList retval = dir.entryInfoList();
119  return retval;
120 }
121 
122 QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
123 {
124  QStringList retval;
125  QDir dir(path);
126  dir.setFilter(QDir::Files);
127  dir.setNameFilters(nameFilters);
128 
129  foreach(QFileInfo file, dir.entryInfoList())
130  retval << file.absoluteFilePath();
131 
132  if (includeSubDirs)
133  foreach(QFileInfo directory, getDirs(path))
134  retval << getAbsolutePathToFiles(directory.absoluteFilePath(), nameFilters, includeSubDirs);
135 
136  return retval;
137 }
138 
139 QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
140 {
141  return getAbsolutePathToFiles(path, QStringList("*.xml"), includeSubDirs);
142 }
143 
144 void forceNewlineBeforeEof(QString path)
145 {
146  QFile the_file(path);
147  the_file.open(QFile::ReadWrite);
148  QByteArray all = the_file.readAll();
149  if(!all.endsWith("\n"))
150  {
151  CX_LOG_WARNING() << "File does not end with whitespace, adding newline to the file: " << path;
152  QTextStream out(&the_file);
153  out << endl;
154  }
155  the_file.close();
156 
157 }
158 
159 } // namespace cx
bool removeNonemptyDirRecursively(const QString &dirName)
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
QFileInfoList getDirs(QString path)
QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
void forceNewlineBeforeEof(QString path)
#define CX_LOG_WARNING
Definition: cxLogger.h:119
Namespace for all CustusX production code.