libfilezilla
Public Types | Public Member Functions | List of all members
file Class Referencefinal

Lean class for file access. More...

#include <file.hpp>

Public Types

enum  mode { reading, writing }
 Files can be opened for reading or writing, but not both.
 
enum  creation_flags { existing, empty }
 Creation flags when opening file for writing. More...
 
enum  seek_mode { begin, current, end }
 Used by seek. More...
 

Public Member Functions

 file (native_string const &f, mode m, creation_flags d=existing)
 
 file (file const &)=delete
 
fileoperator= (file const &)=delete
 
bool opened () const
 
bool open (native_string const &f, mode m, creation_flags d=existing)
 
void close ()
 
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...
 

Detailed Description

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.

Member Enumeration Documentation

◆ 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.

Enumerator
existing 

Keep existing data if file exists, otherwise create new.

empty 

Truncate file if already existing, otherwise create new.

◆ seek_mode

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.

Member Function Documentation

◆ fsync()

bool fsync ( )

Ensure data is flushed to disk.

Returns
true Data has been flushed to disk.
false Data could not be flushed to disk.

◆ read()

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.

Parameters
bufThe buffer that should receive the data. Must be large enough to hold at least count octets
countThe number of octets to read
Returns
>0 The number of octets read and placed into buf. It may be less than count.
0 at EOF
-1 on error
Note
Reading less than count octets can happen at any time, it does not indicate EOF.

◆ seek()

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.

Returns
-1 on error, otherwise new absolute offset in file
Note
On failure, the new position in the file is undefined.

◆ size()

int64_t size ( ) const

Gets size of file.

Returns
Size of file or -1 on error

◆ truncate()

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.

◆ write()

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

Parameters
bufThe buffer that holds the data to be written. Must hold at least count octets
countThe number of octets to write
Returns
>=0 The number of octets written to the file. It may be less than count.
-1 on error

The documentation for this class was generated from the following file: