openMSX
Public Types | Public Member Functions | Friends | List of all members
openmsx::File Class Reference

#include <File.hh>

Public Types

enum class  OpenMode {
  NORMAL , TRUNCATE , CREATE , LOAD_PERSISTENT ,
  SAVE_PERSISTENT , PRE_CACHE
}
 

Public Member Functions

 File ()
 Create a closed file handle.
 
 File (std::string filename, OpenMode mode=OpenMode::NORMAL)
 Create file object and open underlying file.
 
 File (const Filename &filename, OpenMode mode=OpenMode::NORMAL)
 
 File (Filename &&filename, OpenMode mode=OpenMode::NORMAL)
 
 File (std::string filename, const char *mode)
 This constructor maps very closely on the fopen() libc function.
 
 File (const Filename &filename, const char *mode)
 
 File (Filename &&filename, const char *mode)
 
 File (File &&other) noexcept
 
 File (std::unique_ptr< FileBase > file_)
 
 ~File ()
 
Fileoperator= (File &&other) noexcept
 
bool is_open () const
 Return true iff this file handle refers to an open file.
 
void close ()
 Close the current file.
 
void read (std::span< uint8_t > buffer)
 Read from file.
 
template<typename T >
void read (std::span< T > buffer)
 
void write (std::span< const uint8_t > buffer)
 Write to file.
 
template<typename T >
void write (std::span< T > buffer)
 
std::span< const uint8_t > mmap ()
 Map file in memory.
 
void munmap ()
 Unmap file from memory.
 
size_t getSize ()
 Returns the size of this file.
 
void seek (size_t pos)
 Move read/write pointer to the specified position.
 
size_t getPos ()
 Get the current position of the read/write pointer.
 
void truncate (size_t size)
 Truncate file size.
 
void flush ()
 Force a write of all buffered data to disk.
 
const std::string & getURL () const
 Returns the URL of this file object.
 
std::string_view getOriginalName ()
 Get Original filename for this object.
 
bool isReadOnly () const
 Check if this file is readonly.
 
time_t getModificationDate ()
 Get the date/time of last modification.
 

Friends

class LocalFileReference
 

Detailed Description

Definition at line 17 of file File.hh.

Member Enumeration Documentation

◆ OpenMode

enum class openmsx::File::OpenMode
strong
Enumerator
NORMAL 
TRUNCATE 
CREATE 
LOAD_PERSISTENT 
SAVE_PERSISTENT 
PRE_CACHE 

Definition at line 20 of file File.hh.

Constructor & Destructor Documentation

◆ File() [1/9]

openmsx::File::File ( )
default

Create a closed file handle.

The only valid operations on such an object are is_open() and the move-assignment operator.

◆ File() [2/9]

openmsx::File::File ( std::string  filename,
OpenMode  mode = OpenMode::NORMAL 
)
explicit

Create file object and open underlying file.

Parameters
filenameName of the file to be opened.
modeMode to open the file in:
Exceptions
FileNotFoundExceptionif file not found
FileExceptionfor other errors

Definition at line 39 of file File.cc.

◆ File() [3/9]

openmsx::File::File ( const Filename filename,
OpenMode  mode = OpenMode::NORMAL 
)
explicit

Definition at line 44 of file File.cc.

◆ File() [4/9]

openmsx::File::File ( Filename &&  filename,
OpenMode  mode = OpenMode::NORMAL 
)
explicit

Definition at line 49 of file File.cc.

◆ File() [5/9]

openmsx::File::File ( std::string  filename,
const char *  mode 
)

This constructor maps very closely on the fopen() libc function.

Compared to constructor above, it does not transparently uncompress files.

Parameters
filenameName of the file to be opened.
modeOpen mode, same meaning as in fopen(), but we assert that it contains a 'b' character.

Definition at line 54 of file File.cc.

◆ File() [6/9]

openmsx::File::File ( const Filename filename,
const char *  mode 
)

Definition at line 59 of file File.cc.

◆ File() [7/9]

openmsx::File::File ( Filename &&  filename,
const char *  mode 
)

Definition at line 64 of file File.cc.

◆ File() [8/9]

openmsx::File::File ( File &&  other)
noexcept

Definition at line 69 of file File.cc.

◆ File() [9/9]

openmsx::File::File ( std::unique_ptr< FileBase file_)
explicit

Definition at line 74 of file File.cc.

◆ ~File()

openmsx::File::~File ( )
default

Member Function Documentation

◆ close()

void openmsx::File::close ( )

Close the current file.

Equivalent to assigning a default constructed value to this object.

Definition at line 87 of file File.cc.

Referenced by openmsx::IDECDROM::eject(), openmsx::HD::serialize(), openmsx::PrinterPortLogger::unplugHelper(), and openmsx::AviWriter::~AviWriter().

◆ flush()

void openmsx::File::flush ( )

Force a write of all buffered data to disk.

There is no need to call this function before destroying a File object.

Definition at line 132 of file File.cc.

Referenced by openmsx::WavWriter::flush(), and openmsx::PrinterPortLogger::setStrobe().

◆ getModificationDate()

time_t openmsx::File::getModificationDate ( )

Get the date/time of last modification.

Exceptions
FileException

Definition at line 158 of file File.cc.

Referenced by openmsx::FilePoolCore::getSha1Sum().

◆ getOriginalName()

std::string_view openmsx::File::getOriginalName ( )

Get Original filename for this object.

This will usually just return the filename portion of the URL. However for compressed files this will be different.

Returns
Original file name
Exceptions
FileException

Definition at line 147 of file File.cc.

References getURL().

◆ getPos()

size_t openmsx::File::getPos ( )

Get the current position of the read/write pointer.

Returns
Position in bytes from the beginning of the file.
Exceptions
FileException

Definition at line 122 of file File.cc.

Referenced by TEST_CASE().

◆ getSize()

size_t openmsx::File::getSize ( )

◆ getURL()

const std::string & openmsx::File::getURL ( ) const

◆ is_open()

bool openmsx::File::is_open ( ) const
inline

◆ isReadOnly()

bool openmsx::File::isReadOnly ( ) const

Check if this file is readonly.

Returns
true iff file is readonly
Exceptions
FileException

Definition at line 153 of file File.cc.

Referenced by TEST_CASE().

◆ mmap()

std::span< const uint8_t > openmsx::File::mmap ( )

◆ munmap()

void openmsx::File::munmap ( )

Unmap file from memory.

Definition at line 107 of file File.cc.

◆ operator=()

File & openmsx::File::operator= ( File &&  other)
noexcept

Definition at line 81 of file File.cc.

◆ read() [1/2]

template<typename T >
void openmsx::File::read ( std::span< T >  buffer)
inline

Definition at line 79 of file File.hh.

References read().

◆ read() [2/2]

void openmsx::File::read ( std::span< uint8_t >  buffer)

◆ seek()

void openmsx::File::seek ( size_t  pos)

Move read/write pointer to the specified position.

Parameters
posPosition in bytes from the beginning of the file.
Exceptions
FileException

Definition at line 117 of file File.cc.

Referenced by openmsx::WavWriter::flush(), openmsx::IDECDROM::readBlockStart(), openmsx::OggReader::seek(), TEST_CASE(), and openmsx::AviWriter::~AviWriter().

◆ truncate()

void openmsx::File::truncate ( size_t  size)

Truncate file size.

Enlarging file size always works, but making file smaller doesn't work on some platforms (windows)

Exceptions
FileException

Definition at line 127 of file File.cc.

Referenced by openmsx::DiskManipulator::create(), and openmsx::HD::HD().

◆ write() [1/2]

void openmsx::File::write ( std::span< const uint8_t >  buffer)

◆ write() [2/2]

template<typename T >
void openmsx::File::write ( std::span< T >  buffer)
inline

Definition at line 90 of file File.hh.

References write().

Friends And Related Symbol Documentation

◆ LocalFileReference

friend class LocalFileReference
friend

Definition at line 158 of file File.hh.


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