1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2021 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  */
20 // xmpparse.cpp
21 // Read an XMP packet from a file, parse it and print all (known) properties.
22 
23 #include <exiv2/exiv2.hpp>
24 #include <string>
25 #include <iostream>
26 #include <iomanip>
27 
main(int argc,char * const argv[])28 int main(int argc, char* const argv[])
29 try {
30     Exiv2::XmpParser::initialize();
31     ::atexit(Exiv2::XmpParser::terminate);
32 #ifdef EXV_ENABLE_BMFF
33     Exiv2::enableBMFF();
34 #endif
35 
36     if (argc != 2) {
37         std::cout << "Usage: " << argv[0] << " file\n";
38         return 1;
39     }
40     Exiv2::DataBuf buf = Exiv2::readFile(argv[1]);
41     std::string xmpPacket;
42     xmpPacket.assign(reinterpret_cast<char*>(buf.pData_), buf.size_);
43     Exiv2::XmpData xmpData;
44     if (0 != Exiv2::XmpParser::decode(xmpData, xmpPacket)) {
45         std::string error(argv[1]);
46         error += ": Failed to parse file contents (XMP packet)";
47         throw Exiv2::Error(Exiv2::kerErrorMessage, error);
48     }
49     if (xmpData.empty()) {
50         std::string error(argv[1]);
51         error += ": No XMP properties found in the XMP packet";
52         throw Exiv2::Error(Exiv2::kerErrorMessage, error);
53     }
54     for (Exiv2::XmpData::const_iterator md = xmpData.begin();
55          md != xmpData.end(); ++md) {
56         std::cout << std::setfill(' ') << std::left
57                   << std::setw(44)
58                   << md->key() << " "
59                   << std::setw(9) << std::setfill(' ') << std::left
60                   << md->typeName() << " "
61                   << std::dec << std::setw(3)
62                   << std::setfill(' ') << std::right
63                   << md->count() << "  "
64                   << std::dec << md->toString()
65                   << std::endl;
66     }
67     Exiv2::XmpParser::terminate();
68     return 0;
69 }
70 catch (Exiv2::AnyError& e) {
71     std::cout << "Caught Exiv2 exception '" << e << "'\n";
72     return -1;
73 }
74