BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FileSystemUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Utils/FileSystemUtils.cpp
6 //! @brief Implements namespace FileSystemUtils
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Utils/Assert.h"
17 #include <codecvt>
18 #include <filesystem>
19 #include <locale>
20 #include <regex>
21 #include <stdexcept>
22 
23 namespace fs = std::filesystem; // make the code more readable
24 
25 std::string FileSystemUtils::extension(const std::string& path)
26 {
27  return fs::path(path).extension().string();
28 }
29 
30 std::string FileSystemUtils::extensions(const std::string& path)
31 {
32  const auto name = FileSystemUtils::filename(path);
33  if (name == "..")
34  return {};
35 
36  const auto pos = name.find_first_of('.', 1); // 1: ignore any file-is-hidden dot
37  return pos != std::string::npos ? name.substr(pos, name.size() - pos) : std::string();
38 }
39 
40 bool FileSystemUtils::createDirectory(const std::string& dir_name)
41 {
42 #ifdef _WIN32
44 #else
45  return fs::create_directory(dir_name);
46 #endif
47 }
48 
49 bool FileSystemUtils::createDirectories(const std::string& dir_name)
50 {
51 #ifdef _WIN32
52  return fs::create_directories(convert_utf8_to_utf16(dir_name));
53 #else
54  return fs::create_directories(dir_name);
55 #endif
56 }
57 
58 std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name)
59 {
60  std::vector<std::string> ret;
61  if (!fs::exists(dir_name))
62  throw std::runtime_error("FileSystemUtils::filesInDirectory '" + dir_name
63  + "' does not exist");
64 
65  for (const auto& entry : fs::directory_iterator(dir_name))
66  if (entry.is_regular_file())
67  ret.push_back(entry.path().filename().string());
68 
69  return ret;
70 }
71 
72 std::string FileSystemUtils::jointPath(const std::string& path1, const std::string& path2)
73 {
74  ASSERT(path1 != "");
75  ASSERT(path2 != "");
76 
77  return (fs::path(path1) / fs::path(path2)).string();
78 }
79 
80 std::string FileSystemUtils::filename(const std::string& path)
81 {
82  return fs::path(path).filename().string();
83 }
84 
85 std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern)
86 {
87  std::vector<std::string> ret;
88  for (const std::string& fname : filesInDirectory(dir))
89  if (std::regex_match(fname, std::regex(pattern)))
90  ret.push_back(fname);
91  return ret;
92 }
93 
94 std::string FileSystemUtils::stem(const std::string& path)
95 {
96  return fs::path(path).stem().string();
97 }
98 
99 std::string FileSystemUtils::stem_ext(const std::string& path)
100 {
101  const auto name = FileSystemUtils::filename(path);
102  if (name == "..")
103  return name;
104 
105  const auto pos = name.find_first_of('.', 1); // 1: ignore any file-is-hidden dot
106  return pos != std::string::npos ? name.substr(0, pos) : name;
107 }
108 
109 std::wstring FileSystemUtils::convert_utf8_to_utf16(const std::string& str)
110 {
111  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
112  return converter.from_bytes(str);
113 }
114 
115 bool FileSystemUtils::IsFileExists(const std::string& path)
116 {
117 #ifdef _WIN32
118  return fs::exists(convert_utf8_to_utf16(path));
119 #else
120  return fs::exists(path);
121 #endif
122 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines namespace FileSystemUtils.
std::string extensions(const std::string &path)
Returns extension(s) of given filename.
std::wstring convert_utf8_to_utf16(const std::string &str)
Converts utf8 string represented by std::string to utf16 string represented by std::wstring.
std::string stem_ext(const std::string &path)
Returns filename without extension(s).
bool IsFileExists(const std::string &path)
Returns true if file with given name exists on disk.
std::string extension(const std::string &path)
Returns extension of given filename.
std::string jointPath(const std::string &path1, const std::string &path2)
Returns joint path name.
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
std::vector< std::string > glob(const std::string &dir, const std::string &pattern)
Returns file names that agree with a regex glob pattern.
std::string stem(const std::string &path)
Returns filename without (last) extension.
std::vector< std::string > filesInDirectory(const std::string &dir_name)
Returns filenames of files in directory.
bool createDirectories(const std::string &dir_name)
Creates directories in current directory for any element of dir_name which doesn't exist.
bool createDirectory(const std::string &dir_name)
Creates directory in current directory.
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47
bool exists(const QString &fileName)
Returns true if file exists.
QString const & name(EShape k)
Definition: particles.cpp:21