Exiv2
value.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#ifndef VALUE_HPP_
21#define VALUE_HPP_
22
23// *****************************************************************************
24#include "exiv2lib_export.h"
25
26// included header files
27#include "types.hpp"
28
29// + standard includes
30#include <map>
31#include <iomanip>
32#include <memory>
33#include <cstring>
34#include <climits>
35
36// *****************************************************************************
37// namespace extensions
38namespace Exiv2 {
39
40// *****************************************************************************
41// class definitions
42
51 class EXIV2API Value {
52 public:
54 typedef std::unique_ptr<Value> UniquePtr;
55
57
58
59 explicit Value(TypeId typeId);
61 virtual ~Value() = default;
63
65
74 virtual int read(const byte* buf, long len, ByteOrder byteOrder) =0;
85 virtual int read(const std::string& buf) =0;
98 virtual int setDataArea(const byte* buf, long len);
100
102
103
104 TypeId typeId() const { return type_; }
110 UniquePtr clone() const { return UniquePtr(clone_()); }
121 virtual long copy(byte* buf, ByteOrder byteOrder) const =0;
123 virtual long count() const =0;
125 virtual long size() const =0;
132 virtual std::ostream& write(std::ostream& os) const =0;
137 std::string toString() const;
144 virtual std::string toString(long n) const;
152 virtual long toLong(long n =0) const =0;
160 virtual float toFloat(long n =0) const =0;
168 virtual Rational toRational(long n =0) const =0;
170 virtual long sizeDataArea() const;
183 virtual DataBuf dataArea() const;
188 bool ok() const { return ok_; }
190
227 static UniquePtr create(TypeId typeId);
228
229 protected:
234 Value& operator=(const Value& rhs) = default;
235 // DATA
236 mutable bool ok_;
237
238 private:
240 virtual Value* clone_() const =0;
241 // DATA
242 TypeId type_;
243
244 }; // class Value
245
247 inline std::ostream& operator<<(std::ostream& os, const Value& value)
248 {
249 return value.write(os);
250 }
251
253 class EXIV2API DataValue : public Value {
254 public:
256 typedef std::unique_ptr<DataValue> UniquePtr;
257
258 explicit DataValue(TypeId typeId =undefined);
259
260 DataValue(const byte* buf,
261 long len, ByteOrder byteOrder =invalidByteOrder,
262 TypeId typeId =undefined);
263
264 ~DataValue() override = default;
265
267
268
280 int read(const byte* buf, long len, ByteOrder byteOrder = invalidByteOrder) override;
282 int read(const std::string& buf) override;
284
286
287 UniquePtr clone() const { return UniquePtr(clone_()); }
301 long copy(byte* buf, ByteOrder byteOrder = invalidByteOrder) const override;
302 long count() const override;
303 long size() const override;
304 std::ostream& write(std::ostream& os) const override;
310 std::string toString(long n) const override;
311 long toLong(long n = 0) const override;
312 float toFloat(long n = 0) const override;
313 Rational toRational(long n = 0) const override;
315
316 private:
318 DataValue* clone_() const override;
319
321 typedef std::vector<byte> ValueType;
322 // DATA
323 ValueType value_;
324
325 }; // class DataValue
326
333 class EXIV2API StringValueBase : public Value {
334 public:
336 typedef std::unique_ptr<StringValueBase> UniquePtr;
337
339
340
341 explicit StringValueBase(TypeId typeId);
343 StringValueBase(TypeId typeId, const std::string& buf);
345 StringValueBase(const StringValueBase& rhs) = default;
347 ~StringValueBase() override = default;
349
351
352
353 int read(const std::string& buf) override;
366 int read(const byte* buf, long len, ByteOrder byteOrder = invalidByteOrder) override;
368
370
371 UniquePtr clone() const { return UniquePtr(clone_()); }
385 long copy(byte* buf, ByteOrder byteOrder = invalidByteOrder) const override;
386 long count() const override;
387 long size() const override;
388 long toLong(long n = 0) const override;
389 float toFloat(long n = 0) const override;
390 Rational toRational(long n = 0) const override;
391 std::ostream& write(std::ostream& os) const override;
393
394 protected:
396 StringValueBase& operator=(const StringValueBase& rhs);
398 StringValueBase* clone_() const override = 0;
399
400 public:
401 // DATA
402 std::string value_;
403
404 }; // class StringValueBase
405
413 class EXIV2API StringValue : public StringValueBase {
414 public:
416 typedef std::unique_ptr<StringValue> UniquePtr;
417
419
420
421 StringValue();
423 explicit StringValue(const std::string& buf);
425 ~StringValue() override = default;
427
429
430 UniquePtr clone() const { return UniquePtr(clone_()); }
432
433 private:
435 StringValue* clone_() const override;
436
437 }; // class StringValue
438
445 class EXIV2API AsciiValue : public StringValueBase {
446 public:
448 typedef std::unique_ptr<AsciiValue> UniquePtr;
449
451
452
453 AsciiValue();
455 explicit AsciiValue(const std::string& buf);
457 ~AsciiValue() override = default;
459
461
468 int read(const std::string& buf) override;
470
472
473 UniquePtr clone() const { return UniquePtr(clone_()); }
479 std::ostream& write(std::ostream& os) const override;
481
482 private:
484 AsciiValue* clone_() const override;
485
486 }; // class AsciiValue
487
496 class EXIV2API CommentValue : public StringValueBase {
497 public:
499 enum CharsetId { ascii, jis, unicode, undefined,
500 invalidCharsetId, lastCharsetId };
504 CharsetTable(CharsetId charsetId,
505 const char* name,
506 const char* code);
508 const char* name_;
509 const char* code_;
510 }; // struct CharsetTable
511
513 class EXIV2API CharsetInfo {
514 public:
516 CharsetInfo() = delete;
518 CharsetInfo(const CharsetInfo&) = delete;
521
523 static const char* name(CharsetId charsetId);
525 static const char* code(CharsetId charsetId);
527 static CharsetId charsetIdByName(const std::string& name);
529 static CharsetId charsetIdByCode(const std::string& code);
530
531 private:
532 static const CharsetTable charsetTable_[];
533 }; // class CharsetInfo
534
536 typedef std::unique_ptr<CommentValue> UniquePtr;
537
539
540
541 CommentValue();
543 explicit CommentValue(const std::string& comment);
545 ~CommentValue() override = default;
547
549
550
562 int read(const std::string& comment) override;
566 int read(const byte* buf, long len, ByteOrder byteOrder) override;
568
570
571 UniquePtr clone() const { return UniquePtr(clone_()); }
572 long copy(byte* buf, ByteOrder byteOrder) const override;
577 std::ostream& write(std::ostream& os) const override;
592 std::string comment(const char* encoding = nullptr) const;
602 const char* detectCharset(std::string& c) const;
604 CharsetId charsetId() const;
606
607 private:
609 CommentValue* clone_() const override;
610
611 public:
612 // DATA
614
615 }; // class CommentValue
616
620 class EXIV2API XmpValue : public Value {
621 public:
623 typedef std::unique_ptr<XmpValue> UniquePtr;
624
626 enum XmpArrayType { xaNone, xaAlt, xaBag, xaSeq };
628 enum XmpStruct { xsNone, xsStruct };
629
631
632 explicit XmpValue(TypeId typeId);
634
636
637
638 XmpArrayType xmpArrayType() const;
640 XmpStruct xmpStruct() const;
641 long size() const override;
655 long copy(byte* buf, ByteOrder byteOrder = invalidByteOrder) const override;
657
659
660
661 void setXmpArrayType(XmpArrayType xmpArrayType);
663 void setXmpStruct(XmpStruct xmpStruct =xsStruct);
678 int read(const byte* buf, long len, ByteOrder byteOrder = invalidByteOrder) override;
679 int read(const std::string& buf) override = 0;
681
686 static XmpArrayType xmpArrayType(TypeId typeId);
687
688 private:
689 // DATA
690 XmpArrayType xmpArrayType_;
691 XmpStruct xmpStruct_;
692
693 }; // class XmpValue
694
702 class EXIV2API XmpTextValue : public XmpValue {
703 public:
705 typedef std::unique_ptr<XmpTextValue> UniquePtr;
706
708
709
710 XmpTextValue();
712 explicit XmpTextValue(const std::string& buf);
714
716
717 using XmpValue::read;
733 int read(const std::string& buf) override;
735
737
738 UniquePtr clone() const;
739 long size() const override;
740 long count() const override;
747 long toLong(long n = 0) const override;
754 float toFloat(long n = 0) const override;
761 Rational toRational(long n = 0) const override;
762 std::ostream& write(std::ostream& os) const override;
764
765 private:
767 XmpTextValue* clone_() const override;
768
769 public:
770 // DATA
771 std::string value_;
772
773 }; // class XmpTextValue
774
784 class EXIV2API XmpArrayValue : public XmpValue {
785 public:
787 typedef std::unique_ptr<XmpArrayValue> UniquePtr;
788
790
791
792 explicit XmpArrayValue(TypeId typeId =xmpBag);
794
796
797 using XmpValue::read;
808 int read(const std::string& buf) override;
810
812
813 UniquePtr clone() const;
814 long count() const override;
820 std::string toString(long n) const override;
821 long toLong(long n = 0) const override;
822 float toFloat(long n = 0) const override;
823 Rational toRational(long n = 0) const override;
830 std::ostream& write(std::ostream& os) const override;
832
833 private:
835 XmpArrayValue* clone_() const override;
836
837 std::vector<std::string> value_;
838
839 }; // class XmpArrayValue
840
850 bool operator() (const std::string& str1, const std::string& str2) const
851 {
852 int result = str1.size() < str2.size() ? 1
853 : str1.size() > str2.size() ? -1
854 : 0
855 ;
856 std::string::const_iterator c1 = str1.begin();
857 std::string::const_iterator c2 = str2.begin();
858 if ( result==0 ) for (
859 ; result==0 && c1 != str1.end()
860 ; ++c1, ++c2
861 ) {
862 result = tolower(*c1) < tolower(*c2) ? 1
863 : tolower(*c1) > tolower(*c2) ? -1
864 : 0
865 ;
866 }
867 return result < 0 ;
868 }
869 };
870
877 class EXIV2API LangAltValue : public XmpValue {
878 public:
880 typedef std::unique_ptr<LangAltValue> UniquePtr;
881
883
884
885 LangAltValue();
887 explicit LangAltValue(const std::string& buf);
889
891
892 using XmpValue::read;
910 int read(const std::string& buf) override;
912
914
915 UniquePtr clone() const;
916 long count() const override;
924 std::string toString(long n) const override;
930 std::string toString(const std::string& qualifier) const;
931 long toLong(long n = 0) const override;
932 float toFloat(long n = 0) const override;
933 Rational toRational(long n = 0) const override;
940 std::ostream& write(std::ostream& os) const override;
942
943 private:
945 LangAltValue* clone_() const override;
946
947 public:
949 typedef std::map<std::string, std::string,LangAltValueComparator> ValueType;
950 // DATA
956
957 }; // class LangAltValue
958
965 class EXIV2API DateValue : public Value {
966 public:
968 typedef std::unique_ptr<DateValue> UniquePtr;
969
971
972
973 DateValue();
975 DateValue(int year, int month, int day);
977 ~DateValue() override = default;
979
981 struct EXIV2API Date {
982 Date() = default;
983 int year{0};
984 int month{0};
985 int day{0};
986 };
987
989
990
1003 int read(const byte* buf, long len, ByteOrder byteOrder = invalidByteOrder) override;
1012 int read(const std::string& buf) override;
1014 void setDate(const Date& src);
1016
1018
1019 UniquePtr clone() const { return UniquePtr(clone_()); }
1033 long copy(byte* buf, ByteOrder byteOrder = invalidByteOrder) const override;
1035 virtual const Date& getDate() const;
1036 long count() const override;
1037 long size() const override;
1038 std::ostream& write(std::ostream& os) const override;
1040 long toLong(long n = 0) const override;
1042 float toFloat(long n = 0) const override;
1044 Rational toRational(long n = 0) const override;
1046
1047 private:
1049 DateValue* clone_() const override;
1050
1051 // DATA
1052 Date date_;
1053
1054 }; // class DateValue
1055
1064 class EXIV2API TimeValue : public Value {
1065 public:
1067 typedef std::unique_ptr<TimeValue> UniquePtr;
1068
1070
1071
1072 TimeValue();
1074 TimeValue(int hour, int minute, int second =0,
1075 int tzHour =0, int tzMinute =0);
1076
1078 ~TimeValue() override = default;
1080
1082 struct Time
1083 {
1084 Time() = default;
1085
1086 int hour{0};
1087 int minute{0};
1088 int second{0};
1089 int tzHour{0};
1090 int tzMinute{0};
1091 };
1092
1094
1095
1108 int read(const byte* buf, long len, ByteOrder byteOrder = invalidByteOrder) override;
1117 int read(const std::string& buf) override;
1119 void setTime(const Time& src);
1121
1123
1124 UniquePtr clone() const { return UniquePtr(clone_()); }
1138 long copy(byte* buf, ByteOrder byteOrder = invalidByteOrder) const override;
1140 virtual const Time& getTime() const;
1141 long count() const override;
1142 long size() const override;
1143 std::ostream& write(std::ostream& os) const override;
1145 long toLong(long n = 0) const override;
1147 float toFloat(long n = 0) const override;
1149 Rational toRational(long n = 0) const override;
1151
1152 private:
1154
1155
1165 int scanTime3(const char* buf, const char* format);
1176 int scanTime6(const char* buf, const char* format);
1178
1180
1181
1182 TimeValue* clone_() const override;
1184
1185 // DATA
1186 Time time_;
1187
1188 }; // class TimeValue
1189
1191 template<typename T> TypeId getType();
1192
1194 template<> inline TypeId getType<uint16_t>() { return unsignedShort; }
1196 template<> inline TypeId getType<uint32_t>() { return unsignedLong; }
1198 template<> inline TypeId getType<URational>() { return unsignedRational; }
1200 template<> inline TypeId getType<int16_t>() { return signedShort; }
1202 template<> inline TypeId getType<int32_t>() { return signedLong; }
1204 template<> inline TypeId getType<Rational>() { return signedRational; }
1206 template<> inline TypeId getType<float>() { return tiffFloat; }
1208 template<> inline TypeId getType<double>() { return tiffDouble; }
1209
1210 // No default implementation: let the compiler/linker complain
1211 // template<typename T> inline TypeId getType() { return invalid; }
1212
1217 template<typename T>
1218 class ValueType : public Value {
1219 public:
1221 typedef std::unique_ptr<ValueType<T> > UniquePtr;
1222
1224
1225
1226 ValueType();
1228 // The default c'tor and this one can be combined, but that causes MSVC 7.1 to fall on its nose
1229 explicit ValueType(TypeId typeId);
1231 ValueType(const byte* buf, long len, ByteOrder byteOrder, TypeId typeId =getType<T>());
1233 explicit ValueType(const T& val, TypeId typeId =getType<T>());
1235 ValueType(const ValueType<T>& rhs);
1237 ~ValueType() override;
1239
1241
1242
1243 ValueType<T>& operator=(const ValueType<T>& rhs);
1244 int read(const byte* buf, long len, ByteOrder byteOrder) override;
1251 int read(const std::string& buf) override;
1256 int setDataArea(const byte* buf, long len) override;
1258
1260
1261 UniquePtr clone() const { return UniquePtr(clone_()); }
1262 long copy(byte* buf, ByteOrder byteOrder) const override;
1263 long count() const override;
1264 long size() const override;
1265 std::ostream& write(std::ostream& os) const override;
1272 std::string toString(long n) const override;
1273 long toLong(long n = 0) const override;
1274 float toFloat(long n = 0) const override;
1275 Rational toRational(long n = 0) const override;
1277 long sizeDataArea() const override;
1282 DataBuf dataArea() const override;
1284
1286 typedef std::vector<T> ValueList;
1288 typedef typename std::vector<T>::iterator iterator;
1290 typedef typename std::vector<T>::const_iterator const_iterator;
1291
1292 // DATA
1300
1301 private:
1303 ValueType<T>* clone_() const override;
1304
1305 // DATA
1307 byte* pDataArea_{nullptr};
1309 long sizeDataArea_{0};
1310 }; // class ValueType
1311
1328
1329// *****************************************************************************
1330// free functions, template and inline definitions
1331
1343 template<typename T> T getValue(const byte* buf, ByteOrder byteOrder);
1344 // Specialization for a 2 byte unsigned short value.
1345 template<>
1346 inline uint16_t getValue(const byte* buf, ByteOrder byteOrder)
1347 {
1348 return getUShort(buf, byteOrder);
1349 }
1350 // Specialization for a 4 byte unsigned long value.
1351 template<>
1352 inline uint32_t getValue(const byte* buf, ByteOrder byteOrder)
1353 {
1354 return getULong(buf, byteOrder);
1355 }
1356 // Specialization for an 8 byte unsigned rational value.
1357 template<>
1358 inline URational getValue(const byte* buf, ByteOrder byteOrder)
1359 {
1360 return getURational(buf, byteOrder);
1361 }
1362 // Specialization for a 2 byte signed short value.
1363 template<>
1364 inline int16_t getValue(const byte* buf, ByteOrder byteOrder)
1365 {
1366 return getShort(buf, byteOrder);
1367 }
1368 // Specialization for a 4 byte signed long value.
1369 template<>
1370 inline int32_t getValue(const byte* buf, ByteOrder byteOrder)
1371 {
1372 return getLong(buf, byteOrder);
1373 }
1374 // Specialization for an 8 byte signed rational value.
1375 template<>
1376 inline Rational getValue(const byte* buf, ByteOrder byteOrder)
1377 {
1378 return getRational(buf, byteOrder);
1379 }
1380 // Specialization for a 4 byte float value.
1381 template<>
1382 inline float getValue(const byte* buf, ByteOrder byteOrder)
1383 {
1384 return getFloat(buf, byteOrder);
1385 }
1386 // Specialization for a 8 byte double value.
1387 template<>
1388 inline double getValue(const byte* buf, ByteOrder byteOrder)
1389 {
1390 return getDouble(buf, byteOrder);
1391 }
1392
1405 template<typename T> long toData(byte* buf, T t, ByteOrder byteOrder);
1410 template<>
1411 inline long toData(byte* buf, uint16_t t, ByteOrder byteOrder)
1412 {
1413 return us2Data(buf, t, byteOrder);
1414 }
1419 template<>
1420 inline long toData(byte* buf, uint32_t t, ByteOrder byteOrder)
1421 {
1422 return ul2Data(buf, t, byteOrder);
1423 }
1428 template<>
1429 inline long toData(byte* buf, URational t, ByteOrder byteOrder)
1430 {
1431 return ur2Data(buf, t, byteOrder);
1432 }
1437 template<>
1438 inline long toData(byte* buf, int16_t t, ByteOrder byteOrder)
1439 {
1440 return s2Data(buf, t, byteOrder);
1441 }
1446 template<>
1447 inline long toData(byte* buf, int32_t t, ByteOrder byteOrder)
1448 {
1449 return l2Data(buf, t, byteOrder);
1450 }
1455 template<>
1456 inline long toData(byte* buf, Rational t, ByteOrder byteOrder)
1457 {
1458 return r2Data(buf, t, byteOrder);
1459 }
1464 template<>
1465 inline long toData(byte* buf, float t, ByteOrder byteOrder)
1466 {
1467 return f2Data(buf, t, byteOrder);
1468 }
1473 template<>
1474 inline long toData(byte* buf, double t, ByteOrder byteOrder)
1475 {
1476 return d2Data(buf, t, byteOrder);
1477 }
1478
1479 template <typename T>
1481 {
1482 }
1483
1484 template <typename T>
1486 {
1487 }
1488
1489 template <typename T>
1490 ValueType<T>::ValueType(const byte* buf, long len, ByteOrder byteOrder, TypeId typeId) : Value(typeId)
1491 {
1492 read(buf, len, byteOrder);
1493 }
1494
1495 template <typename T>
1496 ValueType<T>::ValueType(const T& val, TypeId typeId) : Value(typeId)
1497 {
1498 value_.push_back(val);
1499 }
1500
1501 template<typename T>
1503 : Value(rhs.typeId())
1504 , value_(rhs.value_)
1505
1506 {
1507 if (rhs.sizeDataArea_ > 0) {
1508 pDataArea_ = new byte[rhs.sizeDataArea_];
1509 std::memcpy(pDataArea_, rhs.pDataArea_, rhs.sizeDataArea_);
1510 sizeDataArea_ = rhs.sizeDataArea_;
1511 }
1512 }
1513
1514 template<typename T>
1516 {
1517 delete[] pDataArea_;
1518 }
1519
1520 template<typename T>
1522 {
1523 if (this == &rhs) return *this;
1524 Value::operator=(rhs);
1525 value_ = rhs.value_;
1526
1527 byte* tmp = nullptr;
1528 if (rhs.sizeDataArea_ > 0) {
1529 tmp = new byte[rhs.sizeDataArea_];
1530 std::memcpy(tmp, rhs.pDataArea_, rhs.sizeDataArea_);
1531 }
1532 delete[] pDataArea_;
1533 pDataArea_ = tmp;
1534 sizeDataArea_ = rhs.sizeDataArea_;
1535
1536 return *this;
1537 }
1538
1539 template<typename T>
1540 int ValueType<T>::read(const byte* buf, long len, ByteOrder byteOrder)
1541 {
1542 value_.clear();
1543 long ts = TypeInfo::typeSize(typeId());
1544 if (ts > 0)
1545 if (len % ts != 0) len = (len / ts) * ts;
1546 for (long i = 0; i < len; i += ts) {
1547 value_.push_back(getValue<T>(buf + i, byteOrder));
1548 }
1549 return 0;
1550 }
1551
1552 template<typename T>
1553 int ValueType<T>::read(const std::string& buf)
1554 {
1555 std::istringstream is(buf);
1556 T tmp = T();
1557 ValueList val;
1558 while (!(is.eof())) {
1559 is >> tmp;
1560 if (is.fail()) return 1;
1561 val.push_back(tmp);
1562 }
1563 value_.swap(val);
1564 return 0;
1565 }
1566
1567 template<typename T>
1568 long ValueType<T>::copy(byte* buf, ByteOrder byteOrder) const
1569 {
1570 long offset = 0;
1571 typename ValueList::const_iterator end = value_.end();
1572 for (typename ValueList::const_iterator i = value_.begin(); i != end; ++i) {
1573 offset += toData(buf + offset, *i, byteOrder);
1574 }
1575 return offset;
1576 }
1577
1578 template<typename T>
1580 {
1581 return static_cast<long>(value_.size());
1582 }
1583
1584 template<typename T>
1586 {
1587 return static_cast<long>(TypeInfo::typeSize(typeId()) * value_.size());
1588 }
1589
1590 template<typename T>
1592 {
1593 return new ValueType<T>(*this);
1594 }
1595
1596 template<typename T>
1597 std::ostream& ValueType<T>::write(std::ostream& os) const
1598 {
1599 typename ValueList::const_iterator end = value_.end();
1600 typename ValueList::const_iterator i = value_.begin();
1601 while (i != end) {
1602 os << std::setprecision(15) << *i;
1603 if (++i != end) os << " ";
1604 }
1605 return os;
1606 }
1607
1608 template<typename T>
1609 std::string ValueType<T>::toString(long n) const
1610 {
1611 ok_ = true;
1612 return Exiv2::toString<T>(value_.at(n));
1613 }
1614
1615 // Default implementation
1616 template<typename T>
1617 long ValueType<T>::toLong(long n) const
1618 {
1619 ok_ = true;
1620 return static_cast<long>(value_.at(n));
1621 }
1622// #55 crash when value_.at(n).first == LONG_MIN
1623#define LARGE_INT 1000000
1624 // Specialization for double
1625 template<>
1626 inline long ValueType<double>::toLong(long n) const
1627 {
1628 const double v = value_.at(n);
1629 ok_ = (INT_MIN <= v && v <= INT_MAX);
1630 if (!ok_) return 0;
1631 return static_cast<long>(v);
1632 }
1633 // Specialization for float
1634 template<>
1635 inline long ValueType<float>::toLong(long n) const
1636 {
1637 const double v = value_.at(n);
1638 ok_ = (INT_MIN <= v && v <= INT_MAX);
1639 if (!ok_) return 0;
1640 return static_cast<long>(v);
1641 }
1642 // Specialization for rational
1643 template<>
1644 inline long ValueType<Rational>::toLong(long n) const
1645 {
1646 ok_ = (value_.at(n).second > 0 && INT_MIN < value_.at(n).first && value_.at(n).first < INT_MAX );
1647 if (!ok_) return 0;
1648 return value_.at(n).first / value_.at(n).second;
1649 }
1650 // Specialization for unsigned rational
1651 template<>
1652 inline long ValueType<URational>::toLong(long n) const
1653 {
1654 ok_ = (value_.at(n).second > 0 && value_.at(n).first < LARGE_INT);
1655 if (!ok_) return 0;
1656 return value_.at(n).first / value_.at(n).second;
1657 }
1658 // Default implementation
1659 template<typename T>
1660 float ValueType<T>::toFloat(long n) const
1661 {
1662 ok_ = true;
1663 return static_cast<float>(value_.at(n));
1664 }
1665 // Specialization for rational
1666 template<>
1667 inline float ValueType<Rational>::toFloat(long n) const
1668 {
1669 ok_ = (value_.at(n).second != 0);
1670 if (!ok_) return 0.0f;
1671 return static_cast<float>(value_.at(n).first) / value_.at(n).second;
1672 }
1673 // Specialization for unsigned rational
1674 template<>
1675 inline float ValueType<URational>::toFloat(long n) const
1676 {
1677 ok_ = (value_.at(n).second != 0);
1678 if (!ok_) return 0.0f;
1679 return static_cast<float>(value_.at(n).first) / value_.at(n).second;
1680 }
1681 // Default implementation
1682 template<typename T>
1684 {
1685 ok_ = true;
1686 return {value_.at(n), 1};
1687 }
1688 // Specialization for rational
1689 template<>
1691 {
1692 ok_ = true;
1693 return {value_.at(n).first, value_.at(n).second};
1694 }
1695 // Specialization for unsigned rational
1696 template<>
1698 {
1699 ok_ = true;
1700 return {value_.at(n).first, value_.at(n).second};
1701 }
1702 // Specialization for float.
1703 template<>
1705 {
1706 ok_ = true;
1707 // Warning: This is a very simple conversion, see floatToRationalCast()
1708 return floatToRationalCast(value_.at(n));
1709 }
1710 // Specialization for double.
1711 template<>
1713 {
1714 ok_ = true;
1715 // Warning: This is a very simple conversion, see floatToRationalCast()
1716 return floatToRationalCast(static_cast<float>(value_.at(n)));
1717 }
1718
1719 template<typename T>
1721 {
1722 return sizeDataArea_;
1723 }
1724
1725 template<typename T>
1727 {
1728 return DataBuf(pDataArea_, sizeDataArea_);
1729 }
1730
1731 template<typename T>
1732 int ValueType<T>::setDataArea(const byte* buf, long len)
1733 {
1734 byte* tmp = nullptr;
1735 if (len > 0) {
1736 tmp = new byte[len];
1737 std::memcpy(tmp, buf, len);
1738 }
1739 delete[] pDataArea_;
1740 pDataArea_ = tmp;
1741 sizeDataArea_ = len;
1742 return 0;
1743 }
1744} // namespace Exiv2
1745
1746#endif // #ifndef VALUE_HPP_
Value for an Ascii string type.
Definition: value.hpp:445
~AsciiValue() override=default
Virtual destructor.
std::unique_ptr< AsciiValue > UniquePtr
Shortcut for a AsciiValue auto pointer.
Definition: value.hpp:448
Charset information lookup functions. Implemented as a static class.
Definition: value.hpp:513
CharsetInfo & operator=(const CharsetInfo &)=delete
Prevent assignment: not implemented.
CharsetInfo(const CharsetInfo &)=delete
Prevent copy-construction: not implemented.
CharsetInfo()=delete
Prevent construction: not implemented.
Value for an Exif comment.
Definition: value.hpp:496
std::unique_ptr< CommentValue > UniquePtr
Shortcut for a CommentValue auto pointer.
Definition: value.hpp:536
ByteOrder byteOrder_
Byte order of the comment string that was read.
Definition: value.hpp:613
~CommentValue() override=default
Virtual destructor.
CharsetId
Character set identifiers for the character sets defined by Exif.
Definition: value.hpp:499
Value for an undefined data type.
Definition: value.hpp:253
std::unique_ptr< DataValue > UniquePtr
Shortcut for a DataValue auto pointer.
Definition: value.hpp:256
Value for simple ISO 8601 dates
Definition: value.hpp:965
std::unique_ptr< DateValue > UniquePtr
Shortcut for a DateValue auto pointer.
Definition: value.hpp:968
~DateValue() override=default
Virtual destructor.
Value type for XMP language alternative properties.
Definition: value.hpp:877
std::unique_ptr< LangAltValue > UniquePtr
Shortcut for a LangAltValue auto pointer.
Definition: value.hpp:880
std::map< std::string, std::string, LangAltValueComparator > ValueType
Type used to store language alternative arrays.
Definition: value.hpp:949
ValueType value_
Map to store the language alternative values. The language qualifier is used as the key for the map e...
Definition: value.hpp:955
Abstract base class for a string based Value type.
Definition: value.hpp:333
StringValueBase * clone_() const override=0
Internal virtual copy constructor.
std::unique_ptr< StringValueBase > UniquePtr
Shortcut for a StringValueBase auto pointer.
Definition: value.hpp:336
std::string value_
Stores the string value.
Definition: value.hpp:402
int read(const std::string &buf) override
Read the value from buf. This default implementation uses buf as it is.
Definition: value.cpp:257
StringValueBase(const StringValueBase &rhs)=default
Copy constructor.
~StringValueBase() override=default
Virtual destructor.
Value for string type.
Definition: value.hpp:413
std::unique_ptr< StringValue > UniquePtr
Shortcut for a StringValue auto pointer.
Definition: value.hpp:416
~StringValue() override=default
Virtual destructor.
Value for simple ISO 8601 times.
Definition: value.hpp:1064
~TimeValue() override=default
Virtual destructor.
std::unique_ptr< TimeValue > UniquePtr
Shortcut for a TimeValue auto pointer.
Definition: value.hpp:1067
static long typeSize(TypeId typeId)
Return the size in bytes of one element of this type.
Definition: types.cpp:117
Template for a Value of a basic type. This is used for unsigned and signed short, long and rationals.
Definition: value.hpp:1218
long size() const override
Return the size of the value in bytes.
Definition: value.hpp:1585
ValueType< T > & operator=(const ValueType< T > &rhs)
Assignment operator.
Definition: value.hpp:1521
float toFloat(long n=0) const override
Convert the n-th component of the value to a float. The behaviour of this method may be undefined if ...
Definition: value.hpp:1660
long toLong(long n=0) const override
Convert the n-th component of the value to a long. The behaviour of this method may be undefined if t...
Definition: value.hpp:1617
long count() const override
Return the number of components of the value.
Definition: value.hpp:1579
long sizeDataArea() const override
Return the size of the data area.
Definition: value.hpp:1720
std::vector< T >::iterator iterator
Iterator type defined for convenience.
Definition: value.hpp:1288
int read(const byte *buf, long len, ByteOrder byteOrder) override
Read the value from a character buffer.
Definition: value.hpp:1540
DataBuf dataArea() const override
Return a copy of the data area in a DataBuf. The caller owns this copy and DataBuf ensures that it wi...
Definition: value.hpp:1726
Rational toRational(long n=0) const override
Convert the n-th component of the value to a Rational. The behaviour of this method may be undefined ...
Definition: value.hpp:1683
std::vector< T > ValueList
Container for values.
Definition: value.hpp:1286
ValueType()
Default Constructor.
Definition: value.hpp:1480
~ValueType() override
Virtual destructor.
Definition: value.hpp:1515
std::vector< T >::const_iterator const_iterator
Const iterator type defined for convenience.
Definition: value.hpp:1290
int setDataArea(const byte *buf, long len) override
Set the data area. This method copies (clones) the buffer pointed to by buf.
Definition: value.hpp:1732
long copy(byte *buf, ByteOrder byteOrder) const override
Write value to a data buffer.
Definition: value.hpp:1568
ValueList value_
The container for all values. In your application, if you know what subclass of Value you're dealing ...
Definition: value.hpp:1299
std::ostream & write(std::ostream &os) const override
Write the value to an output stream. You do not usually have to use this function; it is used for the...
Definition: value.hpp:1597
std::unique_ptr< ValueType< T > > UniquePtr
Shortcut for a ValueType<T> auto pointer.
Definition: value.hpp:1221
Common interface for all types of values used with metadata.
Definition: value.hpp:51
TypeId typeId() const
Return the type identifier (Exif data format type).
Definition: value.hpp:104
virtual ~Value()=default
Virtual destructor.
virtual std::ostream & write(std::ostream &os) const =0
Write the value to an output stream. You do not usually have to use this function; it is used for the...
virtual float toFloat(long n=0) const =0
Convert the n-th component of the value to a float. The behaviour of this method may be undefined if ...
virtual int read(const std::string &buf)=0
Set the value from a string buffer. The format of the string corresponds to that of the write() metho...
virtual long size() const =0
Return the size of the value in bytes.
virtual long copy(byte *buf, ByteOrder byteOrder) const =0
Write value to a data buffer.
virtual long toLong(long n=0) const =0
Convert the n-th component of the value to a long. The behaviour of this method may be undefined if t...
bool ok_
Indicates the status of the previous to<Type> conversion.
Definition: value.hpp:236
bool ok() const
Check the ok status indicator. After a to<Type> conversion, this indicator shows whether the conversi...
Definition: value.hpp:188
std::unique_ptr< Value > UniquePtr
Shortcut for a Value auto pointer.
Definition: value.hpp:54
virtual Rational toRational(long n=0) const =0
Convert the n-th component of the value to a Rational. The behaviour of this method may be undefined ...
std::string toString() const
Return the value as a string. Implemented in terms of write(std::ostream& os) const of the concrete c...
Definition: value.cpp:125
virtual int read(const byte *buf, long len, ByteOrder byteOrder)=0
Read the value from a character buffer.
Value & operator=(const Value &rhs)=default
Assignment operator. Protected so that it can only be used by subclasses but not directly.
virtual long count() const =0
Return the number of components of the value.
UniquePtr clone() const
Return an auto-pointer to a copy of itself (deep copy). The caller owns this copy and the auto-pointe...
Definition: value.hpp:110
Value type for simple arrays. Each item in the array is a simple value, without qualifiers....
Definition: value.hpp:784
std::unique_ptr< XmpArrayValue > UniquePtr
Shortcut for a XmpArrayValue auto pointer.
Definition: value.hpp:787
Value type suitable for simple XMP properties and XMP nodes of complex types which are not parsed int...
Definition: value.hpp:702
std::unique_ptr< XmpTextValue > UniquePtr
Shortcut for a XmpTextValue auto pointer.
Definition: value.hpp:705
std::string value_
Stores the string values.
Definition: value.hpp:771
Base class for all Exiv2 values used to store XMP property values.
Definition: value.hpp:620
int read(const std::string &buf) override=0
Set the value from a string buffer. The format of the string corresponds to that of the write() metho...
XmpStruct
XMP structure indicator.
Definition: value.hpp:628
std::unique_ptr< XmpValue > UniquePtr
Shortcut for a XmpValue auto pointer.
Definition: value.hpp:623
int read(const byte *buf, long len, ByteOrder byteOrder=invalidByteOrder) override
Read the value from a character buffer.
Definition: value.cpp:591
XmpArrayType
XMP array types.
Definition: value.hpp:626
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: basicio.hpp:43
EXIV2API std::ostream & operator<<(std::ostream &os, const DataSet &dataSet)
Output operator for dataSet.
Definition: datasets.cpp:677
EXIV2API double getDouble(const byte *buf, ByteOrder byteOrder)
Read an 8 byte double precision floating point value (IEEE 754 binary64) from the data buffer.
Definition: types.cpp:440
TypeId getType< int32_t >()
Specialization for a signed long.
Definition: value.hpp:1202
EXIV2API long f2Data(byte *buf, float f, ByteOrder byteOrder)
Convert a single precision floating point (IEEE 754 binary32) float to data, write the data to the bu...
Definition: types.cpp:565
EXIV2API long d2Data(byte *buf, double d, ByteOrder byteOrder)
Convert a double precision floating point (IEEE 754 binary64) double to data, write the data to the b...
Definition: types.cpp:579
ValueType< URational > URationalValue
Unsigned rational value type.
Definition: value.hpp:1317
EXIV2API float getFloat(const byte *buf, ByteOrder byteOrder)
Read a 4 byte single precision floating point value (IEEE 754 binary32) from the data buffer.
Definition: types.cpp:426
TypeId getType< int16_t >()
Specialization for a signed short.
Definition: value.hpp:1200
EXIV2API Rational floatToRationalCast(float f)
Very simple conversion of a float to a Rational.
Definition: types.cpp:792
EXIV2API long l2Data(byte *buf, int32_t l, ByteOrder byteOrder)
Convert a signed long to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:541
EXIV2API int16_t getShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte signed short value from the data buffer.
Definition: types.cpp:403
ValueType< uint16_t > UShortValue
Unsigned short value type.
Definition: value.hpp:1313
TypeId getType< uint32_t >()
Specialization for an unsigned long.
Definition: value.hpp:1196
EXIV2API uint32_t getULong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte unsigned long value from the data buffer.
Definition: types.cpp:374
EXIV2API long ul2Data(byte *buf, uint32_t l, ByteOrder byteOrder)
Convert an unsigned long to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:487
ValueType< int32_t > LongValue
Signed long value type.
Definition: value.hpp:1321
T getValue(const byte *buf, ByteOrder byteOrder)
Read a value of type T from the data buffer.
Definition: value.hpp:1346
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:100
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:103
@ signedRational
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:110
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:104
@ signedShort
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:108
@ signedLong
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:109
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:120
@ tiffDouble
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:112
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:107
@ xmpBag
XMP bag type.
Definition: types.hpp:124
@ tiffFloat
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:111
@ unsignedRational
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:105
long toData(byte *buf, T t, ByteOrder byteOrder)
Convert a value of type T to data, write the data to the data buffer.
EXIV2API uint16_t getUShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte unsigned short value from the data buffer.
Definition: types.cpp:369
ValueType< int16_t > ShortValue
Signed short value type.
Definition: value.hpp:1319
ValueType< float > FloatValue
Float value type.
Definition: value.hpp:1325
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:83
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:80
TypeId getType< uint16_t >()
Specialization for an unsigned short.
Definition: value.hpp:1194
ValueType< uint32_t > ULongValue
Unsigned long value type.
Definition: value.hpp:1315
ValueType< Rational > RationalValue
Signed rational value type.
Definition: value.hpp:1323
EXIV2API Rational getRational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte signed rational value from the data buffer.
Definition: types.cpp:419
EXIV2API long r2Data(byte *buf, Rational l, ByteOrder byteOrder)
Convert a signed rational to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:558
TypeId getType< URational >()
Specialization for an unsigned rational.
Definition: value.hpp:1198
TypeId getType< float >()
Specialization for a float.
Definition: value.hpp:1206
EXIV2API long ur2Data(byte *buf, URational l, ByteOrder byteOrder)
Convert an unsigned rational to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:521
EXIV2API int32_t getLong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte signed long value from the data buffer.
Definition: types.cpp:411
TypeId getType< Rational >()
Specialization for a signed rational.
Definition: value.hpp:1204
TypeId getType< double >()
Specialization for a double.
Definition: value.hpp:1208
EXIV2API long s2Data(byte *buf, int16_t s, ByteOrder byteOrder)
Convert a signed short to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:528
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:510
std::pair< uint32_t, uint32_t > URational
8 byte unsigned rational type.
Definition: types.hpp:78
TypeId getType()
Template to determine the TypeId for a type T.
EXIV2API URational getURational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte unsigned rational value from the data buffer.
Definition: types.cpp:396
ValueType< double > DoubleValue
Double value type.
Definition: value.hpp:1327
EXIV2API long us2Data(byte *buf, uint16_t s, ByteOrder byteOrder)
Convert an unsigned short to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:474
Information pertaining to the defined character sets.
Definition: value.hpp:502
CharsetId charsetId_
Charset id.
Definition: value.hpp:507
const char * name_
Name of the charset.
Definition: value.hpp:508
const char * code_
Code of the charset.
Definition: value.hpp:509
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:162
Simple Date helper structure.
Definition: value.hpp:981
LangAltValueComparator
Definition: value.hpp:848
bool operator()(const std::string &str1, const std::string &str2) const
LangAltValueComparator comparison case insensitive function.
Definition: value.hpp:850
Simple Time helper structure.
Definition: value.hpp:1083