ROSE  0.11.96.11
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;
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 
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);
199  if (!stream.good())
200  throw Exception("unable to open file " + boost::lexical_cast<std::string>(fileName));
201  Container container;
202  std::copy(streamIterator(stream), streamIterator(), std::back_inserter(container));
203  if (stream.fail())
204  throw Exception("unable to read from file " + boost::lexical_cast<std::string>(fileName));
205  return container;
206 }
207 
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);
212  if (!stream.good())
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);
216  stream.close();
217  if (stream.fail())
218  throw Exception("unable to write to file " + boost::lexical_cast<std::string>(fileName));
219 }
220 
221 } // namespace
222 } // namespace
223 
224 #endif
Rose::FileSystem::makeAbsolute
ROSE_UTIL_API Path makeAbsolute(const Path &path, const Path &root=boost::filesystem::current_path())
Make path absolute.
Rose::FileSystem::tempNamePattern
const char * tempNamePattern
Pattern to use when creating temporary files.
Rose::FileSystem::Path
boost::filesystem::path Path
Name of entities in a filesystem.
Definition: Rose/FileSystem.h:24
Rose::FileSystem::toString
ROSE_UTIL_API std::string toString(const Path &)
Convert a path to a string.
Rose::FileSystem::isSymbolicLink
ROSE_UTIL_API bool isSymbolicLink(const Path &path)
Predicate returning true for existing symbolic links.
Rose::FileSystem::copyFilesRecursively
void copyFilesRecursively(const Path &root, const Path &destination, Select select, Descend descend)
Recursively copy files.
Definition: Rose/FileSystem.h:179
Rose::FileSystem::isExisting
ROSE_UTIL_API bool isExisting(const Path &path)
Predicate returning true if path exists.
Rose::Exception
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9
Rose::FileSystem::readFile
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.
Definition: Rose/FileSystem.h:195
Rose::FileSystem::RecursiveDirectoryIterator
boost::filesystem::recursive_directory_iterator RecursiveDirectoryIterator
Iterate recursively into subdirectories.
Definition: Rose/FileSystem.h:30
Rose::FileSystem::isDirectory
ROSE_UTIL_API bool isDirectory(const Path &path)
Predicate returning true for existing directories.
Rose::FileSystem::copyFile
ROSE_UTIL_API void copyFile(const Path &sourceFileName, const Path &destinationFileName)
Copy a file.
Rose::FileSystem::copyFiles
ROSE_UTIL_API void copyFiles(const std::vector< Path > &files, const Path &root, const Path &destinationDirectory)
Copy files from one directory to another.
Rose::FileSystem::createTemporaryDirectory
ROSE_UTIL_API Path createTemporaryDirectory()
Create a temporary directory.
Rose::FileSystem::DirectoryIterator
boost::filesystem::directory_iterator DirectoryIterator
Iterate over directory contents non-recursively.
Definition: Rose/FileSystem.h:27
Rose::FileSystem::isFile
ROSE_UTIL_API bool isFile(const Path &path)
Predicate returning true for existing regular files.
Rose
Main namespace for the ROSE library.
Definition: BinaryTutorial.dox:3
Rose::FileSystem::isNotSymbolicLink
ROSE_UTIL_API bool isNotSymbolicLink(const Path &path)
Predicate returning inverse of isSymbolicLink.
Rose::FileSystem::makeRelative
ROSE_UTIL_API Path makeRelative(const Path &path, const Path &root=boost::filesystem::current_path())
Make path relative.
Rose::FileSystem::findNamesRecursively
std::vector< Path > findNamesRecursively(const Path &root, Select select, Descend descend)
Recursive list of names satisfying predicate.
Definition: Rose/FileSystem.h:135
Rose::FileSystem::baseNameMatches
Predicate returning true for matching names.
Definition: Rose/FileSystem.h:59
Rose::FileSystem::findNames
std::vector< Path > findNames(const Path &root, Select select)
Entries within a directory.
Definition: Rose/FileSystem.h:106
Rose::FileSystem::findRoseFilesRecursively
ROSE_UTIL_API std::vector< Path > findRoseFilesRecursively(const Path &root)
Return a list of all rose_* files.
Rose::FileSystem::makeNormal
ROSE_UTIL_API Path makeNormal(const Path &)
Normalize a path name.