| Exiv2

Loading...
Searching...
No Matches
Classes | List of all members
Exiv2::FileIo Class Reference

Provides binary file IO by implementing the BasicIo interface. More...

`#include <basicio.hpp>`

Inheritance diagram for Exiv2::FileIo:
[legend]

## Classes
class | Impl
| Internal Pimpl structure of class FileIo. More...

## Public Member Functions
Creators
| FileIo (const std::string &path)
| Constructor that accepts the file path on which IO will be performed. The constructor does not open the file, and therefore never fails.
| ~FileIo () override
| Destructor. Flushes and closes an open file.
Manipulators
int | open (const std::string &mode)
| Open the file using the specified mode.
int | open () override
| Open the file using the default access mode of "rb". This method can also be used to "reopen" a file which will flush any unwritten data and reset the IO position to the start.
int | close () override
| Flush and unwritten data and close the file . It is safe to call close on an already closed instance.
size_t | write (const byte *data, size_t wcount) override
| Write data to the file. The file position is advanced by the number of bytes written.
size_t | write (BasicIo &src) override
| Write data that is read from another BasicIo instance to the file. The file position is advanced by the number of bytes written.
int | putb (byte data) override
| Write one byte to the file. The file position is advanced by one byte.
DataBuf | read (size_t rcount) override
| Read data from the file. Reading starts at the current file position and the position is advanced by the number of bytes read.
size_t | read (byte *buf, size_t rcount) override
| Read data from the file. Reading starts at the current file position and the position is advanced by the number of bytes read.
int | getb () override
| Read one byte from the file. The file position is advanced by one byte.
void | transfer (BasicIo &src) override
| Remove the contents of the file and then transfer data from the src BasicIo object into the empty file.
int | seek (int64_t offset, Position pos) override
| Move the current IO position.
byte * | mmap (bool isWriteable=false) override
| Map the file into the process's address space. The file must be open before mmap() is called. If the mapped area is writeable, changes may not be written back to the underlying file until munmap() is called. The pointer is valid only as long as the FileIo object exists.
int | munmap () override
| Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes are written back to the underlying file.
virtual void | setPath (const std::string &path)
| close the file source and set a new path.
Public Member Functions inherited from Exiv2::BasicIo
virtual | ~BasicIo ()=default
| Destructor.
void | readOrThrow (byte *buf, size_t rcount, ErrorCode err=ErrorCode::kerCorruptedMetadata)
| Safe version of read() that checks for errors and throws an exception if the read was unsuccessful.
void | seekOrThrow (int64_t offset, Position pos, ErrorCode err)
| Safe version of seek() that checks for errors and throws an exception if the seek was unsuccessful.

## Accessors
size_t | tell () const override
| Get the current file position.
size_t | size () const override
| Flush any buffered writes and get the current file size in bytes.
bool | isopen () const override
| Returns true if the file is open, otherwise false.
int | error () const override
| Returns 0 if the file is in a valid state, otherwise nonzero.
bool | eof () const override
| Returns true if the file position has reached the end, otherwise false.
const std::string & | path () const noexcept override
| Returns the path of the file.
void | populateFakeData () override
| Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain image-date (non-metadata/pixel data).
| FileIo (const FileIo &)=delete
| Copy constructor.
FileIo & | operator= (const FileIo &)=delete
| Assignment operator.

## Additional Inherited Members
Public Types inherited from Exiv2::BasicIo
enum | Position { beg , cur , end }
| Seek starting positions.
using | UniquePtr = std::unique_ptr<BasicIo>
| BasicIo auto_ptr type.
byte * | bigBlock_ {}
| this is allocated and populated by mmap()

## Detailed Description

Provides binary file IO by implementing the BasicIo interface.

Examples
  metacopy.cpp, and mrwthumb.cpp.

## Constructor & Destructor Documentation

## ◆ FileIo()

Exiv2::FileIo::FileIo | ( | const std::string & | path | ) |
| explicit

Constructor that accepts the file path on which IO will be performed. The constructor does not open the file, and therefore never fails.

Parameters

  path | The full path of a file

References path().

Referenced by FileIo(), operator=(), transfer(), and Exiv2::XPathIo::XPathIo().

## Member Function Documentation

## ◆ close()

int Exiv2::FileIo::close | ( |  | ) |
| override virtual

Flush and unwritten data and close the file . It is safe to call close on an already closed instance.

Returns
  0 if successful;
  Nonzero if failure;

Implements Exiv2::BasicIo.

Examples
  mrwthumb.cpp.

References munmap().

Referenced by open(), setPath(), transfer(), and ~FileIo().

## ◆ eof()

bool Exiv2::FileIo::eof | ( |  | ) | const
| nodiscard override virtual

Returns true if the file position has reached the end, otherwise false.

Implements Exiv2::BasicIo.

## ◆ error()

int Exiv2::FileIo::error | ( |  | ) | const
| nodiscard override virtual

Returns 0 if the file is in a valid state, otherwise nonzero.

Implements Exiv2::BasicIo.

Referenced by mmap(), and transfer().

## ◆ getb()

int Exiv2::FileIo::getb | ( |  | ) |
| override virtual

Read one byte from the file. The file position is advanced by one byte.

Returns
  The byte read from the file if successful;
  EOF if failure;

Implements Exiv2::BasicIo.

## ◆ isopen()

bool Exiv2::FileIo::isopen | ( |  | ) | const
| nodiscard override virtual

Returns true if the file is open, otherwise false.

Implements Exiv2::BasicIo.

## ◆ mmap()

byte * Exiv2::FileIo::mmap | ( | bool | isWriteable = false | ) |
| override virtual

Map the file into the process's address space. The file must be open before mmap() is called. If the mapped area is writeable, changes may not be written back to the underlying file until munmap() is called. The pointer is valid only as long as the FileIo object exists.

Parameters

  isWriteable | Set to true if the mapped area should be writeable (default is false).

Returns
  A pointer to the mapped area.

Exceptions

  Error | In case of failure.

Implements Exiv2::BasicIo.

References error(), mmap(), munmap(), path(), read(), size(), and Exiv2::strError().

Referenced by mmap().

## ◆ munmap()

int Exiv2::FileIo::munmap | ( |  | ) |
| override virtual

Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes are written back to the underlying file.

Returns
  0 if successful;
  Nonzero if failure;

Implements Exiv2::BasicIo.

References munmap(), seek(), and write().

Referenced by close(), mmap(), and munmap().

## ◆ open() [1/2]

int Exiv2::FileIo::open | ( |  | ) |
| override virtual

Open the file using the default access mode of "rb". This method can also be used to "reopen" a file which will flush any unwritten data and reset the IO position to the start.

Returns
  0 if successful;
  Nonzero if failure.

Implements Exiv2::BasicIo.

References open().

Referenced by open(), and transfer().

## ◆ open() [2/2]

int Exiv2::FileIo::open | ( | const std::string & | mode | ) |

Open the file using the specified mode.

This method can also be used to "reopen" a file which will flush any unwritten data and reset the IO position to the start. Although files can be opened in binary or text mode, this class has only been tested carefully in binary mode.

Parameters

  mode | Specified that type of access allowed on the file. Valid values match those of the C fopen command exactly.

Returns
  0 if successful;
  Nonzero if failure.

Examples
  mrwthumb.cpp.

References close(), and path().

Referenced by Exiv2::readFile(), and Exiv2::writeFile().

## ◆ path()

const std::string & Exiv2::FileIo::path | ( |  | ) | const
| nodiscard override virtual noexcept

Returns the path of the file.

Implements Exiv2::BasicIo.

Referenced by FileIo(), Exiv2::FileIo::Impl::Impl(), mmap(), open(), setPath(), transfer(), Exiv2::XPathIo::transfer(), Exiv2::XPathIo::writeDataToFile(), and Exiv2::XPathIo::XPathIo().

## ◆ populateFakeData()

void Exiv2::FileIo::populateFakeData | ( |  | ) |
| override virtual

Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain image-date (non-metadata/pixel data).

Note
  This method should be only called after the concerned data (metadata) are all downloaded from the remote file to memory.

Implements Exiv2::BasicIo.

## ◆ putb()

int Exiv2::FileIo::putb | ( | byte | data | ) |
| override virtual

Write one byte to the file. The file position is advanced by one byte.

Parameters

  data | The single byte to be written.

Returns
  The value of the byte written if successful;
  EOF if failure;

Implements Exiv2::BasicIo.

## ◆ read() [1/2]

size_t Exiv2::FileIo::read | ( | byte * | buf,
|  | size_t | rcount )
| override virtual

Read data from the file. Reading starts at the current file position and the position is advanced by the number of bytes read.

Parameters

  buf | Pointer to a block of memory into which the read data is stored. The memory block must be at least rcount bytes long.
  rcount | Maximum number of bytes to read. Fewer bytes may be read if rcount bytes are not available.

Returns
  Number of bytes read from the file successfully;
  0 if failure;

Implements Exiv2::BasicIo.

## ◆ read() [2/2]

DataBuf Exiv2::FileIo::read | ( | size_t | rcount | ) |
| override virtual

Read data from the file. Reading starts at the current file position and the position is advanced by the number of bytes read.

Parameters

  rcount | Maximum number of bytes to read. Fewer bytes may be read if rcount bytes are not available.

Returns
  DataBuf instance containing the bytes read. Use the DataBuf::size_ member to find the number of bytes read. DataBuf::size_ will be 0 on failure.

Implements Exiv2::BasicIo.

References Exiv2::DataBuf::data(), read(), Exiv2::DataBuf::resize(), and size().

Referenced by mmap(), read(), and Exiv2::readFile().

## ◆ seek()

int Exiv2::FileIo::seek | ( | int64_t | offset,
|  | Position | pos )
| override virtual

Move the current IO position.

Parameters

  offset | Number of bytes to move the position relative to the starting position specified by pos
  pos | Position from which the seek should start

Returns
  0 if successful;
  Nonzero if failure;

Implements Exiv2::BasicIo.

Referenced by munmap().

## ◆ size()

size_t Exiv2::FileIo::size | ( |  | ) | const
| nodiscard override virtual

Flush any buffered writes and get the current file size in bytes.

Returns
  Size of the file in bytes;
  -1 if failure;

Implements Exiv2::BasicIo.

References Exiv2::FileIo::Impl::StructStat::st_size.

Referenced by mmap(), read(), and Exiv2::XPathIo::writeDataToFile().

## ◆ tell()

size_t Exiv2::FileIo::tell | ( |  | ) | const
| nodiscard override virtual

Get the current file position.

Returns
  Offset from the start of the file

Implements Exiv2::BasicIo.

References Exiv2::Internal::enforce().

## ◆ transfer()

void Exiv2::FileIo::transfer | ( | BasicIo & | src | ) |
| override virtual

Remove the contents of the file and then transfer data from the src BasicIo object into the empty file.

This method is optimized to simply rename the source file if the source object is another FileIo instance. The source BasicIo object is invalidated by this operation and should not be used after this method returns. This method exists primarily to be used with the BasicIo::temporary() method.

Note
  If the caller doesn't have permissions to write to the file, an exception is raised and src is deleted.

Parameters

  src | Reference to another BasicIo instance. The entire contents of src are transferred to this object. The src object is invalidated by the method.

Exceptions

  Error | In case of failure

Implements Exiv2::BasicIo.

Reimplemented in Exiv2::XPathIo.

References Exiv2::BasicIo::close(), close(), Exiv2::BasicIo::error(), error(), EXV_WARNING, Exiv2::fileExists(), FileIo(), Exiv2::BasicIo::open(), open(), Exiv2::BasicIo::path(), path(), Exiv2::FileIo::Impl::StructStat::st_mode, Exiv2::strError(), and write().

Referenced by Exiv2::XPathIo::transfer().

## ◆ write() [1/2]

size_t Exiv2::FileIo::write | ( | BasicIo & | src | ) |
| override virtual

Write data that is read from another BasicIo instance to the file. The file position is advanced by the number of bytes written.

Parameters

  src | Reference to another BasicIo instance. Reading start at the source's current IO position

Returns
  Number of bytes written to the file successfully;
  0 if failure;

Implements Exiv2::BasicIo.

References Exiv2::BasicIo::isopen(), Exiv2::BasicIo::read(), and Exiv2::BasicIo::seek().

## ◆ write() [2/2]

size_t Exiv2::FileIo::write | ( | const byte * | data,
|  | size_t | wcount )
| override virtual

Write data to the file. The file position is advanced by the number of bytes written.

Parameters

  data | Pointer to data. Data must be at least wcount bytes long
  wcount | Number of bytes to be written.

Returns
  Number of bytes written to the file successfully;
  0 if failure;

Implements Exiv2::BasicIo.

Examples
  mrwthumb.cpp.

Referenced by munmap(), transfer(), and Exiv2::writeFile().

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

- basicio.hpp
- basicio.cpp