Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
File.h
Go to the documentation of this file.
1#pragma once
2#include <fcntl.h>
3#include <filesystem>
4#include <ftw.h>
6#include <misc/global.h>
7
8namespace sf::lnx
9{
10
15{
16 public:
20 typedef struct stat stat_type;
21
26
30 explicit File(const std::filesystem::path& path, int flags = O_CREAT | O_RDWR | O_APPEND, mode_t mode = S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP);
31
36
41 void initialize(const std::filesystem::path& path, int flags = O_CREAT | O_RDWR | O_APPEND, mode_t mode = S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP);
42
47 void open(const std::string& path, int flags = O_CREAT | O_RDWR | O_APPEND, mode_t mode = S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP);
48
54 void createTemporary(const std::string& name_tpl, int flags = O_CREAT | O_RDWR);
55
60 void allocate(size_t sz);
61
66 void open(bool reopen = false);
67
72 [[nodiscard]] bool isOpen() const;
73
78 void close();
79
84 bool close(bool exceptions);
85
91 void write(const File& f, size_t pos, size_t sz, size_t buf_sz = 0);
92
97 void write(const void* buf, size_t sz, size_t* written = nullptr);
98
99 void write(const std::string& s);
100
101 void write(const char* s);
102
108 template<typename T>
109 void write(const T& t);
110
115 ssize_t read(void* buf, size_t pos, size_t sz) const;
116
123 void read(DynamicBuffer buf, size_t pos, size_t sz) const;
124
131 template<typename T>
132 void read(T& t, size_t pos) const;
133
138 void getStat(stat_type& s) const;
139
143 [[nodiscard]] const stat_type& getStatus() const;
144
150 const stat_type& getStatus(bool update);
151
155 [[nodiscard]] std::string getStatText() const;
156
161 void rename(const std::string& path, bool assign = false);
162
167 void truncate(size_t length);
168
173 void remove();
174
179
183 [[nodiscard]] std::string getPath() const;
184
188 [[nodiscard]] mode_t getMode() const;
189
193 [[nodiscard]] int getFlags() const;
194
198 [[nodiscard]] int getDescriptor() const;
199
204 bool exists();
205
209 void unlink();
210
211 private:
215 int setErrorNo(int error_no);
219 std::filesystem::path _path;
223 mode_t _mode{};
227 mode_t _flags{};
231 int _descriptor{};
235 int _errorNo{};
239 stat_type _stat{};
240};
241
242inline int File::getDescriptor() const
243{
244 return _descriptor;
245}
246
247inline std::string File::getPath() const
248{
249 return _path;
250}
251
252inline mode_t File::getMode() const
253{
254 return _mode;
255}
256
257inline int File::getFlags() const
258{
259 return _flags;
260}
261
262inline void File::close()
263{
264 close(true);
265}
266
267inline const File::stat_type& File::getStatus() const
268{
269 return _stat;
270}
271
272inline const File::stat_type& File::getStatus(bool update)
273{
274 if (update)
275 {
276 getStat(_stat);
277 }
278 return _stat;
279}
280
281inline void File::write(const std::string& s)
282{
283 write(s.c_str(), s.length());
284}
285
286inline void File::write(const char* s)
287{
288 write(s, strlen(s));
289}
290
291template<typename T>
292inline void File::write(const T& t)
293{
294 write(&t, sizeof(T));
295}
296
297template<typename T>
298inline void File::read(T& t, size_t pos) const
299{
300 read(&t, pos, sizeof(T));
301}
302
303inline bool File::isOpen() const
304{
305 return _descriptor != -1;
306}
307
311_MISC_FUNC std::ostream& operator<<(std::ostream& stream, const File& file);
312
313}// namespace sf::lnx
Definition File.h:15
mode_t getMode() const
Definition File.h:252
bool isOpen() const
Definition File.h:303
struct stat stat_type
Definition File.h:20
void write(const void *buf, size_t sz, size_t *written=nullptr)
bool close(bool exceptions)
void synchronise()
void getStat(stat_type &s) const
void truncate(size_t length)
void rename(const std::string &path, bool assign=false)
File(const std::filesystem::path &path, int flags=O_CREAT|O_RDWR|O_APPEND, mode_t mode=S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP)
const stat_type & getStatus() const
Definition File.h:267
std::string getPath() const
Definition File.h:247
void open(const std::string &path, int flags=O_CREAT|O_RDWR|O_APPEND, mode_t mode=S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP)
std::string getStatText() const
void open(bool reopen=false)
void createTemporary(const std::string &name_tpl, int flags=O_CREAT|O_RDWR)
int getFlags() const
Definition File.h:257
void initialize(const std::filesystem::path &path, int flags=O_CREAT|O_RDWR|O_APPEND, mode_t mode=S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP)
void read(DynamicBuffer buf, size_t pos, size_t sz) const
void close()
Definition File.h:262
int getDescriptor() const
Definition File.h:242
void allocate(size_t sz)
void write(const File &f, size_t pos, size_t sz, size_t buf_sz=0)
ssize_t read(void *buf, size_t pos, size_t sz) const
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition File.h:9
_MISC_FUNC std::ostream & operator<<(std::ostream &stream, const File &file)
File class out stream operator.