-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.cpp
280 lines (246 loc) · 9.78 KB
/
timestamp.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <getopt.h>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include "color.h"
#include "exif_file.h"
namespace fs = std::filesystem;
// Display proposed file name changes
void display_proposed_changes(const std::vector<ExifFile>& files, bool first_display) {
if (!first_display) std::cout << std::endl;
std::cout << CYAN << "Files to rename:" << RESET << std::endl;
for (size_t i = files.size(); i > 0; --i) {
std::string current_name = files[i-1].get_path().filename().string();
std::string proposed_name = files[i-1].get_proposed_name();
std::cout << i << "\t" << current_name << " -> ";
if (files[i-1].is_skipped()) {
std::cout << current_name;
}
else {
std::cout << proposed_name;
}
if (files[i-1].is_clashing()) std::cout << RED << " (clashing)" << RESET;
if (files[i-1].is_skipped()) std::cout << YELLOW << " (skipped)" << RESET << std::endl;
else if (current_name == proposed_name) std::cout << GRAY << " (no change)" << RESET << std::endl;
else std::cout << std::endl;
}
}
// Check for any clashes
bool has_clashes(const std::shared_ptr<std::map<std::string, int>>& name_count) {
for (const auto& entry : *name_count) {
if (entry.second > 1) {
return true; // Found a clash
}
}
return false; // No clashes found
}
// Rename files
void rename_files(std::vector<ExifFile>& files) {
int skip_count = 0;
for (auto file : files) {
if (!file.rename()) skip_count++;
}
int rename_count = files.size() - skip_count;
std::cout << CYAN
<< "Renamed " << rename_count << " "
<< (rename_count == 1 ? "file" : "files") << ". "
<< "Skipped " << skip_count << " "
<< (skip_count == 1 ? "file" : "files") << "."
<< RESET
<< std::endl;
}
// Print help menu
void print_help() {
std::cout << "Usage: timestamp [directory] [-f|--force] [-e|--exif <tag>] [-x|--xmp <tag>] [-d|--date-format <format>] [-i|--interactive] [-h|--help]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -f, --force Force execution (will delete clashing files, not recommended)" << std::endl;
std::cout << " -e, --exif <tag> Specify EXIF metadata date tag" << std::endl;
std::cout << " -x, --xmp <tag> Specify XMP metadata date tag" << std::endl;
std::cout << " -d, --date-format <format> Specify date format for renamed files" << std::endl;
std::cout << " -i, --interactive Enable interactive mode" << std::endl;
std::cout << " -h, --help Show this help message" << std::endl;
}
int main(int argc, char* argv[]) {
// Variables to hold the argument values
std::string directory = ".";
std::string exif_date_tag = "Exif.Photo.DateTimeOriginal"; // Default value
std::string xmp_date_tag = "Xmp.video.ModificationDate"; // Default value
std::string date_format = "%Y-%m-%d-%H%M-%S"; // Default value
bool force = false;
bool interactive = false;
// Option structure for getopt_long
static struct option long_options[] = {
{"force", no_argument, 0, 'f' },
{"exif", required_argument, 0, 'e' },
{"xmp", required_argument, 0, 'x' },
{"date-format", required_argument, 0, 'd' },
{"interactive", no_argument, 0, 'i' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
int opt;
int option_index = 0;
opterr = 0;
// Loop to parse command-line arguments
while ((opt = getopt_long(argc, argv, "fe:x:d:ih", long_options, &option_index)) != -1) {
switch (opt) {
case 'f':
force = true;
break;
case 'e':
exif_date_tag = optarg;
// TODO: validate
break;
case 'x':
xmp_date_tag = optarg;
// TODO: validate
break;
case 'd':
date_format = optarg;
// TODO: validate
break;
case 'i':
interactive = true;
break;
case 'h':
print_help();
return 0;
default:
std::cerr << RED << "[ERROR] " << RESET << "Invalid option. Use -h or --help for usage" << std::endl;
return 1;
}
}
// Handle any positional argument as directory
for (int i = optind; i < argc; ++i) {
if (argv[i][0] != '-') {
directory = argv[i]; // Set the first positional argument as directory
}
else {
std::cerr << RED << "[ERROR] " << RESET << "Unexpected option or argument: " << argv[i] << ". Use -h or --help for usage" << std::endl;
return 1;
}
}
if (force && interactive) {
std::cerr << YELLOW << "[WARNING] " << RESET << "-f or --force has no effect in interactive mode" << std::endl;
force = false;
}
std::vector<ExifFile> files;
auto proposed_name_counts_ptr = std::make_shared<std::map<std::string, int>>();
// Collect files from the specified directory
for (const auto& entry : fs::directory_iterator(directory)) {
if (fs::is_regular_file(entry)) {
ExifFile file = ExifFile(entry.path(), proposed_name_counts_ptr, exif_date_tag, xmp_date_tag, date_format);
// Ignore files without valid EXIF dates
if (!file.is_skipped()) {
files.push_back(file);
}
else {
std::cerr << YELLOW << "[Warning] " << RESET << "Ignoring file without valid date " << file.get_path().filename().string() << std::endl;
}
}
}
// Check if no files found
if (files.empty()) {
std::cout << CYAN << "No files found in the specified directory." << RESET << std::endl;
return 0;
}
// Sort by filename in reverse alphabetical order (will be shown in alphabetical order)
std::sort(files.begin(), files.end(), [](const ExifFile& a, const ExifFile& b) {
return a.get_path() > b.get_path(); // Reverse order based on paths
});
bool first_loop = true;
bool show_clash_error = false;
while(true) {
display_proposed_changes(files, first_loop);
first_loop = false;
// Only show clash error (or add newline) in interactive mode
if (interactive) {
std::cout << std::endl;
if (show_clash_error) {
std::cerr << RED << "[Error]: " << RESET << "Please resolve clashes before continuing" << std::endl;
show_clash_error = false;
}
}
// Only ask to edit operations in interactive mode
std::string input;
if (interactive) {
std::cout << "Operations to edit (e.g., '1 2 3', '1-3', '^4'): ";
std::getline(std::cin, input);
}
// Exit the loop if no options are selected (or if not in interactive mode)
if (input.empty()) {
// If there are clashes
if (has_clashes(proposed_name_counts_ptr)) {
// In interactive mode, give the user the opportunity to fix clashes
if (interactive) {
show_clash_error = true;
continue;
}
// Otherwise, unless force, abort
else if (!force) {
std::cout << CYAN << "Clashes detected, aborting. Use -f or --force to ignore clashes." << RESET << std::endl;
return 0;
}
else {
std::cerr << YELLOW << "[WARNING] " << RESET << "At least once clashing file has been deleted" << std::endl;
}
}
break;
}
std::set<size_t> selected_files; // Use a set to store unique selections
std::istringstream stream(input);
std::string token;
while (stream >> token) {
// Handle range selection
if (token.front() == '^') {
size_t start = std::stoi(token.substr(1));
for (size_t i = start; i < files.size(); ++i) {
selected_files.insert(i + 1);
}
}
// Handle ranges
else if (token.find('-') != std::string::npos) {
auto range = token.find('-');
size_t start = std::stoi(token.substr(0, range));
size_t end = std::stoi(token.substr(range + 1));
for (size_t i = start; i <= end; ++i) {
selected_files.insert(i);
}
}
// Handle individual selections
else {
selected_files.insert(std::stoi(token));
}
}
// Apply the edits for selected files
std::vector<ExifFile> files_to_edit;
for (const auto& index : selected_files) {
if (index > 0 && index <= files.size()) {
files[index - 1].edit_proposed_name();
}
}
std::cout << std::endl;
}
// If force, just do it
if (force) {
std::cout << std::endl; // Formatting
rename_files(files);
}
// Else confirm renaming
else {
std::string confirm;
std::cout << "\nRename files? (y/N): ";
std::getline(std::cin, confirm);
if (confirm == "y" || confirm == "Y") {
rename_files(files);
}
else {
std::cout << CYAN << "Operation aborted." << RESET << std::endl;
}
}
return 0;
}