Developer documentation
Version 3.0.3-105-gd3941f44
path.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __file_path_h__
18#define __file_path_h__
19
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <dirent.h>
23#include <string>
24#include <cstdlib>
25#include <cstring>
26#include <cerrno>
27#include <unistd.h>
28#include <algorithm>
29
30#include "exception.h"
31#include "mrtrix.h"
32#include "types.h"
33
34#define HOME_ENV "HOME"
35
47#ifdef MRTRIX_WINDOWS
48// Preferentially use forward-slash when inserting via PATH_SEPARATORS[0]
49#define PATH_SEPARATORS "/\\"
50#else
51#define PATH_SEPARATORS "/"
52#endif
53
54
55namespace MR
56{
57 namespace Path
58 {
59
60 inline std::string basename (const std::string& name)
61 {
62 size_t i = name.find_last_of (PATH_SEPARATORS);
63 return (i == std::string::npos ? name : name.substr (i+1));
64 }
65
66
67 inline std::string dirname (const std::string& name)
68 {
69 size_t i = name.find_last_of (PATH_SEPARATORS);
70 return (i == std::string::npos ? std::string ("") : (i ? name.substr (0,i) : std::string(1, PATH_SEPARATORS[0])));
71 }
72
73
74 inline std::string join (const std::string& first, const std::string& second)
75 {
76 if (first.empty())
77 return second;
78 if (first[first.size()-1] != PATH_SEPARATORS[0]
79#ifdef MRTRIX_WINDOWS
80 && first[first.size()-1] != PATH_SEPARATORS[1]
81#endif
82 )
83 return first + PATH_SEPARATORS[0] + second;
84 return first + second;
85 }
86
87
88 inline bool exists (const std::string& path)
89 {
90 struct stat buf;
91#ifdef MRTRIX_WINDOWS
92 const std::string stripped (strip (path, PATH_SEPARATORS, false, true));
93 if (!stat (stripped.c_str(), &buf))
94#else
95 if (!stat (path.c_str(), &buf))
96#endif
97 return true;
98 if (errno == ENOENT) return false;
99 throw Exception (strerror (errno));
100 return false;
101 }
102
103
104 inline bool is_dir (const std::string& path)
105 {
106 struct stat buf;
107#ifdef MRTRIX_WINDOWS
108 const std::string stripped (strip (path, PATH_SEPARATORS, false, true));
109 if (!stat (stripped.c_str(), &buf))
110#else
111 if (!stat (path.c_str(), &buf))
112#endif
113 return S_ISDIR (buf.st_mode);
114 if (errno == ENOENT) return false;
115 throw Exception (strerror (errno));
116 return false;
117 }
118
119
120 inline bool is_file (const std::string& path)
121 {
122 struct stat buf;
123 if (!stat (path.c_str(), &buf)) return S_ISREG (buf.st_mode);
124 if (errno == ENOENT) return false;
125 throw Exception (strerror (errno));
126 return false;
127 }
128
129
130 inline bool has_suffix (const std::string& name, const std::string& suffix)
131 {
132 return name.size() >= suffix.size() &&
133 name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0;
134 }
135
136 inline bool has_suffix (const std::string &name, const std::initializer_list<const std::string> &suffix_list)
137 {
138 return std::any_of (suffix_list.begin(),
139 suffix_list.end(),
140 [&] (const std::string& suffix) { return has_suffix (name, suffix); });
141 }
142
143 inline bool has_suffix (const std::string &name, const vector<std::string> &suffix_list)
144 {
145 return std::any_of (suffix_list.begin(),
146 suffix_list.end(),
147 [&] (const std::string& suffix) { return has_suffix (name, suffix); });
148 }
149
150 inline bool is_mrtrix_image (const std::string& name)
151 {
152 return strcmp(name.c_str(), std::string("-").c_str()) == 0 ||
153 Path::has_suffix (name, {".mif", ".mih", ".mif.gz"});
154 }
155
156 inline std::string cwd ()
157 {
158 std::string path;
159 size_t buf_size = 32;
160 while (true) {
161 path.reserve (buf_size);
162 if (getcwd (&path[0], buf_size))
163 break;
164 if (errno != ERANGE)
165 throw Exception ("failed to get current working directory!");
166 buf_size *= 2;
167 }
168 return path;
169 }
170
171 inline std::string home ()
172 {
173 const char* home = getenv (HOME_ENV);
174 if (!home)
175 throw Exception (HOME_ENV " environment variable is not set!");
176 return home;
177 }
178
180 public:
181 Dir (const std::string& name) :
182 p (opendir (name.size() ? name.c_str() : ".")) {
183 if (!p)
184 throw Exception ("error opening folder " + name + ": " + strerror (errno));
185 }
186 ~Dir () {
187 if (p) closedir (p);
188 }
189
190 std::string read_name () {
191 std::string ret;
192 struct dirent* entry = readdir (p);
193 if (entry) {
194 ret = entry->d_name;
195 if (ret == "." || ret == "..")
196 ret = read_name();
197 }
198 return ret;
199 }
200 void rewind () {
201 rewinddir (p);
202 }
203 void close () {
204 if (p) closedir (p);
205 p = NULL;
206 }
207
208 protected:
209 DIR* p;
210 };
211
212
213
214 inline char delimiter (const std::string& filename)
215 {
216 if (Path::has_suffix (filename, ".tsv"))
217 return '\t';
218 else if (Path::has_suffix (filename, ".csv"))
219 return ',';
220 else
221 return ' ';
222 }
223
224
225 }
226}
227
228#endif
229
void rewind()
Definition: path.h:200
DIR * p
Definition: path.h:209
std::string read_name()
Definition: path.h:190
void close()
Definition: path.h:203
Dir(const std::string &name)
Definition: path.h:181
#define NOMEMALIGN
Definition: memory.h:22
std::string join(const std::string &first, const std::string &second)
Definition: path.h:74
char delimiter(const std::string &filename)
Definition: path.h:214
bool is_file(const std::string &path)
Definition: path.h:120
std::string home()
Definition: path.h:171
std::string basename(const std::string &name)
Definition: path.h:60
std::string dirname(const std::string &name)
Definition: path.h:67
bool is_mrtrix_image(const std::string &name)
Definition: path.h:150
std::string cwd()
Definition: path.h:156
bool exists(const std::string &path)
Definition: path.h:88
bool is_dir(const std::string &path)
Definition: path.h:104
bool has_suffix(const std::string &name, const std::string &suffix)
Definition: path.h:130
Definition: base.h:24
std::string strip(const std::string &string, const std::string &ws={" \0\t\r\n", 5}, bool left=true, bool right=true)
Definition: mrtrix.h:131
#define PATH_SEPARATORS
symbols used for separating directories in filesystem paths
Definition: path.h:51
#define HOME_ENV
Definition: path.h:34
const std::string name
Definition: thread.h:108