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