Skip to content

Commit 9515dfb

Browse files
norbertwgkmilos
authored andcommitted
fix compile error Ubuntu and MacOS;
optimise regex and remove duplicate call of ImageFactory
1 parent b0635be commit 9515dfb

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

app/actions.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int metacopy(const std::string& source, const std::string& tgt, Exiv2::ImageType
9898
the file to.
9999
@return 0 if successful, -1 if the file was skipped, 1 on error.
100100
*/
101-
int renameFile(std::string& path, const tm* tm);
101+
int renameFile(std::string& path, const tm* tm, Exiv2::ExifData& exifData);
102102

103103
/*!
104104
@brief Make a file path from the current file path, destination
@@ -643,7 +643,7 @@ int Rename::run(const std::string& path) {
643643
std::cout << _("Updating timestamp to") << " " << v << '\n';
644644
}
645645
} else {
646-
rc = renameFile(newPath, &tm);
646+
rc = renameFile(newPath, &tm, exifData);
647647
if (rc == -1)
648648
return 0; // skip
649649
}
@@ -1806,7 +1806,7 @@ void replace(std::string& text, const std::string& searchText, const std::string
18061806
}
18071807
}
18081808

1809-
int renameFile(std::string& newPath, const tm* tm) {
1809+
int renameFile(std::string& newPath, const tm* tm, Exiv2::ExifData& exifData) {
18101810
auto p = fs::path(newPath);
18111811
std::string path = newPath;
18121812
auto oldFsPath = fs::path(path);
@@ -1833,8 +1833,7 @@ int renameFile(std::string& newPath, const tm* tm) {
18331833
// rename using exiv2 tags
18341834
// is done after calling setting date/time: the value retrieved from tag might include something like %Y, which then
18351835
// should not be replaced by year
1836-
Exiv2::Image::UniquePtr image;
1837-
std::regex format_regex(":{1}?(Exif\\..*?):{1}?");
1836+
std::regex format_regex(":{1}?(.*?):{1}?");
18381837
#if defined(_WIN32)
18391838
std::string illegalChars = "\\/:*?\"<>|";
18401839
#elif defined(__APPLE__)
@@ -1846,21 +1845,12 @@ int renameFile(std::string& newPath, const tm* tm) {
18461845
std::regex_token_iterator<std::string::iterator> token(format.begin(), format.end(), format_regex);
18471846
while (token != rend) {
18481847
std::string tag = token->str().substr(1, token->str().length() - 2);
1849-
if (image == 0) {
1850-
image = Exiv2::ImageFactory::open(path);
1851-
image->readMetadata();
1852-
if (image->exifData().empty()) {
1853-
std::string error("No Exif data found in file");
1854-
throw Exiv2::Error(Exiv2::ErrorCode::kerErrorMessage, error);
1855-
}
1856-
}
1857-
Exiv2::ExifData& exifData = image->exifData();
1858-
const auto key = exifData.findKey(Exiv2::ExifKey::ExifKey(tag));
1848+
const auto key = exifData.findKey(Exiv2::ExifKey(tag));
18591849
std::string val = "";
18601850
if (key != exifData.end()) {
18611851
val = key->print(&exifData);
18621852
if (val.length() == 0) {
1863-
std::cerr << _("Warning: ") << tag << _(" is empty.") << std::endl;
1853+
std::cerr << path << ": " << _("Warning: ") << tag << _(" is empty.") << std::endl;
18641854
} else {
18651855
// replace characters invalid in file name
18661856
for (std::string::iterator it = val.begin(); it < val.end(); ++it) {
@@ -1871,7 +1861,7 @@ int renameFile(std::string& newPath, const tm* tm) {
18711861
}
18721862
}
18731863
} else {
1874-
std::cerr << _("Warning: ") << tag << _(" is not included.") << std::endl;
1864+
std::cerr << path << ": " << _("Warning: ") << tag << _(" is not included.") << std::endl;
18751865
}
18761866
replace(newPath, *token++, val);
18771867
}

tests/bash_tests/test_rename.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Rename_TagNotIncluded(metaclass=CaseMeta):
148148
Renaming file to $outfilename
149149
"""
150150
]
151-
stderr = ["""Warning: Exif.Image.ImageDescription is not included.
151+
stderr = ["""$infilename: Warning: Exif.Image.ImageDescription is not included.
152152
"""]
153153
retval = [0] * len(commands)
154154

0 commit comments

Comments
 (0)