lfilesystem  0.0.1
C++ filesystem library
limes::files::CFile Class Referencefinal

This class is a wrapper around a C-style FILE* that takes care of freeing the file when the object is destroyed, and provides a few other convenience methods. More...

#include <lfilesystem_CFile.h>

Public Types

enum class  Mode {
  Read , Write , Append , ReadExtended ,
  WriteExtended , AppendExtended
}
 Represents possible modes a file can be opened in. More...
 

Public Member Functions

 CFile (const CFile &)=delete
 
 ~CFile () noexcept
 Destructor. More...
 
void close () noexcept
 If the file is currently open, this closes it by calling fclose() . More...
 
File getFile () const
 Returns a File object representing this file. More...
 
bool isOpen () const noexcept
 Returns true if the file is currently open. More...
 
bool open (const Path &filepath, Mode mode) noexcept
 Closes the current file (if one is open) and opens the file at the specified path. More...
 
 operator bool () const noexcept
 Evaluates to true if the file is currently open. More...
 
CFileoperator= (CFile &&other) noexcept
 Move assignment operator. More...
 
CFileoperator= (const CFile &)=delete
 
Constructors
 CFile ()=default
 Creates a default CFile that holds a nullptr. More...
 
 CFile (std::FILE *fileHandle) noexcept
 Creates a CFile that takes ownership of the passed FILE* . More...
 
 CFile (const Path &filepath, Mode mode) noexcept
 Creates a CFile referencing the given filepath, in the given mode. More...
 
 CFile (CFile &&other) noexcept
 Move constructor. More...
 
Accessors
std::FILE * get () const noexcept
 Returns the pointer this object holds. More...
 
std::FILE * operator-> () const noexcept
 Returns the pointer this object holds. More...
 
std::FILE & operator* () const
 Dereferences the pointer this object holds. More...
 
Path getPath () const
 Returns the path of the file this object represents. More...
 
 operator std::FILE * () const noexcept
 Returns the pointer held by this object. More...
 

Static Public Member Functions

static CFile createTempFile ()
 Creates an automatically named, self-deleting temporary file using the C function std::tmpfile . More...
 

Detailed Description

This class is a wrapper around a C-style FILE* that takes care of freeing the file when the object is destroyed, and provides a few other convenience methods.

This class is intended as a way to allow interfacing with C APIs that require a FILE* while maintaining RAII-style resource management.

For example:

const limes::files::File myFile { "/path/to/some/file" };
const auto cFile = myFile.getCfile();
call_some_c_api (cFile.get());
This class represents a file on the filesystem.
CFile getCfile(CFile::Mode mode=CFile::Mode::Read) const noexcept
Returns a CFile referring to this filepath.
See also
File

Definition at line 50 of file lfilesystem_CFile.h.

Member Enumeration Documentation

◆ Mode

Represents possible modes a file can be opened in.

Enumerator
Read 

Reads the file from the start.

Write 

Write to the file. Creates a new file if this path didn't exist; overwrites the file's contents if it did exist.

Append 

Append to the file. A new file will be created if this path did not exist.

ReadExtended 

The same as Read, but in extended mode.

WriteExtended 

The same as Write, but in extended mode.

AppendExtended 

The same as Append, but in extended mode.

Definition at line 54 of file lfilesystem_CFile.h.

Constructor & Destructor Documentation

◆ CFile() [1/4]

limes::files::CFile::CFile ( )
default

Creates a default CFile that holds a nullptr.

Call open() to actually open a file.

◆ CFile() [2/4]

limes::files::CFile::CFile ( std::FILE *  fileHandle)
explicitnoexcept

Creates a CFile that takes ownership of the passed FILE* .

Note
The CFile object will take ownership of the passed pointer, so you should not delete it manually!

◆ CFile() [3/4]

limes::files::CFile::CFile ( const Path filepath,
Mode  mode 
)
explicitnoexcept

Creates a CFile referencing the given filepath, in the given mode.

fopen() is called to open the file.

See also
open()

◆ CFile() [4/4]

limes::files::CFile::CFile ( CFile &&  other)
noexcept

Move constructor.

◆ ~CFile()

limes::files::CFile::~CFile ( )
noexcept

Destructor.

If the file is open when the destructor is called, the destructor will close the file.

See also
close()

Member Function Documentation

◆ close()

void limes::files::CFile::close ( )
noexcept

If the file is currently open, this closes it by calling fclose() .

Postcondition
Calling get() after calling this function returns a nullptr and calling getPath() after calling this function returns an empty path.

◆ createTempFile()

static CFile limes::files::CFile::createTempFile ( )
static

Creates an automatically named, self-deleting temporary file using the C function std::tmpfile .

See also
TempFile

◆ get()

std::FILE* limes::files::CFile::get ( ) const
noexcept

Returns the pointer this object holds.

Note that the CFile object still retains ownership of this pointer, you should not free it!

◆ getFile()

File limes::files::CFile::getFile ( ) const

Returns a File object representing this file.

Note that if getPath() returns an empty path, then this function will return an invalid File object. The returned File object will also be invalid if no file handle was open when you called this function.

◆ getPath()

Path limes::files::CFile::getPath ( ) const

Returns the path of the file this object represents.

Returns an empty path if no file is currently open.

◆ isOpen()

bool limes::files::CFile::isOpen ( ) const
noexcept

Returns true if the file is currently open.

◆ open()

bool limes::files::CFile::open ( const Path filepath,
Mode  mode 
)
noexcept

Closes the current file (if one is open) and opens the file at the specified path.

fopen() is called to open the file.

Returns
True if opening the file was successful

◆ operator bool()

limes::files::CFile::operator bool ( ) const
explicitnoexcept

Evaluates to true if the file is currently open.

◆ operator std::FILE *()

limes::files::CFile::operator std::FILE * ( ) const
noexcept

Returns the pointer held by this object.

◆ operator*()

std::FILE& limes::files::CFile::operator* ( ) const

Dereferences the pointer this object holds.

Note that the pointer may be null, so be careful!

◆ operator->()

std::FILE* limes::files::CFile::operator-> ( ) const
noexcept

Returns the pointer this object holds.

◆ operator=()

CFile& limes::files::CFile::operator= ( CFile &&  other)
noexcept

Move assignment operator.


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