~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to src/exiv2wrapper.cpp

  • Committer: Olivier Tilloy
  • Date: 2009-04-06 07:12:35 UTC
  • Revision ID: olivier@tilloy.net-20090406071235-7lik9lecq6nuae2l
Private value setter for IPTC tags.

Show diffs side-by-side

added added

removed removed

Lines of Context:
123
123
    }
124
124
}
125
125
 
126
 
void Image::setExifTag(std::string key, std::string value)
 
126
void Image::setExifTagValue(std::string key, std::string value)
127
127
{
128
128
    if(_dataRead)
129
129
    {
221
221
    }
222
222
}
223
223
 
224
 
/*boost::python::list Image::getIptcTag(std::string key)
225
 
{
226
 
    if(_dataRead)
227
 
    {
228
 
        boost::python::list valuesList;
229
 
        unsigned int valueOccurences = 0;
230
 
        Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
231
 
        for (Exiv2::IptcMetadata::iterator dataIterator = _iptcData.begin();
232
 
            dataIterator != _iptcData.end(); ++dataIterator)
233
 
        {
234
 
            if (dataIterator->key() == key)
235
 
            {
236
 
                valuesList.append(boost::python::make_tuple(std::string(dataIterator->typeName()), dataIterator->toString()));
237
 
                ++valueOccurences;
238
 
            }
239
 
        }
240
 
        if (valueOccurences > 0)
241
 
            return valuesList;
242
 
        else
243
 
            throw Exiv2::Error(KEY_NOT_FOUND, key);
244
 
    }
245
 
    else
246
 
        throw Exiv2::Error(METADATA_NOT_READ);
247
 
}
248
 
 
249
 
boost::python::tuple Image::setIptcTag(std::string key, std::string value, unsigned int index=0)
250
 
{
251
 
    if(_dataRead)
252
 
    {
253
 
        boost::python::tuple returnValue;
 
224
/*void Image::setIptcTag(std::string key, std::string value, unsigned int index=0)
 
225
{
 
226
    if(_dataRead)
 
227
    {
254
228
        unsigned int indexCounter = index;
255
229
        Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
256
230
        Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey);
263
237
        if (dataIterator != _iptcData.end())
264
238
        {
265
239
            // The tag at given index already exists, override it
266
 
            returnValue = boost::python::make_tuple(std::string(dataIterator->typeName()), dataIterator->toString());
267
240
            dataIterator->setValue(value);
268
241
        }
269
242
        else
271
244
            // Either index is greater than the index of the last repetition
272
245
            // of the tag, or the tag does not exist yet.
273
246
            // In both cases, it is created.
274
 
            returnValue = boost::python::make_tuple(std::string(""), std::string(""));
275
247
            Exiv2::Iptcdatum iptcDatum(iptcKey);
276
248
            iptcDatum.setValue(value);
277
249
            int state = _iptcData.add(iptcDatum);
278
250
            if (state == 6)
279
251
                throw Exiv2::Error(NON_REPEATABLE);
280
252
        }
281
 
        return returnValue;
282
253
    }
283
254
    else
284
255
        throw Exiv2::Error(METADATA_NOT_READ);
 
256
}*/
 
257
 
 
258
void Image::setIptcTagValues(std::string key, boost::python::tuple values)
 
259
{
 
260
    if (!_dataRead)
 
261
    {
 
262
        throw Exiv2::Error(METADATA_NOT_READ);
 
263
    }
 
264
 
 
265
    Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
 
266
    unsigned int index = 0;
 
267
    unsigned int max = len(values);
 
268
    Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey);
 
269
    while (index < max)
 
270
    {
 
271
        std::string value = boost::python::extract<std::string>(values[index++]);
 
272
        if (dataIterator != _iptcData.end())
 
273
        {
 
274
            // Override an existing value
 
275
            dataIterator->setValue(value);
 
276
            dataIterator = std::find_if(++dataIterator, _iptcData.end(),
 
277
                Exiv2::FindMetadatumById::FindMetadatumById(iptcKey.tag(),
 
278
                                                            iptcKey.record()));
 
279
        }
 
280
        else
 
281
        {
 
282
            // Append a new value
 
283
            Exiv2::Iptcdatum iptcDatum(iptcKey);
 
284
            iptcDatum.setValue(value);
 
285
            int state = _iptcData.add(iptcDatum);
 
286
            if (state == 6)
 
287
            {
 
288
                throw Exiv2::Error(NON_REPEATABLE);
 
289
            }
 
290
        }
 
291
    }
 
292
    // Erase the remaining values if any
 
293
    while (dataIterator != _iptcData.end())
 
294
    {
 
295
        _iptcData.erase(dataIterator);
 
296
        dataIterator = std::find_if(dataIterator, _iptcData.end(),
 
297
                Exiv2::FindMetadatumById::FindMetadatumById(iptcKey.tag(),
 
298
                                                            iptcKey.record()));
 
299
    }
285
300
}
286
301
 
287
 
boost::python::tuple Image::deleteIptcTag(std::string key, unsigned int index=0)
 
302
/*void Image::deleteIptcTag(std::string key, unsigned int index=0)
288
303
{
289
304
    if(_dataRead)
290
305
    {
291
 
        boost::python::tuple returnValue;
292
306
        unsigned int indexCounter = index;
293
307
        Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key);
294
308
        Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey);
301
315
        if (dataIterator != _iptcData.end())
302
316
        {
303
317
            // The tag at given index already exists, delete it
304
 
            returnValue = boost::python::make_tuple(std::string(dataIterator->typeName()), dataIterator->toString());
305
318
            _iptcData.erase(dataIterator);
306
 
            return returnValue;
307
319
        }
308
320
        else
309
321
            throw Exiv2::Error(KEY_NOT_FOUND, key);