libfilezilla
file.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_FILE_HEADER
2 #define LIBFILEZILLA_FILE_HEADER
3 
4 #include "fsresult.hpp"
5 #include "libfilezilla.hpp"
6 
7 #ifdef FZ_WINDOWS
8 #include "glue/windows.hpp"
9 #endif
10 
15 #include <stdint.h>
16 
17 namespace fz {
18 
26 class FZ_PUBLIC_SYMBOL file final
27 {
28 public:
29 #ifdef FZ_WINDOWS
30  typedef HANDLE file_t;
31 #else
32  typedef int file_t;
33 #endif
34 
36  enum mode {
37  reading,
38  writing
39  };
40 
49  existing = 0x1,
50 
52  empty = 0x2,
53 
60  current_user_only = 0x4,
61 
72  current_user_and_admins_only = 0x8
73  };
74 
75  file() = default;
76  file(native_string const& f, mode m, creation_flags d = existing);
77 
78 
83  explicit file(file_t fd);
84 
85  ~file();
86 
87  file(file const&) = delete;
88  file& operator=(file const&) = delete;
89 
90  file(file && op) noexcept;
91  file& operator=(file && op) noexcept;
92 
93  bool opened() const;
94  explicit operator bool() const { return opened(); }
95 
96  result open(native_string const& f, mode m, creation_flags d = existing);
97 
98  void close();
99 
101  file_t fd() {
102  return fd_;
103  }
104 
105  file_t detach();
106 
108  enum seek_mode {
111 
114 
116  end
117  };
118 
122  int64_t size() const;
123 
136  int64_t seek(int64_t offset, seek_mode m);
137 
139  int64_t position() { return seek(0, current); }
140 
146  bool truncate();
147 
161  int64_t read(void *buf, int64_t count);
162 
173  int64_t write(void const* buf, int64_t count);
174 
180  bool fsync();
181 
182 private:
183 #ifdef FZ_WINDOWS
184  HANDLE fd_{INVALID_HANDLE_VALUE};
185 #else
186  int fd_{-1};
187 #endif
188 };
189 
194 bool FZ_PUBLIC_SYMBOL remove_file(native_string const& name);
195 
197  return static_cast<file::creation_flags>(static_cast<unsigned int>(lhs) | rhs);
198 }
199 
200 }
201 #endif
mode
Files can be opened for reading or writing, but not both.
Definition: file.hpp:36
Seek from current position in the file.
Definition: file.hpp:113
int64_t position()
Get Current position in file.
Definition: file.hpp:139
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
Seek from beginning of file.
Definition: file.hpp:110
bool remove_file(native_string const &name)
remove the specified file.
The namespace used by libfilezilla.
Definition: apply.hpp:17
seek_mode
Used by seek.
Definition: file.hpp:108
creation_flags
Creation flags when opening file for writing.
Definition: file.hpp:47
Lean class for file access.
Definition: file.hpp:26
Sets some global macros and further includes string.hpp.
file_t fd()
Returns the raw file descriptor, but retains ownership.
Definition: file.hpp:101