Blame samples/exifprint.cpp

Packit Service 21b5d1
// ***************************************************************** -*- C++ -*-
Packit Service 21b5d1
// exifprint.cpp
Packit Service 21b5d1
// Sample program to print the Exif metadata of an image
Packit Service 21b5d1
Packit Service 21b5d1
#include <exiv2/exiv2.hpp>
Packit Service 21b5d1
Packit Service 21b5d1
#include <iostream>
Packit Service 21b5d1
#include <iomanip>
Packit Service 21b5d1
#include <cassert>
Packit Service 21b5d1
Packit Service 21b5d1
// https://github.com/Exiv2/exiv2/issues/468
Packit Service 21b5d1
#if defined(EXV_UNICODE_PATH) && defined(__MINGW__)
Packit Service 21b5d1
#undef  EXV_UNICODE_PATH
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
#ifdef  EXV_UNICODE_PATH
Packit Service 21b5d1
#define _tchar      wchar_t
Packit Service 21b5d1
#define _tstrcmp    wcscmp
Packit Service 21b5d1
#define _t(s)       L##s
Packit Service 21b5d1
#define _tcout      wcout
Packit Service 21b5d1
#define _tmain      wmain
Packit Service 21b5d1
#else
Packit Service 21b5d1
#define _tchar      char
Packit Service 21b5d1
#define _tstrcmp    strcmp
Packit Service 21b5d1
#define _t(s)       s
Packit Service 21b5d1
#define _tcout      cout
Packit Service 21b5d1
#define _tmain      main
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
int _tmain(int argc, _tchar* const argv[])
Packit Service 21b5d1
try {
Packit Service 21b5d1
    const _tchar* prog = argv[0];
Packit Service 21b5d1
    const _tchar* file = argv[1];
Packit Service 21b5d1
Packit Service 21b5d1
    if (argc != 2) {
Packit Service 21b5d1
        std::_tcout << _t("Usage: ") << prog << _t(" [ file | --version || --version-test ]") << std::endl;
Packit Service 21b5d1
        return 1;
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    if ( _tstrcmp(file,_t("--version")) == 0 ) {
Packit Service 21b5d1
        exv_grep_keys_t keys;
Packit Service 21b5d1
        Exiv2::dumpLibraryInfo(std::cout,keys);
Packit Service 21b5d1
        return 0;
Packit Service 21b5d1
    } else if ( _tstrcmp(file,_t("--version-test")) == 0 ) {
Packit Service 21b5d1
        // verifies/test macro EXIV2_TEST_VERSION
Packit Service 21b5d1
        // described in include/exiv2/version.hpp
Packit Service 21b5d1
        std::cout << "EXV_PACKAGE_VERSION             " << EXV_PACKAGE_VERSION             << std::endl
Packit Service 21b5d1
                  << "Exiv2::version()                " << Exiv2::version()                << std::endl
Packit Service 21b5d1
                  << "strlen(Exiv2::version())        " << ::strlen(Exiv2::version())      << std::endl
Packit Service 21b5d1
                  << "Exiv2::versionNumber()          " << Exiv2::versionNumber()          << std::endl
Packit Service 21b5d1
                  << "Exiv2::versionString()          " << Exiv2::versionString()          << std::endl
Packit Service 21b5d1
                  << "Exiv2::versionNumberHexString() " << Exiv2::versionNumberHexString() << std::endl
Packit Service 21b5d1
                  ;
Packit Service 21b5d1
Packit Service 21b5d1
        // Test the Exiv2 version available at runtime but compile the if-clause only if
Packit Service 21b5d1
        // the compile-time version is at least 0.15. Earlier versions didn't have a
Packit Service 21b5d1
        // testVersion() function:
Packit Service 21b5d1
        #if EXIV2_TEST_VERSION(0,15,0)
Packit Service 21b5d1
            if (Exiv2::testVersion(0,13,0)) {
Packit Service 21b5d1
              std::cout << "Available Exiv2 version is equal to or greater than 0.13\n";
Packit Service 21b5d1
            } else {
Packit Service 21b5d1
              std::cout << "Installed Exiv2 version is less than 0.13\n";
Packit Service 21b5d1
            }
Packit Service 21b5d1
        #else
Packit Service 21b5d1
              std::cout << "Compile-time Exiv2 version doesn't have Exiv2::testVersion()\n";
Packit Service 21b5d1
        #endif
Packit Service 21b5d1
        return 0;
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
Packit Service 21b5d1
    assert(image.get() != 0);
Packit Service 21b5d1
    image->readMetadata();
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::ExifData &exifData = image->exifData();
Packit Service 21b5d1
    if (exifData.empty()) {
Packit Service 21b5d1
        std::string error("No Exif data found in file");
Packit Service 21b5d1
        throw Exiv2::Error(Exiv2::kerErrorMessage, error);
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::ExifData::const_iterator end = exifData.end();
Packit Service 21b5d1
    for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
Packit Service 21b5d1
        const char* tn = i->typeName();
Packit Service 21b5d1
        std::cout << std::setw(44) << std::setfill(' ') << std::left
Packit Service 21b5d1
                  << i->key() << " "
Packit Service 21b5d1
                  << "0x" << std::setw(4) << std::setfill('0') << std::right
Packit Service 21b5d1
                  << std::hex << i->tag() << " "
Packit Service 21b5d1
                  << std::setw(9) << std::setfill(' ') << std::left
Packit Service 21b5d1
                  << (tn ? tn : "Unknown") << " "
Packit Service 21b5d1
                  << std::dec << std::setw(3)
Packit Service 21b5d1
                  << std::setfill(' ') << std::right
Packit Service 21b5d1
                  << i->count() << "  "
Packit Service 21b5d1
                  << std::dec << i->value()
Packit Service 21b5d1
                  << "\n";
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    return 0;
Packit Service 21b5d1
}
Packit Service 21b5d1
//catch (std::exception& e) {
Packit Service 21b5d1
//catch (Exiv2::AnyError& e) {
Packit Service 21b5d1
catch (Exiv2::Error& e) {
Packit Service 21b5d1
    std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
Packit Service 21b5d1
    return -1;
Packit Service 21b5d1
}