aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2007-03-12 23:57:55 +0100
committerOlivier Tilloy <olivier@tilloy.net>2007-03-12 23:57:55 +0100
commit1b48deeb7952c27e8dba6c5b3c702efc5e582a65 (patch)
tree2bb95fe2595c9180cde8b7caa8c88d07b8b93393 /src
parentda26eb1a24eadc3ed9bf6f1850197ac5240486ec (diff)
downloadpyexiv2-1b48deeb7952c27e8dba6c5b3c702efc5e582a65.tar.gz
Modified methods Image::getIptcTag(...) and Image::setIptcTag(...) to take into account the possibility for an IPTC tag to be repeatable.
Diffstat (limited to 'src')
-rw-r--r--src/libpyexiv2.cpp59
-rw-r--r--src/libpyexiv2.hpp15
2 files changed, 51 insertions, 23 deletions
diff --git a/src/libpyexiv2.cpp b/src/libpyexiv2.cpp
index fdae167..9ce4a9d 100644
--- a/src/libpyexiv2.cpp
+++ b/src/libpyexiv2.cpp
@@ -28,9 +28,10 @@
// Custom error codes for Exiv2 exceptions
#define METADATA_NOT_READ 101
-#define KEY_NOT_FOUND 102
-#define THUMB_ACCESS 103
-#define NO_THUMBNAIL 104
+#define NON_REPEATABLE 102
+#define KEY_NOT_FOUND 103
+#define THUMB_ACCESS 104
+#define NO_THUMBNAIL 105
namespace LibPyExiv2
{
@@ -217,17 +218,24 @@ namespace LibPyExiv2
throw Exiv2::Error(METADATA_NOT_READ);
}
- boost::python::tuple Image::getIptcTag(std::string key)
+ boost::python::list Image::getIptcTag(std::string key)
{
if(_dataRead)
{
+ boost::python::list valuesList;
+ unsigned int valueOccurences = 0;
Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
- Exiv2::IptcMetadata::iterator i = _iptcData.findKey(iptcKey);
- if(i != _iptcData.end())
+ for (Exiv2::IptcMetadata::iterator dataIterator = _iptcData.begin();
+ dataIterator != _iptcData.end(); ++dataIterator)
{
- Exiv2::Iptcdatum iptcDatum = _iptcData[key];
- return boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString());
+ if (dataIterator->key() == key)
+ {
+ valuesList.append(boost::python::make_tuple(dataIterator->typeName(), dataIterator->toString()));
+ ++valueOccurences;
+ }
}
+ if (valueOccurences > 0)
+ return valuesList;
else
throw Exiv2::Error(KEY_NOT_FOUND, key);
}
@@ -235,26 +243,38 @@ namespace LibPyExiv2
throw Exiv2::Error(METADATA_NOT_READ);
}
- boost::python::tuple Image::setIptcTag(std::string key, std::string value)
+ boost::python::tuple Image::setIptcTag(std::string key, std::string value, unsigned int index=0)
{
- boost::python::tuple returnValue;
if(_dataRead)
{
+ boost::python::tuple returnValue;
+ unsigned int indexCounter = index;
Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
- Exiv2::IptcMetadata::iterator i = _iptcData.findKey(iptcKey);
- if(i != _iptcData.end())
+ Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey);
+ while ((indexCounter > 0) && (dataIterator != _iptcData.end()))
{
- Exiv2::Iptcdatum iptcDatum = _iptcData[key];
- returnValue = boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString());
- // First erase the existing tag
- _iptcData.erase(i);
+ dataIterator = std::find_if(++dataIterator, _iptcData.end(),
+ Exiv2::FindMetadatumById::FindMetadatumById(iptcKey.tag(), iptcKey.record()));
+ --indexCounter;
+ }
+ if (dataIterator != _iptcData.end())
+ {
+ // The tag at given index already exists, override it
+ returnValue = boost::python::make_tuple(dataIterator->typeName(), dataIterator->toString());
+ dataIterator->setValue(value);
}
else
{
- // The key was not found
+ // Either index is greater than the index of the last repetition
+ // of the tag, or the tag does not exist yet.
+ // In both cases, it is created.
returnValue = boost::python::make_tuple(std::string(""), std::string(""));
+ Exiv2::Iptcdatum iptcDatum(iptcKey);
+ iptcDatum.setValue(value);
+ int state = _iptcData.add(iptcDatum);
+ if (state == 6)
+ throw Exiv2::Error(NON_REPEATABLE);
}
- _iptcData[key] = value;
return returnValue;
}
else
@@ -419,6 +439,9 @@ namespace LibPyExiv2
case METADATA_NOT_READ:
PyErr_SetString(PyExc_IOError, "Image metadata has not been read yet");
break;
+ case NON_REPEATABLE:
+ PyErr_SetString(PyExc_KeyError, "Tag is not repeatable");
+ break;
case KEY_NOT_FOUND:
PyErr_SetString(PyExc_ValueError, "Tag not set");
break;
diff --git a/src/libpyexiv2.hpp b/src/libpyexiv2.hpp
index a63a05f..514094a 100644
--- a/src/libpyexiv2.hpp
+++ b/src/libpyexiv2.hpp
@@ -88,18 +88,23 @@ namespace LibPyExiv2
// even if a tag is present more than once.
boost::python::list getAvailableIptcTags();
- // Return true if the required IPTC tag is set, false otherwise.
+ // Return true if the required IPTC tag is set at least once, false
+ // otherwise.
bool isIptcTagSet(std::string key);
- // Return a tuple containing the type (as a string) and the value
- // (as a string as well) of the required IPTC tag.
+ // Return a list of tuples, each containing the type (as a string) and
+ // the value (as a string as well) of the required IPTC tag.
// Throw an exception if the tag is not set.
- boost::python::tuple getIptcTag(std::string key);
+ boost::python::list getIptcTag(std::string key);
// Set the IPTC tag's value and return a tuple containing the
// type and previous value of the tag (empty strings if not previously
// set). If the tag was not previously set, it is created.
- boost::python::tuple setIptcTag(std::string key, std::string value);
+ // If the key references a repeatable tag, the parameter index (starting
+ // from 0 like a list index) is used to determine which of the
+ // repetitions is to be set. In case of an index greater than the
+ // highest existing one, adds a repetition of the tag.
+ boost::python::tuple setIptcTag(std::string key, std::string value, unsigned int index);
// Delete the required IPTC tag and return a tuple containing the
// type and previous value.