BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
fileutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/utils/fileutils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "mvvm/utils/fileutils.h"
16 #include <QDir>
17 #include <QFile>
18 #include <QFileInfo>
19 #include <stdexcept>
20 
21 #ifdef ENABLE_FILESYSTEM
22 #include "mvvm/core/filesystem.h"
23 #endif
24 
25 using namespace ModelView;
26 
27 bool Utils::exists(const std::string& fileName)
28 {
29 #ifdef ENABLE_FILESYSTEM
30  return std::filesystem::exists(fileName);
31 #else
32  QFileInfo info(QString::fromStdString(fileName));
33  return info.exists();
34 #endif
35 }
36 
37 std::string Utils::join(const std::string& part1, const std::string& part2)
38 {
39 #ifdef ENABLE_FILESYSTEM
40  auto path = std::filesystem::path(part1) / std::filesystem::path(part2);
41  return path.string();
42 #else
43  return part1 + std::string("/") + part2;
44 #endif
45 }
46 
47 bool Utils::create_directory(const std::string& path)
48 {
49 #ifdef ENABLE_FILESYSTEM
51 #else
52  QDir dir(QString::fromStdString(path));
53  return dir.mkpath(".");
54 #endif
55 }
56 
57 bool Utils::remove(const std::string& path)
58 {
59 #ifdef ENABLE_FILESYSTEM
60  return std::filesystem::remove(path);
61 #else
62  QFile file(QString::fromStdString(path));
63  return file.remove();
64 #endif
65 }
66 
67 void Utils::remove_all(const std::string& path)
68 {
69 #ifdef ENABLE_FILESYSTEM
71 #else
72  QDir dir(QString::fromStdString(path));
73  if (dir.exists())
74  dir.removeRecursively();
75 #endif
76 }
77 
78 std::string Utils::base_name(const std::string& path)
79 {
80 #ifdef ENABLE_FILESYSTEM
81  return std::filesystem::path(path).stem().string();
82 #else
83  return QFileInfo(QString::fromStdString(path)).completeBaseName().toStdString();
84 #endif
85 }
86 
87 std::vector<std::string> Utils::FindFiles(const std::string& dirname, const std::string& ext)
88 {
89 #ifdef ENABLE_FILESYSTEM
90  std::vector<std::string> result;
91  for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
92  const auto filenameStr = entry.path().filename().string();
93  if (entry.is_regular_file() && entry.path().extension() == ext)
94  result.push_back(entry.path().string());
95  }
96  return result;
97 #else
98  std::vector<std::string> result;
99  QDir dir(QString::fromStdString(dirname));
100  if (dir.exists()) {
101  QStringList filters = {QString::fromStdString("*" + ext)};
102  for (auto entry : dir.entryList(filters)) {
103  auto name = dir.filePath(entry);
104  result.push_back(name.toStdString());
105  }
106  }
107  return result;
108 #endif
109 }
110 
111 std::string Utils::parent_path(const std::string& path)
112 {
113 #ifdef ENABLE_FILESYSTEM
114  return std::filesystem::path(path).parent_path().string();
115 #else
116  QFileInfo info(QString::fromStdString(path));
117  return info.dir().path().toStdString();
118 #endif
119 }
120 
121 bool Utils::is_empty(const std::string& path)
122 {
123 #ifdef ENABLE_FILESYSTEM
124  return std::filesystem::is_empty(path);
125 #else
126  QFileInfo info(QString::fromStdString(path));
127  if (info.isDir()) {
128  QDir dir(QString::fromStdString(path));
129  return dir.isEmpty();
130  } else {
131  return info.size() == 0;
132  }
133  return false;
134 #endif
135 }
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT std::string base_name(const std::string &path)
Provide the filename of a file path.
Definition: fileutils.cpp:78
MVVM_MODEL_EXPORT std::vector< std::string > FindFiles(const std::string &dirname, const std::string &ext)
Returns list of files with given extention found in given directory.
Definition: fileutils.cpp:87
MVVM_MODEL_EXPORT bool is_empty(const std::string &path)
Returns true if the file indicated by 'path' refers to empty file or directory.
Definition: fileutils.cpp:121
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57
MVVM_MODEL_EXPORT void remove_all(const std::string &path)
Removes directory with all its content.
Definition: fileutils.cpp:67
MVVM_MODEL_EXPORT std::string parent_path(const std::string &path)
Returns the path to the parent directory.
Definition: fileutils.cpp:111
MVVM_MODEL_EXPORT std::string join(const std::string &part1, const std::string &part2)
Joins two path elements into the path.
Definition: fileutils.cpp:37
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47
materialitems.h Collection of materials to populate MaterialModel.
bool exists(const QString &fileName)
Returns true if file exists.
QString const & name(EShape k)
Definition: particles.cpp:21