ROSE  0.11.145.0
Rose/FileSystem.h
1 #ifndef ROSE_FileSystem_H
2 #define ROSE_FileSystem_H
3 
4 #include <Rose/Exception.h>
5 
6 #include <boost/filesystem.hpp>
7 #include <boost/lexical_cast.hpp>
8 #include <boost/regex.hpp>
9 #include <fstream>
10 #include <streambuf>
11 #include <string>
12 #include <vector>
13 #include "rosedll.h"
14 
15 namespace Rose {
16 
18 namespace FileSystem {
19 
21 extern const char *tempNamePattern;
22 
24 typedef boost::filesystem::path Path;
25 
27 typedef boost::filesystem::directory_iterator DirectoryIterator;
28 
30 typedef boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator;
31 
33 ROSE_UTIL_API bool isExisting(const Path &path);
34 
36 ROSE_UTIL_API bool isFile(const Path &path);
37 
39 ROSE_UTIL_API bool isDirectory(const Path &path);
40 
42 ROSE_UTIL_API bool isSymbolicLink(const Path &path);
43 
45 ROSE_UTIL_API bool isNotSymbolicLink(const Path &path);
46 
59 class ROSE_UTIL_API baseNameMatches {
60  const boost::regex &re_;
61 public:
62  baseNameMatches(const boost::regex &re): re_(re) {}
63  bool operator()(const Path &path);
64 };
65 
72 ROSE_UTIL_API Path createTemporaryDirectory();
73 
82 ROSE_UTIL_API Path makeNormal(const Path&);
83 
87 ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root = boost::filesystem::current_path());
88 
93 ROSE_UTIL_API Path makeAbsolute(const Path &path, const Path &root = boost::filesystem::current_path());
94 
105 template<class Select>
106 std::vector<Path> findNames(const Path &root, Select select) {
107  std::vector<Path> matching;
108  if (isDirectory(root)) {
109  for (DirectoryIterator iter(root); iter!=DirectoryIterator(); ++iter) {
110  if (select(iter->path()))
111  matching.push_back(iter->path());
112  }
113  }
114  std::sort(matching.begin(), matching.end());
115  return matching;
116 }
117 
118 ROSE_UTIL_API std::vector<Path> findNames(const Path &root);
134 template<class Select, class Descend>
135 std::vector<Path> findNamesRecursively(const Path &root, Select select, Descend descend) {
136  std::vector<Path> matching;
137  RecursiveDirectoryIterator end;
138  for (RecursiveDirectoryIterator dentry(root); dentry!=end; ++dentry) {
139  if (select(dentry->path()))
140  matching.push_back(dentry->path());
141  if (!descend(dentry->path()))
142  dentry.no_push();
143  }
144  std::sort(matching.begin(), matching.end());
145  return matching;
146 }
147 
148 template<class Select>
149 std::vector<Path> findNamesRecursively(const Path &root, Select select) {
150  return findNamesRecursively(root, select, isDirectory);
151 }
152 
153 ROSE_UTIL_API std::vector<Path> findNamesRecursively(const Path &root);
159 ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName);
160 
171 ROSE_UTIL_API void copyFiles(const std::vector<Path> &files, const Path &root, const Path &destinationDirectory);
172 
178 template<class Select, class Descend>
179 void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend) {
180  std::vector<Path> files = findNamesRecursively(root, select, descend);
181  files.erase(files.begin(), std::remove_if(files.begin(), files.end(), isFile)); // keep only isFile names
182  copyFiles(files, root, destination);
183 }
184 
186 ROSE_UTIL_API std::vector<Path> findRoseFilesRecursively(const Path &root);
187 
191 ROSE_UTIL_API std::string toString(const Path&);
192 
196 template<class Container>
197 Container readFile(const boost::filesystem::path &fileName,
198  std::ios_base::openmode openMode = std::ios_base::in | std::ios_base::binary) {
199  using streamIterator = std::istreambuf_iterator<char>;
200  std::ifstream stream(fileName.c_str(), openMode);
201  if (!stream.good())
202  throw Exception("unable to open file " + boost::lexical_cast<std::string>(fileName));
203  Container container;
204  std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
205  if (stream.fail())
206  throw Exception("unable to read from file " + boost::lexical_cast<std::string>(fileName));
207  return container;
208 }
209 
214 template<class Container>
215 void writeFile(const boost::filesystem::path &fileName, const Container &data,
216  std::ios_base::openmode openMode = std::ios_base::out | std::ios_base::binary) {
217  std::ofstream stream(fileName.c_str(), openMode);
218  if (!stream.good())
219  throw Exception("unable to open file " + boost::lexical_cast<std::string>(fileName));
220  std::ostream_iterator<char> streamIterator(stream);
221  std::copy(data.begin(), data.end(), streamIterator);
222  stream.close();
223  if (stream.fail())
224  throw Exception("unable to write to file " + boost::lexical_cast<std::string>(fileName));
225 }
226 
227 } // namespace
228 } // namespace
229 
230 #endif
Predicate returning true for matching names.
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.
ROSE_UTIL_API Path makeNormal(const Path &)
Normalize a path name.
boost::filesystem::path Path
Name of entities in a filesystem.
void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend)
Recursively copy files.
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.
ROSE_UTIL_API bool isDirectory(const Path &path)
Predicate returning true for existing directories.
Container readFile(const boost::filesystem::path &fileName, std::ios_base::openmode openMode=std::ios_base::in|std::ios_base::binary)
Load an entire file into an STL container.
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.
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.
ROSE_UTIL_API std::vector< Path > findRoseFilesRecursively(const Path &root)
Return a list of all rose_* files.
void writeFile(const boost::filesystem::path &fileName, const Container &data, std::ios_base::openmode openMode=std::ios_base::out|std::ios_base::binary)
Write a container to a file.
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.
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9