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

Functions

std::string extension (const std::string &path)
 
std::string extensions (const std::string &path)
 
bool createDirectory (const std::string &dir_name)
 
bool createDirectories (const std::string &dir_name)
 
std::vector< std::string > filesInDirectory (const std::string &dir_name)
 
std::string jointPath (const std::string &spath1, const std::string &spath2)
 
std::string filename (const std::string &path)
 
std::string stem (const std::string &path)
 
std::string stem_ext (const std::string &path)
 
std::vector< std::string > glob (const std::string &dir, const std::string &pattern)
 
std::wstring convert_utf8_to_utf16 (const std::string &str)
 
bool IsFileExists (const std::string &str)
 

Detailed Description

Utility functions to deal with file system.

Function Documentation

◆ 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 24 of file FileSystemUtils.cpp.

25 {
26  return boost::filesystem::extension(path.c_str());
27 }
std::string extension(const std::string &path)
Returns extension of given filename.

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 29 of file FileSystemUtils.cpp.

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

◆ createDirectory()

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

Creates directory in current directory.

Definition at line 36 of file FileSystemUtils.cpp.

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

◆ 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 46 of file FileSystemUtils.cpp.

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 }

References convert_utf8_to_utf16().

Here is the call graph for this function:

◆ filesInDirectory()

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

Returns filenames of files in directory.

Definition at line 56 of file FileSystemUtils.cpp.

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 }

Referenced by glob().

◆ jointPath()

std::string FileSystemUtils::jointPath ( const std::string &  spath1,
const std::string &  spath2 
)

Returns joint path name.

Definition at line 72 of file FileSystemUtils.cpp.

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

References ASSERT.

◆ filename()

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

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

Definition at line 83 of file FileSystemUtils.cpp.

84 {
85  return boost::filesystem::path(path).filename().string();
86 }

Referenced by IHistogram::createFrom(), extensions(), IHistogram::load(), IHistogram::save(), and stem_ext().

◆ stem()

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

Returns filename without extension.

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

Definition at line 97 of file FileSystemUtils.cpp.

98 {
99  return boost::filesystem::path(path).stem().string();
100 }

◆ 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 102 of file FileSystemUtils.cpp.

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 }

References filename().

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 88 of file FileSystemUtils.cpp.

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

◆ 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(), OutputDataReader::getOutputData(), IsFileExists(), and OutputDataWriter::writeOutputData().

◆ IsFileExists()

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

Returns true if file with given name exists on disk.

Definition at line 115 of file FileSystemUtils.cpp.

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 }

References convert_utf8_to_utf16().

Referenced by IntensityDataIOFactory::readOutputData(), and IntensityDataIOFactory::readReflectometryData().

Here is the call graph for this function: