BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
FileSystemUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/FileSystemUtils.cpp
6 //! @brief Implements namespace BaseUtils::Filesystem
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/Util/Assert.h"
17 #include "Base/Util/StringUtils.h"
18 #include <codecvt>
19 #include <filesystem>
20 #include <locale>
21 #include <regex>
22 #include <stdexcept>
23 
24 namespace fs = std::filesystem; // make the code more readable
25 
26 std::string BaseUtils::Filesystem::extension(const std::string& path)
27 {
28  return fs::path(path).extension().string();
29 }
30 
31 bool BaseUtils::Filesystem::hasExtension(const std::string& path, const std::string& ref_extension)
32 {
33  return BaseUtils::String::to_lower(extension(path)) == ref_extension;
34 }
35 
36 std::string BaseUtils::Filesystem::extensions(const std::string& path)
37 {
38  const auto name = BaseUtils::Filesystem::filename(path);
39  if (name == "..")
40  return {};
41 
42  const auto pos = name.find_first_of('.', 1); // 1: ignore any file-is-hidden dot
43  return pos != std::string::npos ? name.substr(pos, name.size() - pos) : "";
44 }
45 
46 bool BaseUtils::Filesystem::createDirectory(const std::string& dir_name)
47 {
48 #ifdef _WIN32
49  return fs::create_directory(convert_utf8_to_utf16(dir_name));
50 #else
51  return fs::create_directory(dir_name);
52 #endif
53 }
54 
55 bool BaseUtils::Filesystem::createDirectories(const std::string& dir_name)
56 {
57 #ifdef _WIN32
58  return fs::create_directories(convert_utf8_to_utf16(dir_name));
59 #else
60  return fs::create_directories(dir_name);
61 #endif
62 }
63 
64 std::vector<std::string> BaseUtils::Filesystem::filesInDirectory(const std::string& dir_name)
65 {
66  std::vector<std::string> result;
67  if (!fs::exists(dir_name))
68  throw std::runtime_error("BaseUtils::Filesystem::filesInDirectory '" + dir_name
69  + "' does not exist");
70 
71  for (const auto& entry : fs::directory_iterator(dir_name))
72  if (entry.is_regular_file())
73  result.push_back(entry.path().filename().string());
74 
75  return result;
76 }
77 
78 std::string BaseUtils::Filesystem::jointPath(const std::string& path1, const std::string& path2)
79 {
80  ASSERT(!path2.empty());
81 
82  if (path1.empty())
83  return fs::path(path2).string();
84 
85  return (fs::path(path1) / fs::path(path2)).string();
86 }
87 
88 std::string BaseUtils::Filesystem::filename(const std::string& path)
89 {
90  return fs::path(path).filename().string();
91 }
92 
93 std::vector<std::string> BaseUtils::Filesystem::glob(const std::string& dir,
94  const std::string& pattern)
95 {
96  std::vector<std::string> result;
97  for (const std::string& fname : filesInDirectory(dir))
98  if (std::regex_match(fname, std::regex(pattern)))
99  result.push_back(fname);
100  return result;
101 }
102 
103 std::string BaseUtils::Filesystem::stem(const std::string& path)
104 {
105  return fs::path(path).stem().string();
106 }
107 
108 std::string BaseUtils::Filesystem::stem_ext(const std::string& path)
109 {
110  const auto name = BaseUtils::Filesystem::filename(path);
111  if (name == "..")
112  return "..";
113 
114  const auto pos = name.find_first_of('.', 1); // 1: ignore any file-is-hidden dot
115  return pos != std::string::npos ? name.substr(0, pos) : name;
116 }
117 
118 std::wstring BaseUtils::Filesystem::convert_utf8_to_utf16(const std::string& str)
119 {
120  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
121  return converter.from_bytes(str);
122 }
123 
124 bool BaseUtils::Filesystem::IsFileExists(const std::string& path)
125 {
126 #ifdef _WIN32
127  return fs::exists(convert_utf8_to_utf16(path));
128 #else
129  return fs::exists(path);
130 #endif
131 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:45
Defines a few helper functions.
Defines namespace FileSystemUtils.
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
std::string extensions(const std::string &path)
Returns extension(s) of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename....
std::string jointPath(const std::string &path1, const std::string &path2)
Returns joint path name. Argument path1 may be empty, argument path2 not.
std::string stem_ext(const std::string &path)
Returns filename without extension(s). "/home/user/filename.int" -> "filename", "/home/user/filename....
bool createDirectory(const std::string &dir_name)
Creates directory in current directory.
std::string extension(const std::string &path)
Returns extension of given filename. "/home/user/filename.int" -> ".int", "/home/user/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::vector< std::string > filesInDirectory(const std::string &dir_name)
Returns filenames of files in directory.
std::string stem(const std::string &path)
Returns filename without (last) extension. "/home/user/filename.int" -> "filename",...
bool createDirectories(const std::string &dir_name)
Creates directories in current directory for any element of dir_name which doesn't exist.
std::vector< std::string > glob(const std::string &dir, const std::string &pattern)
Returns file names that agree with a regex glob pattern.
bool hasExtension(const std::string &path, const std::string &ref_extension)
Returns true if extension of path, converted to lower case, matches given reference extension.
bool IsFileExists(const std::string &path)
Returns true if file with given name exists on disk.
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:58