Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
cFile Class Reference

#include <File.h>

Public Types

enum  eMode { fmRead , fmWrite , fmReadWrite , fmAppend }
 The mode in which to open the file. More...
 

Public Member Functions

 cFile (const AString &iFileName, eMode iMode)
 Constructs and opens / creates the file specified, use IsOpen() to check for success. More...
 
 cFile (void)
 Simple constructor - creates an unopened file object, use Open() to open / create a real file. More...
 
void Close (void)
 
void Flush ()
 Flushes all the bufferef output into the file (only when writing) More...
 
long GetSize (void) const
 Returns the size of file, in bytes, or -1 for failure; asserts if not open. More...
 
bool IsEOF (void) const
 
bool IsOpen (void) const
 
bool Open (const AString &iFileName, eMode iMode)
 
std::basic_string< std::byte > Read (size_t a_NumBytes)
 Reads up to a_NumBytes bytes, returns the bytes actually read, or empty string on failure; asserts if not open. More...
 
int Read (void *a_Buffer, size_t a_NumBytes)
 Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read, or -1 on failure; asserts if not open. More...
 
int ReadRestOfFile (AString &a_Contents)
 Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error. More...
 
long Seek (int iPosition)
 Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open. More...
 
long Tell (void) const
 Returns the current position (bytes from file start) or -1 for failure; asserts if not open. More...
 
int Write (const void *a_Buffer, size_t a_NumBytes)
 Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written, or -1 on failure; asserts if not open. More...
 
int Write (std::string_view a_String)
 
 ~cFile ()
 Auto-closes the file, if open. More...
 

Static Public Member Functions

static AString ChangeFileExt (const AString &a_FileName, const AString &a_NewExt)
 Returns a_FileName with its extension changed to a_NewExt. More...
 
static bool Copy (const AString &a_SrcFileName, const AString &a_DstFileName)
 Copies a file, returns true if successful. More...
 
static bool CreateFolder (const AString &a_FolderPath)
 Creates a new folder with the specified name. More...
 
static bool CreateFolderRecursive (const AString &a_FolderPath)
 Creates a new folder with the specified name, creating its parents if needed. More...
 
static bool Delete (const AString &a_Path)
 Deletes a file or a folder, returns true if successful. More...
 
static bool DeleteFile (const AString &a_FileName)
 Deletes a file, returns true if successful. More...
 
static bool DeleteFolder (const AString &a_FolderName)
 Deletes a folder, returns true if successful. More...
 
static bool DeleteFolderContents (const AString &a_FolderName)
 Deletes all content from the specified folder. More...
 
static bool Exists (const AString &a_FileName)
 Returns true if the file specified exists. More...
 
static AString GetExecutableExt ()
 Returns the customary executable extension used by the current platform. More...
 
static AStringVector GetFolderContents (const AString &a_Folder)
 Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there). More...
 
static unsigned GetLastModificationTime (const AString &a_FileName)
 Returns the last modification time (in current timezone) of the specified file. More...
 
static AString GetPathSeparator ()
 Returns the path separator used by the current platform. More...
 
static long GetSize (const AString &a_FileName)
 Returns the size of the file, or a negative number on error. More...
 
static bool IsFile (const AString &a_Path)
 Returns true if the specified path is a regular file. More...
 
static bool IsFolder (const AString &a_Path)
 Returns true if the specified path is a folder. More...
 
static char PathSeparator ()
 
static AString ReadWholeFile (const AString &a_FileName)
 Returns the entire contents of the specified file as a string. More...
 
static bool Rename (const AString &a_OrigPath, const AString &a_NewPath)
 Renames a file or folder, returns true if successful. More...
 

Private Attributes

FILE * m_File
 

Detailed Description

Definition at line 37 of file File.h.

Member Enumeration Documentation

◆ eMode

The mode in which to open the file.

Enumerator
fmRead 
fmWrite 
fmReadWrite 
fmAppend 

Definition at line 52 of file File.h.

Constructor & Destructor Documentation

◆ cFile() [1/2]

cFile::cFile ( void  )

Simple constructor - creates an unopened file object, use Open() to open / create a real file.

Definition at line 20 of file File.cpp.

◆ cFile() [2/2]

cFile::cFile ( const AString iFileName,
eMode  iMode 
)

Constructs and opens / creates the file specified, use IsOpen() to check for success.

Definition at line 30 of file File.cpp.

◆ ~cFile()

cFile::~cFile ( )

Auto-closes the file, if open.

Definition at line 40 of file File.cpp.

Member Function Documentation

◆ ChangeFileExt()

AString cFile::ChangeFileExt ( const AString a_FileName,
const AString a_NewExt 
)
static

Returns a_FileName with its extension changed to a_NewExt.

a_FileName may contain path specification.

Definition at line 589 of file File.cpp.

◆ Close()

void cFile::Close ( void  )

Definition at line 102 of file File.cpp.

◆ Copy()

bool cFile::Copy ( const AString a_SrcFileName,
const AString a_DstFileName 
)
static

Copies a file, returns true if successful.

Overwrites the dest file if it already exists.

Definition at line 386 of file File.cpp.

◆ CreateFolder()

bool cFile::CreateFolder ( const AString a_FolderPath)
static

Creates a new folder with the specified name.

Returns true if successful. Path may be relative or absolute

Definition at line 454 of file File.cpp.

◆ CreateFolderRecursive()

bool cFile::CreateFolderRecursive ( const AString a_FolderPath)
static

Creates a new folder with the specified name, creating its parents if needed.

Path may be relative or absolute. Returns true if the folder exists at the end of the operation (either created, or already existed). Supports only paths that use the path separator used by the current platform (MSVC CRT supports slashes for file paths, too, but this function doesn't)

Definition at line 467 of file File.cpp.

◆ Delete()

bool cFile::Delete ( const AString a_Path)
static

Deletes a file or a folder, returns true if successful.

Prefer to use DeleteFile or DeleteFolder, since those don't have the penalty of checking whether a_Path is a folder.

Definition at line 304 of file File.cpp.

◆ DeleteFile()

bool cFile::DeleteFile ( const AString a_FileName)
static

Deletes a file, returns true if successful.

Returns false if a_FileName points to a folder.

Definition at line 368 of file File.cpp.

◆ DeleteFolder()

bool cFile::DeleteFolder ( const AString a_FolderName)
static

Deletes a folder, returns true if successful.

Returns false if a_FolderName points to a file.

Definition at line 320 of file File.cpp.

◆ DeleteFolderContents()

bool cFile::DeleteFolderContents ( const AString a_FolderName)
static

Deletes all content from the specified folder.

The specified folder itself stays intact. Returns true on success, false on failure.

Definition at line 333 of file File.cpp.

◆ Exists()

bool cFile::Exists ( const AString a_FileName)
static

Returns true if the file specified exists.

Definition at line 294 of file File.cpp.

◆ Flush()

void cFile::Flush ( )

Flushes all the bufferef output into the file (only when writing)

Definition at line 695 of file File.cpp.

◆ GetExecutableExt()

AString cFile::GetExecutableExt ( )
static

Returns the customary executable extension used by the current platform.

Definition at line 682 of file File.cpp.

◆ GetFolderContents()

AStringVector cFile::GetFolderContents ( const AString a_Folder)
static

Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there).

Definition at line 498 of file File.cpp.

◆ GetLastModificationTime()

unsigned cFile::GetLastModificationTime ( const AString a_FileName)
static

Returns the last modification time (in current timezone) of the specified file.

The value returned is in the same units as the value returned by time() function. If the file is not found / accessible, zero is returned. Works for folders, too, when specified without the trailing path separator.

Definition at line 645 of file File.cpp.

◆ GetPathSeparator()

AString cFile::GetPathSeparator ( )
static

Returns the path separator used by the current platform.

Note that the platform / CRT may support additional path separators (such as slashes on Windows), these don't get reported.

Definition at line 669 of file File.cpp.

◆ GetSize() [1/2]

long cFile::GetSize ( const AString a_FileName)
static

Returns the size of the file, or a negative number on error.

Definition at line 440 of file File.cpp.

◆ GetSize() [2/2]

long cFile::GetSize ( void  ) const

Returns the size of file, in bytes, or -1 for failure; asserts if not open.

Definition at line 233 of file File.cpp.

◆ IsEOF()

bool cFile::IsEOF ( void  ) const

Definition at line 127 of file File.cpp.

◆ IsFile()

bool cFile::IsFile ( const AString a_Path)
static

Returns true if the specified path is a regular file.

Definition at line 425 of file File.cpp.

◆ IsFolder()

bool cFile::IsFolder ( const AString a_Path)
static

Returns true if the specified path is a folder.

Definition at line 410 of file File.cpp.

◆ IsOpen()

bool cFile::IsOpen ( void  ) const

Definition at line 118 of file File.cpp.

◆ Open()

bool cFile::Open ( const AString iFileName,
eMode  iMode 
)

Definition at line 52 of file File.cpp.

◆ PathSeparator()

static char cFile::PathSeparator ( )
inlinestatic

Definition at line 42 of file File.h.

◆ Read() [1/2]

ContiguousByteBuffer cFile::Read ( size_t  a_NumBytes)

Reads up to a_NumBytes bytes, returns the bytes actually read, or empty string on failure; asserts if not open.

Definition at line 160 of file File.cpp.

◆ Read() [2/2]

int cFile::Read ( void *  a_Buffer,
size_t  a_NumBytes 
)

Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read, or -1 on failure; asserts if not open.

Definition at line 144 of file File.cpp.

◆ ReadRestOfFile()

int cFile::ReadRestOfFile ( AString a_Contents)

Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error.

Definition at line 263 of file File.cpp.

◆ ReadWholeFile()

AString cFile::ReadWholeFile ( const AString a_FileName)
static

Returns the entire contents of the specified file as a string.

Returns empty string on error.

Definition at line 573 of file File.cpp.

◆ Rename()

bool cFile::Rename ( const AString a_OrigPath,
const AString a_NewPath 
)
static

Renames a file or folder, returns true if successful.

May fail if dest already exists (libc-dependant)!

Definition at line 377 of file File.cpp.

◆ Seek()

long cFile::Seek ( int  iPosition)

Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open.

Definition at line 197 of file File.cpp.

◆ Tell()

long cFile::Tell ( void  ) const

Returns the current position (bytes from file start) or -1 for failure; asserts if not open.

Definition at line 217 of file File.cpp.

◆ Write() [1/2]

int cFile::Write ( const void *  a_Buffer,
size_t  a_NumBytes 
)

Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written, or -1 on failure; asserts if not open.

Definition at line 180 of file File.cpp.

◆ Write() [2/2]

int cFile::Write ( std::string_view  a_String)
inline

Definition at line 83 of file File.h.

Member Data Documentation

◆ m_File

FILE* cFile::m_File
private

Definition at line 175 of file File.h.


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