Files.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__FILES
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__FILES
19 
21 
22 #include <boost/iostreams/filtering_streambuf.hpp>
23 #include <boost/iostreams/copy.hpp>
24 #include <boost/iostreams/filter/gzip.hpp>
25 
26 #include <sstream>
27 
28 namespace std { // NOLINT
29 
30 template <> struct hash<boost::filesystem::directory_entry> {
31  size_t operator () (const boost::filesystem::directory_entry & value) const noexcept {
32  return hash<boost::filesystem::path::string_type>()(value.path().native());
33  }
34 };
35 
36 template <> struct equal_to<boost::filesystem::directory_entry> {
37  bool operator () (const boost::filesystem::directory_entry & x,
38  const boost::filesystem::directory_entry & y) const {
39  return x == y;
40  }
41 };
42 
43 } // namespace std
44 
45 namespace boost::filesystem {
46 
52 template <typename AllocatorT>
53 inline Balau::U8String<AllocatorT> toString(const boost::filesystem::path & path) {
54  return toString<AllocatorT>(path.string());
55 }
56 
62 inline std::string toString(const boost::filesystem::path & path) {
63  return path.string();
64 }
65 
66 } // namespace boost::filesystem
67 
68 namespace Balau::Util {
69 
73 struct Files final {
75 
79  static void copy(const Resource::File & src, const Resource::File & dst) {
80  boost::filesystem::copy(src.getEntry(), dst.getEntry());
81  }
82 
84 
90  static std::vector<std::string> toLines(const Resource::File & file) {
91  if (!file.exists()) {
93  }
94 
95  boost::filesystem::ifstream inputStream(file.getEntry());
96  std::vector<std::string> lines;
97  std::string s;
98 
99  while (std::getline(inputStream, s)){
100  lines.push_back(s);
101  }
102 
103  return lines;
104  }
105 
111  static std::string readToString(const Resource::File & file) {
112  if (!file.exists()) {
114  }
115 
116  boost::filesystem::ifstream inputStream(file.getEntry());
117  std::istreambuf_iterator<char> eos;
118  return std::string(std::istreambuf_iterator<char>(inputStream), eos);
119  }
120 
126  static std::vector<char> readToVector(const Resource::File & file) {
127  if (!file.exists()) {
129  }
130 
131  boost::filesystem::ifstream inputStream(file.getEntry());
132  std::istreambuf_iterator<char> eos;
133  return std::vector<char>(std::istreambuf_iterator<char>(inputStream), eos);
134  }
135 
137 
138  Files() = delete;
139  Files(const Files &) = delete;
140  Files & operator = (const Files &) = delete;
141 };
142 
143 } // namespace Balau::Util
144 
145 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__FILES
static std::string readToString(const Resource::File &file)
Read the specified file into a string.
Definition: Files.hpp:111
static std::vector< std::string > toLines(const Resource::File &file)
Read all lines of text of the specified file into a string vector.
Definition: Files.hpp:90
Definition: NetworkTypes.hpp:39
Utility functions.
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
STL namespace.
boost::filesystem::directory_entry getEntry() const
Get the underlying directory entry for this file URI.
Definition: File.hpp:400
static void copy(const Resource::File &src, const Resource::File &dst)
Copy the contents of the source file into the destination file.
Definition: Files.hpp:79
A file on the local file system.
Definition: File.hpp:35
Definition: Files.hpp:45
static std::vector< char > readToVector(const Resource::File &file)
Read the specified file into a character vector.
Definition: Files.hpp:126
Balau exceptions for resources.
Thrown when a resource is not found.
Definition: ResourceExceptions.hpp:63
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
File utilities.
Definition: Files.hpp:73
bool exists() const
Returns true if an item exits in the file system for the file URI.
Definition: File.hpp:449