From 4bc4a0dc400d108f0fa97c2a0bd2b6e53634d9fa Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Fri, 2 Apr 2004 09:53:14 +0000 Subject: [PATCH] Added Task Insert, implemented extract and insert functionality --- src/actions.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++------ src/actions.hpp | 22 +++++++++++++--- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index 90422e0d..9c195434 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -20,13 +20,13 @@ */ /* File: actions.cpp - Version: $Name: $ $Revision: 1.12 $ + Version: $Name: $ $Revision: 1.13 $ Author(s): Andreas Huggel (ahu) History: 08-Dec-03, ahu: created */ // ***************************************************************************** #include "rcsid.hpp" -EXIV2_RCSID("@(#) $Name: $ $Revision: 1.12 $ $RCSfile: actions.cpp,v $") +EXIV2_RCSID("@(#) $Name: $ $Revision: 1.13 $ $RCSfile: actions.cpp,v $") // ***************************************************************************** // included header files @@ -105,6 +105,7 @@ namespace Action { registerTask(rename, Task::AutoPtr(new Rename)); registerTask(erase, Task::AutoPtr(new Erase)); registerTask(extract, Task::AutoPtr(new Extract)); + registerTask(insert, Task::AutoPtr(new Insert)); } // TaskFactory c'tor Task::AutoPtr TaskFactory::create(TaskType type) @@ -504,11 +505,25 @@ namespace Action { return 1; } // Extract::run - int Extract::writeExifData(const Exif::ExifData& exifData) const + int Extract::writeExifData(Exif::ExifData& exifData) const { - // Todo: implement me! - std::cout << "Sorry, the extract action for Exif data has not been implemented yet.\n"; - return 0; + std::string exvPath = Util::dirname(path_) + "/" + + Util::basename(path_, true) + ".exv"; + if (Params::instance().verbose_) { + std::cout << "Writing Exif data to " << exvPath << "\n"; + } + if (!Params::instance().force_ && Util::fileExists(exvPath)) { + std::cout << Params::instance().progname() + << ": Overwrite `" << exvPath << "'? "; + std::string s; + std::cin >> s; + if (s[0] != 'y' && s[0] != 'Y') return 0; + } + int rc = exifData.writeExifData(exvPath); + if (rc) { + std::cerr << exifWriteError(rc, exvPath) << "\n"; + } + return rc; } int Extract::writeThumbnail(const Exif::ExifData& exifData) const @@ -535,6 +550,9 @@ namespace Action { if (s[0] != 'y' && s[0] != 'Y') return 0; } int rc = exifData.writeThumbnail(thumb); + if (rc) { + std::cerr << exifWriteError(rc, thumb) << "\n"; + } return rc; } @@ -548,6 +566,42 @@ namespace Action { return new Extract(*this); } + int Insert::run(const std::string& path) + try { + std::string exvPath = Util::dirname(path) + "/" + + Util::basename(path, true) + ".exv"; + Exif::ExifData exifData; + int rc = exifData.read(exvPath); + if (rc) { + std::cerr << exifReadError(rc, exvPath) << "\n"; + return rc; + } + if (Params::instance().verbose_) { + std::cout << "Inserting metadata from " << exvPath << "\n"; + } + rc = exifData.write(path); + if (rc) { + std::cerr << exifWriteError(rc, path) << "\n"; + } + return rc; + } + catch(const Exif::Error& e) + { + std::cerr << "Exif exception in insert action for file " << path + << ":\n" << e << "\n"; + return 1; + } // Insert::run + + Insert::AutoPtr Insert::clone() const + { + return AutoPtr(dynamic_cast(clone_())); + } + + Task* Insert::clone_() const + { + return new Insert(*this); + } + int Adjust::run(const std::string& path) try { adjustment_ = Params::instance().adjustment_; @@ -681,7 +735,7 @@ namespace { std::string error; switch (rc) { case -1: - error = "Couldn't open file `" + path + "'"; + error = "Failed to open file `" + path + "'"; break; case -2: error = "The file contains data of an unknown image type"; @@ -710,7 +764,7 @@ namespace { std::string error; switch (rc) { case -1: - error = "Couldn't open file `" + path + "'"; + error = "Failed to open file `" + path + "'"; break; case -2: error = "The file contains data of an unknown image type"; diff --git a/src/actions.hpp b/src/actions.hpp index 6b6fc7dd..042521a4 100644 --- a/src/actions.hpp +++ b/src/actions.hpp @@ -22,7 +22,7 @@ @file actions.hpp @brief Implements base class Task, TaskFactory and the various supported actions (derived from Task). - @version $Name: $ $Revision: 1.5 $ + @version $Name: $ $Revision: 1.6 $ @author Andreas Huggel (ahu) ahuggel@gmx.net @date 11-Dec-03, ahu: created @@ -52,7 +52,7 @@ namespace Exif { namespace Action { //! Enumerates all tasks - enum TaskType { none, adjust, print, rename, erase, extract }; + enum TaskType { none, adjust, print, rename, erase, extract, insert }; // ***************************************************************************** // class definitions @@ -250,13 +250,29 @@ namespace Action { @brief Write the %Exif data to a file. The filename is composed by replacing the suffix of the image filename with ".exf". */ - int writeExifData(const Exif::ExifData& exifData) const; + int writeExifData(Exif::ExifData& exifData) const; private: virtual Task* clone_() const; std::string path_; }; // class Extract + + /*! + @brief %Insert the %Exif data from corresponding *.exv files. + */ + class Insert : public Task { + public: + virtual ~Insert() {} + virtual int run(const std::string& path); + typedef std::auto_ptr AutoPtr; + AutoPtr clone() const; + + private: + virtual Task* clone_() const; + + }; // class Insert + } // namespace Action #endif // #ifndef ACTIONS_HPP_