Loading...
Searching...
No Matches
basicio.hpp
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
/*
File: basicio.hpp
*/
#ifndef BASICIO_HPP_
#define BASICIO_HPP_
// *****************************************************************************
#include "exiv2lib_export.h"
// included header files
#include "types.hpp"
// + standard includes
#include <memory> // for std::auto_ptr
// 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:
typedef std::auto_ptr<BasicIo> AutoPtr;
enum Position { beg, cur, end };
virtual ~BasicIo();
virtual int open() = 0;
virtual int close() = 0;
virtual long write(const byte* data, long wcount) = 0;
virtual long write(BasicIo& src) = 0;
virtual int putb(byte data) = 0;
virtual DataBuf read(long rcount) = 0;
virtual long read(byte* buf, long rcount) = 0;
virtual int getb() = 0;
virtual void transfer(BasicIo& src) = 0;
#if defined(_MSC_VER)
virtual int seek(int64_t offset, Position pos) = 0;
#else
virtual int seek(long offset, Position pos) = 0;
#endif
virtual byte* mmap(bool isWriteable =false) =0;
virtual int munmap() =0;
virtual long tell() const = 0;
virtual size_t size() const = 0;
virtual bool isopen() const = 0;
virtual int error() const = 0;
virtual bool eof() const = 0;
virtual std::string path() const =0;
#ifdef EXV_UNICODE_PATH
virtual std::wstring wpath() const =0;
#endif
virtual void populateFakeData() {}
byte* bigBlock_;
protected:
BasicIo() : bigBlock_(NULL) {};
}; // 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_;
private:
// Not implemented
IoCloser(const IoCloser&);
IoCloser& operator=(const IoCloser&);
}; // class IoCloser
class EXIV2API FileIo : public BasicIo {
public:
explicit FileIo(const std::string& path);
#ifdef EXV_UNICODE_PATH
FileIo(const std::wstring& wpath);
#endif
virtual ~FileIo();
int open(const std::string& mode);
virtual int open();
virtual int close();
virtual long write(const byte* data, long wcount);
virtual long write(BasicIo& src);
virtual int putb(byte data);
virtual DataBuf read(long rcount);
virtual long read(byte* buf, long rcount);
virtual int getb();
virtual void transfer(BasicIo& src);
#if defined(_MSC_VER)
virtual int seek(int64_t offset, Position pos);
#else
virtual int seek(long offset, Position pos);
#endif
virtual byte* mmap(bool isWriteable =false);
virtual int munmap();
virtual void setPath(const std::string& path);
#ifdef EXV_UNICODE_PATH
virtual void setPath(const std::wstring& wpath);
#endif
virtual long tell() const;
virtual size_t size() const;
virtual bool isopen() const;
virtual int error() const;
virtual bool eof() const;
virtual std::string path() const;
#ifdef EXV_UNICODE_PATH
/*
@brief Like path() but returns the unicode path of the file in an std::wstring.
@note This function is only available on Windows.
*/
virtual std::wstring wpath() const;
#endif
virtual void populateFakeData();
private:
// NOT IMPLEMENTED
FileIo(FileIo& rhs);
FileIo& operator=(const FileIo& rhs);
// Pimpl idiom
class Impl;
std::auto_ptr<Impl> p_;
}; // class FileIo
class EXIV2API MemIo : public BasicIo {
public:
MemIo();
MemIo(const byte* data, long size);
virtual ~MemIo();
virtual int open();
virtual int close();
virtual long write(const byte* data, long wcount);
virtual long write(BasicIo& src);
virtual int putb(byte data);
virtual DataBuf read(long rcount);
virtual long read(byte* buf, long rcount);
virtual int getb();
virtual void transfer(BasicIo& src);
#if defined(_MSC_VER)
virtual int seek(int64_t offset, Position pos);
#else
virtual int seek(long offset, Position pos);
#endif
virtual byte* mmap(bool /*isWriteable*/ =false);
virtual int munmap();
virtual long tell() const;
virtual size_t size() const;
virtual bool isopen() const;
virtual int error() const;
virtual bool eof() const;
virtual std::string path() const;
#ifdef EXV_UNICODE_PATH
/*
@brief Like path() but returns a unicode dummy path in an std::wstring.
@note This function is only available on Windows.
*/
virtual std::wstring wpath() const;
#endif
virtual void populateFakeData();
private:
// NOT IMPLEMENTED
MemIo(MemIo& rhs);
MemIo& operator=(const MemIo& rhs);
// Pimpl idiom
class Impl;
std::auto_ptr<Impl> p_;
}; // class MemIo
#if EXV_XPATH_MEMIO
class EXIV2API XPathIo : public MemIo {
public:
XPathIo(const std::string& path);
#ifdef EXV_UNICODE_PATH
XPathIo(const std::wstring& wpath);
#endif
private:
void ReadStdin();
void ReadDataUri(const std::string& path);
}; // class XPathIo
#else
class EXIV2API XPathIo : public FileIo {
public:
static const std::string TEMP_FILE_EXT;
static const std::string GEN_FILE_EXT;
explicit XPathIo(const std::string& orgPath);
#ifdef EXV_UNICODE_PATH
XPathIo(const std::wstring& wOrgPathpath);
#endif
virtual ~XPathIo();
virtual void transfer(BasicIo& src);
static std::string writeDataToFile(const std::string& orgPath);
#ifdef EXV_UNICODE_PATH
static std::string writeDataToFile(const std::wstring& wOrgPath);
#endif
private:
// True if the file is a temporary file and it should be deleted in destructor.
bool isTemp_;
std::string tempFilePath_;
}; // class XPathIo
#endif
class EXIV2API RemoteIo : public BasicIo {
public:
virtual ~RemoteIo();
virtual int open();
virtual int close();
virtual long write(const byte* data, long wcount);
virtual long write(BasicIo& src);
virtual int putb(byte data);
virtual DataBuf read(long rcount);
virtual long read(byte* buf, long rcount);
virtual int getb();
virtual void transfer(BasicIo& src);
#if defined(_MSC_VER)
virtual int seek(int64_t offset, Position pos);
#else
virtual int seek(long offset, Position pos);
#endif
virtual byte* mmap(bool /*isWriteable*/ =false);
virtual int munmap();
virtual long tell() const;
virtual size_t size() const;
virtual bool isopen() const;
virtual int error() const;
virtual bool eof() const;
virtual std::string path() const;
#ifdef EXV_UNICODE_PATH
/*
@brief Like path() but returns a unicode URL path in an std::wstring.
@note This function is only available on Windows.
*/
virtual std::wstring wpath() const;
#endif
virtual void populateFakeData();
protected:
RemoteIo() {p_=NULL;}
// Pimpl idiom
class Impl;
Impl* p_;
}; // class RemoteIo
class EXIV2API HttpIo : public RemoteIo {
public:
HttpIo(const std::string& url, size_t blockSize = 1024);
#ifdef EXV_UNICODE_PATH
HttpIo(const std::wstring& wurl, size_t blockSize = 1024);
#endif
protected:
// NOT IMPLEMENTED
HttpIo(HttpIo& rhs);
HttpIo& operator=(const HttpIo& rhs);
// Pimpl idiom
class HttpImpl;
virtual ~HttpIo(){}
};
#ifdef EXV_USE_CURL
class EXIV2API CurlIo : public RemoteIo {
public:
CurlIo(const std::string& url, size_t blockSize = 0);
#ifdef EXV_UNICODE_PATH
CurlIo(const std::wstring& wurl, size_t blockSize = 0);
#endif
long write(const byte* data, long wcount);
long write(BasicIo& src);
protected:
// NOT IMPLEMENTED
CurlIo(CurlIo& rhs);
CurlIo& operator=(const CurlIo& rhs);
// Pimpl idiom
class CurlImpl;
virtual ~CurlIo(){}
};
#endif
#ifdef EXV_USE_SSH
class EXIV2LIB_DEPRECATED_EXPORT SshIo : public RemoteIo {
public:
SshIo(const std::string& url, size_t blockSize = 1024);
#ifdef EXV_UNICODE_PATH
SshIo(const std::wstring& wurl, size_t blockSize = 1024);
#endif
protected:
// NOT IMPLEMENTED
SshIo(SshIo& rhs);
SshIo& operator=(const SshIo& rhs);
// Pimpl idiom
class SshImpl;
virtual ~SshIo(){}
};
#endif
// *****************************************************************************
// template, inline and free functions
EXIV2API DataBuf readFile(const std::string& path);
#ifdef EXV_UNICODE_PATH
EXIV2API DataBuf readFile(const std::wstring& wpath);
#endif
EXIV2API long writeFile(const DataBuf& buf, const std::string& path);
#ifdef EXV_UNICODE_PATH
EXIV2API long writeFile(const DataBuf& buf, const std::wstring& wpath);
#endif
EXIV2API std::string ReplaceStringInPlace(std::string subject, const std::string& search,
const std::string& replace);
#ifdef EXV_UNICODE_PATH
EXIV2API std::wstring ReplaceStringInPlace(std::wstring subject, const std::wstring& search,
const std::wstring& replace);
#endif
#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:55
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:61
Exiv2::BasicIo::eof
virtual bool eof() const =0
Returns true if the IO position has reached the end, otherwise false.
Exiv2::BasicIo::write
virtual long write(BasicIo &src)=0
Write data that is read from another BasicIo instance to the IO source. Current IO position is advanc...
Exiv2::BasicIo::populateFakeData
virtual void populateFakeData()
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.hpp:241
Exiv2::BasicIo::seek
virtual int seek(long offset, Position pos)=0
Move the current IO position.
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::bigBlock_
byte * bigBlock_
this is allocated and populated by mmap()
Definition basicio.hpp:246
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::path
virtual std::string path() const =0
Return the path to the IO resource. Often used to form comprehensive error messages where only a Basi...
Exiv2::BasicIo::read
virtual DataBuf read(long rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
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 long write(const byte *data, long wcount)=0
Write data to the IO source. Current IO position is advanced by the number of bytes written.
Exiv2::BasicIo::BasicIo
BasicIo()
Default Constructor.
Definition basicio.hpp:254
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::tell
virtual long tell() const =0
Get the current IO position.
Exiv2::BasicIo::read
virtual long read(byte *buf, long rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
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::AutoPtr
std::auto_ptr< BasicIo > AutoPtr
BasicIo auto_ptr type.
Definition basicio.hpp:58
Exiv2::DataBuf
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition types.hpp:193
Exiv2::FileIo::Impl
Internal Pimpl structure of class FileIo.
Definition basicio.cpp:89
Exiv2::FileIo
Provides binary file IO by implementing the BasicIo interface.
Definition basicio.hpp:296
Exiv2::HttpIo::HttpImpl
Internal Pimpl structure of class HttpIo.
Definition basicio.cpp:2031
Exiv2::HttpIo
Provides the http read/write access for the RemoteIo.
Definition basicio.hpp:1015
Exiv2::HttpIo::~HttpIo
virtual ~HttpIo()
Default Destructor.
Definition basicio.hpp:1050
Exiv2::HttpIo::operator=
HttpIo & operator=(const HttpIo &rhs)
Assignment operator.
Exiv2::HttpIo::HttpIo
HttpIo(HttpIo &rhs)
Copy constructor.
Exiv2::IoCloser
Utility class that closes a BasicIo instance upon destruction. Meant to be used as a stack variable i...
Definition basicio.hpp:264
Exiv2::IoCloser::IoCloser
IoCloser(BasicIo &bio)
Constructor, takes a BasicIo reference.
Definition basicio.hpp:269
Exiv2::IoCloser::~IoCloser
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition basicio.hpp:271
Exiv2::IoCloser::close
void close()
Close the BasicIo if it is open.
Definition basicio.hpp:277
Exiv2::IoCloser::bio_
BasicIo & bio_
The BasicIo reference.
Definition basicio.hpp:282
Exiv2::MemIo::Impl
Internal Pimpl structure of class MemIo.
Definition basicio.cpp:1054
Exiv2::MemIo
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition basicio.hpp:540
Exiv2::RemoteIo::Impl
Internal Pimpl abstract structure of class RemoteIo.
Definition basicio.cpp:1587
Exiv2::RemoteIo
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class....
Definition basicio.hpp:832
Exiv2::RemoteIo::RemoteIo
RemoteIo()
Default Constructor.
Definition basicio.hpp:1003
Exiv2::RemoteIo::p_
Impl * p_
Pointer to implementation.
Definition basicio.hpp:1009
Exiv2::XPathIo
Provides binary IO for the data from stdin and data uri path.
Definition basicio.hpp:761
Exiv2::XPathIo::TEMP_FILE_EXT
static const std::string TEMP_FILE_EXT
The extension of the temporary file which is created when getting input data to read metadata....
Definition basicio.hpp:767
Exiv2::XPathIo::GEN_FILE_EXT
static const std::string GEN_FILE_EXT
The extension of the generated file which is created when getting input data to add or modify the met...
Definition basicio.hpp:772
Exiv2
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition asfvideo.hpp:36
Exiv2::ReplaceStringInPlace
EXIV2API std::string ReplaceStringInPlace(std::string subject, const std::string &search, const std::string &replace)
replace each substring of the subject that matches the given search string with the given replacement...
Definition basicio.cpp:2723
Exiv2::writeFile
EXIV2API long writeFile(const DataBuf &buf, const std::string &path)
Write DataBuf buf to file path.
Definition basicio.cpp:2703
Exiv2::readFile
EXIV2API DataBuf readFile(const std::string &path)
Read file path into a DataBuf, which is returned.
Definition basicio.cpp:2665