BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
BaseUtils::Filesystem Namespace Reference

Description

Utility functions to deal with file system.

Functions

std::wstring convert_utf8_to_utf16 (const std::string &str)
 Converts utf8 string represented by std::string to utf16 string represented by std::wstring. More...
 
bool createDirectories (const std::string &dir_name)
 Creates directories in current directory for any element of dir_name which doesn't exist. More...
 
bool createDirectory (const std::string &dir_name)
 Creates directory in current directory. More...
 
std::string extension (const std::string &path)
 Returns extension of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".gz". More...
 
std::string extensions (const std::string &path)
 Returns extension(s) of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".int.gz". More...
 
std::string filename (const std::string &path)
 Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz") More...
 
std::vector< std::string > filesInDirectory (const std::string &dir_name)
 Returns filenames of files in directory. More...
 
std::vector< std::string > glob (const std::string &dir, const std::string &pattern)
 Returns file names that agree with a regex glob pattern. More...
 
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. More...
 
bool IsFileExists (const std::string &path)
 Returns true if file with given name exists on disk. More...
 
std::string jointPath (const std::string &path1, const std::string &path2)
 Returns joint path name. Argument path1 may be empty, argument path2 not. More...
 
std::string stem (const std::string &path)
 Returns filename without (last) extension. "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename.int". More...
 
std::string stem_ext (const std::string &path)
 Returns filename without extension(s). "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename". More...
 

Function Documentation

◆ convert_utf8_to_utf16()

std::wstring BaseUtils::Filesystem::convert_utf8_to_utf16 ( const std::string &  str)

Converts utf8 string represented by std::string to utf16 string represented by std::wstring.

Definition at line 118 of file FileSystemUtils.cpp.

119 {
120  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
121  return converter.from_bytes(str);
122 }

Referenced by createDirectories(), createDirectory(), IsFileExists(), IOFactory::readDatafield(), and IOFactory::writeDatafield().

◆ createDirectories()

bool BaseUtils::Filesystem::createDirectories ( const std::string &  dir_name)

Creates directories in current directory for any element of dir_name which doesn't exist.

Definition at line 55 of file FileSystemUtils.cpp.

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 }
std::wstring convert_utf8_to_utf16(const std::string &str)
Converts utf8 string represented by std::string to utf16 string represented by std::wstring.

References convert_utf8_to_utf16().

Here is the call graph for this function:

◆ createDirectory()

bool BaseUtils::Filesystem::createDirectory ( const std::string &  dir_name)

Creates directory in current directory.

Definition at line 46 of file FileSystemUtils.cpp.

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 }

References convert_utf8_to_utf16().

Here is the call graph for this function:

◆ extension()

std::string BaseUtils::Filesystem::extension ( const std::string &  path)

Returns extension of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".gz".

Definition at line 26 of file FileSystemUtils.cpp.

27 {
28  return fs::path(path).extension().string();
29 }

Referenced by hasExtension().

◆ extensions()

std::string BaseUtils::Filesystem::extensions ( const std::string &  path)

Returns extension(s) of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".int.gz".

Definition at line 36 of file FileSystemUtils.cpp.

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 }
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")

References filename().

Here is the call graph for this function:

◆ filename()

std::string BaseUtils::Filesystem::filename ( const std::string &  path)

Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")

Definition at line 88 of file FileSystemUtils.cpp.

89 {
90  return fs::path(path).filename().string();
91 }

Referenced by extensions(), ROOT::Math::MixMaxEngineImpl< ROOT_MM_N >::ReadState(), and stem_ext().

◆ filesInDirectory()

std::vector< std::string > BaseUtils::Filesystem::filesInDirectory ( const std::string &  dir_name)

Returns filenames of files in directory.

Definition at line 64 of file FileSystemUtils.cpp.

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 }

Referenced by glob().

◆ glob()

std::vector< std::string > BaseUtils::Filesystem::glob ( const std::string &  dir,
const std::string &  pattern 
)

Returns file names that agree with a regex glob pattern.

Definition at line 93 of file FileSystemUtils.cpp.

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 }
std::vector< std::string > filesInDirectory(const std::string &dir_name)
Returns filenames of files in directory.

References filesInDirectory().

Here is the call graph for this function:

◆ hasExtension()

bool BaseUtils::Filesystem::hasExtension ( const std::string &  path,
const std::string &  ref_extension 
)

Returns true if extension of path, converted to lower case, matches given reference extension.

Definition at line 31 of file FileSystemUtils.cpp.

32 {
33  return BaseUtils::String::to_lower(extension(path)) == ref_extension;
34 }
std::string extension(const std::string &path)
Returns extension of given filename. "/home/user/filename.int" -> ".int", "/home/user/filename....
std::string to_lower(std::string text)
Returns new string which is lower case of text.
Definition: StringUtils.cpp:58

References extension(), and BaseUtils::String::to_lower().

Referenced by DataUtils::Format::isBZipped(), DataUtils::Format::isGZipped(), DataUtils::Format::isIntFile(), DataUtils::Format::isNicosFile(), and DataUtils::Format::isTiffFile().

Here is the call graph for this function:

◆ IsFileExists()

bool BaseUtils::Filesystem::IsFileExists ( const std::string &  path)

Returns true if file with given name exists on disk.

Definition at line 124 of file FileSystemUtils.cpp.

125 {
126 #ifdef _WIN32
127  return fs::exists(convert_utf8_to_utf16(path));
128 #else
129  return fs::exists(path);
130 #endif
131 }

References convert_utf8_to_utf16().

Referenced by IOFactory::readDatafield().

Here is the call graph for this function:

◆ jointPath()

std::string BaseUtils::Filesystem::jointPath ( const std::string &  path1,
const std::string &  path2 
)

Returns joint path name. Argument path1 may be empty, argument path2 not.

Definition at line 78 of file FileSystemUtils.cpp.

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 }
#define ASSERT(condition)
Definition: Assert.h:45

References ASSERT.

◆ stem()

std::string BaseUtils::Filesystem::stem ( const std::string &  path)

Returns filename without (last) extension. "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename.int".

Definition at line 103 of file FileSystemUtils.cpp.

104 {
105  return fs::path(path).stem().string();
106 }

◆ stem_ext()

std::string BaseUtils::Filesystem::stem_ext ( const std::string &  path)

Returns filename without extension(s). "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename".

Definition at line 108 of file FileSystemUtils.cpp.

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 }

References filename().

Here is the call graph for this function: