ROSE 0.11.145.192
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
15namespace Rose {
16
18namespace FileSystem {
19
21extern const char *tempNamePattern;
22
24typedef boost::filesystem::path Path;
25
27typedef boost::filesystem::directory_iterator DirectoryIterator;
28
30typedef boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator;
31
33ROSE_UTIL_API bool isExisting(const Path &path);
34
36ROSE_UTIL_API bool isFile(const Path &path);
37
39ROSE_UTIL_API bool isDirectory(const Path &path);
40
42ROSE_UTIL_API bool isSymbolicLink(const Path &path);
43
45ROSE_UTIL_API bool isNotSymbolicLink(const Path &path);
46
59class ROSE_UTIL_API baseNameMatches {
60 const boost::regex &re_;
61public:
62 baseNameMatches(const boost::regex &re): re_(re) {}
63 bool operator()(const Path &path);
64};
65
73
82ROSE_UTIL_API Path makeNormal(const Path&);
83
87ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root = boost::filesystem::current_path());
88
93ROSE_UTIL_API Path makeAbsolute(const Path &path, const Path &root = boost::filesystem::current_path());
94
105template<class Select>
106std::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
118ROSE_UTIL_API std::vector<Path> findNames(const Path &root);
134template<class Select, class Descend>
135std::vector<Path> findNamesRecursively(const Path &root, Select select, Descend descend) {
136 std::vector<Path> matching;
138 for (RecursiveDirectoryIterator dentry(root); dentry!=end; ++dentry) {
139 if (select(dentry->path())) {
140 matching.push_back(dentry->path());
141 }
142 if (!descend(dentry->path())) {
143 dentry.disable_recursion_pending();
144 }
145 }
146 std::sort(matching.begin(), matching.end());
147 return matching;
148}
149
150template<class Select>
151std::vector<Path> findNamesRecursively(const Path &root, Select select) {
152 return findNamesRecursively(root, select, isDirectory);
153}
154
155ROSE_UTIL_API std::vector<Path> findNamesRecursively(const Path &root);
161ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName);
162
173ROSE_UTIL_API void copyFiles(const std::vector<Path> &files, const Path &root, const Path &destinationDirectory);
174
180template<class Select, class Descend>
181void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend) {
182 std::vector<Path> files = findNamesRecursively(root, select, descend);
183 files.erase(files.begin(), std::remove_if(files.begin(), files.end(), isFile)); // keep only isFile names
184 copyFiles(files, root, destination);
185}
186
188ROSE_UTIL_API std::vector<Path> findRoseFilesRecursively(const Path &root);
189
193ROSE_UTIL_API std::string toString(const Path&);
194
198template<class Container>
199Container readFile(const boost::filesystem::path &fileName,
200 std::ios_base::openmode openMode = std::ios_base::in | std::ios_base::binary) {
201 using streamIterator = std::istreambuf_iterator<char>;
202 std::ifstream stream(fileName.c_str(), openMode);
203 if (!stream.good())
204 throw Exception("unable to open file " + boost::lexical_cast<std::string>(fileName));
205 Container container;
206 std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
207 if (stream.fail())
208 throw Exception("unable to read from file " + boost::lexical_cast<std::string>(fileName));
209 return container;
210}
211
216template<class Container>
217void writeFile(const boost::filesystem::path &fileName, const Container &data,
218 std::ios_base::openmode openMode = std::ios_base::out | std::ios_base::binary) {
219 std::ofstream stream(fileName.c_str(), openMode);
220 if (!stream.good())
221 throw Exception("unable to open file " + boost::lexical_cast<std::string>(fileName));
222 std::ostream_iterator<char> streamIterator(stream);
223 std::copy(data.begin(), data.end(), streamIterator);
224 stream.close();
225 if (stream.fail())
226 throw Exception("unable to write to file " + boost::lexical_cast<std::string>(fileName));
227}
228
229} // namespace
230} // namespace
231
232#endif
Base class for all ROSE exceptions.
Predicate returning true for matching names.
const char * tempNamePattern
Pattern to use when creating temporary files.
std::vector< Path > findNamesRecursively(const Path &root, Select select, Descend descend)
Recursive list of names satisfying predicate.
ROSE_UTIL_API bool isDirectory(const Path &path)
Predicate returning true for existing directories.
ROSE_UTIL_API bool isNotSymbolicLink(const Path &path)
Predicate returning inverse of isSymbolicLink.
void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend)
Recursively copy files.
ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root=boost::filesystem::current_path())
Make path relative.
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 Path makeAbsolute(const Path &path, const Path &root=boost::filesystem::current_path())
Make path absolute.
ROSE_UTIL_API std::vector< Path > findRoseFilesRecursively(const Path &root)
Return a list of all rose_* files.
boost::filesystem::directory_iterator DirectoryIterator
Iterate over directory contents non-recursively.
ROSE_UTIL_API Path makeNormal(const Path &)
Normalize a path name.
ROSE_UTIL_API Path createTemporaryDirectory()
Create a temporary directory.
std::vector< Path > findNames(const Path &root, Select select)
Entries within a directory.
ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName)
Copy a file.
ROSE_UTIL_API bool isFile(const Path &path)
Predicate returning true for existing regular files.
ROSE_UTIL_API bool isExisting(const Path &path)
Predicate returning true if path exists.
ROSE_UTIL_API bool isSymbolicLink(const Path &path)
Predicate returning true for existing symbolic links.
boost::filesystem::path Path
Name of entities in a filesystem.
ROSE_UTIL_API std::string toString(const Path &)
Convert a path to a string.
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.
boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator
Iterate recursively into subdirectories.
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.
The ROSE library.