libfilezilla
|
Lean class for file access. More...
#include <file.hpp>
Public Types | |
enum | mode { reading , writing , readwrite } |
Files can be opened for reading, writing, or both. | |
enum | creation_flags { existing = 0x1 , empty = 0x2 , current_user_only = 0x4 , current_user_and_admins_only = 0x8 } |
Creation flags when opening file for writing. More... | |
enum | seek_mode { begin , current , end } |
Used by seek. More... | |
typedef HANDLE | file_t |
Public Member Functions | |
file (native_string const &f, mode m, creation_flags d=existing) | |
file (file_t fd) | |
Creates file from descriptor. More... | |
file (file const &)=delete | |
file & | operator= (file const &)=delete |
file (file &&op) noexcept | |
file & | operator= (file &&op) noexcept |
bool | opened () const |
operator bool () const | |
result | open (native_string const &f, mode m, creation_flags d=existing) |
void | close () |
file_t | fd () |
Returns the raw file descriptor, but retains ownership. | |
file_t | detach () |
int64_t | size () const |
Gets size of file. More... | |
int64_t | seek (int64_t offset, seek_mode m) |
Relative seek based on seek mode. More... | |
int64_t | position () |
Get Current position in file. | |
bool | truncate () |
Truncate the file to the current position of the file pointer. More... | |
int64_t | read (void *buf, int64_t count) |
Read data from file. More... | |
int64_t | write (void const *buf, int64_t count) |
Write data to file. More... | |
bool | fsync () |
Ensure data is flushed to disk. More... | |
bool | set_modification_time (datetime const &t) |
Sets modification time to specified time. More... | |
Lean class for file access.
This class uses the system's native file access functions. It is a less convoluted and much faster alternative to the almost useless std::fstream.
Supports large files exceeding the 32bit limits.
enum creation_flags |
Creation flags when opening file for writing.
Only evaluated when opening existing files for writing Non-existing files will always be created when writing. Opening for reading never creates files.
enum seek_mode |
Used by seek.
Enumerator | |
---|---|
begin | Seek from beginning of file. |
current | Seek from current position in the file. |
end | Seek from end of file. |
|
explicit |
Creates file from descriptor.
Takes ownership of descriptor/handle.
bool fsync | ( | ) |
Ensure data is flushed to disk.
int64_t read | ( | void * | buf, |
int64_t | count | ||
) |
Read data from file.
Reading from file advances the file pointer with the number of octets read.
buf | The buffer that should receive the data. Must be large enough to hold at least count octets |
count | The number of octets to read |
buf
. It may be less than count
. count
octets can happen at any time, it does not indicate EOF. int64_t seek | ( | int64_t | offset, |
seek_mode | m | ||
) |
Relative seek based on seek mode.
It is possible to seek past the end of the file. Doing so does not change the size of the file. It will only change on subsequent writes.
You can get the current position int the file by passing current
as seek_mode with a 0 offset.
bool set_modification_time | ( | datetime const & | t | ) |
Sets modification time to specified time.
File must be opened for writing, of the call will fail.
int64_t size | ( | ) | const |
Gets size of file.
bool truncate | ( | ) |
Truncate the file to the current position of the file pointer.
Despite its name, this function can extend the size of the file if the current file pointer is past the end of the file.
int64_t write | ( | void const * | buf, |
int64_t | count | ||
) |
Write data to file.
Writing to file advances the file pointer with the number of octets written
buf | The buffer that holds the data to be written. Must hold at least count octets |
count | The number of octets to write |
count
.