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;
60 const boost::regex &re_;
63 bool operator()(
const Path &path);
105 template<
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());
134 template<
class Select,
class Descend>
136 std::vector<Path> matching;
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));
194 template<
class Container>
195 Container
readFile(
const boost::filesystem::path &fileName,
196 std::ios_base::openmode openMode = std::ios_base::in | std::ios_base::binary) {
197 using streamIterator = std::istreambuf_iterator<char>;
198 std::ifstream stream(fileName.c_str(), openMode);
200 throw Exception(
"unable to open file " + boost::lexical_cast<std::string>(fileName));
202 std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
204 throw Exception(
"unable to read from file " + boost::lexical_cast<std::string>(fileName));
208 template<
class Container>
209 void writeFile(
const boost::filesystem::path &fileName,
const Container &data,
210 std::ios_base::openmode openMode = std::ios_base::out | std::ios_base::binary) {
211 std::ofstream stream(fileName.c_str(),openMode);
213 throw Exception(
"unable to open file " + boost::lexical_cast<std::string>(fileName));
214 std::ostream_iterator<char> streamIterator(stream);
215 std::copy(data.begin(), data.end(), streamIterator);
218 throw Exception(
"unable to write to file " + boost::lexical_cast<std::string>(fileName));