Exiv2
basicio.hpp
1// ***************************************************************** -*- C++ -*-
2/*
3 * Copyright (C) 2004-2021 Exiv2 authors
4 * This program is part of the Exiv2 distribution.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19 */
20/*
21 File: basicio.hpp
22 */
23#ifndef BASICIO_HPP_
24#define BASICIO_HPP_
25
26// *****************************************************************************
27#include "exiv2lib_export.h"
28
29// included header files
30#include "types.hpp"
31
32// + standard includes
33#include <memory>
34
35// The way to handle data from stdin or data uri path. If EXV_XPATH_MEMIO = 1,
36// it uses MemIo. Otherwises, it uses FileIo.
37#ifndef EXV_XPATH_MEMIO
38#define EXV_XPATH_MEMIO 0
39#endif
40
41// *****************************************************************************
42// namespace extensions
43namespace Exiv2 {
44
45// *****************************************************************************
46// class definitions
47
55 class EXIV2API BasicIo {
56 public:
58 typedef std::unique_ptr<BasicIo> UniquePtr;
59
61 enum Position { beg, cur, end };
62
64
65
66 virtual ~BasicIo() = default;
68
70
71
83 virtual int open() = 0;
84
92 virtual int close() = 0;
102 virtual long write(const byte* data, long wcount) = 0;
112 virtual long write(BasicIo& src) = 0;
120 virtual int putb(byte data) = 0;
131 virtual DataBuf read(long rcount) = 0;
144 virtual long read(byte* buf, long rcount) = 0;
151 virtual int getb() = 0;
165 virtual void transfer(BasicIo& src) = 0;
174#if defined(_MSC_VER)
175 virtual int seek(int64_t offset, Position pos) = 0;
176#else
177 virtual int seek(long offset, Position pos) = 0;
178#endif
179
189 virtual byte* mmap(bool isWriteable =false) =0;
196 virtual int munmap() =0;
197
199
201
202
207 virtual long tell() const = 0;
213 virtual size_t size() const = 0;
215 virtual bool isopen() const = 0;
217 virtual int error() const = 0;
219 virtual bool eof() const = 0;
225 virtual std::string path() const =0;
226#ifdef EXV_UNICODE_PATH
231 virtual std::wstring wpath() const =0;
232#endif
233
241 virtual void populateFakeData() {}
242
246 byte* bigBlock_{};
247
249 }; // class BasicIo
250
257 class EXIV2API IoCloser {
258 public:
260
261
262 explicit IoCloser(BasicIo& bio) : bio_(bio) {}
264 virtual ~IoCloser() { close(); }
266
268
269
270 void close() { if (bio_.isopen()) bio_.close(); }
272
273 // DATA
276
277 // Not implemented
279 IoCloser(const IoCloser&) = delete;
281 IoCloser& operator=(const IoCloser&) = delete;
282 }; // class IoCloser
283
288 class EXIV2API FileIo : public BasicIo {
289 public:
291
292
298 explicit FileIo(const std::string& path);
299#ifdef EXV_UNICODE_PATH
305 FileIo(const std::wstring& wpath);
306#endif
308 ~FileIo() override;
310
312
313
326 int open(const std::string& mode);
334 int open() override;
341 int close() override;
351 long write(const byte* data, long wcount) override;
361 long write(BasicIo& src) override;
369 int putb(byte data) override;
380 DataBuf read(long rcount) override;
393 long read(byte* buf, long rcount) override;
400 int getb() override;
419 void transfer(BasicIo& src) override;
428#if defined(_MSC_VER)
429 int seek(int64_t offset, Position pos) override;
430#else
431 int seek(long offset, Position pos) override;
432#endif
444 byte* mmap(bool isWriteable = false) override;
452 int munmap() override;
456 virtual void setPath(const std::string& path);
457#ifdef EXV_UNICODE_PATH
463 virtual void setPath(const std::wstring& wpath);
464#endif
466
468
473 long tell() const override;
480 size_t size() const override;
482 bool isopen() const override;
484 int error() const override;
486 bool eof() const override;
488 std::string path() const override;
489#ifdef EXV_UNICODE_PATH
490 /*
491 @brief Like path() but returns the unicode path of the file in an std::wstring.
492 @note This function is only available on Windows.
493 */
494 virtual std::wstring wpath() const;
495#endif
496
504 void populateFakeData() override;
506
507 // NOT IMPLEMENTED
509 FileIo(FileIo& rhs) = delete;
511 FileIo& operator=(const FileIo& rhs) = delete;
512
513 private:
514 // Pimpl idiom
515 class Impl;
516 std::unique_ptr<Impl> p_;
517
518 }; // class FileIo
519
532 class EXIV2API MemIo : public BasicIo {
533 public:
535
536
537 MemIo();
546 MemIo(const byte* data, long size);
548 ~MemIo() override;
550
552
553
559 int open() override;
564 int close() override;
575 long write(const byte* data, long wcount) override;
586 long write(BasicIo& src) override;
594 int putb(byte data) override;
605 DataBuf read(long rcount) override;
618 long read(byte* buf, long rcount) override;
625 int getb() override;
641 void transfer(BasicIo& src) override;
650#if defined(_MSC_VER)
651 virtual int seek(int64_t offset, Position pos);
652#else
653 int seek(long offset, Position pos) override;
654#endif
663 byte* mmap(bool /*isWriteable*/ = false) override;
664 int munmap() override;
666
668
669
673 long tell() const override;
679 size_t size() const override;
681 bool isopen() const override;
683 int error() const override;
685 bool eof() const override;
687 std::string path() const override;
688#ifdef EXV_UNICODE_PATH
689 /*
690 @brief Like path() but returns a unicode dummy path in an std::wstring.
691 @note This function is only available on Windows.
692 */
693 virtual std::wstring wpath() const;
694#endif
695
703 void populateFakeData() override;
704
706
707 // NOT IMPLEMENTED
709 MemIo(MemIo& rhs) = delete;
711 MemIo& operator=(const MemIo& rhs) = delete;
712
713 private:
714 // Pimpl idiom
715 class Impl;
716 std::unique_ptr<Impl> p_;
717
718 }; // class MemIo
719
723#if EXV_XPATH_MEMIO
724 class EXIV2API XPathIo : public MemIo {
725 public:
727
728
729 XPathIo(const std::string& path);
730#ifdef EXV_UNICODE_PATH
736 XPathIo(const std::wstring& wpath);
737#endif
739 private:
744 void ReadStdin();
750 void ReadDataUri(const std::string& path);
751 }; // class XPathIo
752#else
753 class EXIV2API XPathIo : public FileIo {
754 public:
759 static const std::string TEMP_FILE_EXT;
764 static const std::string GEN_FILE_EXT;
765
767
768
769 explicit XPathIo(const std::string& orgPath);
770#ifdef EXV_UNICODE_PATH
776 XPathIo(const std::wstring& wOrgPathpath);
777#endif
779 ~XPathIo() override;
781
783
784
788 void transfer(BasicIo& src) override;
789
791
793
794
800 static std::string writeDataToFile(const std::string& orgPath);
801#ifdef EXV_UNICODE_PATH
807 static std::string writeDataToFile(const std::wstring& wOrgPath);
808#endif
810
811 private:
812 // True if the file is a temporary file and it should be deleted in destructor.
813 bool isTemp_;
814 std::string tempFilePath_;
815 }; // class XPathIo
816#endif
817
818
824 class EXIV2API RemoteIo : public BasicIo {
825 public:
827 ~RemoteIo() override;
829
831
832
841 int open() override;
842
848 int close() override;
853 long write(const byte* data, long wcount) override;
868 long write(BasicIo& src) override;
869
874 int putb(byte data) override;
887 DataBuf read(long rcount) override;
902 long read(byte* buf, long rcount) override;
911 int getb() override;
926 void transfer(BasicIo& src) override;
935#if defined(_MSC_VER)
936 virtual int seek(int64_t offset, Position pos);
937#else
938 int seek(long offset, Position pos) override;
939#endif
944 byte* mmap(bool /*isWriteable*/ = false) override;
949 int munmap() override;
951
953
957 long tell() const override;
963 size_t size() const override;
965 bool isopen() const override;
967 int error() const override;
969 bool eof() const override;
971 std::string path() const override;
972#ifdef EXV_UNICODE_PATH
973 /*
974 @brief Like path() but returns a unicode URL path in an std::wstring.
975 @note This function is only available on Windows.
976 */
977 virtual std::wstring wpath() const;
978#endif
979
987 void populateFakeData() override;
988
990
991 protected:
992
993 // Pimpl idiom
994 class Impl;
996 Impl* p_ {nullptr};
997 }; // class RemoteIo
998
1002 class EXIV2API HttpIo : public RemoteIo {
1003 public:
1005
1006
1015 explicit HttpIo(const std::string& url, size_t blockSize = 1024);
1016#ifdef EXV_UNICODE_PATH
1022 HttpIo(const std::wstring& wurl, size_t blockSize = 1024);
1023#endif
1024 // NOT IMPLEMENTED
1026 HttpIo(HttpIo& rhs) = delete;
1028 HttpIo& operator=(const HttpIo& rhs) = delete;
1029
1030 private:
1031 // Pimpl idiom
1032 class HttpImpl;
1033 };
1034
1035#ifdef EXV_USE_CURL
1040 class EXIV2API CurlIo : public RemoteIo {
1041 public:
1043
1044
1053 explicit CurlIo(const std::string& url, size_t blockSize = 0);
1054#ifdef EXV_UNICODE_PATH
1060 CurlIo(const std::wstring& wurl, size_t blockSize = 0);
1061#endif
1067 long write(const byte* data, long wcount) override;
1073 long write(BasicIo& src) override;
1074
1075 // NOT IMPLEMENTED
1077 CurlIo(CurlIo& rhs) = delete;
1079 CurlIo& operator=(const CurlIo& rhs) = delete;
1080
1081 protected:
1082 // Pimpl idiom
1083 class CurlImpl;
1084 };
1085#endif
1086
1087// *****************************************************************************
1088// template, inline and free functions
1089
1095 EXIV2API DataBuf readFile(const std::string& path);
1096#ifdef EXV_UNICODE_PATH
1101 EXIV2API DataBuf readFile(const std::wstring& wpath);
1102#endif
1108 EXIV2API long writeFile(const DataBuf& buf, const std::string& path);
1109#ifdef EXV_UNICODE_PATH
1114 EXIV2API long writeFile(const DataBuf& buf, const std::wstring& wpath);
1115#endif
1120 EXIV2API std::string ReplaceStringInPlace(std::string subject, const std::string& search,
1121 const std::string& replace);
1122#ifdef EXV_UNICODE_PATH
1128 EXIV2API std::wstring ReplaceStringInPlace(std::wstring subject, const std::wstring& search,
1129 const std::wstring& replace);
1130#endif
1131#ifdef EXV_USE_CURL
1135 EXIV2API size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData);
1136#endif
1137} // namespace Exiv2
1138#endif // #ifndef BASICIO_HPP_
An interface for simple binary IO.
Definition: basicio.hpp:55
virtual bool isopen() const =0
Returns true if the IO source is open, otherwise false.
virtual int open()=0
Open the IO source using the default access mode. The default mode should allow for reading and writi...
Position
Seek starting positions.
Definition: basicio.hpp:61
virtual bool eof() const =0
Returns true if the IO position has reached the end, otherwise false.
virtual long write(BasicIo &src)=0
Write data that is read from another BasicIo instance to the IO source. Current IO position is advanc...
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
virtual int seek(long offset, Position pos)=0
Move the current IO position.
virtual size_t size() const =0
Get the current size of the IO source in bytes.
virtual int getb()=0
Read one byte from the IO source. Current IO position is advanced by one byte.
virtual int close()=0
Close the IO source. After closing a BasicIo instance can not be read or written. Closing flushes any...
virtual ~BasicIo()=default
Destructor.
virtual std::string path() const =0
Return the path to the IO resource. Often used to form comprehensive error messages where only a Basi...
virtual DataBuf read(long rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
virtual int munmap()=0
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
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.
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 ...
std::unique_ptr< BasicIo > UniquePtr
BasicIo auto_ptr type.
Definition: basicio.hpp:58
virtual int putb(byte data)=0
Write one byte to the IO source. Current IO position is advanced by one byte.
virtual int error() const =0
Returns 0 if the IO source is in a valid state, otherwise nonzero.
virtual long tell() const =0
Get the current IO position.
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 ...
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 ...
Internal Pimpl structure of class FileIo.
Definition: basicio.cpp:81
Provides binary file IO by implementing the BasicIo interface.
Definition: basicio.hpp:288
FileIo(FileIo &rhs)=delete
Copy constructor.
FileIo & operator=(const FileIo &rhs)=delete
Assignment operator.
Internal Pimpl structure of class HttpIo.
Definition: basicio.cpp:2022
Provides the http read/write access for the RemoteIo.
Definition: basicio.hpp:1002
HttpIo(HttpIo &rhs)=delete
Copy constructor.
HttpIo & operator=(const HttpIo &rhs)=delete
Assignment operator.
Utility class that closes a BasicIo instance upon destruction. Meant to be used as a stack variable i...
Definition: basicio.hpp:257
IoCloser & operator=(const IoCloser &)=delete
Assignment operator.
IoCloser(BasicIo &bio)
Constructor, takes a BasicIo reference.
Definition: basicio.hpp:262
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition: basicio.hpp:264
void close()
Close the BasicIo if it is open.
Definition: basicio.hpp:270
BasicIo & bio_
The BasicIo reference.
Definition: basicio.hpp:275
IoCloser(const IoCloser &)=delete
Copy constructor.
Internal Pimpl structure of class MemIo.
Definition: basicio.cpp:1053
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition: basicio.hpp:532
MemIo(MemIo &rhs)=delete
Copy constructor.
MemIo & operator=(const MemIo &rhs)=delete
Assignment operator.
Internal Pimpl abstract structure of class RemoteIo.
Definition: basicio.cpp:1566
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class....
Definition: basicio.hpp:824
Provides binary IO for the data from stdin and data uri path.
Definition: basicio.hpp:753
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:759
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:764
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: basicio.hpp:43
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:2492
EXIV2API long writeFile(const DataBuf &buf, const std::string &path)
Write DataBuf buf to file path.
Definition: basicio.cpp:2472
EXIV2API DataBuf readFile(const std::string &path)
Read file path into a DataBuf, which is returned.
Definition: basicio.cpp:2434
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:162