BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FileSystemUtils Namespace Reference

Utility functions to deal with file system. More...

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. More...
 
std::string extensions (const std::string &path)
 Returns extension(s) of given filename. 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 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. More...
 
std::string stem (const std::string &path)
 Returns filename without (last) extension. More...
 
std::string stem_ext (const std::string &path)
 Returns filename without extension(s). More...
 

Detailed Description

Utility functions to deal with file system.

Function Documentation

◆ convert_utf8_to_utf16()

std::wstring FileSystemUtils::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 109 of file FileSystemUtils.cpp.

110 {
111  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
112  return converter.from_bytes(str);
113 }

Referenced by createDirectories(), createDirectory(), IsFileExists(), IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::writeOutputData().

◆ createDirectories()

bool FileSystemUtils::createDirectories ( const std::string &  dir_name)

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

Definition at line 49 of file FileSystemUtils.cpp.

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 }
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 FileSystemUtils::createDirectory ( const std::string &  dir_name)

Creates directory in current directory.

Definition at line 40 of file FileSystemUtils.cpp.

41 {
42 #ifdef _WIN32
44 #else
45  return fs::create_directory(dir_name);
46 #endif
47 }
MVVM_MODEL_EXPORT bool create_directory(const std::string &path)
Create directory, parent directory must exist.
Definition: fileutils.cpp:47

References convert_utf8_to_utf16(), and ModelView::Utils::create_directory().

Here is the call graph for this function:

◆ extension()

std::string FileSystemUtils::extension ( const std::string &  path)

Returns extension of given filename.

"/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".gz"

Definition at line 25 of file FileSystemUtils.cpp.

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

Referenced by DataFormatUtils::GetFileMainExtension(), DataFormatUtils::isBZipped(), and DataFormatUtils::isGZipped().

◆ extensions()

std::string FileSystemUtils::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 30 of file FileSystemUtils.cpp.

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 }
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
QString const & name(EShape k)
Definition: particles.cpp:21

References filename(), and RealSpace::Particles::name().

Here is the call graph for this function:

◆ filename()

◆ filesInDirectory()

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

Returns filenames of files in directory.

Definition at line 58 of file FileSystemUtils.cpp.

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 }
bool exists(const QString &fileName)
Returns true if file exists.

References ProjectUtils::exists().

Referenced by glob().

Here is the call graph for this function:

◆ glob()

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

Returns file names that agree with a regex glob pattern.

Definition at line 85 of file FileSystemUtils.cpp.

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 }
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:

◆ IsFileExists()

bool FileSystemUtils::IsFileExists ( const std::string &  path)

Returns true if file with given name exists on disk.

Definition at line 115 of file FileSystemUtils.cpp.

116 {
117 #ifdef _WIN32
118  return fs::exists(convert_utf8_to_utf16(path));
119 #else
120  return fs::exists(path);
121 #endif
122 }

References convert_utf8_to_utf16(), and ProjectUtils::exists().

Referenced by IntensityDataIOFactory::readOutputData().

Here is the call graph for this function:

◆ jointPath()

std::string FileSystemUtils::jointPath ( const std::string &  path1,
const std::string &  path2 
)

Returns joint path name.

Definition at line 72 of file FileSystemUtils.cpp.

73 {
74  ASSERT(path1 != "");
75  ASSERT(path2 != "");
76 
77  return (fs::path(path1) / fs::path(path2)).string();
78 }
#define ASSERT(condition)
Definition: Assert.h:31

References ASSERT.

◆ stem()

std::string FileSystemUtils::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 94 of file FileSystemUtils.cpp.

95 {
96  return fs::path(path).stem().string();
97 }

◆ stem_ext()

std::string FileSystemUtils::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 99 of file FileSystemUtils.cpp.

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 }

References filename(), and RealSpace::Particles::name().

Here is the call graph for this function: