Blame src/exiv2app.hpp

Packit Service 21b5d1
// ***************************************************************** -*- C++ -*-
Packit Service 21b5d1
/*
Packit Service 21b5d1
 * Copyright (C) 2004-2018 Exiv2 authors
Packit Service 21b5d1
 * This program is part of the Exiv2 distribution.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * This program is free software; you can redistribute it and/or
Packit Service 21b5d1
 * modify it under the terms of the GNU General Public License
Packit Service 21b5d1
 * as published by the Free Software Foundation; either version 2
Packit Service 21b5d1
 * of the License, or (at your option) any later version.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * This program is distributed in the hope that it will be useful,
Packit Service 21b5d1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 21b5d1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 21b5d1
 * GNU General Public License for more details.
Packit Service 21b5d1
 *
Packit Service 21b5d1
 * You should have received a copy of the GNU General Public License
Packit Service 21b5d1
 * along with this program; if not, write to the Free Software
Packit Service 21b5d1
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
Packit Service 21b5d1
 */
Packit Service 21b5d1
/*!
Packit Service 21b5d1
  @file    exiv2app.hpp
Packit Service 21b5d1
  @brief   Defines class Params, used for the command line handling of exiv2
Packit Service 21b5d1
  @author  Andreas Huggel (ahu)
Packit Service 21b5d1
           ahuggel@gmx.net
Packit Service 21b5d1
  @date    08-Dec-03, ahu: created
Packit Service 21b5d1
 */
Packit Service 21b5d1
#ifndef EXIV2APP_HPP_
Packit Service 21b5d1
#define EXIV2APP_HPP_
Packit Service 21b5d1
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// included header files
Packit Service 21b5d1
#include <exiv2/exiv2.hpp>
Packit Service 21b5d1
Packit Service 21b5d1
#include "utils.hpp"
Packit Service 21b5d1
#include "types.hpp"
Packit Service 21b5d1
#include "getopt.hpp"
Packit Service 21b5d1
Packit Service 21b5d1
// + standard includes
Packit Service 21b5d1
#include <string>
Packit Service 21b5d1
#include <vector>
Packit Service 21b5d1
#include <set>
Packit Service 21b5d1
#include <iostream>
Packit Service 21b5d1
Packit Service 21b5d1
#ifdef EXV_HAVE_UNISTD_H
Packit Service 21b5d1
#include <unistd.h>
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
// stdin handler includes
Packit Service 21b5d1
#ifndef  _MSC_VER
Packit Service 21b5d1
#include <cstdlib>
Packit Service 21b5d1
#include <stdio.h>
Packit Service 21b5d1
#include <string.h>
Packit Service 21b5d1
#if defined(__CYGWIN__) || defined(__MINGW__)
Packit Service 21b5d1
#include <windows.h>
Packit Service 21b5d1
#else
Packit Service 21b5d1
#include <sys/select.h>
Packit Service 21b5d1
#endif
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW__) || defined(_MSC_VER)
Packit Service 21b5d1
#include <fcntl.h>
Packit Service 21b5d1
#include <io.h>
Packit Service 21b5d1
#endif
Packit Service 21b5d1
Packit Service 21b5d1
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// class definitions
Packit Service 21b5d1
Packit Service 21b5d1
//! Command identifiers
Packit Service 21b5d1
enum CmdId { invalidCmdId, add, set, del, reg };
Packit Service 21b5d1
//! Metadata identifiers
Packit Service 21b5d1
// enum MetadataId { invalidMetadataId, iptc, exif, xmp };
Packit Service 21b5d1
//! Metadata identifiers
Packit Service 21b5d1
// mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8
Packit Service 21b5d1
enum MetadataId { invalidMetadataId = Exiv2::mdNone
Packit Service 21b5d1
                , iptc = Exiv2::mdIptc
Packit Service 21b5d1
                , exif = Exiv2::mdExif
Packit Service 21b5d1
                , xmp  = Exiv2::mdXmp
Packit Service 21b5d1
                } ;
Packit Service 21b5d1
Packit Service 21b5d1
//! Structure for one parsed modification command
Packit Service 21b5d1
struct ModifyCmd {
Packit Service 21b5d1
    //! C'tor
Packit Service 21b5d1
    ModifyCmd() :
Packit Service 21b5d1
        cmdId_(invalidCmdId), metadataId_(invalidMetadataId),
Packit Service 21b5d1
        typeId_(Exiv2::invalidTypeId), explicitType_(false) {}
Packit Service 21b5d1
    CmdId cmdId_;                               //!< Command identifier
Packit Service 21b5d1
    std::string key_;                           //!< Exiv2 key string
Packit Service 21b5d1
    MetadataId metadataId_;                     //!< Metadata identifier
Packit Service 21b5d1
    Exiv2::TypeId typeId_;                      //!< Exiv2 type identifier
Packit Service 21b5d1
    //! Flag to indicate if the type was explicitly specified (true)
Packit Service 21b5d1
    bool explicitType_;
Packit Service 21b5d1
    std::string value_;                         //!< Data
Packit Service 21b5d1
};
Packit Service 21b5d1
//! Container for modification commands
Packit Service 21b5d1
typedef std::vector<ModifyCmd> ModifyCmds;
Packit Service 21b5d1
//! Structure to link command identifiers to strings
Packit Service 21b5d1
struct CmdIdAndString {
Packit Service 21b5d1
    CmdId cmdId_;                               //!< Commands identifier
Packit Service 21b5d1
    std::string cmdString_;                     //!< Command string
Packit Service 21b5d1
};
Packit Service 21b5d1
Packit Service 21b5d1
/*!
Packit Service 21b5d1
  @brief Implements the command line handling for the program.
Packit Service 21b5d1
Packit Service 21b5d1
  Derives from Util::Getopt to use the command line argument parsing
Packit Service 21b5d1
  functionality provided there. This class is implemented as a singleton,
Packit Service 21b5d1
  i.e., there is only one global instance of it, which can be accessed
Packit Service 21b5d1
  from everywhere.
Packit Service 21b5d1
Packit Service 21b5d1
  Usage example: 
Packit Service 21b5d1
  @code
Packit Service 21b5d1
  #include "params.h"
Packit Service 21b5d1
Packit Service 21b5d1
  int main(int argc, char* const argv[])
Packit Service 21b5d1
  {
Packit Service 21b5d1
      Params& params = Params::instance();
Packit Service 21b5d1
      if (params.getopt(argc, argv)) {
Packit Service 21b5d1
          params.usage();
Packit Service 21b5d1
          return 1;
Packit Service 21b5d1
      }
Packit Service 21b5d1
      if (params.help_) {
Packit Service 21b5d1
          params.help();
Packit Service 21b5d1
          return 0;
Packit Service 21b5d1
      }
Packit Service 21b5d1
      if (params.version_) {
Packit Service 21b5d1
          params.version();
Packit Service 21b5d1
          return 0;
Packit Service 21b5d1
      }
Packit Service 21b5d1
Packit Service 21b5d1
      // do something useful here...
Packit Service 21b5d1
Packit Service 21b5d1
      return 0;
Packit Service 21b5d1
  }
Packit Service 21b5d1
  @endcode
Packit Service 21b5d1
 */
Packit Service 21b5d1
Packit Service 21b5d1
class Params : public Util::Getopt {
Packit Service 21b5d1
private:
Packit Service 21b5d1
    std::string optstring_;
Packit Service 21b5d1
Packit Service 21b5d1
public:
Packit Service 21b5d1
    //! Container for command files
Packit Service 21b5d1
    typedef std::vector<std::string> CmdFiles;
Packit Service 21b5d1
    //! Container for commands from the command line
Packit Service 21b5d1
    typedef std::vector<std::string> CmdLines;
Packit Service 21b5d1
    //! Container to store filenames.
Packit Service 21b5d1
    typedef std::vector<std::string> Files;
Packit Service 21b5d1
    //! Container for preview image numbers
Packit Service 21b5d1
    typedef std::set<int> PreviewNumbers;
Packit Service 21b5d1
    //! Container for greps
Packit Service 21b5d1
    typedef  exv_grep_keys_t Greps;
Packit Service 21b5d1
    //! Container for keys
Packit Service 21b5d1
    typedef std::vector<std::string> Keys;
Packit Service 21b5d1
Packit Service 21b5d1
    /*!
Packit Service 21b5d1
      @brief Controls all access to the global Params instance.
Packit Service 21b5d1
      @return Reference to the global Params instance.
Packit Service 21b5d1
    */
Packit Service 21b5d1
    static Params& instance();
Packit Service 21b5d1
    //! Destructor
Packit Service 21b5d1
    void cleanup();
Packit Service 21b5d1
Packit Service 21b5d1
    //! Enumerates print modes
Packit Service 21b5d1
    enum PrintMode {
Packit Service 21b5d1
        pmSummary,
Packit Service 21b5d1
        pmList,
Packit Service 21b5d1
        pmComment,
Packit Service 21b5d1
        pmPreview,
Packit Service 21b5d1
        pmStructure,
Packit Service 21b5d1
        pmXMP,
Packit Service 21b5d1
        pmIccProfile,
Packit Service 21b5d1
        pmRecursive
Packit Service 21b5d1
    };
Packit Service 21b5d1
Packit Service 21b5d1
    //! Individual items to print, bitmap
Packit Service 21b5d1
    enum PrintItem {
Packit Service 21b5d1
        prTag   =    1,
Packit Service 21b5d1
        prGroup =    2,
Packit Service 21b5d1
        prKey   =    4,
Packit Service 21b5d1
        prName  =    8,
Packit Service 21b5d1
        prLabel =   16,
Packit Service 21b5d1
        prType  =   32,
Packit Service 21b5d1
        prCount =   64,
Packit Service 21b5d1
        prSize  =  128,
Packit Service 21b5d1
        prValue =  256,
Packit Service 21b5d1
        prTrans =  512,
Packit Service 21b5d1
        prHex   = 1024,
Packit Service 21b5d1
        prSet   = 2048
Packit Service 21b5d1
    };
Packit Service 21b5d1
Packit Service 21b5d1
    //! Enumerates common targets, bitmap
Packit Service 21b5d1
    enum CommonTarget {
Packit Service 21b5d1
        ctExif       =   1,
Packit Service 21b5d1
        ctIptc       =   2,
Packit Service 21b5d1
        ctComment    =   4,
Packit Service 21b5d1
        ctThumb      =   8,
Packit Service 21b5d1
        ctXmp        =  16,
Packit Service 21b5d1
        ctXmpSidecar =  32,
Packit Service 21b5d1
        ctPreview    =  64,
Packit Service 21b5d1
        ctIccProfile = 128,
Packit Service 21b5d1
        ctXmpRaw     = 256,
Packit Service 21b5d1
        ctStdInOut   = 512,
Packit Service 21b5d1
        ctIptcRaw    =1024
Packit Service 21b5d1
    };
Packit Service 21b5d1
Packit Service 21b5d1
    //! Enumerates the policies to handle existing files in rename action
Packit Service 21b5d1
    enum FileExistsPolicy { overwritePolicy, renamePolicy, askPolicy };
Packit Service 21b5d1
Packit Service 21b5d1
    //! Enumerates year, month and day adjustments.
Packit Service 21b5d1
    enum Yod { yodYear, yodMonth, yodDay };
Packit Service 21b5d1
Packit Service 21b5d1
    //! Structure for year, month and day adjustment command line arguments.
Packit Service 21b5d1
    struct YodAdjust {
Packit Service 21b5d1
        bool        flag_;              //!< Adjustment flag.
Packit Service 21b5d1
        const char* option_;            //!< Adjustment option string.
Packit Service 21b5d1
        long        adjustment_;        //!< Adjustment value.
Packit Service 21b5d1
    };
Packit Service 21b5d1
Packit Service 21b5d1
    bool help_;                         //!< Help option flag.
Packit Service 21b5d1
    bool version_;                      //!< Version option flag.
Packit Service 21b5d1
    bool verbose_;                      //!< Verbose (talkative) option flag.
Packit Service 21b5d1
    bool force_;                        //!< Force overwrites flag.
Packit Service 21b5d1
    bool binary_;                       //!< Suppress long binary values.
Packit Service 21b5d1
    bool unknown_;                      //!< Suppress unknown tags.
Packit Service 21b5d1
    bool preserve_;                     //!< Preserve timestamps flag.
Packit Service 21b5d1
    bool timestamp_;                    //!< Rename also sets the file timestamp.
Packit Service 21b5d1
    bool timestampOnly_;                //!< Rename only sets the file timestamp.
Packit Service 21b5d1
    FileExistsPolicy fileExistsPolicy_; //!< What to do if file to rename exists.
Packit Service 21b5d1
    bool adjust_;                       //!< Adjustment flag.
Packit Service 21b5d1
    PrintMode printMode_;               //!< Print mode.
Packit Service 21b5d1
    unsigned long printItems_;          //!< Print items.
Packit Service 21b5d1
    unsigned long printTags_;           //!< Print tags (bitmap of MetadataId flags).
Packit Service 21b5d1
    //! %Action (integer rather than TaskType to avoid dependency).
Packit Service 21b5d1
    int  action_;
Packit Service 21b5d1
    int  target_;                       //!< What common target to process.
Packit Service 21b5d1
Packit Service 21b5d1
    long adjustment_;                   //!< Adjustment in seconds.
Packit Service 21b5d1
    YodAdjust yodAdjust_[3];            //!< Year, month and day adjustment info.
Packit Service 21b5d1
    std::string format_;                //!< Filename format (-r option arg).
Packit Service 21b5d1
    bool formatSet_;                    //!< Whether the format is set with -r
Packit Service 21b5d1
    CmdFiles cmdFiles_;                 //!< Names of the modification command files
Packit Service 21b5d1
    CmdLines cmdLines_;                 //!< Commands from the command line
Packit Service 21b5d1
    ModifyCmds modifyCmds_;             //!< Parsed modification commands
Packit Service 21b5d1
    std::string jpegComment_;           //!< Jpeg comment to set in the image
Packit Service 21b5d1
    std::string directory_;             //!< Location for files to extract/insert
Packit Service 21b5d1
    std::string suffix_;                //!< File extension of the file to insert
Packit Service 21b5d1
    Files files_;                       //!< List of non-option arguments.
Packit Service 21b5d1
    PreviewNumbers previewNumbers_;     //!< List of preview numbers
Packit Service 21b5d1
    Greps greps_;                       //!< List of keys to 'grep' from the metadata
Packit Service 21b5d1
    Keys  keys_;                        //!< List of keys to match from the metadata
Packit Service 21b5d1
    std::string charset_;               //!< Charset to use for UNICODE Exif user comment
Packit Service 21b5d1
Packit Service 21b5d1
    Exiv2::DataBuf  stdinBuf;           //!< DataBuf with the binary bytes from stdin
Packit Service 21b5d1
Packit Service 21b5d1
private:
Packit Service 21b5d1
    //! Pointer to the global Params object.
Packit Service 21b5d1
    static Params* instance_;
Packit Service 21b5d1
    //! Initializer for year, month and day adjustment info.
Packit Service 21b5d1
    static const YodAdjust emptyYodAdjust_[];
Packit Service 21b5d1
Packit Service 21b5d1
    bool first_;
Packit Service 21b5d1
Packit Service 21b5d1
private:
Packit Service 21b5d1
    /*!
Packit Service 21b5d1
      @brief Default constructor. Note that optstring_ is initialized here.
Packit Service 21b5d1
             The c'tor is private to force instantiation through instance().
Packit Service 21b5d1
     */
Packit Service 21b5d1
    Params() : optstring_(":hVvqfbuktTFa:Y:O:D:r:p:P:d:e:i:c:m:M:l:S:g:K:n:Q:"),
Packit Service 21b5d1
               help_(false),
Packit Service 21b5d1
               version_(false),
Packit Service 21b5d1
               verbose_(false),
Packit Service 21b5d1
               force_(false),
Packit Service 21b5d1
               binary_(true),
Packit Service 21b5d1
               unknown_(true),
Packit Service 21b5d1
               preserve_(false),
Packit Service 21b5d1
               timestamp_(false),
Packit Service 21b5d1
               timestampOnly_(false),
Packit Service 21b5d1
               fileExistsPolicy_(askPolicy),
Packit Service 21b5d1
               adjust_(false),
Packit Service 21b5d1
               printMode_(pmSummary),
Packit Service 21b5d1
               printItems_(0),
Packit Service 21b5d1
               printTags_(Exiv2::mdNone),
Packit Service 21b5d1
               action_(0),
Packit Service 21b5d1
               target_(ctExif|ctIptc|ctComment|ctXmp),
Packit Service 21b5d1
               adjustment_(0),
Packit Service 21b5d1
               format_("%Y%m%d_%H%M%S"),
Packit Service 21b5d1
               formatSet_(false),
Packit Service 21b5d1
               first_(true)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        yodAdjust_[yodYear]  = emptyYodAdjust_[yodYear];
Packit Service 21b5d1
        yodAdjust_[yodMonth] = emptyYodAdjust_[yodMonth];
Packit Service 21b5d1
        yodAdjust_[yodDay]   = emptyYodAdjust_[yodDay];
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    //! Prevent copy-construction: not implemented.
Packit Service 21b5d1
    Params(const Params& rhs);
Packit Service 21b5d1
Packit Service 21b5d1
    //! Destructor, frees any allocated regexes in greps_
Packit Service 21b5d1
    ~Params();
Packit Service 21b5d1
Packit Service 21b5d1
    //! @name Helpers
Packit Service 21b5d1
    //@{
Packit Service 21b5d1
    int setLogLevel(const std::string& optarg);
Packit Service 21b5d1
    int evalGrep( const std::string& optarg);
Packit Service 21b5d1
    int evalKey( const std::string& optarg);
Packit Service 21b5d1
    int evalRename(int opt, const std::string& optarg);
Packit Service 21b5d1
    int evalAdjust(const std::string& optarg);
Packit Service 21b5d1
    int evalYodAdjust(const Yod& yod, const std::string& optarg);
Packit Service 21b5d1
    int evalPrint(const std::string& optarg);
Packit Service 21b5d1
    int evalPrintFlags(const std::string& optarg);
Packit Service 21b5d1
    int evalDelete(const std::string& optarg);
Packit Service 21b5d1
    int evalExtract(const std::string& optarg);
Packit Service 21b5d1
    int evalInsert(const std::string& optarg);
Packit Service 21b5d1
    int evalModify(int opt, const std::string& optarg);
Packit Service 21b5d1
    //@}
Packit Service 21b5d1
Packit Service 21b5d1
public:
Packit Service 21b5d1
    /*!
Packit Service 21b5d1
      @brief Call Getopt::getopt() with optstring, to inititate command line
Packit Service 21b5d1
             argument parsing, perform consistency checks after all command line
Packit Service 21b5d1
             arguments are parsed.
Packit Service 21b5d1
Packit Service 21b5d1
      @param argc Argument count as passed to main() on program invocation.
Packit Service 21b5d1
      @param argv Argument array as passed to main() on program invocation.
Packit Service 21b5d1
Packit Service 21b5d1
      @return 0 if successful, >0 in case of errors.
Packit Service 21b5d1
     */
Packit Service 21b5d1
    int getopt(int argc, char* const argv[]);
Packit Service 21b5d1
Packit Service 21b5d1
    //! Handle options and their arguments.
Packit Service 21b5d1
    virtual int option(int opt, const std::string& optarg, int optopt);
Packit Service 21b5d1
Packit Service 21b5d1
    //! Handle non-option parameters.
Packit Service 21b5d1
    virtual int nonoption(const std::string& argv);
Packit Service 21b5d1
Packit Service 21b5d1
    //! Print a minimal usage note to an output stream.
Packit Service 21b5d1
    void usage(std::ostream& os =std::cout) const;
Packit Service 21b5d1
Packit Service 21b5d1
    //! Print further usage explanations to an output stream.
Packit Service 21b5d1
    void help(std::ostream& os =std::cout) const;
Packit Service 21b5d1
Packit Service 21b5d1
    //! Print version information to an output stream.
Packit Service 21b5d1
    void version(bool verbose =false, std::ostream& os =std::cout) const;
Packit Service 21b5d1
Packit Service 21b5d1
    //! Print target_
Packit Service 21b5d1
    static std::string printTarget(const std::string& before,int target,bool bPrint=false,std::ostream& os=std::cout);
Packit Service 21b5d1
Packit Service 21b5d1
    //! getStdin binary data read from stdin to DataBuf
Packit Service 21b5d1
    /*
Packit Service 21b5d1
        stdin can be used by multiple images in the exiv2 command line:
Packit Service 21b5d1
        For example: $ cat foo.icc | exiv2 -iC- a.jpg b.jpg c.jpg will modify the ICC profile in several images.
Packit Service 21b5d1
    */
Packit Service 21b5d1
    void getStdin(Exiv2::DataBuf& buf);
Packit Service 21b5d1
Packit Service 21b5d1
}; // class Params
Packit Service 21b5d1
Packit Service 21b5d1
#endif                                  // #ifndef EXIV2APP_HPP_