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> // for std::auto_ptr
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::auto_ptr<BasicIo> AutoPtr;
59
61 enum Position { beg, cur, end };
62
64
65
66 virtual ~BasicIo();
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
247
249
250 protected:
252
253
254 BasicIo() : bigBlock_(NULL) {};
256 }; // class BasicIo
257
264 class EXIV2API IoCloser {
265 public:
267
268
269 explicit IoCloser(BasicIo& bio) : bio_(bio) {}
271 virtual ~IoCloser() { close(); }
273
275
276
277 void close() { if (bio_.isopen()) bio_.close(); }
279
280 // DATA
283
284 private:
285 // Not implemented
287 IoCloser(const IoCloser&);
289 IoCloser& operator=(const IoCloser&);
290 }; // class IoCloser
291
296 class EXIV2API FileIo : public BasicIo {
297 public:
299
300
306 explicit FileIo(const std::string& path);
307#ifdef EXV_UNICODE_PATH
313 FileIo(const std::wstring& wpath);
314#endif
316 virtual ~FileIo();
318
320
321
334 int open(const std::string& mode);
342 virtual int open();
349 virtual int close();
359 virtual long write(const byte* data, long wcount);
369 virtual long write(BasicIo& src);
377 virtual int putb(byte data);
388 virtual DataBuf read(long rcount);
401 virtual long read(byte* buf, long rcount);
408 virtual int getb();
427 virtual void transfer(BasicIo& src);
436#if defined(_MSC_VER)
437 virtual int seek(int64_t offset, Position pos);
438#else
439 virtual int seek(long offset, Position pos);
440#endif
452 virtual byte* mmap(bool isWriteable =false);
460 virtual int munmap();
464 virtual void setPath(const std::string& path);
465#ifdef EXV_UNICODE_PATH
471 virtual void setPath(const std::wstring& wpath);
472#endif
474
476
481 virtual long tell() const;
488 virtual size_t size() const;
490 virtual bool isopen() const;
492 virtual int error() const;
494 virtual bool eof() const;
496 virtual std::string path() const;
497#ifdef EXV_UNICODE_PATH
498 /*
499 @brief Like path() but returns the unicode path of the file in an std::wstring.
500 @note This function is only available on Windows.
501 */
502 virtual std::wstring wpath() const;
503#endif
504
512 virtual void populateFakeData();
514
515 private:
516 // NOT IMPLEMENTED
518 FileIo(FileIo& rhs);
520 FileIo& operator=(const FileIo& rhs);
521
522 // Pimpl idiom
523 class Impl;
524 std::auto_ptr<Impl> p_;
525
526 }; // class FileIo
527
540 class EXIV2API MemIo : public BasicIo {
541 public:
543
544
545 MemIo();
554 MemIo(const byte* data, long size);
556 virtual ~MemIo();
558
560
561
567 virtual int open();
572 virtual int close();
583 virtual long write(const byte* data, long wcount);
594 virtual long write(BasicIo& src);
602 virtual int putb(byte data);
613 virtual DataBuf read(long rcount);
626 virtual long read(byte* buf, long rcount);
633 virtual int getb();
649 virtual void transfer(BasicIo& src);
658#if defined(_MSC_VER)
659 virtual int seek(int64_t offset, Position pos);
660#else
661 virtual int seek(long offset, Position pos);
662#endif
671 virtual byte* mmap(bool /*isWriteable*/ =false);
672 virtual int munmap();
674
676
677
681 virtual long tell() const;
687 virtual size_t size() const;
689 virtual bool isopen() const;
691 virtual int error() const;
693 virtual bool eof() const;
695 virtual std::string path() const;
696#ifdef EXV_UNICODE_PATH
697 /*
698 @brief Like path() but returns a unicode dummy path in an std::wstring.
699 @note This function is only available on Windows.
700 */
701 virtual std::wstring wpath() const;
702#endif
703
711 virtual void populateFakeData();
712
714
715 private:
716 // NOT IMPLEMENTED
718 MemIo(MemIo& rhs);
720 MemIo& operator=(const MemIo& rhs);
721
722 // Pimpl idiom
723 class Impl;
724 std::auto_ptr<Impl> p_;
725
726 }; // class MemIo
727
731#if EXV_XPATH_MEMIO
732 class EXIV2API XPathIo : public MemIo {
733 public:
735
736
737 XPathIo(const std::string& path);
738#ifdef EXV_UNICODE_PATH
744 XPathIo(const std::wstring& wpath);
745#endif
747 private:
752 void ReadStdin();
758 void ReadDataUri(const std::string& path);
759 }; // class XPathIo
760#else
761 class EXIV2API XPathIo : public FileIo {
762 public:
767 static const std::string TEMP_FILE_EXT;
772 static const std::string GEN_FILE_EXT;
773
775
776
777 explicit XPathIo(const std::string& orgPath);
778#ifdef EXV_UNICODE_PATH
784 XPathIo(const std::wstring& wOrgPathpath);
785#endif
787 virtual ~XPathIo();
789
791
792
796 virtual void transfer(BasicIo& src);
797
799
801
802
808 static std::string writeDataToFile(const std::string& orgPath);
809#ifdef EXV_UNICODE_PATH
815 static std::string writeDataToFile(const std::wstring& wOrgPath);
816#endif
818
819 private:
820 // True if the file is a temporary file and it should be deleted in destructor.
821 bool isTemp_;
822 std::string tempFilePath_;
823 }; // class XPathIo
824#endif
825
826
832 class EXIV2API RemoteIo : public BasicIo {
833 public:
835 virtual ~RemoteIo();
837
839
840
849 virtual int open();
850
856 virtual int close();
861 virtual long write(const byte* data, long wcount);
876 virtual long write(BasicIo& src);
877
882 virtual int putb(byte data);
895 virtual DataBuf read(long rcount);
910 virtual long read(byte* buf, long rcount);
919 virtual int getb();
934 virtual void transfer(BasicIo& src);
943#if defined(_MSC_VER)
944 virtual int seek(int64_t offset, Position pos);
945#else
946 virtual int seek(long offset, Position pos);
947#endif
952 virtual byte* mmap(bool /*isWriteable*/ =false);
957 virtual int munmap();
959
961
965 virtual long tell() const;
971 virtual size_t size() const;
973 virtual bool isopen() const;
975 virtual int error() const;
977 virtual bool eof() const;
979 virtual std::string path() const;
980#ifdef EXV_UNICODE_PATH
981 /*
982 @brief Like path() but returns a unicode URL path in an std::wstring.
983 @note This function is only available on Windows.
984 */
985 virtual std::wstring wpath() const;
986#endif
987
995 virtual void populateFakeData();
996
998
999 protected:
1001
1002
1003 RemoteIo() {p_=NULL;}
1005
1006 // Pimpl idiom
1007 class Impl;
1010 }; // class RemoteIo
1011
1015 class EXIV2API HttpIo : public RemoteIo {
1016 public:
1018
1019
1028 HttpIo(const std::string& url, size_t blockSize = 1024);
1029#ifdef EXV_UNICODE_PATH
1035 HttpIo(const std::wstring& wurl, size_t blockSize = 1024);
1036#endif
1038 protected:
1039 // NOT IMPLEMENTED
1044 // Pimpl idiom
1045 class HttpImpl;
1046
1048
1049
1050 virtual ~HttpIo(){}
1052 };
1053
1054#ifdef EXV_USE_CURL
1059 class EXIV2API CurlIo : public RemoteIo {
1060 public:
1062
1063
1072 CurlIo(const std::string& url, size_t blockSize = 0);
1073#ifdef EXV_UNICODE_PATH
1079 CurlIo(const std::wstring& wurl, size_t blockSize = 0);
1080#endif
1086 long write(const byte* data, long wcount);
1092 long write(BasicIo& src);
1093 protected:
1094 // NOT IMPLEMENTED
1096 CurlIo(CurlIo& rhs);
1098 CurlIo& operator=(const CurlIo& rhs);
1099 // Pimpl idiom
1100 class CurlImpl;
1101
1103
1104
1105 virtual ~CurlIo(){}
1107 };
1108#endif
1109
1110#ifdef EXV_USE_SSH
1115 class EXIV2LIB_DEPRECATED_EXPORT SshIo : public RemoteIo {
1116 public:
1118
1119
1128 SshIo(const std::string& url, size_t blockSize = 1024);
1129#ifdef EXV_UNICODE_PATH
1135 SshIo(const std::wstring& wurl, size_t blockSize = 1024);
1136#endif
1138 protected:
1139 // NOT IMPLEMENTED
1141 SshIo(SshIo& rhs);
1143 SshIo& operator=(const SshIo& rhs);
1144 // Pimpl idiom
1145 class SshImpl;
1146
1148
1149
1150 virtual ~SshIo(){}
1152 };
1153#endif
1154
1155// *****************************************************************************
1156// template, inline and free functions
1157
1163 EXIV2API DataBuf readFile(const std::string& path);
1164#ifdef EXV_UNICODE_PATH
1169 EXIV2API DataBuf readFile(const std::wstring& wpath);
1170#endif
1176 EXIV2API long writeFile(const DataBuf& buf, const std::string& path);
1177#ifdef EXV_UNICODE_PATH
1182 EXIV2API long writeFile(const DataBuf& buf, const std::wstring& wpath);
1183#endif
1188 EXIV2API std::string ReplaceStringInPlace(std::string subject, const std::string& search,
1189 const std::string& replace);
1190#ifdef EXV_UNICODE_PATH
1196 EXIV2API std::wstring ReplaceStringInPlace(std::wstring subject, const std::wstring& search,
1197 const std::wstring& replace);
1198#endif
1199#ifdef EXV_USE_CURL
1203 EXIV2API size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData);
1204#endif
1205} // namespace Exiv2
1206#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.
byte * bigBlock_
this is allocated and populated by mmap()
Definition: basicio.hpp:246
virtual int close()=0
Close the IO source. After closing a BasicIo instance can not be read or written. Closing flushes any...
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.
BasicIo()
Default Constructor.
Definition: basicio.hpp:254
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 ...
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 ...
std::auto_ptr< BasicIo > AutoPtr
BasicIo auto_ptr type.
Definition: basicio.hpp:58
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:193
Internal Pimpl structure of class FileIo.
Definition: basicio.cpp:89
Provides binary file IO by implementing the BasicIo interface.
Definition: basicio.hpp:296
Internal Pimpl structure of class HttpIo.
Definition: basicio.cpp:2031
Provides the http read/write access for the RemoteIo.
Definition: basicio.hpp:1015
virtual ~HttpIo()
Default Destructor.
Definition: basicio.hpp:1050
HttpIo & operator=(const HttpIo &rhs)
Assignment operator.
HttpIo(HttpIo &rhs)
Copy constructor.
Utility class that closes a BasicIo instance upon destruction. Meant to be used as a stack variable i...
Definition: basicio.hpp:264
IoCloser(BasicIo &bio)
Constructor, takes a BasicIo reference.
Definition: basicio.hpp:269
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition: basicio.hpp:271
void close()
Close the BasicIo if it is open.
Definition: basicio.hpp:277
BasicIo & bio_
The BasicIo reference.
Definition: basicio.hpp:282
Internal Pimpl structure of class MemIo.
Definition: basicio.cpp:1054
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition: basicio.hpp:540
Internal Pimpl abstract structure of class RemoteIo.
Definition: basicio.cpp:1587
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class....
Definition: basicio.hpp:832
RemoteIo()
Default Constructor.
Definition: basicio.hpp:1003
Impl * p_
Pointer to implementation.
Definition: basicio.hpp:1009
Provides binary IO for the data from stdin and data uri path.
Definition: basicio.hpp:761
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
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
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
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
EXIV2API long writeFile(const DataBuf &buf, const std::string &path)
Write DataBuf buf to file path.
Definition: basicio.cpp:2703
EXIV2API DataBuf readFile(const std::string &path)
Read file path into a DataBuf, which is returned.
Definition: basicio.cpp:2665