Exiv2
exiv2app.hpp
Go to the documentation of this file.
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2018 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  */
27 #ifndef EXIV2APP_HPP_
28 #define EXIV2APP_HPP_
29 
30 // *****************************************************************************
31 // included header files
32 #include <exiv2/exiv2.hpp>
33 
34 #include "utils.hpp"
35 #include "types.hpp"
36 #include "getopt.hpp"
37 
38 // + standard includes
39 #include <string>
40 #include <vector>
41 #include <set>
42 #include <iostream>
43 
44 #ifdef EXV_HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 
48 // stdin handler includes
49 #ifndef _MSC_VER
50 #include <cstdlib>
51 #include <stdio.h>
52 #include <string.h>
53 #if defined(__CYGWIN__) || defined(__MINGW__)
54 #include <windows.h>
55 #else
56 #include <sys/select.h>
57 #endif
58 #endif
59 
60 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW__) || defined(_MSC_VER)
61 #include <fcntl.h>
62 #include <io.h>
63 #endif
64 
65 
66 // *****************************************************************************
67 // class definitions
68 
70 enum CmdId { invalidCmdId, add, set, del, reg };
72 // enum MetadataId { invalidMetadataId, iptc, exif, xmp };
74 // mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8
75 enum MetadataId { invalidMetadataId = Exiv2::mdNone
76  , iptc = Exiv2::mdIptc
77  , exif = Exiv2::mdExif
78  , xmp = Exiv2::mdXmp
79  } ;
80 
82 struct ModifyCmd {
85  cmdId_(invalidCmdId), metadataId_(invalidMetadataId),
86  typeId_(Exiv2::invalidTypeId), explicitType_(false) {}
91  bool explicitType_;
94 };
96 typedef std::vector<ModifyCmd> ModifyCmds;
101 };
102 
138 class Params : public Util::Getopt {
139 private:
140  std::string optstring_;
141 
142 public:
144  typedef std::vector<std::string> CmdFiles;
146  typedef std::vector<std::string> CmdLines;
148  typedef std::vector<std::string> Files;
150  typedef std::set<int> PreviewNumbers;
154  typedef std::vector<std::string> Keys;
155 
160  static Params& instance();
162  void cleanup();
163 
165  enum PrintMode {
166  pmSummary,
167  pmList,
168  pmComment,
169  pmPreview,
170  pmStructure,
171  pmXMP,
172  pmIccProfile,
173  pmRecursive
174  };
175 
177  enum PrintItem {
178  prTag = 1,
179  prGroup = 2,
180  prKey = 4,
181  prName = 8,
182  prLabel = 16,
183  prType = 32,
184  prCount = 64,
185  prSize = 128,
186  prValue = 256,
187  prTrans = 512,
188  prHex = 1024,
189  prSet = 2048
190  };
191 
194  ctExif = 1,
195  ctIptc = 2,
196  ctComment = 4,
197  ctThumb = 8,
198  ctXmp = 16,
199  ctXmpSidecar = 32,
200  ctPreview = 64,
201  ctIccProfile = 128,
202  ctXmpRaw = 256,
203  ctStdInOut = 512,
204  ctIptcRaw =1024
205  };
206 
208  enum FileExistsPolicy { overwritePolicy, renamePolicy, askPolicy };
209 
211  enum Yod { yodYear, yodMonth, yodDay };
212 
214  struct YodAdjust {
215  bool flag_;
216  const char* option_;
217  long adjustment_;
218  };
219 
220  bool help_;
221  bool version_;
222  bool verbose_;
223  bool force_;
224  bool binary_;
225  bool unknown_;
226  bool preserve_;
227  bool timestamp_;
230  bool adjust_;
232  unsigned long printItems_;
233  unsigned long printTags_;
234  int action_;
236  int target_;
237 
238  long adjustment_;
241  bool formatSet_;
253 
255 
256 private:
258  static Params* instance_;
260  static const YodAdjust emptyYodAdjust_[];
261 
262  bool first_;
263 
264 private:
269  Params() : optstring_(":hVvqfbuktTFa:Y:O:D:r:p:P:d:e:i:c:m:M:l:S:g:K:n:Q:"),
270  help_(false),
271  version_(false),
272  verbose_(false),
273  force_(false),
274  binary_(true),
275  unknown_(true),
276  preserve_(false),
277  timestamp_(false),
278  timestampOnly_(false),
279  fileExistsPolicy_(askPolicy),
280  adjust_(false),
281  printMode_(pmSummary),
282  printItems_(0),
283  printTags_(Exiv2::mdNone),
284  action_(0),
285  target_(ctExif|ctIptc|ctComment|ctXmp),
286  adjustment_(0),
287  format_("%Y%m%d_%H%M%S"),
288  formatSet_(false),
289  first_(true)
290  {
291  yodAdjust_[yodYear] = emptyYodAdjust_[yodYear];
292  yodAdjust_[yodMonth] = emptyYodAdjust_[yodMonth];
293  yodAdjust_[yodDay] = emptyYodAdjust_[yodDay];
294  }
295 
297  Params(const Params& rhs);
298 
300  ~Params();
301 
303 
304  int setLogLevel(const std::string& optarg);
305  int evalGrep( const std::string& optarg);
306  int evalKey( const std::string& optarg);
307  int evalRename(int opt, const std::string& optarg);
308  int evalAdjust(const std::string& optarg);
309  int evalYodAdjust(const Yod& yod, const std::string& optarg);
310  int evalPrint(const std::string& optarg);
311  int evalPrintFlags(const std::string& optarg);
312  int evalDelete(const std::string& optarg);
313  int evalExtract(const std::string& optarg);
314  int evalInsert(const std::string& optarg);
315  int evalModify(int opt, const std::string& optarg);
317 
318 public:
329  int getopt(int argc, char* const argv[]);
330 
332  virtual int option(int opt, const std::string& optarg, int optopt);
333 
335  virtual int nonoption(const std::string& argv);
336 
338  void usage(std::ostream& os =std::cout) const;
339 
341  void help(std::ostream& os =std::cout) const;
342 
344  void version(bool verbose =false, std::ostream& os =std::cout) const;
345 
347  static std::string printTarget(const std::string& before,int target,bool bPrint=false,std::ostream& os=std::cout);
348 
350  /*
351  stdin can be used by multiple images in the exiv2 command line:
352  For example: $ cat foo.icc | exiv2 -iC- a.jpg b.jpg c.jpg will modify the ICC profile in several images.
353  */
354  void getStdin(Exiv2::DataBuf& buf);
355 
356 }; // class Params
357 
358 #endif // #ifndef EXIV2APP_HPP_
bool formatSet_
Whether the format is set with -r.
Definition: exiv2app.hpp:241
bool preserve_
Preserve timestamps flag.
Definition: exiv2app.hpp:226
void help(std::ostream &os=std::cout) const
Print further usage explanations to an output stream.
Definition: exiv2.cpp:269
unsigned long printItems_
Print items.
Definition: exiv2app.hpp:232
PrintItem
Individual items to print, bitmap.
Definition: exiv2app.hpp:177
long adjustment_
Adjustment value.
Definition: exiv2app.hpp:217
virtual int nonoption(const std::string &argv)
Handle non-option parameters.
std::set< int > PreviewNumbers
Container for preview image numbers.
Definition: exiv2app.hpp:150
Keys keys_
List of keys to match from the metadata.
Definition: exiv2app.hpp:251
bool adjust_
Adjustment flag.
Definition: exiv2app.hpp:230
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:204
CmdId
Command identifiers.
Definition: exiv2app.hpp:70
std::string charset_
Charset to use for UNICODE Exif user comment.
Definition: exiv2app.hpp:252
YodAdjust yodAdjust_[3]
Year, month and day adjustment info.
Definition: exiv2app.hpp:239
Exiv2::TypeId typeId_
Exiv2 type identifier Flag to indicate if the type was explicitly specified (true) ...
Definition: exiv2app.hpp:90
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:130
bool help_
Help option flag.
Definition: exiv2app.hpp:220
Structure for one parsed modification command.
Definition: exiv2app.hpp:82
Files files_
List of non-option arguments.
Definition: exiv2app.hpp:248
unsigned long printTags_
Print tags (bitmap of MetadataId flags). Action (integer rather than TaskType to avoid dependency)...
Definition: exiv2app.hpp:233
Structure for year, month and day adjustment command line arguments.
Definition: exiv2app.hpp:214
std::vector< Exiv2_grep_key_t > exv_grep_keys_t
exv_grep_keys_t is a vector of keys to match to strings
Definition: version.hpp:67
std::string directory_
Location for files to extract/insert.
Definition: exiv2app.hpp:246
void getStdin(Exiv2::DataBuf &buf)
getStdin binary data read from stdin to DataBuf
A collection of utility functions.
std::vector< ModifyCmd > ModifyCmds
Container for modification commands.
Definition: exiv2app.hpp:96
std::string suffix_
File extension of the file to insert.
Definition: exiv2app.hpp:247
bool force_
Force overwrites flag.
Definition: exiv2app.hpp:223
Invalid type id.
Definition: types.hpp:157
Yod
Enumerates year, month and day adjustments.
Definition: exiv2app.hpp:211
const char * option_
Adjustment option string.
Definition: exiv2app.hpp:216
Type definitions for Exiv2 and related functionality.
std::string jpegComment_
Jpeg comment to set in the image.
Definition: exiv2app.hpp:245
MetadataId
Metadata identifiers.
Definition: exiv2app.hpp:75
static Params & instance()
Controls all access to the global Params instance.
Definition: exiv2.cpp:197
MetadataId metadataId_
Metadata identifier.
Definition: exiv2app.hpp:89
ModifyCmd()
C&#39;tor.
Definition: exiv2app.hpp:84
std::string cmdString_
Command string.
Definition: exiv2app.hpp:100
bool verbose_
Verbose (talkative) option flag.
Definition: exiv2app.hpp:222
int target_
What common target to process.
Definition: exiv2app.hpp:236
CmdId cmdId_
Command identifier.
Definition: exiv2app.hpp:87
Exiv2::DataBuf stdinBuf
DataBuf with the binary bytes from stdin.
Definition: exiv2app.hpp:254
Greps greps_
List of keys to &#39;grep&#39; from the metadata.
Definition: exiv2app.hpp:250
PrintMode printMode_
Print mode.
Definition: exiv2app.hpp:231
FileExistsPolicy fileExistsPolicy_
What to do if file to rename exists.
Definition: exiv2app.hpp:229
bool binary_
Suppress long binary values.
Definition: exiv2app.hpp:224
std::string format_
Filename format (-r option arg).
Definition: exiv2app.hpp:240
std::string key_
Exiv2 key string.
Definition: exiv2app.hpp:88
Implements the command line handling for the program.
Definition: exiv2app.hpp:138
std::vector< std::string > Keys
Container for keys.
Definition: exiv2app.hpp:154
IPTC string type.
Definition: types.hpp:147
virtual int option(int opt, const std::string &optarg, int optopt)
Handle options and their arguments.
CmdId cmdId_
Commands identifier.
Definition: exiv2app.hpp:99
std::vector< std::string > Files
Container to store filenames.
Definition: exiv2app.hpp:148
PrintMode
Enumerates print modes.
Definition: exiv2app.hpp:165
exv_grep_keys_t Greps
Container for greps.
Definition: exiv2app.hpp:152
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
std::string value_
Data.
Definition: exiv2app.hpp:93
bool unknown_
Suppress unknown tags.
Definition: exiv2app.hpp:225
void cleanup()
Destructor.
Definition: exiv2.cpp:213
void usage(std::ostream &os=std::cout) const
Print a minimal usage note to an output stream.
Definition: exiv2.cpp:243
bool version_
Version option flag.
Definition: exiv2app.hpp:221
bool timestampOnly_
Rename only sets the file timestamp.
Definition: exiv2app.hpp:228
void version(bool verbose=false, std::ostream &os=std::cout) const
Print version information to an output stream.
Definition: exiv2.cpp:219
static std::string printTarget(const std::string &before, int target, bool bPrint=false, std::ostream &os=std::cout)
Print target_.
Definition: exiv2.cpp:250
CommonTarget
Enumerates common targets, bitmap.
Definition: exiv2app.hpp:193
bool timestamp_
Rename also sets the file timestamp.
Definition: exiv2app.hpp:227
bool flag_
Adjustment flag.
Definition: exiv2app.hpp:215
CmdLines cmdLines_
Commands from the command line.
Definition: exiv2app.hpp:243
Structure to link command identifiers to strings.
Definition: exiv2app.hpp:98
PreviewNumbers previewNumbers_
List of preview numbers.
Definition: exiv2app.hpp:249
ModifyCmds modifyCmds_
Parsed modification commands.
Definition: exiv2app.hpp:244
long adjustment_
Adjustment in seconds.
Definition: exiv2app.hpp:238
Include all Exiv2 header files.
int getopt(int argc, char *const argv[])
Call Getopt::getopt() with optstring, to inititate command line argument parsing, perform consistency...
CmdFiles cmdFiles_
Names of the modification command files.
Definition: exiv2app.hpp:242
std::vector< std::string > CmdLines
Container for commands from the command line.
Definition: exiv2app.hpp:146
std::vector< std::string > CmdFiles
Container for command files.
Definition: exiv2app.hpp:144
FileExistsPolicy
Enumerates the policies to handle existing files in rename action.
Definition: exiv2app.hpp:208