wex v24.10.0
Public Member Functions | Protected Member Functions | List of all members
wex::file Class Reference

Offers several methods to read / write files. More...

#include <wex/core/file.h>

Inheritance diagram for wex::file:
Inheritance graph
[legend]

Public Member Functions

 file ()
 Default constructor.
 
 file (const path &p)
 Constructor taking a path.
 
 file (const char *filename)
 Constructor taking a filename.
 
 file (const path &p, std::ios_base::openmode mode)
 Constructor taking a path, opens the file.
 
 file (const char *filename, std::ios_base::openmode mode)
 Constructor taking a filename, opens the file.
 
 file (const file &rhs)
 Copy constructor.
 
virtual ~file ()=default
 Destructor.
 
fileoperator= (const file &f)
 Assignment operator.
 
bool check_sync ()
 Checks whether this file can be synced, and syncs (invokes do_file_load) the file if so.
 
bool close ()
 Closes the file.
 
bool file_load (const path &p)
 Sets the path, opens the file if asked for, invokes do_file_load, and closes the file again (unless use_stream was invoked).
 
bool file_new (const path &p)
 Sets the path and invokes do_file_new.
 
bool file_save (const wex::path &p=wex::path())
 Sets the path if path is ok, opens the file if asked for, invokes do_file_save, and closes the file again.
 
bool is_open () const
 Returns true if file is open.
 
bool is_written () const
 Returns true if file has been written.
 
bool open (std::ios_base::openmode mode=std::ios_base::in)
 Opens current path.
 
bool open (const path &p, std::ios_base::openmode mode=std::ios_base::in)
 Opens specified path.
 
const wex::pathpath () const
 Returns the path.
 
wex::pathpath ()
 Returns the path.
 
const std::string * read (std::streampos seek_position=0)
 Reads this file into a buffer.
 
std::fstream & stream ()
 Returns stream.
 
void use_stream (bool use=true)
 Default file is closed after loading, if you call this method, stream remains open.
 
bool write (std::span< const char > buffer)
 Writes file from buffer.
 
bool write (const std::string &s)
 Writes file from string.
 
virtual bool is_contents_changed () const
 Returns whether contents have been changed.
 
virtual void reset_contents_changed ()
 Resets contents changed.
 

Protected Member Functions

virtual bool do_file_load (bool synced=false)
 Invoked by file_load, allows you to load the file.
 
virtual void do_file_new ()
 Invoked by file_new, allows you to make a new (empty) file.
 
virtual void do_file_save (bool save_as=false)
 Invoked by file_save, allows you to save the file.
 

Detailed Description

Offers several methods to read / write files.

Also takes care of synchronization, all you have to do is call check_sync once in a while.

Member Function Documentation

◆ check_sync()

bool wex::file::check_sync ( )

Checks whether this file can be synced, and syncs (invokes do_file_load) the file if so.

Returns true if this file was synced.

◆ do_file_load()

virtual bool wex::file::do_file_load ( bool synced = false)
inlineprotectedvirtual

Invoked by file_load, allows you to load the file.

The file is already opened, so you can call Read. If synced is true, this call was a result of check_sync and not of file_load.

◆ do_file_save()

virtual void wex::file::do_file_save ( bool save_as = false)
protectedvirtual

Invoked by file_save, allows you to save the file.

The file is already opened. The default save_as copies the file to new file.