17#ifndef __file_path_h__
18#define __file_path_h__
34#define HOME_ENV "HOME"
49#define PATH_SEPARATORS "/\\"
51#define PATH_SEPARATORS "/"
63 return (i == std::string::npos ?
name :
name.substr (i+1));
70 return (i == std::string::npos ? std::string (
"") : (i ?
name.substr (0,i) : std::string(1,
PATH_SEPARATORS[0])));
74 inline std::string
join (
const std::string& first,
const std::string& second)
84 return first + second;
88 inline bool exists (
const std::string& path)
93 if (!stat (stripped.c_str(), &buf))
95 if (!stat (path.c_str(), &buf))
98 if (errno == ENOENT)
return false;
104 inline bool is_dir (
const std::string& path)
109 if (!stat (stripped.c_str(), &buf))
111 if (!stat (path.c_str(), &buf))
113 return S_ISDIR (buf.st_mode);
114 if (errno == ENOENT)
return false;
123 if (!stat (path.c_str(), &buf))
return S_ISREG (buf.st_mode);
124 if (errno == ENOENT)
return false;
132 return name.size() >= suffix.size() &&
133 name.compare(
name.size() - suffix.size(), suffix.size(), suffix) == 0;
136 inline bool has_suffix (
const std::string &
name,
const std::initializer_list<const std::string> &suffix_list)
138 return std::any_of (suffix_list.begin(),
140 [&] (
const std::string& suffix) { return has_suffix (name, suffix); });
145 return std::any_of (suffix_list.begin(),
147 [&] (
const std::string& suffix) { return has_suffix (name, suffix); });
152 return strcmp(
name.c_str(), std::string(
"-").c_str()) == 0 ||
159 size_t buf_size = 32;
161 path.reserve (buf_size);
162 if (getcwd (&path[0], buf_size))
165 throw Exception (
"failed to get current working directory!");
182 p (opendir (
name.size() ?
name.c_str() :
".")) {
184 throw Exception (
"error opening folder " +
name +
": " + strerror (errno));
192 struct dirent* entry = readdir (
p);
195 if (ret ==
"." || ret ==
"..")
Dir(const std::string &name)
std::string join(const std::string &first, const std::string &second)
char delimiter(const std::string &filename)
bool is_file(const std::string &path)
std::string basename(const std::string &name)
std::string dirname(const std::string &name)
bool is_mrtrix_image(const std::string &name)
bool exists(const std::string &path)
bool is_dir(const std::string &path)
bool has_suffix(const std::string &name, const std::string &suffix)
std::string strip(const std::string &string, const std::string &ws={" \0\t\r\n", 5}, bool left=true, bool right=true)
#define PATH_SEPARATORS
symbols used for separating directories in filesystem paths