Exiv2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
value.hpp
Go to the documentation of this file.
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2018 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  */
29 #ifndef VALUE_HPP_
30 #define VALUE_HPP_
31 
32 // *****************************************************************************
33 #include "exiv2lib_export.h"
34 
35 // included header files
36 #include "types.hpp"
37 
38 // + standard includes
39 #include <map>
40 #include <iomanip>
41 #include <memory>
42 #include <cstring>
43 #include <climits>
44 
45 // *****************************************************************************
46 // namespace extensions
47 namespace Exiv2 {
48 
49 // *****************************************************************************
50 // class definitions
51 
60  class EXIV2API Value {
61  public:
63  typedef std::auto_ptr<Value> AutoPtr;
64 
66 
67  explicit Value(TypeId typeId);
70  virtual ~Value();
72 
74 
83  virtual int read(const byte* buf, long len, ByteOrder byteOrder) =0;
94  virtual int read(const std::string& buf) =0;
107  virtual int setDataArea(const byte* buf, long len);
109 
111 
112  TypeId typeId() const { return type_; }
119  AutoPtr clone() const { return AutoPtr(clone_()); }
130  virtual long copy(byte* buf, ByteOrder byteOrder) const =0;
132  virtual long count() const =0;
134  virtual long size() const =0;
141  virtual std::ostream& write(std::ostream& os) const =0;
146  std::string toString() const;
153  virtual std::string toString(long n) const;
161  virtual long toLong(long n =0) const =0;
169  virtual float toFloat(long n =0) const =0;
177  virtual Rational toRational(long n =0) const =0;
179  virtual long sizeDataArea() const;
192  virtual DataBuf dataArea() const;
197  bool ok() const { return ok_; }
199 
236  static AutoPtr create(TypeId typeId);
237 
238  protected:
243  Value& operator=(const Value& rhs);
244  // DATA
245  mutable bool ok_;
246 
247  private:
249  virtual Value* clone_() const =0;
250  // DATA
251  TypeId type_;
252 
253  }; // class Value
254 
256  inline std::ostream& operator<<(std::ostream& os, const Value& value)
257  {
258  return value.write(os);
259  }
260 
262  class EXIV2API DataValue : public Value {
263  public:
265  typedef std::auto_ptr<DataValue> AutoPtr;
266 
267  explicit DataValue(TypeId typeId =undefined);
268 
269  DataValue(const byte* buf,
270  long len, ByteOrder byteOrder =invalidByteOrder,
271  TypeId typeId =undefined);
272 
273  virtual ~DataValue();
274 
276 
277 
289  virtual int read(const byte* buf,
290  long len,
291  ByteOrder byteOrder =invalidByteOrder);
293  virtual int read(const std::string& buf);
295 
297 
298  AutoPtr clone() const { return AutoPtr(clone_()); }
312  virtual long copy(byte* buf, ByteOrder byteOrder =invalidByteOrder) const;
313  virtual long count() const;
314  virtual long size() const;
315  virtual std::ostream& write(std::ostream& os) const;
321  virtual std::string toString(long n) const;
322  virtual long toLong(long n =0) const;
323  virtual float toFloat(long n =0) const;
324  virtual Rational toRational(long n =0) const;
326 
327  private:
329  virtual DataValue* clone_() const;
330 
332  typedef std::vector<byte> ValueType;
333  // DATA
334  ValueType value_;
335 
336  }; // class DataValue
337 
344  class EXIV2API StringValueBase : public Value {
345  public:
347  typedef std::auto_ptr<StringValueBase> AutoPtr;
348 
350 
351  explicit StringValueBase(TypeId typeId);
354  StringValueBase(TypeId typeId, const std::string& buf);
356  StringValueBase(const StringValueBase& rhs);
358  virtual ~StringValueBase();
360 
362 
363  virtual int read(const std::string& buf);
377  virtual int read(const byte* buf,
378  long len,
379  ByteOrder byteOrder =invalidByteOrder);
381 
383 
384  AutoPtr clone() const { return AutoPtr(clone_()); }
398  virtual long copy(byte* buf, ByteOrder byteOrder =invalidByteOrder) const;
399  virtual long count() const;
400  virtual long size() const;
401  virtual long toLong(long n =0) const;
402  virtual float toFloat(long n =0) const;
403  virtual Rational toRational(long n =0) const;
404  virtual std::ostream& write(std::ostream& os) const;
406 
407  protected:
409  StringValueBase& operator=(const StringValueBase& rhs);
411  virtual StringValueBase* clone_() const =0;
412 
413  public:
414  // DATA
416 
417  }; // class StringValueBase
418 
426  class EXIV2API StringValue : public StringValueBase {
427  public:
429  typedef std::auto_ptr<StringValue> AutoPtr;
430 
432 
433  StringValue();
436  explicit StringValue(const std::string& buf);
438  virtual ~StringValue();
440 
442 
443  AutoPtr clone() const { return AutoPtr(clone_()); }
445 
446  private:
448  virtual StringValue* clone_() const;
449 
450  }; // class StringValue
451 
458  class EXIV2API AsciiValue : public StringValueBase {
459  public:
461  typedef std::auto_ptr<AsciiValue> AutoPtr;
462 
464 
465  AsciiValue();
468  explicit AsciiValue(const std::string& buf);
470  virtual ~AsciiValue();
472 
474 
475  using StringValueBase::read;
481  virtual int read(const std::string& buf);
483 
485 
486  AutoPtr clone() const { return AutoPtr(clone_()); }
492  virtual std::ostream& write(std::ostream& os) const;
494 
495  private:
497  virtual AsciiValue* clone_() const;
498 
499  }; // class AsciiValue
500 
509  class EXIV2API CommentValue : public StringValueBase {
510  public:
512  enum CharsetId { ascii, jis, unicode, undefined,
513  invalidCharsetId, lastCharsetId };
515  struct CharsetTable {
517  CharsetTable(CharsetId charsetId,
518  const char* name,
519  const char* code);
521  const char* name_;
522  const char* code_;
523  }; // struct CharsetTable
524 
526  class EXIV2API CharsetInfo {
528  CharsetInfo() {}
530  CharsetInfo(const CharsetInfo&);
532  CharsetInfo& operator=(const CharsetInfo&);
533 
534  public:
536  static const char* name(CharsetId charsetId);
538  static const char* code(CharsetId charsetId);
540  static CharsetId charsetIdByName(const std::string& name);
542  static CharsetId charsetIdByCode(const std::string& code);
543 
544  private:
545  static const CharsetTable charsetTable_[];
546  }; // class CharsetInfo
547 
549  typedef std::auto_ptr<CommentValue> AutoPtr;
550 
552 
553  CommentValue();
556  explicit CommentValue(const std::string& comment);
558  virtual ~CommentValue();
560 
562 
563 
575  int read(const std::string& comment);
579  int read(const byte* buf, long len, ByteOrder byteOrder);
581 
583 
584  AutoPtr clone() const { return AutoPtr(clone_()); }
585  long copy(byte* buf, ByteOrder byteOrder) const;
590  std::ostream& write(std::ostream& os) const;
605  std::string comment(const char* encoding =0) const;
615  const char* detectCharset(std::string& c) const;
617  CharsetId charsetId() const;
619 
620  private:
622  virtual CommentValue* clone_() const;
623 
624  public:
625  // DATA
627 
628  }; // class CommentValue
629 
633  class EXIV2API XmpValue : public Value {
634  public:
636  typedef std::auto_ptr<XmpValue> AutoPtr;
637 
639  enum XmpArrayType { xaNone, xaAlt, xaBag, xaSeq };
641  enum XmpStruct { xsNone, xsStruct };
642 
644 
645  explicit XmpValue(TypeId typeId);
647 
649 
650  XmpArrayType xmpArrayType() const;
653  XmpStruct xmpStruct() const;
654  virtual long size() const;
668  virtual long copy(byte* buf, ByteOrder byteOrder =invalidByteOrder) const;
670 
672 
673  void setXmpArrayType(XmpArrayType xmpArrayType);
676  void setXmpStruct(XmpStruct xmpStruct =xsStruct);
691  virtual int read(const byte* buf,
692  long len,
693  ByteOrder byteOrder =invalidByteOrder);
694  virtual int read(const std::string& buf) =0;
696 
701  static XmpArrayType xmpArrayType(TypeId typeId);
702 
703  protected:
708  XmpValue& operator=(const XmpValue& rhs);
709 
710  private:
711  // DATA
712  XmpArrayType xmpArrayType_;
713  XmpStruct xmpStruct_;
714 
715  }; // class XmpValue
716 
724  class EXIV2API XmpTextValue : public XmpValue {
725  public:
727  typedef std::auto_ptr<XmpTextValue> AutoPtr;
728 
730 
731  XmpTextValue();
734  explicit XmpTextValue(const std::string& buf);
736 
738 
739  using XmpValue::read;
755  virtual int read(const std::string& buf);
757 
759 
760  AutoPtr clone() const;
761  long size() const;
762  virtual long count() const;
769  virtual long toLong(long n =0) const;
776  virtual float toFloat(long n =0) const;
783  virtual Rational toRational(long n =0) const;
784  virtual std::ostream& write(std::ostream& os) const;
786 
787  private:
789  virtual XmpTextValue* clone_() const;
790 
791  public:
792  // DATA
794 
795  }; // class XmpTextValue
796 
806  class EXIV2API XmpArrayValue : public XmpValue {
807  public:
809  typedef std::auto_ptr<XmpArrayValue> AutoPtr;
810 
812 
813  explicit XmpArrayValue(TypeId typeId =xmpBag);
816 
818 
819  using XmpValue::read;
830  virtual int read(const std::string& buf);
832 
834 
835  AutoPtr clone() const;
836  virtual long count() const;
842  virtual std::string toString(long n) const;
843  virtual long toLong(long n =0) const;
844  virtual float toFloat(long n =0) const;
845  virtual Rational toRational(long n =0) const;
852  virtual std::ostream& write(std::ostream& os) const;
854 
855  private:
857  virtual XmpArrayValue* clone_() const;
858 
859  std::vector<std::string> value_;
860 
861  }; // class XmpArrayValue
862 
872  bool operator() (const std::string& str1, const std::string& str2) const
873  {
874  int result = str1.size() < str2.size() ? 1
875  : str1.size() > str2.size() ? -1
876  : 0
877  ;
878  std::string::const_iterator c1 = str1.begin();
879  std::string::const_iterator c2 = str2.begin();
880  if ( result==0 ) for (
881  ; result==0 && c1 != str1.end()
882  ; ++c1, ++c2
883  ) {
884  result = tolower(*c1) < tolower(*c2) ? 1
885  : tolower(*c1) > tolower(*c2) ? -1
886  : 0
887  ;
888  }
889  return result < 0 ;
890  }
891  };
892 
899  class EXIV2API LangAltValue : public XmpValue {
900  public:
902  typedef std::auto_ptr<LangAltValue> AutoPtr;
903 
905 
906  LangAltValue();
909  explicit LangAltValue(const std::string& buf);
911 
913 
914  using XmpValue::read;
932  virtual int read(const std::string& buf);
934 
936 
937  AutoPtr clone() const;
938  virtual long count() const;
946  virtual std::string toString(long n) const;
952  std::string toString(const std::string& qualifier) const;
953  virtual long toLong(long n =0) const;
954  virtual float toFloat(long n =0) const;
955  virtual Rational toRational(long n =0) const;
962  virtual std::ostream& write(std::ostream& os) const;
964 
965  private:
967  virtual LangAltValue* clone_() const;
968 
969  public:
971  typedef std::map<std::string, std::string,LangAltValueComparator> ValueType;
972  // DATA
978 
979  }; // class LangAltValue
980 
987  class EXIV2API DateValue : public Value {
988  public:
990  typedef std::auto_ptr<DateValue> AutoPtr;
991 
993 
994  DateValue();
997  DateValue(int year, int month, int day);
999  virtual ~DateValue();
1001 
1003  struct EXIV2API Date {
1004  Date() : year(0), month(0), day(0) {}
1005  int year;
1006  int month;
1007  int day;
1008  };
1009 
1011 
1012 
1025  virtual int read(const byte* buf,
1026  long len,
1027  ByteOrder byteOrder =invalidByteOrder);
1036  virtual int read(const std::string& buf);
1038  void setDate(const Date& src);
1040 
1042 
1043  AutoPtr clone() const { return AutoPtr(clone_()); }
1057  virtual long copy(byte* buf, ByteOrder byteOrder =invalidByteOrder) const;
1059  virtual const Date& getDate() const;
1060  virtual long count() const;
1061  virtual long size() const;
1062  virtual std::ostream& write(std::ostream& os) const;
1064  virtual long toLong(long n =0) const;
1066  virtual float toFloat(long n =0) const;
1068  virtual Rational toRational(long n =0) const;
1070 
1071  private:
1073  virtual DateValue* clone_() const;
1074 
1075  // DATA
1076  Date date_;
1077 
1078  }; // class DateValue
1079 
1088  class EXIV2API TimeValue : public Value {
1089  public:
1091  typedef std::auto_ptr<TimeValue> AutoPtr;
1092 
1094 
1095  TimeValue();
1098  TimeValue(int hour, int minute, int second =0,
1099  int tzHour =0, int tzMinute =0);
1100 
1102  virtual ~TimeValue();
1104 
1106  struct Time
1107  {
1108  Time() : hour(0), minute(0), second(0), tzHour(0), tzMinute(0) {}
1109 
1110  int hour;
1111  int minute;
1112  int second;
1113  int tzHour;
1114  int tzMinute;
1115  };
1116 
1118 
1119 
1132  virtual int read(const byte* buf,
1133  long len,
1134  ByteOrder byteOrder =invalidByteOrder);
1143  virtual int read(const std::string& buf);
1145  void setTime(const Time& src);
1147 
1149 
1150  AutoPtr clone() const { return AutoPtr(clone_()); }
1164  virtual long copy(byte* buf, ByteOrder byteOrder =invalidByteOrder) const;
1166  virtual const Time& getTime() const;
1167  virtual long count() const;
1168  virtual long size() const;
1169  virtual std::ostream& write(std::ostream& os) const;
1171  virtual long toLong(long n =0) const;
1173  virtual float toFloat(long n =0) const;
1175  virtual Rational toRational(long n =0) const;
1177 
1178  private:
1180 
1181 
1191  int scanTime3(const char* buf, const char* format);
1202  int scanTime6(const char* buf, const char* format);
1204 
1206 
1207  virtual TimeValue* clone_() const;
1210 
1211  // DATA
1212  Time time_;
1213 
1214  }; // class TimeValue
1215 
1217  template<typename T> TypeId getType();
1218 
1220  template<> inline TypeId getType<uint16_t>() { return unsignedShort; }
1222  template<> inline TypeId getType<uint32_t>() { return unsignedLong; }
1224  template<> inline TypeId getType<URational>() { return unsignedRational; }
1226  template<> inline TypeId getType<int16_t>() { return signedShort; }
1228  template<> inline TypeId getType<int32_t>() { return signedLong; }
1230  template<> inline TypeId getType<Rational>() { return signedRational; }
1232  template<> inline TypeId getType<float>() { return tiffFloat; }
1234  template<> inline TypeId getType<double>() { return tiffDouble; }
1235 
1236  // No default implementation: let the compiler/linker complain
1237  // template<typename T> inline TypeId getType() { return invalid; }
1238 
1243  template<typename T>
1244  class ValueType : public Value {
1245  public:
1247  typedef std::auto_ptr<ValueType<T> > AutoPtr;
1248 
1250 
1251  ValueType();
1254  // The default c'tor and this one can be combined, but that causes MSVC 7.1 to fall on its nose
1255  explicit ValueType(TypeId typeId);
1257  ValueType(const byte* buf, long len, ByteOrder byteOrder, TypeId typeId =getType<T>());
1259  explicit ValueType(const T& val, TypeId typeId =getType<T>());
1261  ValueType(const ValueType<T>& rhs);
1263  virtual ~ValueType();
1265 
1267 
1268  ValueType<T>& operator=(const ValueType<T>& rhs);
1270  virtual int read(const byte* buf, long len, ByteOrder byteOrder);
1277  virtual int read(const std::string& buf);
1282  virtual int setDataArea(const byte* buf, long len);
1284 
1286 
1287  AutoPtr clone() const { return AutoPtr(clone_()); }
1288  virtual long copy(byte* buf, ByteOrder byteOrder) const;
1289  virtual long count() const;
1290  virtual long size() const;
1291  virtual std::ostream& write(std::ostream& os) const;
1298  virtual std::string toString(long n) const;
1299  virtual long toLong(long n =0) const;
1300  virtual float toFloat(long n =0) const;
1301  virtual Rational toRational(long n =0) const;
1303  virtual long sizeDataArea() const;
1308  virtual DataBuf dataArea() const;
1310 
1312  typedef std::vector<T> ValueList;
1314  typedef typename std::vector<T>::iterator iterator;
1316  typedef typename std::vector<T>::const_iterator const_iterator;
1317 
1318  // DATA
1326 
1327  private:
1329  virtual ValueType<T>* clone_() const;
1330 
1331  // DATA
1333  byte* pDataArea_;
1335  long sizeDataArea_;
1336  }; // class ValueType
1337 
1354 
1355 // *****************************************************************************
1356 // free functions, template and inline definitions
1357 
1369  template<typename T> T getValue(const byte* buf, ByteOrder byteOrder);
1370  // Specialization for a 2 byte unsigned short value.
1371  template<>
1372  inline uint16_t getValue(const byte* buf, ByteOrder byteOrder)
1373  {
1374  return getUShort(buf, byteOrder);
1375  }
1376  // Specialization for a 4 byte unsigned long value.
1377  template<>
1378  inline uint32_t getValue(const byte* buf, ByteOrder byteOrder)
1379  {
1380  return getULong(buf, byteOrder);
1381  }
1382  // Specialization for an 8 byte unsigned rational value.
1383  template<>
1384  inline URational getValue(const byte* buf, ByteOrder byteOrder)
1385  {
1386  return getURational(buf, byteOrder);
1387  }
1388  // Specialization for a 2 byte signed short value.
1389  template<>
1390  inline int16_t getValue(const byte* buf, ByteOrder byteOrder)
1391  {
1392  return getShort(buf, byteOrder);
1393  }
1394  // Specialization for a 4 byte signed long value.
1395  template<>
1396  inline int32_t getValue(const byte* buf, ByteOrder byteOrder)
1397  {
1398  return getLong(buf, byteOrder);
1399  }
1400  // Specialization for an 8 byte signed rational value.
1401  template<>
1402  inline Rational getValue(const byte* buf, ByteOrder byteOrder)
1403  {
1404  return getRational(buf, byteOrder);
1405  }
1406  // Specialization for a 4 byte float value.
1407  template<>
1408  inline float getValue(const byte* buf, ByteOrder byteOrder)
1409  {
1410  return getFloat(buf, byteOrder);
1411  }
1412  // Specialization for a 8 byte double value.
1413  template<>
1414  inline double getValue(const byte* buf, ByteOrder byteOrder)
1415  {
1416  return getDouble(buf, byteOrder);
1417  }
1418 
1431  template<typename T> long toData(byte* buf, T t, ByteOrder byteOrder);
1436  template<>
1437  inline long toData(byte* buf, uint16_t t, ByteOrder byteOrder)
1438  {
1439  return us2Data(buf, t, byteOrder);
1440  }
1445  template<>
1446  inline long toData(byte* buf, uint32_t t, ByteOrder byteOrder)
1447  {
1448  return ul2Data(buf, t, byteOrder);
1449  }
1454  template<>
1455  inline long toData(byte* buf, URational t, ByteOrder byteOrder)
1456  {
1457  return ur2Data(buf, t, byteOrder);
1458  }
1463  template<>
1464  inline long toData(byte* buf, int16_t t, ByteOrder byteOrder)
1465  {
1466  return s2Data(buf, t, byteOrder);
1467  }
1472  template<>
1473  inline long toData(byte* buf, int32_t t, ByteOrder byteOrder)
1474  {
1475  return l2Data(buf, t, byteOrder);
1476  }
1481  template<>
1482  inline long toData(byte* buf, Rational t, ByteOrder byteOrder)
1483  {
1484  return r2Data(buf, t, byteOrder);
1485  }
1490  template<>
1491  inline long toData(byte* buf, float t, ByteOrder byteOrder)
1492  {
1493  return f2Data(buf, t, byteOrder);
1494  }
1499  template<>
1500  inline long toData(byte* buf, double t, ByteOrder byteOrder)
1501  {
1502  return d2Data(buf, t, byteOrder);
1503  }
1504 
1505  template<typename T>
1507  : Value(getType<T>()), pDataArea_(0), sizeDataArea_(0)
1508  {
1509  }
1510 
1511  template<typename T>
1513  : Value(typeId), pDataArea_(0), sizeDataArea_(0)
1514  {
1515  }
1516 
1517  template<typename T>
1518  ValueType<T>::ValueType(const byte* buf, long len, ByteOrder byteOrder, TypeId typeId)
1519  : Value(typeId), pDataArea_(0), sizeDataArea_(0)
1520  {
1521  read(buf, len, byteOrder);
1522  }
1523 
1524  template<typename T>
1525  ValueType<T>::ValueType(const T& val, TypeId typeId)
1526  : Value(typeId), pDataArea_(0), sizeDataArea_(0)
1527  {
1528  value_.push_back(val);
1529  }
1530 
1531  template<typename T>
1533  : Value(rhs), value_(rhs.value_), pDataArea_(0), sizeDataArea_(0)
1534  {
1535  if (rhs.sizeDataArea_ > 0) {
1536  pDataArea_ = new byte[rhs.sizeDataArea_];
1537  std::memcpy(pDataArea_, rhs.pDataArea_, rhs.sizeDataArea_);
1538  sizeDataArea_ = rhs.sizeDataArea_;
1539  }
1540  }
1541 
1542  template<typename T>
1544  {
1545  delete[] pDataArea_;
1546  }
1547 
1548  template<typename T>
1550  {
1551  if (this == &rhs) return *this;
1552  Value::operator=(rhs);
1553  value_ = rhs.value_;
1554 
1555  byte* tmp = 0;
1556  if (rhs.sizeDataArea_ > 0) {
1557  tmp = new byte[rhs.sizeDataArea_];
1558  std::memcpy(tmp, rhs.pDataArea_, rhs.sizeDataArea_);
1559  }
1560  delete[] pDataArea_;
1561  pDataArea_ = tmp;
1562  sizeDataArea_ = rhs.sizeDataArea_;
1563 
1564  return *this;
1565  }
1566 
1567  template<typename T>
1568  int ValueType<T>::read(const byte* buf, long len, ByteOrder byteOrder)
1569  {
1570  value_.clear();
1571  long ts = TypeInfo::typeSize(typeId());
1572  if (ts != 0)
1573  if (len % ts != 0) len = (len / ts) * ts;
1574  for (long i = 0; i < len; i += ts) {
1575  value_.push_back(getValue<T>(buf + i, byteOrder));
1576  }
1577  return 0;
1578  }
1579 
1580  template<typename T>
1582  {
1583  std::istringstream is(buf);
1584  T tmp;
1585  ValueList val;
1586  while (!(is.eof())) {
1587  is >> tmp;
1588  if (is.fail()) return 1;
1589  val.push_back(tmp);
1590  }
1591  value_.swap(val);
1592  return 0;
1593  }
1594 
1595  template<typename T>
1596  long ValueType<T>::copy(byte* buf, ByteOrder byteOrder) const
1597  {
1598  long offset = 0;
1599  typename ValueList::const_iterator end = value_.end();
1600  for (typename ValueList::const_iterator i = value_.begin(); i != end; ++i) {
1601  offset += toData(buf + offset, *i, byteOrder);
1602  }
1603  return offset;
1604  }
1605 
1606  template<typename T>
1607  long ValueType<T>::count() const
1608  {
1609  return static_cast<long>(value_.size());
1610  }
1611 
1612  template<typename T>
1613  long ValueType<T>::size() const
1614  {
1615  return static_cast<long>(TypeInfo::typeSize(typeId()) * value_.size());
1616  }
1617 
1618  template<typename T>
1620  {
1621  return new ValueType<T>(*this);
1622  }
1623 
1624  template<typename T>
1625  std::ostream& ValueType<T>::write(std::ostream& os) const
1626  {
1627  typename ValueList::const_iterator end = value_.end();
1628  typename ValueList::const_iterator i = value_.begin();
1629  while (i != end) {
1630  os << std::setprecision(15) << *i;
1631  if (++i != end) os << " ";
1632  }
1633  return os;
1634  }
1635 
1636  template<typename T>
1638  {
1639  ok_ = true;
1640  return Exiv2::toString<T>(value_[n]);
1641  }
1642 
1643  // Default implementation
1644  template<typename T>
1645  long ValueType<T>::toLong(long n) const
1646  {
1647  ok_ = true;
1648  return static_cast<long>(value_[n]);
1649  }
1650 // #55 crash when value_[n].first == LONG_MIN
1651 #define LARGE_INT 1000000
1652  // Specialization for rational
1653  template<>
1654  inline long ValueType<Rational>::toLong(long n) const
1655  {
1656  ok_ = (value_[n].second != 0 && INT_MIN < value_[n].first && value_[n].first < INT_MAX );
1657  if (!ok_) return 0;
1658  return value_[n].first / value_[n].second;
1659  }
1660  // Specialization for unsigned rational
1661  template<>
1662  inline long ValueType<URational>::toLong(long n) const
1663  {
1664  ok_ = (value_[n].second != 0 && value_[n].first < LARGE_INT);
1665  if (!ok_) return 0;
1666  return value_[n].first / value_[n].second;
1667  }
1668  // Default implementation
1669  template<typename T>
1670  float ValueType<T>::toFloat(long n) const
1671  {
1672  ok_ = true;
1673  return static_cast<float>(value_[n]);
1674  }
1675  // Specialization for rational
1676  template<>
1677  inline float ValueType<Rational>::toFloat(long n) const
1678  {
1679  ok_ = (value_[n].second != 0);
1680  if (!ok_) return 0.0f;
1681  return static_cast<float>(value_[n].first) / value_[n].second;
1682  }
1683  // Specialization for unsigned rational
1684  template<>
1685  inline float ValueType<URational>::toFloat(long n) const
1686  {
1687  ok_ = (value_[n].second != 0);
1688  if (!ok_) return 0.0f;
1689  return static_cast<float>(value_[n].first) / value_[n].second;
1690  }
1691  // Default implementation
1692  template<typename T>
1694  {
1695  ok_ = true;
1696  return Rational(value_[n], 1);
1697  }
1698  // Specialization for rational
1699  template<>
1701  {
1702  ok_ = true;
1703  return Rational(value_[n].first, value_[n].second);
1704  }
1705  // Specialization for unsigned rational
1706  template<>
1708  {
1709  ok_ = true;
1710  return Rational(value_[n].first, value_[n].second);
1711  }
1712  // Specialization for float.
1713  template<>
1715  {
1716  ok_ = true;
1717  // Warning: This is a very simple conversion, see floatToRationalCast()
1718  return floatToRationalCast(value_[n]);
1719  }
1720  // Specialization for double.
1721  template<>
1723  {
1724  ok_ = true;
1725  // Warning: This is a very simple conversion, see floatToRationalCast()
1726  return floatToRationalCast(static_cast<float>(value_[n]));
1727  }
1728 
1729  template<typename T>
1731  {
1732  return sizeDataArea_;
1733  }
1734 
1735  template<typename T>
1737  {
1738  return DataBuf(pDataArea_, sizeDataArea_);
1739  }
1740 
1741  template<typename T>
1742  int ValueType<T>::setDataArea(const byte* buf, long len)
1743  {
1744  byte* tmp = 0;
1745  if (len > 0) {
1746  tmp = new byte[len];
1747  std::memcpy(tmp, buf, len);
1748  }
1749  delete[] pDataArea_;
1750  pDataArea_ = tmp;
1751  sizeDataArea_ = len;
1752  return 0;
1753  }
1754 } // namespace Exiv2
1755 
1756 #endif // #ifndef VALUE_HPP_
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:133
virtual float toFloat(long n=0) const
Convert the n-th component of the value to a float. The behaviour of this method may be undefined if ...
Definition: value.hpp:1670
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:139
CharsetId charsetId_
Charset id.
Definition: value.hpp:520
TypeId typeId() const
Return the type identifier (Exif data format type).
Definition: value.hpp:113
EXIV2API Rational floatToRationalCast(float f)
Very simple conversion of a float to a Rational.
Definition: types.cpp:694
std::auto_ptr< CommentValue > AutoPtr
Shortcut for a CommentValue auto pointer.
Definition: value.hpp:549
Information pertaining to the defined character sets.
Definition: value.hpp:515
std::auto_ptr< DateValue > AutoPtr
Shortcut for a DateValue auto pointer.
Definition: value.hpp:990
ValueType value_
Map to store the language alternative values. The language qualifier is used as the key for the map e...
Definition: value.hpp:977
TypeId getType< uint16_t >()
Specialization for an unsigned short.
Definition: value.hpp:1220
XmpArrayType
XMP array types.
Definition: value.hpp:639
std::auto_ptr< DataValue > AutoPtr
Shortcut for a DataValue auto pointer.
Definition: value.hpp:265
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:140
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:113
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:204
std::vector< T > ValueList
Container for values.
Definition: value.hpp:1312
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:110
Template for a Value of a basic type. This is used for unsigned and signed short, long and rationals...
Definition: value.hpp:1244
std::auto_ptr< XmpArrayValue > AutoPtr
Shortcut for a XmpArrayValue auto pointer.
Definition: value.hpp:809
Value type suitable for simple XMP properties and XMP nodes of complex types which are not parsed int...
Definition: value.hpp:724
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:445
T getValue(const byte *buf, ByteOrder byteOrder)
Read a value of type T from the data buffer.
Definition: value.hpp:1372
Simple Date helper structure.
Definition: value.hpp:1003
bool operator()(const std::string &str1, const std::string &str2) const
LangAltValueComparator comparison case insensitive function.
Definition: value.hpp:872
std::auto_ptr< TimeValue > AutoPtr
Shortcut for a TimeValue auto pointer.
Definition: value.hpp:1091
int second
Second.
Definition: value.hpp:1112
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:130
virtual long toLong(long n=0) const
Convert the n-th component of the value to a long. The behaviour of this method may be undefined if t...
Definition: value.hpp:1645
ByteOrder byteOrder_
Byte order of the comment string that was read.
Definition: value.hpp:626
int tzHour
Hours ahead or behind UTC.
Definition: value.hpp:1113
long toData(byte *buf, T t, ByteOrder byteOrder)
Convert a value of type T to data, write the data to the data buffer.
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:361
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:483
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:462
std::vector< T >::const_iterator const_iterator
Const iterator type defined for convenience.
Definition: value.hpp:1316
EXIV2API int16_t getShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte signed short value from the data buffer.
Definition: types.cpp:318
Value for simple ISO 8601 times.
Definition: value.hpp:1088
ValueType< int32_t > LongValue
Signed long value type.
Definition: value.hpp:1347
virtual long count() const
Return the number of components of the value.
Definition: value.hpp:1607
virtual int read(const byte *buf, long len, ByteOrder byteOrder=invalidByteOrder)
Read the value from a character buffer.
Definition: value.cpp:665
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:105
Value for an Ascii string type.
Definition: value.hpp:458
Value type for simple arrays. Each item in the array is a simple value, without qualifiers. The array may be an ordered (seq), unordered (bag) or alternative array (alt). The array items must not contain qualifiers. For language alternatives use LangAltValue.
Definition: value.hpp:806
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:521
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:137
bool ok() const
Check the ok status indicator. After a to<Type> conversion, this indicator shows whether the conversi...
Definition: value.hpp:197
virtual int read(const byte *buf, long len, ByteOrder byteOrder)
Read the value from a character buffer.
Definition: value.hpp:1568
int minute
Minute.
Definition: value.hpp:1111
std::auto_ptr< XmpValue > AutoPtr
Shortcut for a XmpValue auto pointer.
Definition: value.hpp:636
Type definitions for Exiv2 and related functionality.
Value type for XMP language alternative properties.
Definition: value.hpp:899
ValueList value_
The container for all values. In your application, if you know what subclass of Value you&#39;re dealing ...
Definition: value.hpp:1325
virtual long sizeDataArea() const
Return the size of the data area.
Definition: value.hpp:1730
Charset information lookup functions. Implemented as a static class.
Definition: value.hpp:526
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:432
ValueType< uint32_t > ULongValue
Unsigned long value type.
Definition: value.hpp:1341
XMP bag type.
Definition: types.hpp:154
virtual long copy(byte *buf, ByteOrder byteOrder) const
Write value to a data buffer.
Definition: value.hpp:1596
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:134
std::vector< T >::iterator iterator
Iterator type defined for convenience.
Definition: value.hpp:1314
EXIV2API URational getURational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte unsigned rational value from the data buffer.
Definition: types.cpp:311
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:469
virtual ~ValueType()
Virtual destructor.
Definition: value.hpp:1543
std::map< std::string, std::string, LangAltValueComparator > ValueType
Type used to store language alternative arrays.
Definition: value.hpp:971
std::string value_
Stores the string value.
Definition: value.hpp:415
TypeId getType< int16_t >()
Specialization for a signed short.
Definition: value.hpp:1226
LangAltValueComparator
Definition: value.hpp:870
virtual int setDataArea(const byte *buf, long len)
Set the data area. This method copies (clones) the buffer pointed to by buf.
Definition: value.hpp:1742
TypeId getType()
Template to determine the TypeId for a type T.
virtual int read(const std::string &buf)
Read the value from buf. This default implementation uses buf as it is.
Definition: value.cpp:321
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:135
virtual DataBuf dataArea() const
Return a copy of the data area in a DataBuf. The caller owns this copy and DataBuf ensures that it wi...
Definition: value.hpp:1736
TypeId getType< uint32_t >()
Specialization for an unsigned long.
Definition: value.hpp:1222
Value for an undefined data type.
Definition: value.hpp:262
std::auto_ptr< StringValueBase > AutoPtr
Shortcut for a StringValueBase auto pointer.
Definition: value.hpp:347
AutoPtr 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:119
ValueType()
Default Constructor.
Definition: value.hpp:1506
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:142
virtual Rational toRational(long n=0) const
Convert the n-th component of the value to a Rational. The behaviour of this method may be undefined ...
Definition: value.hpp:1693
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:408
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:138
ValueType< uint16_t > UShortValue
Unsigned short value type.
Definition: value.hpp:1339
ValueType< int16_t > ShortValue
Signed short value type.
Definition: value.hpp:1345
XmpStruct
XMP structure indicator.
Definition: value.hpp:641
Value for an Exif comment.
Definition: value.hpp:509
std::string value_
Stores the string values.
Definition: value.hpp:793
ValueType< double > DoubleValue
Double value type.
Definition: value.hpp:1353
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:141
std::pair< uint32_t, uint32_t > URational
8 byte unsigned rational type.
Definition: types.hpp:108
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:395
Base class for all Exiv2 values used to store XMP property values.
Definition: value.hpp:633
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:425
EXIV2API int32_t getLong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte signed long value from the data buffer.
Definition: types.cpp:328
EXIV2API uint32_t getULong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte unsigned long value from the data buffer.
Definition: types.cpp:283
IPTC string type.
Definition: types.hpp:147
Common interface for all types of values used with metadata.
Definition: value.hpp:60
std::auto_ptr< Value > AutoPtr
Shortcut for a Value auto pointer.
Definition: value.hpp:63
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...
std::auto_ptr< ValueType< T > > AutoPtr
Shortcut for a ValueType<T> auto pointer.
Definition: value.hpp:1247
std::auto_ptr< LangAltValue > AutoPtr
Shortcut for a LangAltValue auto pointer.
Definition: value.hpp:902
const char * name_
Name of the charset.
Definition: value.hpp:521
ValueType< float > FloatValue
Float value type.
Definition: value.hpp:1351
virtual long size() const
Return the size of the value in bytes.
Definition: value.hpp:1613
ValueType< T > & operator=(const ValueType< T > &rhs)
Assignment operator.
Definition: value.hpp:1549
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
CharsetId
Character set identifiers for the character sets defined by Exif.
Definition: value.hpp:512
int day
Day.
Definition: value.hpp:1007
std::auto_ptr< StringValue > AutoPtr
Shortcut for a StringValue auto pointer.
Definition: value.hpp:429
EXIV2API std::ostream & operator<<(std::ostream &os, const DataSet &dataSet)
Output operator for dataSet.
Definition: datasets.cpp:709
int month
Month.
Definition: value.hpp:1006
bool ok_
Indicates the status of the previous to<Type> conversion.
Definition: value.hpp:245
const char * code_
Code of the charset.
Definition: value.hpp:522
Exiv2 type for the Exif user comment.
Definition: types.hpp:150
int year
Year.
Definition: value.hpp:1005
int tzMinute
Minutes ahead or behind UTC.
Definition: value.hpp:1114
TypeId getType< double >()
Specialization for a double.
Definition: value.hpp:1234
int hour
Hour.
Definition: value.hpp:1110
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:175
Abstract base class for a string based Value type.
Definition: value.hpp:344
Value for simple ISO 8601 dates
Definition: value.hpp:987
EXIV2API Rational getRational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte signed rational value from the data buffer.
Definition: types.cpp:340
static long typeSize(TypeId typeId)
Return the size in bytes of one element of this type.
Definition: types.cpp:121
Value for string type.
Definition: value.hpp:426
TypeId getType< Rational >()
Specialization for a signed rational.
Definition: value.hpp:1230
virtual std::ostream & write(std::ostream &os) const
Write the value to an output stream. You do not usually have to use this function; it is used for the...
Definition: value.hpp:1625
ValueType< URational > URationalValue
Unsigned rational value type.
Definition: value.hpp:1343
std::auto_ptr< XmpTextValue > AutoPtr
Shortcut for a XmpTextValue auto pointer.
Definition: value.hpp:727
std::auto_ptr< AsciiValue > AutoPtr
Shortcut for a AsciiValue auto pointer.
Definition: value.hpp:461
ValueType< Rational > RationalValue
Signed rational value type.
Definition: value.hpp:1349
Value & operator=(const Value &rhs)
Assignment operator. Protected so that it can only be used by subclasses but not directly.
Definition: value.cpp:92
Simple Time helper structure.
Definition: value.hpp:1106
TypeId getType< float >()
Specialization for a float.
Definition: value.hpp:1232
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:347
TypeId getType< int32_t >()
Specialization for a signed long.
Definition: value.hpp:1228
EXIV2API uint16_t getUShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte unsigned short value from the data buffer.
Definition: types.cpp:278
TypeId getType< URational >()
Specialization for an unsigned rational.
Definition: value.hpp:1224