crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
File.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * File.hpp
24  *
25  * Namespace for functions accessing files.
26  *
27  * Created on: Apr 25, 2019
28  * Author: ans
29  */
30 
31 #ifndef DATA_FILE_HPP_
32 #define DATA_FILE_HPP_
33 
34 #include "../Main/Exception.hpp"
35 
36 #include <fstream> // std::ifstream, std::ofstream
37 #include <ios> // std::ios_base
38 #include <streambuf> // std::istreambuf_iterator
39 #include <string> // std::string
40 
43 
44  /*
45  * DECLARATION
46  */
47 
50 
51  std::string read(const std::string& fileName, bool binary);
52  void write(const std::string& fileName, const std::string& content, bool binary);
53 
55 
56  /*
57  * CLASS FOR FILE EXCEPTIONS
58  */
59 
61 
67 
68  /*
69  * IMPLEMENTATION
70  */
71 
73 
87  inline std::string read(const std::string& fileName, bool binary) {
88  // open file for reading
89  std::ifstream in(
90  fileName.c_str(),
91  binary ? std::ios_base::binary : std::ios_base::in
92  );
93 
94  if(!in.is_open()) {
95  throw Exception(
96  "Could not open '"
97  + fileName
98  + "' for "
99  + (binary ? "binary " : "")
100  + "reading"
101  );
102  }
103 
104  // reserve memory
105  std::string result;
106 
107  in.seekg(0, std::ios::end);
108 
109  result.reserve(in.tellg());
110 
111  in.seekg(0, std::ios::beg);
112 
113  // write file content to string
114  result.assign(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
115 
116  // close file
117  in.close();
118 
119  // return string
120  return result;
121  }
122 
124 
137  inline void write(const std::string& fileName, const std::string& content, bool binary) {
138  // open file for writing
139  std::ofstream out(
140  fileName.c_str(),
141  binary ? std::ios_base::binary : std::ios_base::out
142  );
143 
144  if(!out.is_open()) {
145  throw Exception(
146  "Could not open '"
147  + fileName
148  + "' for "
149  + (binary ? "binary " : "")
150  + "writing"
151  );
152  }
153 
154  // write content to file
155  out << content;
156 
157  // close file
158  out.close();
159  }
160 
161 } /* namespace crawlservpp::Data::File */
162 
163 #endif /* DATA_FILE_HPP_ */
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Namespace for functions accessing files.
Definition: File.hpp:42
std::string read(const std::string &fileName, bool binary)
Reads the content of the given file.
Definition: File.hpp:87
void write(const std::string &fileName, const std::string &content, bool binary)
Writes the given content to the given file.
Definition: File.hpp:137
Class for file exceptions.
Definition: File.hpp:66