Blame samples/xmpparser-test.cpp

Packit 01d647
// ***************************************************************** -*- C++ -*-
Packit 01d647
// xmpparser-test.cpp
Packit 01d647
// Read an XMP packet from a file, parse and re-serialize it.
Packit 01d647
Packit 01d647
#include <exiv2/exiv2.hpp>
Packit 01d647
Packit 01d647
#include <string>
Packit 01d647
#include <iostream>
Packit 01d647
#include <iomanip>
Packit 01d647
Packit 01d647
int main(int argc, char* const argv[])
Packit 01d647
try {
Packit 01d647
    if (argc != 2) {
Packit 01d647
        std::cout << "Usage: " << argv[0] << " file\n";
Packit 01d647
        return 1;
Packit 01d647
    }
Packit 01d647
    std::string filename(argv[1]);
Packit 01d647
    Exiv2::DataBuf buf = Exiv2::readFile(filename);
Packit 01d647
    std::string xmpPacket;
Packit 01d647
    xmpPacket.assign(reinterpret_cast<char*>(buf.pData_), buf.size_);
Packit 01d647
    std::cerr << "-----> Decoding XMP data read from " << filename << " <-----\n";
Packit 01d647
    Exiv2::XmpData xmpData;
Packit 01d647
    if (0 != Exiv2::XmpParser::decode(xmpData, xmpPacket)) {
Packit 01d647
        std::string error(argv[1]);
Packit 01d647
        error += ": Failed to parse file contents (XMP packet)";
Packit 01d647
        throw Exiv2::Error(Exiv2::kerErrorMessage, error);
Packit 01d647
    }
Packit 01d647
    if (xmpData.empty()) {
Packit 01d647
        std::string error(argv[1]);
Packit 01d647
        error += ": No XMP properties found in the XMP packet";
Packit 01d647
        throw Exiv2::Error(Exiv2::kerErrorMessage, error);
Packit 01d647
    }
Packit 01d647
    for (Exiv2::XmpData::const_iterator md = xmpData.begin();
Packit 01d647
         md != xmpData.end(); ++md) {
Packit 01d647
        std::cout << std::setfill(' ') << std::left
Packit 01d647
                  << std::setw(44)
Packit 01d647
                  << md->key() << " "
Packit 01d647
                  << std::setw(9) << std::setfill(' ') << std::left
Packit 01d647
                  << md->typeName() << " "
Packit 01d647
                  << std::dec << std::setw(3)
Packit 01d647
                  << std::setfill(' ') << std::right
Packit 01d647
                  << md->count() << "  "
Packit 01d647
                  << std::dec << md->value()
Packit 01d647
                  << std::endl;
Packit 01d647
    }
Packit 01d647
    filename += "-new";
Packit 01d647
    std::cerr << "-----> Encoding XMP data to write to " << filename << " <-----\n";
Packit 01d647
    if (0 != Exiv2::XmpParser::encode(xmpPacket, xmpData)) {
Packit 01d647
        std::string error(argv[1]);
Packit 01d647
        error += ": Failed to encode the XMP data";
Packit 01d647
        throw Exiv2::Error(Exiv2::kerErrorMessage, error);
Packit 01d647
    }
Packit 01d647
    Exiv2::FileIo file(filename);
Packit 01d647
    if (file.open("wb") != 0) {
Packit 01d647
        throw Exiv2::Error(Exiv2::kerFileOpenFailed, filename, "wb", Exiv2::strError());
Packit 01d647
    }
Packit 01d647
    if (file.write(reinterpret_cast<const Exiv2::byte*>(xmpPacket.data()), static_cast<long>(xmpPacket.size())) == 0) {
Packit 01d647
        throw Exiv2::Error(Exiv2::kerCallFailed, filename, Exiv2::strError(), "FileIo::write");
Packit 01d647
    }
Packit 01d647
    Exiv2::XmpParser::terminate();
Packit 01d647
    return 0;
Packit 01d647
}
Packit 01d647
catch (Exiv2::AnyError& e) {
Packit 01d647
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
Packit 01d647
    return -1;
Packit 01d647
}