Enrollment 28.2.0
Loading...
Searching...
No Matches
BinaryFile.h
Go to the documentation of this file.
1#pragma once
10
11#include "enrollment/Export.h"
13#include <cstdint>
14#include <fstream>
15#include <iterator>
16#include <memory>
17#include <mutex>
18#include <ostream>
19#include <vector>
20
21namespace inno
22{
23
27 */
28 class BinaryFileAccess
29 {
30 public:
38 virtual std::vector<uint8_t> ReadAll(const std::string& path) = 0;
46 virtual void WriteAll(const std::string& path, const std::vector<uint8_t>& content) = 0;
47
48 BinaryFileAccess() = default;
49 virtual ~BinaryFileAccess() = default;
50 BinaryFileAccess(const BinaryFileAccess&) = delete;
51 BinaryFileAccess(BinaryFileAccess&&) = delete;
52 BinaryFileAccess& operator=(const BinaryFileAccess&) = delete;
53 BinaryFileAccess& operator=(BinaryFileAccess&&) = delete;
54 };
55
59 */
60 class FilesystemAccess : public BinaryFileAccess
61 {
62 public:
63 static std::shared_ptr<FilesystemAccess> Create()
64 {
65 return std::shared_ptr<FilesystemAccess>(new FilesystemAccess());
66 }
73 */
74 std::vector<uint8_t> ReadAll(const std::string& path) final
75 {
76 try
77 {
78 std::ifstream file(path, std::ios::binary | std::ios::in);
79 file.exceptions(~std::ifstream::goodbit);
80 return { (std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()) };
81 }
82 catch (...)
83 {
84 std::string errorMessage = path + ": " + std::error_code(errno, std::generic_category()).message();
85 throw FileException(std::move(errorMessage));
86 }
87 }
88
95 */
96 void WriteAll(const std::string& path, const std::vector<uint8_t>& content) final
97 {
98 try
99 {
100 std::ofstream file(path, std::ios::out | std::ios::binary);
101 file.exceptions(~std::ofstream::goodbit);
102 const std::ostream_iterator<uint8_t> outputIterator(file);
103 std::copy(content.begin(), content.end(), outputIterator);
104 }
105 catch (...)
106 {
107 std::string errorMessage = path + ": " + std::error_code(errno, std::generic_category()).message();
108 throw FileException(std::move(errorMessage));
109 }
110 }
111
112 virtual ~FilesystemAccess() = default;
113 FilesystemAccess(const FilesystemAccess&) = delete;
115 FilesystemAccess& operator=(const FilesystemAccess&) = delete;
116 FilesystemAccess& operator=(FilesystemAccess&&) = delete;
117
118 private:
119 FilesystemAccess() = default;
120 };
121
127 */
128 class GlobalFileAccess
129 {
130 public:
134 */
135 static std::shared_ptr<BinaryFileAccess> Get()
136 {
137 return GlobalFileAccess::Instance().GetInternal();
138 }
139
144 */
145 static void Set(const std::shared_ptr<BinaryFileAccess>& fileAccess)
146 {
147 if (fileAccess == nullptr)
148 {
149 throw NullArgumentException("BinaryFileAccess can not be null");
150 }
151
152 GlobalFileAccess::Instance().SetInternal(fileAccess);
153 }
154
155 GlobalFileAccess() = delete;
156 virtual ~GlobalFileAccess() = default;
157 GlobalFileAccess(const GlobalFileAccess&) = delete;
159 GlobalFileAccess& operator=(const GlobalFileAccess&) = delete;
160 GlobalFileAccess& operator=(GlobalFileAccess&&) = delete;
161
162 private:
163 std::shared_ptr<BinaryFileAccess> fa;
164 std::mutex faMux;
165
166 std::shared_ptr<BinaryFileAccess> GetInternal()
167 {
168 const std::scoped_lock lock(faMux);
169 return fa;
170 }
171
172 static GlobalFileAccess& Instance()
173 {
174 static GlobalFileAccess e(FilesystemAccess::Create());
175 return e;
176 }
177
178 void SetInternal(const std::shared_ptr<BinaryFileAccess>& fileAccess)
179 {
180 if (fileAccess != nullptr)
181 {
182 UpdateFileAccess(fileAccess);
183 }
184 else
185 {
186 UpdateFileAccess(FilesystemAccess::Create());
187 }
188 }
189
190 explicit GlobalFileAccess(std::shared_ptr<BinaryFileAccess> e)
191 : fa(std::move(e))
192 {
193 }
194
195 void UpdateFileAccess(std::shared_ptr<BinaryFileAccess> fileAccess)
196 {
197 const std::scoped_lock lock(faMux);
198 fa = std::move(fileAccess);
199 }
200 };
201
202 class BinaryFile final
203 {
204 public:
212 static std::vector<uint8_t> ReadAll(const std::string& path)
213 {
214 return GlobalFileAccess::Get()->ReadAll(path);
215 }
216
224 static void WriteAll(const std::string& path, const std::vector<uint8_t>& content)
225 {
226 GlobalFileAccess::Get()->WriteAll(path, content);
227 }
228
229 BinaryFile() = default;
230 BinaryFile(const BinaryFile&) = delete;
231 BinaryFile(BinaryFile&&) = delete;
232 BinaryFile& operator=(const BinaryFile&) = delete;
233 BinaryFile& operator=(BinaryFile&&) = delete;
234 virtual ~BinaryFile() = default;
235 };
236
237} // namespace inno
virtual std::vector< uint8_t > ReadAll(const std::string &path)=0
Reads whole file as binary to memory.
virtual void WriteAll(const std::string &path, const std::vector< uint8_t > &content)=0
Writes whole given memory to file as binary.
Exception for file processing.
Definition FileException.h:22
Standard filesystem implementation.
Definition BinaryFile.h:60
void WriteAll(const std::string &path, const std::vector< uint8_t > &content) final
Writes whole given memory to file as binary.
Definition BinaryFile.h:95
std::vector< uint8_t > ReadAll(const std::string &path) final
Reads whole file as binary to memory.
Definition BinaryFile.h:73
Holds instance of BinaryFileAccess.
Definition BinaryFile.h:128
static void Set(const std::shared_ptr< BinaryFileAccess > &fileAccess)
Sets new global BinaryFileAccess.
Definition BinaryFile.h:144
static std::shared_ptr< BinaryFileAccess > Get()
Provides current BinaryFileAccess.
Definition BinaryFile.h:134
Null argument is not allowed.
Definition EnrollmentException.h:156