Exiv2
Loading...
Searching...
No Matches
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
virtual void setPath(const std::string &path)
close the file source and set a new path.
Definition basicio.cpp:552
int open(const std::string &mode)
Open the file using using the specified mode.
Definition basicio.cpp:958
virtual bool eof() const
Returns true if the file position has reached the end, otherwise false.
Definition basicio.cpp:1023
virtual void populateFakeData()
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:1049
virtual long tell() const
Get the current file position.
Definition basicio.cpp:928
virtual int error() const
Returns 0 if the file is in a valid state, otherwise nonzero.
Definition basicio.cpp:1018
virtual long write(const byte *data, long wcount)
Write data to the file. The file position is advanced by the number of bytes written.
Definition basicio.cpp:579
virtual DataBuf read(long rcount)
Read data from the file. Reading starts at the current file position and the position is advanced by ...
Definition basicio.cpp:992
virtual int putb(byte data)
Write one byte to the file. The file position is advanced by one byte.
Definition basicio.cpp:883
virtual void transfer(BasicIo &src)
Remove the contents of the file and then transfer data from the src BasicIo object into the empty fil...
Definition basicio.cpp:609
virtual int getb()
Read one byte from the file. The file position is advanced by one byte.
Definition basicio.cpp:1011
virtual bool isopen() const
Returns true if the file is open, otherwise false.
Definition basicio.cpp:976
FileIo(const std::string &path)
Constructor that accepts the file path on which IO will be performed. The constructor does not open t...
Definition basicio.cpp:361
virtual byte * mmap(bool isWriteable=false)
Map the file into the process's address space. The file must be open before mmap() is called....
Definition basicio.cpp:412
virtual int munmap()
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Definition basicio.cpp:378
virtual size_t size() const
Flush any buffered writes and get the current file size in bytes.
Definition basicio.cpp:934
virtual int close()
Flush and unwritten data and close the file . It is safe to call close on an already closed instance.
Definition basicio.cpp:981
virtual std::string path() const
Returns the path of the file.
Definition basicio.cpp:1028
virtual int seek(long offset, Position pos)
Move the current file position.
Definition basicio.cpp:910
Internal Pimpl structure of class HttpIo.
Definition basicio.cpp:2031
virtual ~HttpIo()
Default Destructor.
Definition basicio.hpp:1050
HttpIo & operator=(const HttpIo &rhs)
Assignment operator.
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...
Definition basicio.cpp:2184
HttpIo(HttpIo &rhs)
Copy constructor.
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
virtual int putb(byte data)
Write one byte to the memory block. The IO position is advanced by one byte.
Definition basicio.cpp:1282
virtual bool isopen() const
Always returns true.
Definition basicio.cpp:1365
virtual int open()
Memory IO is always open for reading and writing. This method therefore only resets the IO position t...
Definition basicio.cpp:1358
virtual std::string path() const
Returns a dummy path, indicating that memory access is used.
Definition basicio.cpp:1414
virtual int seek(long offset, Position pos)
Move the current IO position.
Definition basicio.cpp:1314
virtual void populateFakeData()
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:1426
virtual int close()
Does nothing on MemIo objects.
Definition basicio.cpp:1370
virtual long tell() const
Get the current IO position.
Definition basicio.cpp:1348
virtual byte * mmap(bool=false)
Allow direct access to the underlying data buffer. The buffer is not protected against write access i...
Definition basicio.cpp:1338
MemIo()
Default constructor that results in an empty object.
Definition basicio.cpp:1209
virtual long write(const byte *data, long wcount)
Write data to the memory block. If needed, the size of the internal memory block is expanded....
Definition basicio.cpp:1226
virtual DataBuf read(long rcount)
Read data from the memory block. Reading starts at the current IO position and the position is advanc...
Definition basicio.cpp:1375
virtual void transfer(BasicIo &src)
Clear the memory block and then transfer data from the src BasicIo object into a new block of memory.
Definition basicio.cpp:1237
virtual int getb()
Read one byte from the memory block. The IO position is advanced by one byte.
Definition basicio.cpp:1395
virtual int munmap()
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Definition basicio.cpp:1343
virtual int error() const
Always returns 0.
Definition basicio.cpp:1404
virtual bool eof() const
Returns true if the IO position has reached the end, otherwise false.
Definition basicio.cpp:1409
virtual size_t size() const
Get the current memory buffer size in bytes.
Definition basicio.cpp:1353
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
virtual int getb()
Read one byte from the memory blocks. The IO position is advanced by one byte. If the memory block is...
Definition basicio.cpp:1891
virtual void populateFakeData()
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:2019
virtual bool isopen() const
Returns true if the memory area is allocated.
Definition basicio.cpp:1992
virtual int open()
Connect to the remote server, get the size of the remote file and allocate the array of blocksMap.
Definition basicio.cpp:1709
Impl * p_
Pointer to implementation.
Definition basicio.hpp:1009
virtual std::string path() const
Returns the URL of the file.
Definition basicio.cpp:2007
virtual int seek(long offset, Position pos)
Move the current IO position.
Definition basicio.cpp:1934
virtual int close()
Reset the IO position to the start. It does not release the data.
Definition basicio.cpp:1743
virtual DataBuf read(long rcount)
Read data from the memory blocks. Reading starts at the current IO position and the position is advan...
Definition basicio.cpp:1845
virtual int munmap()
Not support.
Definition basicio.cpp:1977
virtual long write(const byte *data, long wcount)
Not support this method.
Definition basicio.cpp:1759
virtual int putb(byte data)
Not support.
Definition basicio.cpp:1840
virtual int error() const
Always returns 0.
Definition basicio.cpp:1997
virtual byte * mmap(bool=false)
Not support.
Definition basicio.cpp:1954
virtual void transfer(BasicIo &src)
Remove the contents of the file and then transfer data from the src BasicIo object into the empty fil...
Definition basicio.cpp:1907
virtual bool eof() const
Returns true if the IO position has reached the end, otherwise false.
Definition basicio.cpp:2002
virtual size_t size() const
Get the current memory buffer size in bytes.
Definition basicio.cpp:1987
virtual long tell() const
Get the current IO position.
Definition basicio.cpp:1982
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
XPathIo(const std::string &orgPath)
Default constructor that reads data from stdin/data uri path and writes them to the temp file.
Definition basicio.cpp:1487
static std::string writeDataToFile(const std::string &orgPath)
Read the data from stdin/data uri path and write them to the file.
Definition basicio.cpp:1522
virtual void transfer(BasicIo &src)
Change the name of the temp file and make it untemporary before calling the method of superclass File...
Definition basicio.cpp:1506
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