BornAgain  1.18.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 scattering at grazing incidence
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/Types/Exceptions.h"
17 #include "Base/Utils/Assert.h"
18 #include <boost/filesystem.hpp>
19 #include <codecvt>
20 #include <locale>
21 #include <regex>
22 #include <stdexcept>
23 
24 std::string FileSystemUtils::extension(const std::string& path)
25 {
26  return boost::filesystem::extension(path.c_str());
27 }
28 
29 std::string FileSystemUtils::extensions(const std::string& path)
30 {
31  auto name = FileSystemUtils::filename(path);
32  auto npos = name.find_first_of('.');
33  return npos != std::string::npos ? name.substr(npos, name.size() - npos) : std::string();
34 }
35 
36 bool FileSystemUtils::createDirectory(const std::string& dir_name)
37 {
38 #ifdef _WIN32
39  boost::filesystem::path path(convert_utf8_to_utf16(dir_name));
40 #else
41  boost::filesystem::path path(dir_name);
42 #endif
43  return boost::filesystem::create_directory(dir_name);
44 }
45 
46 bool FileSystemUtils::createDirectories(const std::string& dir_name)
47 {
48 #ifdef _WIN32
49  boost::filesystem::path path(convert_utf8_to_utf16(dir_name));
50 #else
51  boost::filesystem::path path(dir_name);
52 #endif
53  return boost::filesystem::create_directories(path);
54 }
55 
56 std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name)
57 {
58  std::vector<std::string> ret;
59  if (!boost::filesystem::exists(dir_name))
60  throw std::runtime_error("FileSystemUtils::filesInDirectory '" + dir_name
61  + "' does not exist");
62  boost::filesystem::directory_iterator end_it; // default construction yields past-the-end
63  for (boost::filesystem::directory_iterator it(dir_name);
64  it != boost::filesystem::directory_iterator(); ++it) {
65  if (!boost::filesystem::is_regular_file(it->status()))
66  continue;
67  ret.push_back(it->path().filename().string());
68  }
69  return ret;
70 }
71 
72 std::string FileSystemUtils::jointPath(const std::string& spath1, const std::string& spath2)
73 {
74  ASSERT(spath1 != "");
75  ASSERT(spath2 != "");
76  boost::filesystem::path path1(spath1);
77  boost::filesystem::path path2(spath2);
78  boost::filesystem::path full_path = path1 / path2;
79 
80  return full_path.string();
81 }
82 
83 std::string FileSystemUtils::filename(const std::string& path)
84 {
85  return boost::filesystem::path(path).filename().string();
86 }
87 
88 std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern)
89 {
90  std::vector<std::string> ret;
91  for (const std::string& fname : filesInDirectory(dir))
92  if (std::regex_match(fname, std::regex(pattern)))
93  ret.push_back(fname);
94  return ret;
95 }
96 
97 std::string FileSystemUtils::stem(const std::string& path)
98 {
99  return boost::filesystem::path(path).stem().string();
100 }
101 
102 std::string FileSystemUtils::stem_ext(const std::string& path)
103 {
104  auto name = FileSystemUtils::filename(path);
105  auto npos = name.find_first_of('.');
106  return npos != std::string::npos ? name.substr(0, npos) : std::string();
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& str)
116 {
117 #ifdef _WIN32
118  boost::filesystem::path path(convert_utf8_to_utf16(str));
119 #else
120  boost::filesystem::path path(str);
121 #endif
122  return boost::filesystem::exists(path);
123 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:26
Defines many exception classes in namespace Exceptionss.
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).
std::string extension(const std::string &path)
Returns extension of given filename.
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.
bool IsFileExists(const std::string &str)
Returns true if file with given name exists on disk.
std::string stem(const std::string &path)
Returns filename without extension.
std::vector< std::string > filesInDirectory(const std::string &dir_name)
Returns filenames of files in directory.
std::string jointPath(const std::string &spath1, const std::string &spath2)
Returns joint path name.
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.