Loading...
Searching...
No Matches
basicio.hpp
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef BASICIO_HPP_
#define BASICIO_HPP_
// *****************************************************************************
#include "exiv2lib_export.h"
// included header files
#include "error.hpp"
#include "types.hpp"
// + standard includes
#include <memory>
// The way to handle data from stdin or data uri path. If EXV_XPATH_MEMIO = 1,
// it uses MemIo. Otherwises, it uses FileIo.
#ifndef EXV_XPATH_MEMIO
#define EXV_XPATH_MEMIO 0
#endif
// *****************************************************************************
// namespace extensions
namespace Exiv2 {
// *****************************************************************************
// class definitions
class EXIV2API BasicIo {
public:
using UniquePtr = std::unique_ptr<BasicIo>;
enum Position { beg, cur, end };
virtual ~BasicIo() = default;
virtual int open() = 0;
virtual int close() = 0;
virtual size_t write(const byte* data, size_t wcount) = 0;
virtual size_t write(BasicIo& src) = 0;
virtual int putb(byte data) = 0;
virtual DataBuf read(size_t rcount) = 0;
virtual size_t read(byte* buf, size_t rcount) = 0;
void readOrThrow(byte* buf, size_t rcount, ErrorCode err = ErrorCode::kerCorruptedMetadata);
virtual int getb() = 0;
virtual void transfer(BasicIo& src) = 0;
virtual int seek(int64_t offset, Position pos) = 0;
void seekOrThrow(int64_t offset, Position pos, ErrorCode err);
virtual byte* mmap(bool isWriteable = false) = 0;
virtual int munmap() = 0;
[[nodiscard]] virtual size_t tell() const = 0;
[[nodiscard]] virtual size_t size() const = 0;
[[nodiscard]] virtual bool isopen() const = 0;
[[nodiscard]] virtual int error() const = 0;
[[nodiscard]] virtual bool eof() const = 0;
[[nodiscard]] virtual const std::string& path() const noexcept = 0;
virtual void populateFakeData() = 0;
byte* bigBlock_{};
}; // class BasicIo
class EXIV2API IoCloser {
public:
explicit IoCloser(BasicIo& bio) : bio_(bio) {
}
virtual ~IoCloser() {
close();
}
void close() {
if (bio_.isopen())
bio_.close();
}
// DATA
BasicIo& bio_;
// Not implemented
IoCloser(const IoCloser&) = delete;
IoCloser& operator=(const IoCloser&) = delete;
}; // class IoCloser
#ifdef EXV_ENABLE_FILESYSTEM
class EXIV2API FileIo : public BasicIo {
public:
explicit FileIo(const std::string& path);
~FileIo() override;
int open(const std::string& mode);
int open() override;
int close() override;
size_t write(const byte* data, size_t wcount) override;
size_t write(BasicIo& src) override;
int putb(byte data) override;
DataBuf read(size_t rcount) override;
size_t read(byte* buf, size_t rcount) override;
int getb() override;
void transfer(BasicIo& src) override;
int seek(int64_t offset, Position pos) override;
byte* mmap(bool isWriteable = false) override;
int munmap() override;
virtual void setPath(const std::string& path);
[[nodiscard]] size_t tell() const override;
[[nodiscard]] size_t size() const override;
[[nodiscard]] bool isopen() const override;
[[nodiscard]] int error() const override;
[[nodiscard]] bool eof() const override;
[[nodiscard]] const std::string& path() const noexcept override;
void populateFakeData() override;
// NOT IMPLEMENTED
FileIo(const FileIo&) = delete;
FileIo& operator=(const FileIo&) = delete;
private:
// Pimpl idiom
class Impl;
std::unique_ptr<Impl> p_;
}; // class FileIo
#endif
class EXIV2API MemIo : public BasicIo {
public:
MemIo();
MemIo(const byte* data, size_t size);
~MemIo() override;
int open() override;
int close() override;
size_t write(const byte* data, size_t wcount) override;
size_t write(BasicIo& src) override;
int putb(byte data) override;
DataBuf read(size_t rcount) override;
size_t read(byte* buf, size_t rcount) override;
int getb() override;
void transfer(BasicIo& src) override;
int seek(int64_t offset, Position pos) override;
byte* mmap(bool /*isWriteable*/ = false) override;
int munmap() override;
[[nodiscard]] size_t tell() const override;
[[nodiscard]] size_t size() const override;
[[nodiscard]] bool isopen() const override;
[[nodiscard]] int error() const override;
[[nodiscard]] bool eof() const override;
[[nodiscard]] const std::string& path() const noexcept override;
void populateFakeData() override;
// NOT IMPLEMENTED
MemIo(const MemIo&) = delete;
MemIo& operator=(const MemIo&) = delete;
private:
// Pimpl idiom
class Impl;
std::unique_ptr<Impl> p_;
}; // class MemIo
#if EXV_XPATH_MEMIO
class EXIV2API XPathIo : public MemIo {
public:
XPathIo(const std::string& path);
private:
void ReadStdin();
void ReadDataUri(const std::string& path);
}; // class XPathIo
#elif defined(EXV_ENABLE_FILESYSTEM)
class EXIV2API XPathIo : public FileIo {
public:
static constexpr auto TEMP_FILE_EXT = ".exiv2_temp";
static constexpr auto GEN_FILE_EXT = ".exiv2";
explicit XPathIo(const std::string& orgPath);
~XPathIo() override;
XPathIo(const XPathIo&) = delete;
XPathIo& operator=(const XPathIo&) = delete;
void transfer(BasicIo& src) override;
static std::string writeDataToFile(const std::string& orgPath);
private:
// True if the file is a temporary file and it should be deleted in destructor.
bool isTemp_{true};
std::string tempFilePath_;
}; // class XPathIo
#endif
class EXIV2API RemoteIo : public BasicIo {
public:
RemoteIo();
~RemoteIo() override;
RemoteIo(const RemoteIo&) = delete;
RemoteIo& operator=(const RemoteIo&) = delete;
int open() override;
int close() override;
size_t write(const byte* data, size_t wcount) override;
size_t write(BasicIo& src) override;
int putb(byte data) override;
DataBuf read(size_t rcount) override;
size_t read(byte* buf, size_t rcount) override;
int getb() override;
void transfer(BasicIo& src) override;
int seek(int64_t offset, Position pos) override;
byte* mmap(bool /*isWriteable*/ = false) override;
int munmap() override;
[[nodiscard]] size_t tell() const override;
[[nodiscard]] size_t size() const override;
[[nodiscard]] bool isopen() const override;
[[nodiscard]] int error() const override;
[[nodiscard]] bool eof() const override;
[[nodiscard]] const std::string& path() const noexcept override;
void populateFakeData() override;
protected:
// Pimpl idiom
class Impl;
std::unique_ptr<Impl> p_;
}; // class RemoteIo
class EXIV2API HttpIo : public RemoteIo {
public:
explicit HttpIo(const std::string& url, size_t blockSize = 1024);
private:
// Pimpl idiom
class HttpImpl;
};
#ifdef EXV_USE_CURL
class EXIV2API CurlIo : public RemoteIo {
public:
explicit CurlIo(const std::string& url, size_t blockSize = 0);
size_t write(const byte* data, size_t wcount) override;
size_t write(BasicIo& src) override;
protected:
// Pimpl idiom
class CurlImpl;
};
#endif
// *****************************************************************************
// template, inline and free functions
EXIV2API DataBuf readFile(const std::string& path);
EXIV2API size_t writeFile(const DataBuf& buf, const std::string& path);
#ifdef EXV_USE_CURL
EXIV2API size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData);
#endif
} // namespace Exiv2
#endif // #ifndef BASICIO_HPP_
Exiv2::BasicIo
An interface for simple binary IO.
Definition basicio.hpp:35
Exiv2::BasicIo::write
virtual size_t write(const byte *data, size_t wcount)=0
Write data to the IO source. Current IO position is advanced by the number of bytes written.
Exiv2::BasicIo::isopen
virtual bool isopen() const =0
Returns true if the IO source is open, otherwise false.
Exiv2::BasicIo::open
virtual int open()=0
Open the IO source using the default access mode. The default mode should allow for reading and writi...
Exiv2::BasicIo::Position
Position
Seek starting positions.
Definition basicio.hpp:41
Exiv2::BasicIo::eof
virtual bool eof() const =0
Returns true if the IO position has reached the end, otherwise false.
Exiv2::BasicIo::size
virtual size_t size() const =0
Get the current size of the IO source in bytes.
Exiv2::BasicIo::getb
virtual int getb()=0
Read one byte from the IO source. Current IO position is advanced by one byte.
Exiv2::BasicIo::close
virtual int close()=0
Close the IO source. After closing a BasicIo instance can not be read or written. Closing flushes any...
Exiv2::BasicIo::tell
virtual size_t tell() const =0
Get the current IO position.
Exiv2::BasicIo::~BasicIo
virtual ~BasicIo()=default
Destructor.
Exiv2::BasicIo::seek
virtual int seek(int64_t offset, Position pos)=0
Move the current IO position.
Exiv2::BasicIo::path
virtual const std::string & path() const noexcept=0
Return the path to the IO resource. Often used to form comprehensive error messages where only a Basi...
Exiv2::BasicIo::munmap
virtual int munmap()=0
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Exiv2::BasicIo::write
virtual size_t write(BasicIo &src)=0
Write data that is read from another BasicIo instance to the IO source. Current IO position is advanc...
Exiv2::BasicIo::UniquePtr
std::unique_ptr< BasicIo > UniquePtr
BasicIo auto_ptr type.
Definition basicio.hpp:38
Exiv2::BasicIo::read
virtual DataBuf read(size_t rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
Exiv2::BasicIo::transfer
virtual void transfer(BasicIo &src)=0
Remove all data from this object's IO source and then transfer data from the src BasicIo object into ...
Exiv2::BasicIo::putb
virtual int putb(byte data)=0
Write one byte to the IO source. Current IO position is advanced by one byte.
Exiv2::BasicIo::error
virtual int error() const =0
Returns 0 if the IO source is in a valid state, otherwise nonzero.
Exiv2::BasicIo::mmap
virtual byte * mmap(bool isWriteable=false)=0
Direct access to the IO data. For files, this is done by mapping the file into the process's address ...
Exiv2::BasicIo::read
virtual size_t read(byte *buf, size_t rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
Exiv2::FileIo
Provides binary file IO by implementing the BasicIo interface.
Definition basicio.hpp:284
Exiv2::FileIo::open
int open(const std::string &mode)
Open the file using the specified mode.
Exiv2::FileIo::read
DataBuf read(size_t rcount) override
Read data from the file. Reading starts at the current file position and the position is advanced by ...
Exiv2::FileIo::~FileIo
~FileIo() override
Destructor. Flushes and closes an open file.
Exiv2::FileIo::isopen
bool isopen() const override
Returns true if the file is open, otherwise false.
Exiv2::FileIo::open
int open() override
Open the file using the default access mode of "rb". This method can also be used to "reopen" a file ...
Exiv2::FileIo::seek
int seek(int64_t offset, Position pos) override
Move the current IO position.
Exiv2::FileIo::write
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.
Exiv2::FileIo::setPath
virtual void setPath(const std::string &path)
close the file source and set a new path.
Exiv2::FileIo::tell
size_t tell() const override
Get the current file position.
Exiv2::FileIo::path
const std::string & path() const noexcept override
Returns the path of the file.
Exiv2::FileIo::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 ...
Exiv2::FileIo::munmap
int munmap() override
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Exiv2::FileIo::error
int error() const override
Returns 0 if the file is in a valid state, otherwise nonzero.
Exiv2::FileIo::eof
bool eof() const override
Returns true if the file position has reached the end, otherwise false.
Exiv2::FileIo::close
int close() override
Flush and unwritten data and close the file . It is safe to call close on an already closed instance.
Exiv2::FileIo::write
size_t write(BasicIo &src) override
Write data that is read from another BasicIo instance to the file. The file position is advanced by t...
Exiv2::FileIo::FileIo
FileIo(const std::string &path)
Constructor that accepts the file path on which IO will be performed. The constructor does not open t...
Exiv2::FileIo::size
size_t size() const override
Flush any buffered writes and get the current file size in bytes.
Exiv2::FileIo::getb
int getb() override
Read one byte from the file. The file position is advanced by one byte.
Exiv2::FileIo::mmap
byte * mmap(bool isWriteable=false) override
Map the file into the process's address space. The file must be open before mmap() is called....
Exiv2::FileIo::putb
int putb(byte data) override
Write one byte to the file. The file position is advanced by one byte.
Exiv2::FileIo::transfer
void transfer(BasicIo &src) override
Remove the contents of the file and then transfer data from the src BasicIo object into the empty fil...
Exiv2::HttpIo
Provides the http read/write access for the RemoteIo.
Definition basicio.hpp:914
Exiv2::HttpIo::HttpIo
HttpIo(const std::string &url, size_t blockSize=1024)
Constructor that accepts the http URL on which IO will be performed. The constructor does not open th...
Exiv2::IoCloser
Utility class that closes a BasicIo instance upon destruction. Meant to be used as a stack variable i...
Definition basicio.hpp:246
Exiv2::IoCloser::operator=
IoCloser & operator=(const IoCloser &)=delete
Assignment operator.
Exiv2::IoCloser::IoCloser
IoCloser(BasicIo &bio)
Constructor, takes a BasicIo reference.
Definition basicio.hpp:251
Exiv2::IoCloser::~IoCloser
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition basicio.hpp:254
Exiv2::IoCloser::close
void close()
Close the BasicIo if it is open.
Definition basicio.hpp:262
Exiv2::IoCloser::bio_
BasicIo & bio_
The BasicIo reference.
Definition basicio.hpp:270
Exiv2::IoCloser::IoCloser
IoCloser(const IoCloser &)=delete
Copy constructor.
Exiv2::MemIo::Impl
Internal Pimpl structure of class MemIo.
Definition basicio.cpp:563
Exiv2::MemIo
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition basicio.hpp:497
Exiv2::MemIo::operator=
MemIo & operator=(const MemIo &)=delete
Assignment operator.
Exiv2::MemIo::MemIo
MemIo(const MemIo &)=delete
Copy constructor.
Exiv2::RemoteIo::Impl
Internal Pimpl abstract structure of class RemoteIo.
Definition basicio.cpp:1001
Exiv2::RemoteIo
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class....
Definition basicio.hpp:750
Exiv2::RemoteIo::RemoteIo
RemoteIo()
Destructor. Releases all managed memory.
Exiv2::XPathIo
Provides binary IO for the data from stdin and data uri path.
Definition basicio.hpp:692
Exiv2::XPathIo::transfer
void transfer(BasicIo &src) override
Change the name of the temp file and make it untemporary before calling the method of superclass File...
Exiv2::XPathIo::XPathIo
XPathIo(const std::string &orgPath)
Default constructor that reads data from stdin/data uri path and writes them to the temp file.
Exiv2::XPathIo::writeDataToFile
static std::string writeDataToFile(const std::string &orgPath)
Read the data from stdin/data uri path and write them to the file.
Exiv2::XPathIo::~XPathIo
~XPathIo() override
Destructor. Releases all managed memory and removes the temp file.
error.hpp
Error class for exceptions, log message class.
Exiv2
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition asfvideo.hpp:15
Exiv2::ErrorCode
ErrorCode
Complete list of all Exiv2 error codes.
Definition error.hpp:162
Exiv2::writeFile
EXIV2API size_t writeFile(const DataBuf &buf, const std::string &path)
Write DataBuf buf to file path.
Exiv2::readFile
EXIV2API DataBuf readFile(const std::string &path)
Read file path into a DataBuf, which is returned.
Exiv2::DataBuf
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition types.hpp:124