NorMIT-nav  22.09
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxFileHelpers.h"
13 #include <QFile>
14 #include <QDir>
15 #include <QFileInfo>
16 #include <QTextStream>
17 #include "cxLogger.h"
18 
19 namespace cx
20 {
21 
22 //From http://stackoverflow.com/questions/2536524/copy-directory-using-qt
23 bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
24 {
25  QDir originDirectory(sourceDir);
26 
27  if (! originDirectory.exists())
28  {
29  return false;
30  }
31 
32  QDir destinationDirectory(destinationDir);
33 
34  if(destinationDirectory.exists() && !overWriteDirectory)
35  {
36  return false;
37  }
38  else if(destinationDirectory.exists() && overWriteDirectory)
39  {
40  destinationDirectory.removeRecursively();
41  }
42 
43  originDirectory.mkpath(destinationDir);
44 
45  foreach (QString directoryName, originDirectory.entryList(QDir::Dirs | \
46  QDir::NoDotAndDotDot))
47  {
48  QString destinationPath = destinationDir + "/" + directoryName;
49  originDirectory.mkpath(destinationPath);
50  copyRecursively(sourceDir + "/" + directoryName, destinationPath, overWriteDirectory);
51  }
52 
53  foreach (QString fileName, originDirectory.entryList(QDir::Files))
54  {
55  QFile::copy(sourceDir + "/" + fileName, destinationDir + "/" + fileName);
56  }
57 
59  QDir finalDestination(destinationDir);
60  finalDestination.refresh();
61 
62  if(finalDestination.exists())
63  {
64  return true;
65  }
66 
67  return false;
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 void forceNewlineBeforeEof(QString path)
124 {
125  QFile the_file(path);
126  the_file.open(QFile::ReadWrite);
127  QByteArray all = the_file.readAll();
128  if(!all.endsWith("\n"))
129  {
130  CX_LOG_WARNING() << "File does not end with whitespace, adding newline to the file: " << path;
131  QTextStream out(&the_file);
132  out << endl;
133  }
134  the_file.close();
135 
136 }
137 
138 } // namespace cx
cxLogger.h
cx::removeNonemptyDirRecursively
bool removeNonemptyDirRecursively(const QString &dirName)
Definition: cxFileHelpers.cpp:70
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::getDirs
QFileInfoList getDirs(QString path)
Definition: cxFileHelpers.cpp:93
cx::copyRecursively
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
Definition: cxFileHelpers.cpp:23
cx::getAbsolutePathToXmlFiles
QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
Definition: cxFileHelpers.cpp:118
cxFileHelpers.h
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::getAbsolutePathToFiles
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
Definition: cxFileHelpers.cpp:101
cx::forceNewlineBeforeEof
void forceNewlineBeforeEof(QString path)
Definition: cxFileHelpers.cpp:123