diff --git a/src/iptc.cpp b/src/iptc.cpp index f64f4285..9bb1c216 100644 --- a/src/iptc.cpp +++ b/src/iptc.cpp @@ -81,6 +81,21 @@ namespace Exiv2 { return *this; } // Iptcdatum::operator= + Iptcdatum& Iptcdatum::operator=(const uint16_t& value) + { + UShortValue::AutoPtr v = UShortValue::AutoPtr(new UShortValue); + v->value_.push_back(value); + value_ = v; + modified_ = true; + return *this; + } + + Iptcdatum& Iptcdatum::operator=(const std::string& value) + { + setValue(value); + return *this; + } + void Iptcdatum::setValue(const Value* pValue) { modified_ = true; @@ -90,9 +105,18 @@ namespace Exiv2 { void Iptcdatum::setValue(const std::string& buf) { + Value::AutoPtr value; + try { + TypeId type = IptcDataSets::dataSetType(tag(), record()); + value = Value::create(type); + } + catch (const Error&) { + // Default to a StringValue if the type is unknown or the parse fails + value = Value::create(string); + } + value->read(buf); + value_ = value; modified_ = true; - if (value_.get() == 0) value_ = Value::create(string); - value_->read(buf); } const byte IptcData::marker_ = 0x1C; // Dataset marker @@ -107,6 +131,17 @@ namespace Exiv2 { delete[] pData_; } + Iptcdatum& IptcData::operator[](const std::string& key) + { + IptcKey iptcKey(key); + iterator pos = findKey(iptcKey); + if (pos == end()) { + add(Iptcdatum(iptcKey)); + pos = findKey(iptcKey); + } + return *pos; + } + int IptcData::read(const std::string& path) { if (!fileExists(path, true)) return -1; diff --git a/src/iptc.hpp b/src/iptc.hpp index ba8b91d4..c9bf527e 100644 --- a/src/iptc.hpp +++ b/src/iptc.hpp @@ -79,15 +79,27 @@ namespace Exiv2 { //@{ //! Assignment operator Iptcdatum& operator=(const Iptcdatum& rhs); + /*! + @brief Assign \em value to the %Iptcdatum. The type of the new value + is determined from %Iptcdatum. If that fails, a StringValue is + used. Calls setValue(const std::string&). + */ + Iptcdatum& operator=(const std::string& value); + /*! + @brief Assign \em value to the %Iptcdatum. The type of the new value + is set to UShortValue. + */ + Iptcdatum& operator=(const uint16_t& value); /*! @brief Set the value. This method copies (clones) the value pointed to by pValue. */ void setValue(const Value* pValue); /*! - @brief Set the value to the string buf. - Uses Value::read(const std::string& buf). If the Iptcdatum does - not have a value yet, then an StringValue is created. + @brief Set the value to the string buf. Uses Value::read(const + std::string& buf). If the %Iptcdatum does not have a value yet, + then a value of the correct type for this %Iptcdatum is + created. If that fails, a StringValue is created. */ void setValue(const std::string& buf); //@} @@ -342,6 +354,15 @@ namespace Exiv2 { @return Data buffer containing the Iptc data. */ DataBuf copy(); + /*! + @brief Returns a reference to the %Iptcdatum that is associated with a + particular \em key. If %IptcData does not already contain such + an %Iptcdatum, operator[] adds object \em Iptcdatum(key). + + @note Since operator[] might insert a new element, it can't be a const + member function. + */ + Iptcdatum& operator[](const std::string& key); /*! @brief Add an %Iptcdatum from the supplied key and value pair. This method copies (clones) the value. A check for non-repeatable