libfilezilla
file.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_FILE_HEADER
2#define LIBFILEZILLA_FILE_HEADER
3
4#include "libfilezilla.hpp"
5
6#ifdef FZ_WINDOWS
7#include "glue/windows.hpp"
8#endif
9
14#include <stdint.h>
15
16namespace fz {
17
25class FZ_PUBLIC_SYMBOL file final
26{
27public:
29 enum mode {
30 reading,
31 writing
32 };
33
42 existing = 0x1,
43
45 empty = 0x2,
46
53 current_user_only = 0x4,
54
65 current_user_and_admins_only = 0x8
66 };
67
68 file() = default;
69 file(native_string const& f, mode m, creation_flags d = existing);
70
71 ~file();
72
73 file(file const&) = delete;
74 file& operator=(file const&) = delete;
75
76 file(file && op) noexcept;
77 file& operator=(file && op) noexcept;
78
79 bool opened() const;
80 explicit operator bool() const { return opened(); }
81
82 bool open(native_string const& f, mode m, creation_flags d = existing);
83
84 void close();
85
87 enum seek_mode {
90
93
95 end
96 };
97
101 int64_t size() const;
102
115 int64_t seek(int64_t offset, seek_mode m);
116
118 int64_t position() { return seek(0, current); }
119
125 bool truncate();
126
140 int64_t read(void *buf, int64_t count);
141
152 int64_t write(void const* buf, int64_t count);
153
159 bool fsync();
160
161private:
162#ifdef FZ_WINDOWS
163 HANDLE hFile_{INVALID_HANDLE_VALUE};
164#else
165 int fd_{-1};
166#endif
167};
168
173bool FZ_PUBLIC_SYMBOL remove_file(native_string const& name);
174
176 return static_cast<file::creation_flags>(static_cast<unsigned int>(lhs) | rhs);
177}
178
179}
180#endif
Lean class for file access.
Definition: file.hpp:26
mode
Files can be opened for reading or writing, but not both.
Definition: file.hpp:29
creation_flags
Creation flags when opening file for writing.
Definition: file.hpp:40
int64_t seek(int64_t offset, seek_mode m)
Relative seek based on seek mode.
int64_t write(void const *buf, int64_t count)
Write data to file.
int64_t read(void *buf, int64_t count)
Read data from file.
bool truncate()
Truncate the file to the current position of the file pointer.
int64_t size() const
Gets size of file.
seek_mode
Used by seek.
Definition: file.hpp:87
@ begin
Seek from beginning of file.
Definition: file.hpp:89
@ current
Seek from current position in the file.
Definition: file.hpp:92
int64_t position()
Get Current position in file.
Definition: file.hpp:118
bool fsync()
Ensure data is flushed to disk.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
bool remove_file(native_string const &name)
remove the specified file.
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:33