ROSE  0.11.33.0
FileSystem.h
1 #ifndef ROSE_FileSystem_H
2 #define ROSE_FileSystem_H
3 
4 #include <boost/filesystem.hpp>
5 #include <boost/regex.hpp>
6 #include <vector>
7 #include "rosedll.h"
8 
9 namespace Rose {
10 
12 namespace FileSystem {
13 
15 extern const char *tempNamePattern;
16 
18 typedef boost::filesystem::path Path;
19 
21 typedef boost::filesystem::directory_iterator DirectoryIterator;
22 
24 typedef boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator;
25 
27 ROSE_UTIL_API bool isExisting(const Path &path);
28 
30 ROSE_UTIL_API bool isFile(const Path &path);
31 
33 ROSE_UTIL_API bool isDirectory(const Path &path);
34 
36 ROSE_UTIL_API bool isSymbolicLink(const Path &path);
37 
39 ROSE_UTIL_API bool isNotSymbolicLink(const Path &path);
40 
53 class ROSE_UTIL_API baseNameMatches {
54  const boost::regex &re_;
55 public:
56  baseNameMatches(const boost::regex &re): re_(re) {}
57  bool operator()(const Path &path);
58 };
59 
66 ROSE_UTIL_API Path createTemporaryDirectory();
67 
76 ROSE_UTIL_API Path makeNormal(const Path&);
77 
81 ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root = boost::filesystem::current_path());
82 
87 ROSE_UTIL_API Path makeAbsolute(const Path &path, const Path &root = boost::filesystem::current_path());
88 
99 template<class Select>
100 std::vector<Path> findNames(const Path &root, Select select) {
101  std::vector<Path> matching;
102  if (isDirectory(root)) {
103  for (DirectoryIterator iter(root); iter!=DirectoryIterator(); ++iter) {
104  if (select(iter->path()))
105  matching.push_back(iter->path());
106  }
107  }
108  std::sort(matching.begin(), matching.end());
109  return matching;
110 }
111 
112 ROSE_UTIL_API std::vector<Path> findNames(const Path &root);
128 template<class Select, class Descend>
129 std::vector<Path> findNamesRecursively(const Path &root, Select select, Descend descend) {
130  std::vector<Path> matching;
131  RecursiveDirectoryIterator end;
132  for (RecursiveDirectoryIterator dentry(root); dentry!=end; ++dentry) {
133  if (select(dentry->path()))
134  matching.push_back(dentry->path());
135  if (!descend(dentry->path()))
136  dentry.no_push();
137  }
138  std::sort(matching.begin(), matching.end());
139  return matching;
140 }
141 
142 template<class Select>
143 std::vector<Path> findNamesRecursively(const Path &root, Select select) {
144  return findNamesRecursively(root, select, isDirectory);
145 }
146 
147 ROSE_UTIL_API std::vector<Path> findNamesRecursively(const Path &root);
153 ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName);
154 
165 ROSE_UTIL_API void copyFiles(const std::vector<Path> &files, const Path &root, const Path &destinationDirectory);
166 
172 template<class Select, class Descend>
173 void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend) {
174  std::vector<Path> files = findNamesRecursively(root, select, descend);
175  files.erase(files.begin(), std::remove_if(files.begin(), files.end(), isFile)); // keep only isFile names
176  copyFiles(files, root, destination);
177 }
178 
180 ROSE_UTIL_API std::vector<Path> findRoseFilesRecursively(const Path &root);
181 
185 ROSE_UTIL_API std::string toString(const Path&);
186 
187 
188 } // namespace
189 } // namespace
190 
191 #endif
Predicate returning true for matching names.
Definition: FileSystem.h:53
ROSE_UTIL_API bool isExisting(const Path &path)
Predicate returning true if path exists.
std::vector< Path > findNamesRecursively(const Path &root, Select select, Descend descend)
Recursive list of names satisfying predicate.
Definition: FileSystem.h:129
ROSE_UTIL_API Path makeNormal(const Path &)
Normalize a path name.
boost::filesystem::path Path
Name of entities in a filesystem.
Definition: FileSystem.h:18
void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend)
Recursively copy files.
Definition: FileSystem.h:173
ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName)
Copy a file.
ROSE_UTIL_API Path createTemporaryDirectory()
Create a temporary directory.
boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator
Iterate recursively into subdirectories.
Definition: FileSystem.h:24
ROSE_UTIL_API bool isDirectory(const Path &path)
Predicate returning true for existing directories.
Main namespace for the ROSE library.
const char * tempNamePattern
Pattern to use when creating temporary files.
boost::filesystem::directory_iterator DirectoryIterator
Iterate over directory contents non-recursively.
Definition: FileSystem.h:21
ROSE_UTIL_API std::string toString(const Path &)
Convert a path to a string.
ROSE_UTIL_API void copyFiles(const std::vector< Path > &files, const Path &root, const Path &destinationDirectory)
Copy files from one directory to another.
ROSE_UTIL_API bool isSymbolicLink(const Path &path)
Predicate returning true for existing symbolic links.
ROSE_UTIL_API bool isFile(const Path &path)
Predicate returning true for existing regular files.
std::vector< Path > findNames(const Path &root, Select select)
Entries within a directory.
Definition: FileSystem.h:100
ROSE_UTIL_API std::vector< Path > findRoseFilesRecursively(const Path &root)
Return a list of all rose_* files.
ROSE_UTIL_API bool isNotSymbolicLink(const Path &path)
Predicate returning inverse of isSymbolicLink.
ROSE_UTIL_API Path makeAbsolute(const Path &path, const Path &root=boost::filesystem::current_path())
Make path absolute.
ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root=boost::filesystem::current_path())
Make path relative.