-
Notifications
You must be signed in to change notification settings - Fork 282
/
exifdata.cpp
208 lines (173 loc) · 5.12 KB
/
exifdata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-License-Identifier: GPL-2.0-or-later
// Sample program to format exif data in various external formats
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <string>
using format_t = std::map<std::string, int>;
enum format_e { wolf, csv, json, xml };
void syntax(const char* argv[], format_t& formats) {
std::cout << "Usage: " << argv[0] << " file format" << '\n';
int count = 0;
std::cout << "formats: ";
for (auto&& format : formats) {
std::cout << (count++ ? " | " : "") << format.first;
}
std::cout << '\n';
}
size_t formatInit(Exiv2::ExifData& exifData) {
return std::distance(exifData.begin(), exifData.end());
}
///////////////////////////////////////////////////////////////////////
std::string escapeCSV(Exiv2::ExifData::const_iterator it, bool bValue) {
std::string result;
std::ostringstream os;
if (bValue)
os << it->value();
else
os << it->key();
std::string s = os.str();
for (auto&& c : s) {
if (c == ',')
result += '\\';
result += c;
}
return result;
}
std::string formatCSV(Exiv2::ExifData& exifData) {
size_t count = 0;
size_t length = formatInit(exifData);
std::ostringstream result;
for (auto i = exifData.begin(); count++ < length; ++i) {
result << escapeCSV(i, false) << (count != length ? ", " : "");
}
result << '\n';
count = 0;
for (auto i = exifData.begin(); count++ < length; ++i) {
result << escapeCSV(i, true) << (count != length ? ", " : "");
}
return result.str();
}
///////////////////////////////////////////////////////////////////////
std::string formatWolf(Exiv2::ExifData& exifData) {
size_t count = 0;
size_t length = formatInit(exifData);
std::ostringstream result;
result << "{ " << '\n';
for (auto i = exifData.begin(); count++ < length; ++i) {
result << " " << i->key() << " -> " << i->value() << (count != length ? "," : "") << '\n';
}
result << "}";
return result.str();
}
///////////////////////////////////////////////////////////////////////
std::string escapeJSON(Exiv2::ExifData::const_iterator it, bool bValue = true) {
std::string result;
std::ostringstream os;
if (bValue)
os << it->value();
else
os << it->key();
std::string s = os.str();
for (auto&& c : s) {
if (c == '"')
result += "\\\"";
result += c;
}
std::string q = "\"";
return q + result + q;
}
std::string formatJSON(Exiv2::ExifData& exifData) {
size_t count = 0;
size_t length = formatInit(exifData);
std::ostringstream result;
result << "{" << '\n';
for (auto i = exifData.begin(); count++ < length; ++i) {
result << " " << escapeJSON(i, false) << ":" << escapeJSON(i, true) << (count != length ? "," : "") << '\n';
}
result << "}";
return result.str();
}
///////////////////////////////////////////////////////////////////////
std::string escapeXML(Exiv2::ExifData::const_iterator it, bool bValue = true) {
std::string result;
std::ostringstream os;
if (bValue)
os << it->value();
else
os << it->key();
std::string s = os.str();
for (auto&& c : s) {
if (c == '<')
result += "≶";
if (c == '>')
result += ">";
result += c;
}
return result;
}
std::string formatXML(Exiv2::ExifData& exifData) {
size_t count = 0;
size_t length = formatInit(exifData);
std::ostringstream result;
result << "<exif>" << '\n';
for (auto i = exifData.begin(); count++ < length; ++i) {
std::string key = escapeXML(i, false);
std::string value = escapeXML(i, true);
result << " <" << key << ">" << value << "<" << key << "/>" << '\n';
}
result << "</exif>" << '\n';
return result.str();
}
///////////////////////////////////////////////////////////////////////
int main(int argc, const char* argv[]) {
Exiv2::XmpParser::initialize();
::atexit(Exiv2::XmpParser::terminate);
format_t formats;
formats["wolf"] = wolf;
formats["csv"] = csv;
formats["json"] = json;
formats["xml"] = xml;
int result = 0;
if (argc != 3) {
syntax(argv, formats);
result = 1;
}
const char* file = argv[1];
const char* format = argv[2];
if (!result && formats.find(format) == formats.end()) {
std::cout << "Unrecognised format " << format << '\n';
syntax(argv, formats);
result = 2;
}
if (!result)
try {
auto image = Exiv2::ImageFactory::open(file);
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
switch (formats.find(format)->second) {
case wolf:
std::cout << formatWolf(exifData) << '\n';
break;
case csv:
std::cout << formatCSV(exifData) << '\n';
break;
case json:
std::cout << formatJSON(exifData) << '\n';
break;
case xml:
std::cout << formatXML(exifData) << '\n';
break;
default:
std::cout << "*** error: format not implemented yet: " << format << " ***" << '\n';
result = 3;
break;
}
} catch (Exiv2::Error& e) {
std::cerr << "*** error exiv2 exception '" << e << "' ***" << '\n';
result = 4;
} catch (...) {
std::cerr << "*** error exception" << '\n';
result = 5;
}
return result;
}