1 #ifndef ROSE_FileSystem_H
2 #define ROSE_FileSystem_H
4 #include <Rose/Exception.h>
6 #include <boost/filesystem.hpp>
7 #include <boost/lexical_cast.hpp>
8 #include <boost/regex.hpp>
18 namespace FileSystem {
24 typedef boost::filesystem::path
Path;
33 ROSE_UTIL_API
bool isExisting(
const Path &path);
36 ROSE_UTIL_API
bool isFile(
const Path &path);
60 const boost::regex &re_;
63 bool operator()(
const Path &path);
87 ROSE_UTIL_API Path
makeRelative(
const Path &path,
const Path &root = boost::filesystem::current_path());
93 ROSE_UTIL_API Path
makeAbsolute(
const Path &path,
const Path &root = boost::filesystem::current_path());
105 template<
class Select>
106 std::vector<Path>
findNames(
const Path &root, Select select) {
107 std::vector<Path> matching;
110 if (select(iter->path()))
111 matching.push_back(iter->path());
114 std::sort(matching.begin(), matching.end());
118 ROSE_UTIL_API std::vector<Path>
findNames(
const Path &root);
134 template<
class Select,
class 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()))
144 std::sort(matching.begin(), matching.end());
148 template<
class Select>
159 ROSE_UTIL_API
void copyFile(
const Path &sourceFileName,
const Path &destinationFileName);
171 ROSE_UTIL_API
void copyFiles(
const std::vector<Path> &files,
const Path &root,
const Path &destinationDirectory);
178 template<
class Select,
class Descend>
181 files.erase(files.begin(), std::remove_if(files.begin(), files.end(),
isFile));
191 ROSE_UTIL_API std::string
toString(
const Path&);
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);
202 throw Exception(
"unable to open file " + boost::lexical_cast<std::string>(fileName));
204 std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
206 throw Exception(
"unable to read from file " + boost::lexical_cast<std::string>(fileName));
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);
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);
224 throw Exception(
"unable to write to file " + boost::lexical_cast<std::string>(fileName));
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.