Cypress  1.0
C++ Spiking Neural Network Simulation Framework
filesystem.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2016 Andreas Stöckel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef CYPRESS_UTIL_FILESYSTEM_HPP
22 #define CYPRESS_UTIL_FILESYSTEM_HPP
23 
24 #include <algorithm>
25 #include <fstream>
26 #include <string>
27 #include <unordered_set>
28 #include <vector>
29 
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 
34 namespace cypress {
35 namespace filesystem {
39 std::string canonicalise(const std::string &file);
40 
44 template <typename T>
45 void canonicalise_files(T &files)
46 {
47  for (std::string &file : files) {
48  // Make sure the path points at a regular file
49  struct stat stat;
50  if (lstat(file.c_str(), &stat) != 0 || !S_ISREG(stat.st_mode)) {
51  continue;
52  }
53  file = canonicalise(file);
54  }
55 }
56 
61 std::unordered_set<std::string> dirs(const std::vector<std::string> &files);
62 
66 template <typename T>
67 std::string longest_common_path(const T &dirs, char sep = '/')
68 {
69  auto it = dirs.cbegin();
70  size_t n = it->size();
71  const std::string &cmp = *it;
72  for (it++; it != dirs.end(); it++) {
73  auto p = std::mismatch(cmp.begin(), cmp.end(), it->begin());
74  if ((p.first - cmp.begin()) < ssize_t(n)) {
75  n = p.first - cmp.begin();
76  }
77  }
78  return cmp.substr(
79  0, std::max(1UL, (n == dirs.cbegin()->size())
80  ? ((dirs.cbegin()->back() == '/') ? n - 1 : n)
81  : cmp.rfind(sep, n)));
82 }
83 
92 std::string tmpfile(std::string &path);
93 }
94 }
95 
96 #endif /* CYPRESS_UTIL_FILESYSTEM_HPP */
std::unordered_set< std::string > dirs(const std::vector< std::string > &files)
std::string canonicalise(const std::string &file)
std::string longest_common_path(const T &dirs, char sep= '/')
Definition: filesystem.hpp:67
void canonicalise_files(T &files)
Definition: filesystem.hpp:45
Definition: brainscales_lib.hpp:39
std::string tmpfile(std::string &path)