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>
24typedef boost::filesystem::path
Path;
60 const boost::regex &re_;
63 bool operator()(
const Path &path);
105template<
class Select>
107 std::vector<Path> matching;
110 if (select(iter->path()))
111 matching.push_back(iter->path());
114 std::sort(matching.begin(), matching.end());
134template<
class Select,
class Descend>
136 std::vector<Path> matching;
139 if (select(dentry->path())) {
140 matching.push_back(dentry->path());
142 if (!descend(dentry->path())) {
143 dentry.disable_recursion_pending();
146 std::sort(matching.begin(), matching.end());
150template<
class Select>
173ROSE_UTIL_API
void copyFiles(
const std::vector<Path> &files,
const Path &root,
const Path &destinationDirectory);
180template<
class Select,
class Descend>
183 files.erase(files.begin(), std::remove_if(files.begin(), files.end(),
isFile));
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);
204 throw Exception(
"unable to open file " + boost::lexical_cast<std::string>(fileName));
206 std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
208 throw Exception(
"unable to read from file " + boost::lexical_cast<std::string>(fileName));
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);
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);
226 throw Exception(
"unable to write to file " + boost::lexical_cast<std::string>(fileName));
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.