From 4ceb325c8f36265ffbb7f19769882c75383ebc29 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 26 Apr 2021 17:01:09 -0700 Subject: [PATCH] clang-tidy: range for loop conversions Found with modernize-loop-convert Ran through git clang-format. Also removed several questionable loops and replaced with simpler algorithms. Signed-off-by: Rosen Penev --- samples/easyaccess-test.cpp | 6 +- samples/exifdata.cpp | 52 +++++++------- samples/geotag.cpp | 3 +- samples/prevtest.cpp | 14 ++-- samples/stringto-test.cpp | 45 +++++++----- samples/xmpparse.cpp | 16 ++--- samples/xmpparser-test.cpp | 16 ++--- samples/xmpprint.cpp | 17 ++--- samples/xmpsample.cpp | 16 ++--- src/actions.cpp | 39 +++++------ src/convert.cpp | 21 +++--- src/cr2image.cpp | 7 +- src/datasets.cpp | 4 +- src/epsimage.cpp | 51 +++++++------- src/exif.cpp | 38 +++++------ src/exiv2.cpp | 108 +++++++++++++++++------------ src/futils.cpp | 24 ++++--- src/http.cpp | 12 ++-- src/iptc.cpp | 4 +- src/jpgimage.cpp | 11 +-- src/makernote_int.cpp | 4 +- src/orfimage.cpp | 7 +- src/pngimage.cpp | 8 +-- src/preview.cpp | 18 ++--- src/properties.cpp | 10 +-- src/rw2image.cpp | 15 ++-- src/tags_int.cpp | 6 +- src/tiffcomposite_int.cpp | 133 ++++++++++++++++++------------------ src/tiffimage.cpp | 11 ++- src/tiffimage_int.cpp | 10 +-- src/tiffvisitor_int.cpp | 4 +- src/tiffvisitor_int.hpp | 3 +- src/version.cpp | 6 +- src/xmp.cpp | 73 +++++++++----------- src/xmpsidecar.cpp | 32 ++++----- 35 files changed, 406 insertions(+), 438 deletions(-) diff --git a/samples/easyaccess-test.cpp b/samples/easyaccess-test.cpp index 82266bde..a27239f5 100644 --- a/samples/easyaccess-test.cpp +++ b/samples/easyaccess-test.cpp @@ -87,9 +87,9 @@ try { image->readMetadata(); Exiv2::ExifData& ed = image->exifData(); - for (unsigned int i = 0; i < EXV_COUNTOF(easyAccess); ++i) { - auto pos = easyAccess[i].findFct_(ed); - std::cout << std::setw(21) << std::left << easyAccess[i].label_; + for (auto&& ea : easyAccess) { + auto pos = ea.findFct_(ed); + std::cout << std::setw(21) << std::left << ea.label_; if (pos != ed.end()) { std::cout << " (" << std::setw(35) << pos->key() << ") : " << pos->print(&ed) << "\n"; diff --git a/samples/exifdata.cpp b/samples/exifdata.cpp index e5538969..423d5743 100644 --- a/samples/exifdata.cpp +++ b/samples/exifdata.cpp @@ -35,19 +35,15 @@ void syntax(const char* argv[],format_t& formats) std::cout << "Usage: " << argv[0] << " file format" << std::endl; int count = 0; std::cout << "formats: "; - for ( format_i i = formats.begin() ; i != formats.end() ; i++ ) { - std::cout << ( count++ ? " | " : "") << i->first ; - } - std::cout << std::endl; + for (auto&& format : formats) { + std::cout << (count++ ? " | " : "") << format.first; + } + std::cout << std::endl; } size_t formatInit(Exiv2::ExifData& exifData) { - size_t result = 0; - for (auto i = exifData.begin(); i != exifData.end() ; ++i) { - result ++ ; - } - return result ; + return std::distance(exifData.begin(), exifData.end()); } /////////////////////////////////////////////////////////////////////// @@ -59,12 +55,13 @@ std::string escapeCSV(Exiv2::ExifData::const_iterator it,bool bValue) if ( bValue ) os << it->value() ; else os << it->key() ; std::string s = os.str(); - for ( size_t i = 0 ;i < s.length() ; i ++ ) { - if ( s[i] == ',' ) result += '\\'; - result += s[i]; - } + for (auto&& c : s) { + if (c == ',') + result += '\\'; + result += c; + } - return result ; + return result; } std::string formatCSV(Exiv2::ExifData& exifData) @@ -109,13 +106,14 @@ std::string escapeJSON(Exiv2::ExifData::const_iterator it,bool bValue=true) if ( bValue ) os << it->value() ; else os << it->key() ; std::string s = os.str(); - for ( size_t i = 0 ;i < s.length() ; i ++ ) { - if ( s[i] == '"' ) result += "\\\""; - result += s[i]; - } + for (auto&& c : s) { + if (c == '"') + result += "\\\""; + result += c; + } - std::string q = "\""; - return q + result + q ; + std::string q = "\""; + return q + result + q; } std::string formatJSON(Exiv2::ExifData& exifData) @@ -141,13 +139,15 @@ std::string escapeXML(Exiv2::ExifData::const_iterator it,bool bValue=true) if ( bValue ) os << it->value() ; else os << it->key() ; std::string s = os.str(); - for ( size_t i = 0 ;i < s.length() ; i ++ ) { - if ( s[i] == '<' ) result += "≶"; - if ( s[i] == '>' ) result += ">"; - result += s[i]; - } + for (auto&& c : s) { + if (c == '<') + result += "≶"; + if (c == '>') + result += ">"; + result += c; + } - return result ; + return result; } std::string formatXML(Exiv2::ExifData& exifData) diff --git a/samples/geotag.cpp b/samples/geotag.cpp index e613d82a..b5ba9bc5 100644 --- a/samples/geotag.cpp +++ b/samples/geotag.cpp @@ -907,8 +907,7 @@ int main(int argc,const char* argv[]) } } */ - for ( size_t p = 0 ; p < gFiles.size() ; p++ ) { - std::string path = gFiles[p] ; + for (auto&& path : gFiles) { std::string stamp ; try { time_t t = readImageTime(path,&stamp) ; diff --git a/samples/prevtest.cpp b/samples/prevtest.cpp index d4b65f7c..29e5b889 100644 --- a/samples/prevtest.cpp +++ b/samples/prevtest.cpp @@ -45,17 +45,13 @@ try { Exiv2::PreviewManager loader(*image); Exiv2::PreviewPropertiesList list = loader.getPreviewProperties(); - for (Exiv2::PreviewPropertiesList::iterator pos = list.begin(); pos != list.end(); pos++) { - std::cout << pos->mimeType_ - << " preview, type " << pos->id_ << ", " - << pos->size_ << " bytes, " - << pos->width_ << 'x' << pos->height_ << " pixels" + for (auto&& pos : list) { + std::cout << pos.mimeType_ << " preview, type " << pos.id_ << ", " << pos.size_ << " bytes, " << pos.width_ + << 'x' << pos.height_ << " pixels" << "\n"; - Exiv2::PreviewImage preview = loader.getPreviewImage(*pos); - preview.writeFile(filename + "_" - + Exiv2::toString(pos->width_) + "x" - + Exiv2::toString(pos->height_)); + Exiv2::PreviewImage preview = loader.getPreviewImage(pos); + preview.writeFile(filename + "_" + Exiv2::toString(pos.width_) + "x" + Exiv2::toString(pos.height_)); } // Cleanup diff --git a/samples/stringto-test.cpp b/samples/stringto-test.cpp index 898a6558..da334df9 100644 --- a/samples/stringto-test.cpp +++ b/samples/stringto-test.cpp @@ -72,28 +72,37 @@ int main() std::cout << std::endl; - for (unsigned int i = 0; i < EXV_COUNTOF(testcases); ++i) try { - std::string s(testcases[i]); - std::cout << std::setw(12) << std::left << s; - bool ok; + for (auto&& testcase : testcases) { + try { + std::string s(testcase); + std::cout << std::setw(12) << std::left << s; + bool ok; - long l = Exiv2::parseLong(s, ok); - std::cout << std::setw(12) << std::left; - if (ok) std::cout << l; else std::cout << "nok"; + long l = Exiv2::parseLong(s, ok); + std::cout << std::setw(12) << std::left; + if (ok) + std::cout << l; + else + std::cout << "nok"; - float f = Exiv2::parseFloat(s, ok); - std::cout << std::setw(12) << std::left; - if (ok) std::cout << f; else std::cout << "nok"; + float f = Exiv2::parseFloat(s, ok); + std::cout << std::setw(12) << std::left; + if (ok) + std::cout << f; + else + std::cout << "nok"; - Exiv2::Rational r = Exiv2::parseRational(s, ok); - if (ok) std::cout << r.first << "/" << r.second; - else std::cout << "nok"; + Exiv2::Rational r = Exiv2::parseRational(s, ok); + if (ok) + std::cout << r.first << "/" << r.second; + else + std::cout << "nok"; - std::cout << std::endl; - } - catch (Exiv2::AnyError& e) { - std::cout << "Caught Exiv2 exception '" << e << "'\n"; - return -1; + std::cout << std::endl; + } catch (Exiv2::AnyError& e) { + std::cout << "Caught Exiv2 exception '" << e << "'\n"; + return -1; + } } return 0; diff --git a/samples/xmpparse.cpp b/samples/xmpparse.cpp index 13b181b8..136fffbb 100644 --- a/samples/xmpparse.cpp +++ b/samples/xmpparse.cpp @@ -51,18 +51,10 @@ try { error += ": No XMP properties found in the XMP packet"; throw Exiv2::Error(Exiv2::kerErrorMessage, error); } - for (Exiv2::XmpData::const_iterator md = xmpData.begin(); - md != xmpData.end(); ++md) { - std::cout << std::setfill(' ') << std::left - << std::setw(44) - << md->key() << " " - << std::setw(9) << std::setfill(' ') << std::left - << md->typeName() << " " - << std::dec << std::setw(3) - << std::setfill(' ') << std::right - << md->count() << " " - << std::dec << md->toString() - << std::endl; + for (auto&& md : xmpData) { + std::cout << std::setfill(' ') << std::left << std::setw(44) << md.key() << " " << std::setw(9) + << std::setfill(' ') << std::left << md.typeName() << " " << std::dec << std::setw(3) + << std::setfill(' ') << std::right << md.count() << " " << std::dec << md.toString() << std::endl; } Exiv2::XmpParser::terminate(); return 0; diff --git a/samples/xmpparser-test.cpp b/samples/xmpparser-test.cpp index 1028a14e..9145866b 100644 --- a/samples/xmpparser-test.cpp +++ b/samples/xmpparser-test.cpp @@ -53,18 +53,10 @@ try { error += ": No XMP properties found in the XMP packet"; throw Exiv2::Error(Exiv2::kerErrorMessage, error); } - for (Exiv2::XmpData::const_iterator md = xmpData.begin(); - md != xmpData.end(); ++md) { - std::cout << std::setfill(' ') << std::left - << std::setw(44) - << md->key() << " " - << std::setw(9) << std::setfill(' ') << std::left - << md->typeName() << " " - << std::dec << std::setw(3) - << std::setfill(' ') << std::right - << md->count() << " " - << std::dec << md->toString() - << std::endl; + for (auto&& md : xmpData) { + std::cout << std::setfill(' ') << std::left << std::setw(44) << md.key() << " " << std::setw(9) + << std::setfill(' ') << std::left << md.typeName() << " " << std::dec << std::setw(3) + << std::setfill(' ') << std::right << md.count() << " " << std::dec << md.toString() << std::endl; } filename += "-new"; std::cerr << "-----> Encoding XMP data to write to " << filename << " <-----\n"; diff --git a/samples/xmpprint.cpp b/samples/xmpprint.cpp index 8df3d954..7b160d46 100644 --- a/samples/xmpprint.cpp +++ b/samples/xmpprint.cpp @@ -65,19 +65,10 @@ int main(int argc, char** argv) throw Exiv2::Error(Exiv2::kerErrorMessage, error); } - for (Exiv2::XmpData::const_iterator md = xmpData.begin(); - md != xmpData.end(); ++md) - { - std::cout << std::setfill(' ') << std::left - << std::setw(44) - << md->key() << " " - << std::setw(9) << std::setfill(' ') << std::left - << md->typeName() << " " - << std::dec << std::setw(3) - << std::setfill(' ') << std::right - << md->count() << " " - << std::dec << md->toString() - << std::endl; + for (auto&& md : xmpData) { + std::cout << std::setfill(' ') << std::left << std::setw(44) << md.key() << " " << std::setw(9) + << std::setfill(' ') << std::left << md.typeName() << " " << std::dec << std::setw(3) + << std::setfill(' ') << std::right << md.count() << " " << std::dec << md.toString() << std::endl; } Exiv2::XmpParser::terminate(); diff --git a/samples/xmpsample.cpp b/samples/xmpsample.cpp index 1da5cc56..5bfbf67d 100644 --- a/samples/xmpsample.cpp +++ b/samples/xmpsample.cpp @@ -214,18 +214,10 @@ try { // ------------------------------------------------------------------------- // Output XMP properties - for (Exiv2::XmpData::const_iterator md = xmpData.begin(); - md != xmpData.end(); ++md) { - std::cout << std::setfill(' ') << std::left - << std::setw(44) - << md->key() << " " - << std::setw(9) << std::setfill(' ') << std::left - << md->typeName() << " " - << std::dec << std::setw(3) - << std::setfill(' ') << std::right - << md->count() << " " - << std::dec << md->value() - << std::endl; + for (auto &&md : xmpData) { + std::cout << std::setfill(' ') << std::left << std::setw(44) << md.key() << " " << std::setw(9) + << std::setfill(' ') << std::left << md.typeName() << " " << std::dec << std::setw(3) + << std::setfill(' ') << std::right << md.count() << " " << std::dec << md.value() << std::endl; } // ------------------------------------------------------------------------- diff --git a/src/actions.cpp b/src/actions.cpp index 5a8120f0..ebda591d 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -184,8 +184,8 @@ namespace Action { void TaskFactory::cleanup() { if (instance_ != 0) { - for (auto i = registry_.begin(); i != registry_.end(); ++i) { - delete i->second; + for (auto&& i : registry_) { + delete i.second; } delete instance_; instance_ = 0; @@ -459,8 +459,8 @@ namespace Action { bool noExif = false; if (Params::instance().printTags_ & Exiv2::mdExif) { const Exiv2::ExifData& exifData = image->exifData(); - for (auto md = exifData.begin(); md != exifData.end(); ++md) { - ret |= printMetadatum(*md, image); + for (auto&& md : exifData) { + ret |= printMetadatum(md, image); } if (exifData.empty()) noExif = true; } @@ -468,8 +468,8 @@ namespace Action { bool noIptc = false; if (Params::instance().printTags_ & Exiv2::mdIptc) { const Exiv2::IptcData& iptcData = image->iptcData(); - for (auto md = iptcData.begin(); md != iptcData.end(); ++md) { - ret |= printMetadatum(*md, image); + for (auto&& md : iptcData) { + ret |= printMetadatum(md, image); } if (iptcData.empty()) noIptc = true; } @@ -477,8 +477,8 @@ namespace Action { bool noXmp = false; if (Params::instance().printTags_ & Exiv2::mdXmp) { const Exiv2::XmpData& xmpData = image->xmpData(); - for (auto md = xmpData.begin(); md != xmpData.end(); ++md) { - ret |= printMetadatum(*md, image); + for (auto&& md : xmpData) { + ret |= printMetadatum(md, image); } if (xmpData.empty()) noXmp = true; } @@ -679,18 +679,16 @@ namespace Action { int cnt = 0; Exiv2::PreviewManager pm(*image); Exiv2::PreviewPropertiesList list = pm.getPreviewProperties(); - for (auto pos = list.begin(); pos != list.end(); ++pos) { + for (auto&& pos : list) { if (manyFiles) { std::cout << std::setfill(' ') << std::left << std::setw(20) << path_ << " "; } - std::cout << _("Preview") << " " << ++cnt << ": " - << pos->mimeType_ << ", "; - if (pos->width_ != 0 && pos->height_ != 0) { - std::cout << pos->width_ << "x" << pos->height_ << " " - << _("pixels") << ", "; + std::cout << _("Preview") << " " << ++cnt << ": " << pos.mimeType_ << ", "; + if (pos.width_ != 0 && pos.height_ != 0) { + std::cout << pos.width_ << "x" << pos.height_ << " " << _("pixels") << ", "; } - std::cout << pos->size_ << " " << _("bytes") << "\n"; + std::cout << pos.size_ << " " << _("bytes") << "\n"; } return 0; } // Print::printPreviewList @@ -1024,20 +1022,19 @@ namespace Action { Exiv2::PreviewPropertiesList pvList = pvMgr.getPreviewProperties(); const Params::PreviewNumbers& numbers = Params::instance().previewNumbers_; - for (auto n = numbers.begin(); n != numbers.end(); ++n) { - if (*n == 0) { + for (auto&& number : numbers) { + if (number == 0) { // Write all previews for (int num = 0; num < static_cast(pvList.size()); ++num) { writePreviewFile(pvMgr.getPreviewImage(pvList[num]), num + 1); } break; } - if (*n > static_cast(pvList.size())) { - std::cerr << path_ << ": " << _("Image does not have preview") - << " " << *n << "\n"; + if (number > static_cast(pvList.size())) { + std::cerr << path_ << ": " << _("Image does not have preview") << " " << number << "\n"; continue; } - writePreviewFile(pvMgr.getPreviewImage(pvList[*n - 1]), *n); + writePreviewFile(pvMgr.getPreviewImage(pvList[number - 1]), number); } return 0; } // Extract::writePreviews diff --git a/src/convert.cpp b/src/convert.cpp index cdfe0a26..67b0e355 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -453,8 +453,7 @@ namespace Exiv2 { void Converter::cnvToXmp() { - for (unsigned int i = 0; i < EXV_COUNTOF(conversion_); ++i) { - const Conversion& c = conversion_[i]; + for (auto&& c : conversion_) { if ( (c.metadataId_ == mdExif && exifData_) || (c.metadataId_ == mdIptc && iptcData_)) { EXV_CALL_MEMBER_FN(*this, c.key1ToKey2_)(c.key1_, c.key2_); @@ -464,8 +463,7 @@ namespace Exiv2 { void Converter::cnvFromXmp() { - for (unsigned int i = 0; i < EXV_COUNTOF(conversion_); ++i) { - const Conversion& c = conversion_[i]; + for (auto&& c : conversion_) { if ( (c.metadataId_ == mdExif && exifData_) || (c.metadataId_ == mdIptc && iptcData_)) { EXV_CALL_MEMBER_FN(*this, c.key2ToKey1_)(c.key2_, c.key1_); @@ -972,9 +970,7 @@ namespace Exiv2 { return; } - for (unsigned i = 0; i < value.length(); ++i) { - if (value[i] == '.') value[i] = ' '; - } + std::replace(value.begin(), value.end(), '.', ' '); (*exifData_)[to] = value; if (erase_) xmpData_->erase(pos); @@ -1179,8 +1175,7 @@ namespace Exiv2 { unsigned char digest[16]; MD5Init ( &context ); - for (unsigned int i = 0; i < EXV_COUNTOF(conversion_); ++i) { - const Conversion& c = conversion_[i]; + for (auto&& c : conversion_) { if (c.metadataId_ == mdExif) { Exiv2::ExifKey key(c.key1_); if (tiff && key.groupName() != "Image") continue; @@ -1198,8 +1193,8 @@ namespace Exiv2 { MD5Final(digest, &context); res << ';'; res << std::setw(2) << std::setfill('0') << std::hex << std::uppercase; - for (int i = 0; i < 16; ++i) { - res << static_cast(digest[i]); + for (auto&& i : digest) { + res << static_cast(i); } return res.str(); } @@ -1270,8 +1265,8 @@ namespace Exiv2 { MD5Update(&context, data.pData_, data.size_); MD5Final(digest, &context); res << std::setw(2) << std::setfill('0') << std::hex << std::uppercase; - for (int i = 0; i < 16; ++i) { - res << static_cast(digest[i]); + for (auto&& i : digest) { + res << static_cast(i); } return res.str(); #else diff --git a/src/cr2image.cpp b/src/cr2image.cpp index ccf4a5f3..dfcf580c 100644 --- a/src/cr2image.cpp +++ b/src/cr2image.cpp @@ -173,14 +173,11 @@ namespace Exiv2 { static const IfdId filteredIfds[] = { panaRawId }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredIfds); ++i) { + for (auto&& filteredIfd : filteredIfds) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif IFD " << filteredIfds[i] << " not encoded\n"; #endif - ed.erase(std::remove_if(ed.begin(), - ed.end(), - FindExifdatum(filteredIfds[i])), - ed.end()); + ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); } std::unique_ptr header(new Cr2Header(byteOrder)); diff --git a/src/datasets.cpp b/src/datasets.cpp index d9722093..96f50054 100644 --- a/src/datasets.cpp +++ b/src/datasets.cpp @@ -574,9 +574,7 @@ namespace Exiv2 { void IptcDataSets::dataSetList(std::ostream& os) { - const int count = sizeof(records_)/sizeof(records_[0]); - for (int i=0; i < count; ++i) { - const DataSet *record = records_[i]; + for (auto&& record : records_) { for (int j=0; record != 0 && record[j].number_ != 0xffff; ++j) { os << record[j] << "\n"; } diff --git a/src/epsimage.cpp b/src/epsimage.cpp index ba9cd955..898c3f44 100644 --- a/src/epsimage.cpp +++ b/src/epsimage.cpp @@ -36,6 +36,7 @@ // + standard includes #include +#include #include #include #include @@ -53,10 +54,10 @@ namespace { const std::string dosEpsSignature = "\xC5\xD0\xD3\xC6"; // first line of EPS - const std::string epsFirstLine[] = { + const std::array epsFirstLine{ "%!PS-Adobe-3.0 EPSF-3.0", - "%!PS-Adobe-3.0 EPSF-3.0 ", // OpenOffice - "%!PS-Adobe-3.1 EPSF-3.0", // Illustrator + "%!PS-Adobe-3.0 EPSF-3.0 ", // OpenOffice + "%!PS-Adobe-3.1 EPSF-3.0", // Illustrator }; // blank EPS file @@ -197,8 +198,7 @@ namespace { xmpSize = 0; for (xmpPos = startPos; xmpPos < size; xmpPos++) { if (data[xmpPos] != '\x00' && data[xmpPos] != '<') continue; - for (size_t i = 0; i < (sizeof xmpHeaders) / (sizeof *xmpHeaders); i++) { - const std::string &header = xmpHeaders[i]; + for (auto&& header : xmpHeaders) { if (xmpPos + header.size() > size) continue; if (memcmp(data + xmpPos, header.data(), header.size()) != 0) continue; #ifdef DEBUG @@ -208,9 +208,9 @@ namespace { // search for valid XMP trailer for (size_t trailerPos = xmpPos + header.size(); trailerPos < size; trailerPos++) { if (data[xmpPos] != '\x00' && data[xmpPos] != '<') continue; - for (size_t j = 0; j < (sizeof xmpTrailers) / (sizeof *xmpTrailers); j++) { - const std::string &trailer = xmpTrailers[j].trailer; - const bool readOnly = xmpTrailers[j].readOnly; + for (auto&& xmpTrailer : xmpTrailers) { + const std::string& trailer = xmpTrailer.trailer; + const bool readOnly = xmpTrailer.readOnly; if (trailerPos + trailer.size() > size) continue; if (memcmp(data + trailerPos, trailer.data(), trailer.size()) != 0) continue; @@ -335,10 +335,7 @@ namespace { #ifdef DEBUG EXV_DEBUG << "readWriteEpsMetadata: First line: " << firstLine << "\n"; #endif - bool matched = false; - for (size_t i = 0; !matched && i < (sizeof epsFirstLine) / (sizeof *epsFirstLine); i++) { - matched = (firstLine == epsFirstLine[i]); - } + bool matched = std::find(epsFirstLine.begin(), epsFirstLine.end(), firstLine) != epsFirstLine.end(); if (!matched) { throw Error(kerNotAnImage, "EPS"); } @@ -704,8 +701,8 @@ namespace { findXmp(posOtherXmp, sizeOtherXmp, data, posOtherXmp + sizeOtherXmp, posEndPageSetup, write); if (posOtherXmp >= posEndPageSetup) break; bool isRemovableEmbedding = false; - for (std::vector >::const_iterator e = removableEmbeddings.begin(); e != removableEmbeddings.end(); ++e) { - if (e->first <= posOtherXmp && posOtherXmp < e->second) { + for (auto&& removableEmbedding : removableEmbeddings) { + if (removableEmbedding.first <= posOtherXmp && posOtherXmp < removableEmbedding.second) { isRemovableEmbedding = true; break; } @@ -834,8 +831,8 @@ namespace { if (useFlexibleEmbedding) { positions.push_back(xmpPos); } - for (std::vector >::const_iterator e = removableEmbeddings.begin(); e != removableEmbeddings.end(); ++e) { - positions.push_back(e->first); + for (auto&& removableEmbedding : removableEmbeddings) { + positions.push_back(removableEmbedding.first); } std::sort(positions.begin(), positions.end()); @@ -848,8 +845,7 @@ namespace { const uint32_t posEpsNew = posTemp(*tempIo); size_t prevPos = posEps; size_t prevSkipPos = prevPos; - for (std::vector::const_iterator i = positions.begin(); i != positions.end(); ++i) { - const size_t pos = *i; + for (auto&& pos : positions) { if (pos == prevPos) continue; #ifdef DEBUG EXV_DEBUG << "readWriteEpsMetadata: Writing at " << pos << "\n"; @@ -951,10 +947,10 @@ namespace { } if (!useFlexibleEmbedding) { // remove preceding embedding(s) - for (std::vector >::const_iterator e = removableEmbeddings.begin(); e != removableEmbeddings.end(); ++e) { - if (pos == e->first) { - skipPos = e->second; - #ifdef DEBUG + for (auto&& removableEmbedding : removableEmbeddings) { + if (pos == removableEmbedding.first) { + skipPos = removableEmbedding.second; +#ifdef DEBUG EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n"; #endif break; @@ -1164,9 +1160,9 @@ namespace Exiv2 { // read as many bytes as needed for the longest (DOS) EPS signature long bufSize = static_cast(dosEpsSignature.size()); - for (size_t i = 0; i < (sizeof epsFirstLine) / (sizeof *epsFirstLine); i++) { - if (bufSize < static_cast(epsFirstLine[i].size())) { - bufSize = static_cast(epsFirstLine[i].size()); + for (auto&& i : epsFirstLine) { + if (bufSize < static_cast(i.size())) { + bufSize = static_cast(i.size()); } } DataBuf buf = iIo.read(bufSize); @@ -1175,8 +1171,9 @@ namespace Exiv2 } // check for all possible (DOS) EPS signatures bool matched = (memcmp(buf.pData_, dosEpsSignature.data(), dosEpsSignature.size()) == 0); - for (size_t i = 0; !matched && i < (sizeof epsFirstLine) / (sizeof *epsFirstLine); i++) { - matched = (memcmp(buf.pData_, epsFirstLine[i].data(), epsFirstLine[i].size()) == 0); + if (!matched) { + for (auto&& eps : epsFirstLine) + matched = (memcmp(buf.pData_, eps.data(), eps.size()) == 0); } // seek back if possible and requested if (!advance || !matched) { diff --git a/src/exif.cpp b/src/exif.cpp index 7c3f9bf0..3a7983ba 100644 --- a/src/exif.cpp +++ b/src/exif.cpp @@ -687,8 +687,8 @@ namespace Exiv2 { "Exif.Canon.AFPointsSelected", "Exif.Canon.AFPointsUnusable", }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredIfd0Tags); ++i) { - auto pos = ed.findKey(ExifKey(filteredIfd0Tags[i])); + for (auto&& filteredIfd0Tag : filteredIfd0Tags) { + auto pos = ed.findKey(ExifKey(filteredIfd0Tag)); if (pos != ed.end()) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif tag " << pos->key() << " not encoded\n"; @@ -713,11 +713,11 @@ namespace Exiv2 { ifd2Id, ifd3Id }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredIfds); ++i) { + for (auto&& filteredIfd : filteredIfds) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif IFD " << filteredIfds[i] << " not encoded\n"; #endif - eraseIfd(ed, filteredIfds[i]); + eraseIfd(ed, filteredIfd); } // IPTC and XMP are stored elsewhere, not in the Exif APP1 segment. @@ -779,22 +779,22 @@ namespace Exiv2 { }; bool delTags = false; ExifData::iterator pos; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredPvTags); ++i) { - switch (filteredPvTags[i].ptt_) { - case pttLen: - delTags = false; - pos = ed.findKey(ExifKey(filteredPvTags[i].key_)); - if (pos != ed.end() && sumToLong(*pos) > 32768) { - delTags = true; + for (auto&& filteredPvTag : filteredPvTags) { + switch (filteredPvTag.ptt_) { + case pttLen: + delTags = false; + pos = ed.findKey(ExifKey(filteredPvTag.key_)); + if (pos != ed.end() && sumToLong(*pos) > 32768) { + delTags = true; #ifndef SUPPRESS_WARNINGS EXV_WARNING << "Exif tag " << pos->key() << " not encoded\n"; #endif ed.erase(pos); - } + } break; case pttTag: if (delTags) { - pos = ed.findKey(ExifKey(filteredPvTags[i].key_)); + pos = ed.findKey(ExifKey(filteredPvTag.key_)); if (pos != ed.end()) { #ifndef SUPPRESS_WARNINGS EXV_WARNING << "Exif tag " << pos->key() << " not encoded\n"; @@ -806,9 +806,9 @@ namespace Exiv2 { case pttIfd: if (delTags) { #ifndef SUPPRESS_WARNINGS - EXV_WARNING << "Exif IFD " << filteredPvTags[i].key_ << " not encoded\n"; + EXV_WARNING << "Exif IFD " << filteredPvTag.key_ << " not encoded\n"; #endif - eraseIfd(ed, Internal::groupId(filteredPvTags[i].key_)); + eraseIfd(ed, Internal::groupId(filteredPvTag.key_)); } break; } @@ -906,10 +906,10 @@ namespace { { Exiv2::ExifData thumb; // Copy all Thumbnail (IFD1) tags from exifData to Image (IFD0) tags in thumb - for (auto i = exifData.begin(); i != exifData.end(); ++i) { - if (i->groupName() == "Thumbnail") { - std::string key = "Exif.Image." + i->tagName(); - thumb.add(Exiv2::ExifKey(key), &i->value()); + for (auto&& i : exifData) { + if (i.groupName() == "Thumbnail") { + std::string key = "Exif.Image." + i.tagName(); + thumb.add(Exiv2::ExifKey(key), &i.value()); } } diff --git a/src/exiv2.cpp b/src/exiv2.cpp index 31ac810a..3cffb676 100644 --- a/src/exiv2.cpp +++ b/src/exiv2.cpp @@ -158,13 +158,13 @@ int main(int argc, char* const argv[]) int n = 1; int s = static_cast(params.files_.size()); int w = s > 9 ? s > 99 ? 3 : 2 : 1; - for (Params::Files::const_iterator i = params.files_.begin(); i != params.files_.end(); ++i) { + for (auto&& file : params.files_) { if (params.verbose_) { - std::cout << _("File") << " " << std::setw(w) << std::right << n++ << "/" << s << ": " << *i + std::cout << _("File") << " " << std::setw(w) << std::right << n++ << "/" << s << ": " << file << std::endl; } task->setBinary(params.binary_); - int ret = task->run(*i); + int ret = task->run(file); if (rc == 0) rc = ret; } @@ -202,8 +202,8 @@ Params& Params::instance() Params::~Params() { #if defined(EXV_HAVE_REGEX_H) - for (size_t i=0; ic_str()); - bool bStdin = filename->compare("-")== 0; + std::ifstream file(filename.c_str()); + bool bStdin = filename.compare("-") == 0; if (!file && !bStdin) { - std::cerr << *filename << ": " - << _("Failed to open command file for reading\n"); + std::cerr << filename << ": " << _("Failed to open command file for reading\n"); return false; } int num = 0; @@ -1303,7 +1327,7 @@ namespace { } } catch (const Exiv2::AnyError& error) { - std::cerr << *filename << ", " << _("line") << " " << error << "\n"; + std::cerr << filename << ", " << _("line") << " " << error << "\n"; return false; } } @@ -1315,11 +1339,9 @@ namespace { { try { int num = 0; - Params::CmdLines::const_iterator end = cmdLines.end(); - Params::CmdLines::const_iterator line = cmdLines.begin(); - for ( ; line != end; ++line) { + for (auto&& line : cmdLines) { ModifyCmd modifyCmd; - if (parseLine(modifyCmd, *line, ++num)) { + if (parseLine(modifyCmd, line, ++num)) { modifyCmds.push_back(modifyCmd); } } diff --git a/src/futils.cpp b/src/futils.cpp index 60fe1cab..4d9b87ee 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -233,11 +233,15 @@ namespace Exiv2 { , { "data://" ,pDataUri , true } , { "-" ,pStdin , false } }; - for ( size_t i = 0 ; result == pFile && i < sizeof(prots)/sizeof(prots[0]) ; i ++ ) - if ( path.rfind(prots[i].name, 0) == 0 ) + for (auto&& prot : prots) { + if (result != pFile) + break; + + if (path.rfind(prot.name, 0) == 0) // URL's require data. Stdin == "-" and no further data - if ( prots[i].isUrl ? path.size() > prots[i].name.size() : path.size() == prots[i].name.size() ) - result = prots[i].prot; + if (prot.isUrl ? path.size() > prot.name.size() : path.size() == prot.name.size()) + result = prot.prot; + } return result; } // fileProtocol @@ -257,11 +261,15 @@ namespace Exiv2 { , { L"data://" ,pDataUri , true } , { L"-" ,pStdin , false } }; - for ( size_t i = 0 ; result == pFile && i < sizeof(prots)/sizeof(prots[0]) ; i ++ ) - if ( path.rfind(prots[i].name, 0) == 0 ) + for (auto&& prot : prots) { + if (result != pFile) + break; + + if (path.rfind(prot.name, 0) == 0) // URL's require data. Stdin == "-" and no further data - if ( prots[i].isUrl ? path.size() > prots[i].name.size() : path.size() == prots[i].name.size() ) - result = prots[i].prot; + if (prot.isUrl ? path.size() > prot.name.size() : path.size() == prot.name.size()) + result = prot.prot; + } return result; } // fileProtocol diff --git a/src/http.cpp b/src/http.cpp index d05babec..8ecd00b1 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -107,10 +107,6 @@ static const char* httpTemplate = "\r\n" ; -#ifndef lengthof -#define lengthof(x) (sizeof(x)/sizeof((x)[0])) -#endif - #define white(c) ((c == ' ') || (c == '\t')) #define FINISH -999 @@ -325,11 +321,13 @@ int Exiv2::http(Exiv2::Dictionary& request,Exiv2::Dictionary& response,std::stri if ( bSearching ) { // search for the body - for ( size_t b = 0 ; bSearching && b < lengthof(blankLines) ; b++ ) { - const char* blankLinePos = strstr(buffer,blankLines[b]); + for (auto&& line : blankLines) { + if (!bSearching) + break; + const char* blankLinePos = strstr(buffer, line); if ( blankLinePos ) { bSearching = false ; - body = blankLinePos - buffer + strlen(blankLines[b]); + body = blankLinePos - buffer + strlen(line); const char* firstSpace = strchr(buffer,' '); if (firstSpace) { status = atoi(firstSpace); diff --git a/src/iptc.cpp b/src/iptc.cpp index ee8f2bfe..48a1abb3 100644 --- a/src/iptc.cpp +++ b/src/iptc.cpp @@ -387,9 +387,7 @@ namespace Exiv2 { std::string value = pos->toString(); if (pos->value().ok()) { int seqCount = 0; - std::string::iterator i; - for (i = value.begin(); i != value.end(); ++i) { - char c = *i; + for (auto&& c : value) { if (seqCount) { if ((c & 0xc0) != 0x80) { utf8 = false; diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index 0e4ea1be..c5510d0c 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -109,9 +109,10 @@ namespace Exiv2 { long sizePsData) { if (sizePsData < 4) return false; - for (size_t i = 0; i < (sizeof irbId_) / (sizeof *irbId_); i++) { - assert(strlen(irbId_[i]) == 4); - if (memcmp(pPsData, irbId_[i], 4) == 0) return true; + for (auto&& i : irbId_) { + assert(strlen(i) == 4); + if (memcmp(pPsData, i, 4) == 0) + return true; } return false; } @@ -836,8 +837,8 @@ namespace Exiv2 { #ifdef EXIV2_DEBUG_MESSAGES std::cout << "iptc data blocks: " << iptcDataSegs.size() << std::endl; uint32_t toggle = 0; - for (Uint32Vector_i i = iptcDataSegs.begin(); i != iptcDataSegs.end(); i++) { - std::cout << *i; + for (auto&& iptc : iptcDataSegs) { + std::cout << iptc; if (toggle++ % 2) std::cout << std::endl; else diff --git a/src/makernote_int.cpp b/src/makernote_int.cpp index 4db41f46..e3e1c9b6 100644 --- a/src/makernote_int.cpp +++ b/src/makernote_int.cpp @@ -1250,8 +1250,8 @@ namespace Exiv2 { const char* models[] = { "SLT-A58", "SLT-A99", "ILCE-3000", "ILCE-3500", "NEX-3N", "NEX-5R", "NEX-5T", "NEX-6", "VG30E", "VG900", "DSC-RX100", "DSC-RX1", "DSC-RX1R", "DSC-HX300", "DSC-HX50V", "DSC-TX30", "DSC-WX60", "DSC-WX200", "DSC-WX300" }; std::set s2010eModels; - for (size_t i = 0; i < EXV_COUNTOF(models); i++) { - s2010eModels.insert(models[i]); + for (auto&& model : models) { + s2010eModels.insert(model); } std::string model = getExifModel(pRoot); int idx = -1; diff --git a/src/orfimage.cpp b/src/orfimage.cpp index d8b6750f..e71b6da5 100644 --- a/src/orfimage.cpp +++ b/src/orfimage.cpp @@ -179,14 +179,11 @@ namespace Exiv2 { static const IfdId filteredIfds[] = { panaRawId }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredIfds); ++i) { + for (auto&& filteredIfd : filteredIfds) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif IFD " << filteredIfds[i] << " not encoded\n"; #endif - ed.erase(std::remove_if(ed.begin(), - ed.end(), - FindExifdatum(filteredIfds[i])), - ed.end()); + ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); } std::unique_ptr header(new OrfHeader(byteOrder)); diff --git a/src/pngimage.cpp b/src/pngimage.cpp index 9e1fe8ac..b2fdbcbd 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -35,6 +35,7 @@ #include "types.hpp" // + standard includes +#include #include #include #include @@ -148,11 +149,10 @@ namespace Exiv2 { static bool tEXtToDataBuf(const byte* bytes,long length,DataBuf& result) { static const char* hexdigits = "0123456789ABCDEF"; - static int value [256] ; - static bool bFirst = true ; + static std::array value; + static bool bFirst = true; if ( bFirst ) { - for ( int i = 0 ; i < 256 ; i++ ) - value[i] = 0; + value = {}; for ( int i = 0 ; i < 16 ; i++ ) { value[tolower(hexdigits[i])]=i+1; value[toupper(hexdigits[i])]=i+1; diff --git a/src/preview.cpp b/src/preview.cpp index 57d1a7fa..5629a8ba 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -21,6 +21,7 @@ // included header files #include "config.h" +#include #include #include @@ -760,8 +761,8 @@ namespace { ExifData preview; // copy tags - for (auto pos = exifData.begin(); pos != exifData.end(); ++pos) { - if (pos->groupName() == group_) { + for (auto &&pos : exifData) { + if (pos.groupName() == group_) { /* Write only the necessary TIFF image tags tags that especially could cause problems are: @@ -769,9 +770,9 @@ namespace { "Orientation" - this tag typically appears only in the "Image" group. Deleting it ensures consistent result for all previews, including JPEG */ - uint16_t tag = pos->tag(); + uint16_t tag = pos.tag(); if (tag != 0x00fe && tag != 0x00ff && Internal::isTiffImageTag(tag, Internal::ifd0Id)) { - preview.add(ExifKey(tag, "Image"), &pos->value()); + preview.add(ExifKey(tag, "Image"), &pos.value()); } } } @@ -891,8 +892,8 @@ namespace { { // create decoding table byte invalid = 16; - byte decodeHexTable[256]; - for (long i = 0; i < 256; i++) decodeHexTable[i] = invalid; + std::array decodeHexTable; + decodeHexTable.fill(invalid); for (byte i = 0; i < 10; i++) decodeHexTable[static_cast('0') + i] = i; for (byte i = 0; i < 6; i++) decodeHexTable[static_cast('A') + i] = i + 10; for (byte i = 0; i < 6; i++) decodeHexTable[static_cast('a') + i] = i + 10; @@ -929,9 +930,8 @@ namespace { // create decoding table unsigned long invalid = 64; - unsigned long decodeBase64Table[256] = {}; - for (unsigned long i = 0; i < 256; i++) - decodeBase64Table[i] = invalid; + std::array decodeBase64Table; + decodeBase64Table.fill(invalid); for (unsigned long i = 0; i < 64; i++) decodeBase64Table[(unsigned char)encodeBase64Table[i]] = i; diff --git a/src/properties.cpp b/src/properties.cpp index 4554da51..311a07c8 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -2487,9 +2487,9 @@ namespace Exiv2 { const XmpNsInfo* XmpProperties::lookupNsRegistryUnsafe(const XmpNsInfo::Prefix& prefix) { - for (NsRegistry::const_iterator i = nsRegistry_.begin(); - i != nsRegistry_.end(); ++i) { - if (i->second == prefix) return &(i->second); + for (auto&& ns : nsRegistry_) { + if (ns.second == prefix) + return &(ns.second); } return 0; } @@ -2657,8 +2657,8 @@ namespace Exiv2 { void XmpProperties::registeredNamespaces(Exiv2::Dictionary& nsDict) { - for (unsigned int i = 0; i < EXV_COUNTOF(xmpNsInfo); ++i) { - Exiv2::XmpParser::registerNs(xmpNsInfo[i].ns_,xmpNsInfo[i].prefix_); + for (auto&& i : xmpNsInfo) { + Exiv2::XmpParser::registerNs(i.ns_, i.prefix_); } Exiv2::XmpParser::registeredNamespaces(nsDict); } diff --git a/src/rw2image.cpp b/src/rw2image.cpp index b62ee716..177ea55e 100644 --- a/src/rw2image.cpp +++ b/src/rw2image.cpp @@ -150,9 +150,10 @@ namespace Exiv2 { ExifData& prevData = image->exifData(); if (!prevData.empty()) { // Filter duplicate tags - for (auto pos = exifData_.begin(); pos != exifData_.end(); ++pos) { - if (pos->ifdId() == panaRawId) continue; - auto dup = prevData.findKey(ExifKey(pos->key())); + for (auto&& pos : exifData_) { + if (pos.ifdId() == panaRawId) + continue; + auto dup = prevData.findKey(ExifKey(pos.key())); if (dup != prevData.end()) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Filtering duplicate tag " << pos->key() @@ -194,8 +195,8 @@ namespace Exiv2 { "Exif.Image.PrintImageMatching", "Exif.Image.YCbCrPositioning" }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredTags); ++i) { - auto pos = prevData.findKey(ExifKey(filteredTags[i])); + for (auto&& filteredTag : filteredTags) { + auto pos = prevData.findKey(ExifKey(filteredTag)); if (pos != prevData.end()) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Exif tag " << pos->key() << " removed\n"; @@ -205,8 +206,8 @@ namespace Exiv2 { } // Add the remaining tags - for (auto pos = prevData.begin(); pos != prevData.end(); ++pos) { - exifData_.add(*pos); + for (auto&& pos : prevData) { + exifData_.add(pos); } } // Rw2Image::readMetadata diff --git a/src/tags_int.cpp b/src/tags_int.cpp index 1921c287..5ec9828e 100644 --- a/src/tags_int.cpp +++ b/src/tags_int.cpp @@ -3236,10 +3236,8 @@ namespace Exiv2 { if (stringValue[19] == 'Z') { stringValue = stringValue.substr(0, 19); } - for (unsigned int i = 0; i < stringValue.length(); ++i) { - if (stringValue[i] == 'T') stringValue[i] = ' '; - if (stringValue[i] == '-') stringValue[i] = ':'; - } + std::replace(stringValue.begin(), stringValue.end(), 'T', ' '); + std::replace(stringValue.begin(), stringValue.end(), '-', ':'); return os << stringValue; } diff --git a/src/tiffcomposite_int.cpp b/src/tiffcomposite_int.cpp index cb3ce9d8..b94e611d 100644 --- a/src/tiffcomposite_int.cpp +++ b/src/tiffcomposite_int.cpp @@ -178,16 +178,16 @@ namespace Exiv2 { TiffDirectory::~TiffDirectory() { - for (Components::iterator i = components_.begin(); i != components_.end(); ++i) { - delete *i; + for (auto&& component : components_) { + delete component; } delete pNext_; } TiffSubIfd::~TiffSubIfd() { - for (Ifds::iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - delete *i; + for (auto&& ifd : ifds_) { + delete ifd; } } @@ -231,8 +231,8 @@ namespace Exiv2 { TiffBinaryArray::~TiffBinaryArray() { - for (Components::iterator i = elements_.begin(); i != elements_.end(); ++i) { - delete *i; + for (auto&& element : elements_) { + delete element; } } @@ -660,9 +660,9 @@ namespace Exiv2 { tc = pNext_; } else { - for (Components::iterator i = components_.begin(); i != components_.end(); ++i) { - if ((*i)->tag() == tpi.tag() && (*i)->group() == tpi.group()) { - tc = *i; + for (auto&& component : components_) { + if (component->tag() == tpi.tag() && component->group() == tpi.group()) { + tc = component; break; } } @@ -708,9 +708,9 @@ namespace Exiv2 { const TiffPathItem tpi2 = tiffPath.top(); tiffPath.push(tpi1); TiffComponent* tc = 0; - for (Ifds::iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - if ((*i)->group() == tpi2.group()) { - tc = *i; + for (auto&& ifd : ifds_) { + if (ifd->group() == tpi2.group()) { + tc = ifd; break; } } @@ -776,9 +776,9 @@ namespace Exiv2 { // To allow duplicate entries, we only check if the new component already // exists if there is still at least one composite tag on the stack if (tiffPath.size() > 1) { - for (Components::iterator i = elements_.begin(); i != elements_.end(); ++i) { - if ((*i)->tag() == tpi.tag() && (*i)->group() == tpi.group()) { - tc = *i; + for (auto&& element : elements_) { + if (element->tag() == tpi.tag() && element->group() == tpi.group()) { + tc = element; break; } } @@ -1113,15 +1113,15 @@ namespace Exiv2 { // Size of IFD values and additional data uint32_t sizeValue = 0; uint32_t sizeData = 0; - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - uint32_t sv = (*i)->size(); + for (auto&& component : components_) { + uint32_t sv = component->size(); if (sv > 4) { sv += sv & 1; // Align value to word boundary sizeValue += sv; } // Also add the size of data, but only if needed if (isRootDir) { - uint32_t sd = (*i)->sizeData(); + uint32_t sd = component->sizeData(); sd += sd & 1; // Align data to word boundary sizeData += sd; } @@ -1141,14 +1141,14 @@ namespace Exiv2 { ioWrapper.write(buf, 2); idx += 2; // b) Directory entries - may contain pointers to the value or data - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - idx += writeDirEntry(ioWrapper, byteOrder, offset, *i, valueIdx, dataIdx, imageIdx); - uint32_t sv = (*i)->size(); + for (auto&& component : components_) { + idx += writeDirEntry(ioWrapper, byteOrder, offset, component, valueIdx, dataIdx, imageIdx); + uint32_t sv = component->size(); if (sv > 4) { sv += sv & 1; // Align value to word boundary valueIdx += sv; } - uint32_t sd = (*i)->sizeData(); + uint32_t sd = component->sizeData(); sd += sd & 1; // Align data to word boundary dataIdx += sd; } @@ -1166,10 +1166,10 @@ namespace Exiv2 { // 2nd: Write IFD values - may contain pointers to additional data valueIdx = sizeDir; dataIdx = sizeDir + sizeValue; - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - uint32_t sv = (*i)->size(); + for (auto&& component : components_) { + uint32_t sv = component->size(); if (sv > 4) { - uint32_t d = (*i)->write(ioWrapper, byteOrder, offset, valueIdx, dataIdx, imageIdx); + uint32_t d = component->write(ioWrapper, byteOrder, offset, valueIdx, dataIdx, imageIdx); enforce(sv == d, kerImageWriteFailed); if ((sv & 1) == 1) { ioWrapper.putb(0x0); // Align value to word boundary @@ -1178,7 +1178,7 @@ namespace Exiv2 { idx += sv; valueIdx += sv; } - uint32_t sd = (*i)->sizeData(); + uint32_t sd = component->sizeData(); sd += sd & 1; // Align data to word boundary dataIdx += sd; } @@ -1319,13 +1319,13 @@ namespace Exiv2 { DataBuf buf(static_cast(strips_.size()) * 4); memset(buf.pData_, 0x0, buf.size_); uint32_t idx = 0; - for (Strips::const_iterator i = strips_.begin(); i != strips_.end(); ++i) { + for (auto&& strip : strips_) { idx += writeOffset(buf.pData_ + idx, o2, tiffType(), byteOrder); - o2 += i->second; - o2 += i->second & 1; // Align strip data to word boundary + o2 += strip.second; + o2 += strip.second & 1; // Align strip data to word boundary if (!(group() > mnId)) { // Todo: FIX THIS!! SHOULDN'T USE > - imageIdx += i->second; - imageIdx += i->second & 1; // Align strip data to word boundary + imageIdx += strip.second; + imageIdx += strip.second & 1; // Align strip data to word boundary } } ioWrapper.write(buf.pData_, buf.size_); @@ -1343,9 +1343,9 @@ namespace Exiv2 { uint32_t idx = 0; // Sort IFDs by group, needed if image data tags were copied first std::sort(ifds_.begin(), ifds_.end(), cmpGroupLt); - for (Ifds::const_iterator i = ifds_.begin(); i != ifds_.end(); ++i) { + for (auto&& ifd : ifds_) { idx += writeOffset(buf.pData_ + idx, offset + dataIdx, tiffType(), byteOrder); - dataIdx += (*i)->size(); + dataIdx += ifd->size(); } ioWrapper.write(buf.pData_, buf.size_); return buf.size_; @@ -1417,12 +1417,13 @@ namespace Exiv2 { mioWrapper.write(buf, elSize); } // write all tags of the array (Todo: assumes that there are no duplicates, need check) - for (Components::const_iterator i = elements_.begin(); i != elements_.end(); ++i) { + for (auto&& element : elements_) { // Skip the manufactured tag, if it exists - if (cfg()->hasSize_ && (*i)->tag() == 0) continue; - uint32_t newIdx = (*i)->tag() * cfg()->tagStep(); + if (cfg()->hasSize_ && element->tag() == 0) + continue; + uint32_t newIdx = element->tag() * cfg()->tagStep(); idx += fillGap(mioWrapper, idx, newIdx); - idx += (*i)->write(mioWrapper, byteOrder, offset + newIdx, valueIdx, dataIdx, imageIdx); + idx += element->write(mioWrapper, byteOrder, offset + newIdx, valueIdx, dataIdx, imageIdx); } if (cfg()->hasFillers_ && def()) { const ArrayDef* lastDef = def() + defSize() - 1; @@ -1478,8 +1479,8 @@ namespace Exiv2 { uint32_t& imageIdx) const { uint32_t len = 0; - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - len += (*i)->writeData(ioWrapper, byteOrder, offset, dataIdx + len, imageIdx); + for (auto&& component : components_) { + len += component->writeData(ioWrapper, byteOrder, offset, dataIdx + len, imageIdx); } return len; } // TiffDirectory::doWriteData @@ -1531,8 +1532,8 @@ namespace Exiv2 { uint32_t& imageIdx) const { uint32_t len = 0; - for (Ifds::const_iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - len += (*i)->write(ioWrapper, byteOrder, offset + dataIdx + len, uint32_t(-1), uint32_t(-1), imageIdx); + for (auto&& ifd : ifds_) { + len += ifd->write(ioWrapper, byteOrder, offset + dataIdx + len, uint32_t(-1), uint32_t(-1), imageIdx); } // Align data to word boundary uint32_t align = (len & 1); @@ -1562,14 +1563,14 @@ namespace Exiv2 { { uint32_t len = 0; TiffComponent* pSubIfd = 0; - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - if ((*i)->tag() == 0x014a) { + for (auto&& component : components_) { + if (component->tag() == 0x014a) { // Hack: delay writing of sub-IFD image data to get the order correct assert(pSubIfd == 0); - pSubIfd = *i; + pSubIfd = component; continue; } - len += (*i)->writeImage(ioWrapper, byteOrder); + len += component->writeImage(ioWrapper, byteOrder); } if (pSubIfd) { len += pSubIfd->writeImage(ioWrapper, byteOrder); @@ -1590,8 +1591,8 @@ namespace Exiv2 { ByteOrder byteOrder) const { uint32_t len = 0; - for (Ifds::const_iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - len += (*i)->writeImage(ioWrapper, byteOrder); + for (auto&& ifd : ifds_) { + len += ifd->writeImage(ioWrapper, byteOrder); } return len; } // TiffSubIfd::doWriteImage @@ -1633,10 +1634,10 @@ namespace Exiv2 { << ": Writing " << strips_.size() << " strips"; #endif len = 0; - for (Strips::const_iterator i = strips_.begin(); i != strips_.end(); ++i) { - ioWrapper.write(i->first, i->second); - len += i->second; - uint32_t align = i->second & 1; // Align strip data to word boundary + for (auto&& strip : strips_) { + ioWrapper.write(strip.first, strip.second); + len += strip.second; + uint32_t align = strip.second & 1; // Align strip data to word boundary if (align) ioWrapper.putb(0x0); len += align; } @@ -1658,13 +1659,13 @@ namespace Exiv2 { // Size of the directory, without values and additional data uint32_t len = 2 + 12 * compCount + (hasNext_ ? 4 : 0); // Size of IFD values and data - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - uint32_t sv = (*i)->size(); + for (auto&& component : components_) { + uint32_t sv = component->size(); if (sv > 4) { sv += sv & 1; // Align value to word boundary len += sv; } - uint32_t sd = (*i)->sizeData(); + uint32_t sd = component->sizeData(); sd += sd & 1; // Align data to word boundary len += sd; } @@ -1718,10 +1719,10 @@ namespace Exiv2 { // - no duplicate tags in the array uint32_t idx = 0; uint32_t sz = cfg()->tagStep(); - for (Components::const_iterator i = elements_.begin(); i != elements_.end(); ++i) { - if ((*i)->tag() > idx) { - idx = (*i)->tag(); - sz = (*i)->size(); + for (auto&& element : elements_) { + if (element->tag() > idx) { + idx = element->tag(); + sz = element->size(); } } idx = idx * cfg()->tagStep() + sz; @@ -1776,8 +1777,8 @@ namespace Exiv2 { uint32_t TiffSubIfd::doSizeData() const { uint32_t len = 0; - for (Ifds::const_iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - len += (*i)->size(); + for (auto&& ifd : ifds_) { + len += ifd->size(); } return len; } // TiffSubIfd::doSizeData @@ -1796,8 +1797,8 @@ namespace Exiv2 { uint32_t TiffDirectory::doSizeImage() const { uint32_t len = 0; - for (Components::const_iterator i = components_.begin(); i != components_.end(); ++i) { - len += (*i)->sizeImage(); + for (auto&& component : components_) { + len += component->sizeImage(); } if (pNext_) { len += pNext_->sizeImage(); @@ -1808,8 +1809,8 @@ namespace Exiv2 { uint32_t TiffSubIfd::doSizeImage() const { uint32_t len = 0; - for (Ifds::const_iterator i = ifds_.begin(); i != ifds_.end(); ++i) { - len += (*i)->sizeImage(); + for (auto&& ifd : ifds_) { + len += ifd->sizeImage(); } return len; } // TiffSubIfd::doSizeImage @@ -1829,8 +1830,8 @@ namespace Exiv2 { if (!pValue()) return 0; uint32_t len = pValue()->sizeDataArea(); if (len == 0) { - for (Strips::const_iterator i = strips_.begin(); i != strips_.end(); ++i) { - len += i->second; + for (auto&& strip : strips_) { + len += strip.second; } } return len; diff --git a/src/tiffimage.cpp b/src/tiffimage.cpp index 150e7566..84b7716e 100644 --- a/src/tiffimage.cpp +++ b/src/tiffimage.cpp @@ -120,8 +120,8 @@ namespace Exiv2 { }; // Find the group of the primary image, default to "Image" primaryGroup_ = std::string("Image"); - for (unsigned int i = 0; i < EXV_COUNTOF(keys); ++i) { - auto md = exifData_.findKey(ExifKey(keys[i])); + for (auto&& i : keys) { + auto md = exifData_.findKey(ExifKey(i)); // Is it the primary image? if (md != exifData_.end() && md->count() > 0 && md->toLong() == 0) { // Sometimes there is a JPEG primary image; that's not our first choice @@ -289,14 +289,11 @@ namespace Exiv2 { static const IfdId filteredIfds[] = { panaRawId }; - for (unsigned int i = 0; i < EXV_COUNTOF(filteredIfds); ++i) { + for (auto&& filteredIfd : filteredIfds) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif IFD " << filteredIfds[i] << " not encoded\n"; #endif - ed.erase(std::remove_if(ed.begin(), - ed.end(), - FindExifdatum(filteredIfds[i])), - ed.end()); + ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); } std::unique_ptr header(new TiffHeader(byteOrder)); diff --git a/src/tiffimage_int.cpp b/src/tiffimage_int.cpp index ea4d2fa8..66650eb0 100644 --- a/src/tiffimage_int.cpp +++ b/src/tiffimage_int.cpp @@ -1870,8 +1870,8 @@ namespace Exiv2 { subImage9Id }; - for (unsigned int i = 0; i < EXV_COUNTOF(imageGroups); ++i) { - TiffFinder finder(0x00fe, imageGroups[i]); + for (auto&& imageGroup : imageGroups) { + TiffFinder finder(0x00fe, imageGroup); pSourceDir->accept(finder); TiffEntryBase* te = dynamic_cast(finder.result()); const Value* pV = te != NULL ? te->pValue() : NULL; @@ -2137,10 +2137,10 @@ namespace Exiv2 { void OffsetWriter::writeOffsets(BasicIo& io) const { - for (OffsetList::const_iterator it = offsetList_.begin(); it != offsetList_.end(); ++it) { - io.seek(it->second.origin_, BasicIo::beg); + for (auto&& it : offsetList_) { + io.seek(it.second.origin_, BasicIo::beg); byte buf[4] = { 0, 0, 0, 0 }; - l2Data(buf, it->second.target_, it->second.byteOrder_); + l2Data(buf, it.second.target_, it.second.byteOrder_); io.write(buf, 4); } } diff --git a/src/tiffvisitor_int.cpp b/src/tiffvisitor_int.cpp index 475ac112..719f5c5e 100644 --- a/src/tiffvisitor_int.cpp +++ b/src/tiffvisitor_int.cpp @@ -80,9 +80,7 @@ namespace Exiv2 { TiffVisitor::TiffVisitor() { - for (int i = 0; i < events_; ++i) { - go_[i] = true; - } + go_.fill(true); } TiffVisitor::~TiffVisitor() diff --git a/src/tiffvisitor_int.hpp b/src/tiffvisitor_int.hpp index c125a422..4d320a6e 100644 --- a/src/tiffvisitor_int.hpp +++ b/src/tiffvisitor_int.hpp @@ -27,6 +27,7 @@ #include "types.hpp" // + standard includes +#include #include #include #include @@ -75,7 +76,7 @@ namespace Exiv2 { private: static const int events_ = 2; //!< The number of stop/go flags. - bool go_[events_]; //!< Array of stop/go flags. See setGo(). + std::array go_; //!< Array of stop/go flags. See setGo(). public: //! @name Creators diff --git a/src/version.cpp b/src/version.cpp index ca9effcb..3f04d281 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -557,9 +557,9 @@ void Exiv2::dumpLibraryInfo(std::ostream& os,const exv_grep_keys_t& keys) Exiv2::Dictionary ns; Exiv2::XmpProperties::registeredNamespaces(ns); - for ( auto it = ns.begin(); it != ns.end() ; ++it ) { - std::string xmlns = it->first; - std::string uri = it->second; + for (auto&& n : ns) { + std::string xmlns = n.first; + std::string uri = n.second; output(os,keys,name,xmlns+":"+uri); } #endif diff --git a/src/xmp.cpp b/src/xmp.cpp index 086beb9a..eb58099f 100644 --- a/src/xmp.cpp +++ b/src/xmp.cpp @@ -402,7 +402,7 @@ namespace Exiv2 { } } // now erase the family! - for (const auto& k: keys) { + for (auto&& k : keys) { erase(findKey(Exiv2::XmpKey(k))); } } @@ -746,69 +746,64 @@ namespace Exiv2 { return 2; } // Register custom namespaces with XMP-SDK - for (XmpProperties::NsRegistry::iterator i = XmpProperties::nsRegistry_.begin(); - i != XmpProperties::nsRegistry_.end(); ++i) { + for (auto&& i : XmpProperties::nsRegistry_) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Registering " << i->second.prefix_ << " : " << i->first << "\n"; #endif - registerNs(i->first, i->second.prefix_); + registerNs(i.first, i.second.prefix_); } SXMPMeta meta; - for (XmpData::const_iterator i = xmpData.begin(); i != xmpData.end(); ++i) { - const std::string ns = XmpProperties::ns(i->groupName()); + for (auto&& i : xmpData) { + const std::string ns = XmpProperties::ns(i.groupName()); XMP_OptionBits options = 0; - if (i->typeId() == langAlt) { - + if (i.typeId() == langAlt) { // Encode Lang Alt property - const LangAltValue* la = dynamic_cast(&i->value()); - if (la == 0) throw Error(kerEncodeLangAltPropertyFailed, i->key()); + const LangAltValue* la = dynamic_cast(&i.value()); + if (la == 0) + throw Error(kerEncodeLangAltPropertyFailed, i.key()); int idx = 1; - for ( LangAltValue::ValueType::const_iterator k = la->value_.begin() - ; k != la->value_.end() - ; ++k - ) { - if ( k->second.size() ) { // remove lang specs with no value - printNode(ns, i->tagName(), k->second, 0); - meta.AppendArrayItem(ns.c_str(), i->tagName().c_str(), kXMP_PropArrayIsAlternate, k->second.c_str()); - const std::string item = i->tagName() + "[" + toString(idx++) + "]"; - meta.SetQualifier(ns.c_str(), item.c_str(), kXMP_NS_XML, "lang", k->first.c_str()); + for (auto&& k : la->value_) { + if (k.second.size()) { // remove lang specs with no value + printNode(ns, i.tagName(), k.second, 0); + meta.AppendArrayItem(ns.c_str(), i.tagName().c_str(), kXMP_PropArrayIsAlternate, + k.second.c_str()); + const std::string item = i.tagName() + "[" + toString(idx++) + "]"; + meta.SetQualifier(ns.c_str(), item.c_str(), kXMP_NS_XML, "lang", k.first.c_str()); } } continue; } // Todo: Xmpdatum should have an XmpValue, not a Value - const XmpValue* val = dynamic_cast(&i->value()); - if (val == 0) throw Error(kerInvalidKeyXmpValue, i->key(), i->typeName()); + const XmpValue* val = dynamic_cast(&i.value()); + if (val == 0) + throw Error(kerInvalidKeyXmpValue, i.key(), i.typeName()); options = xmpArrayOptionBits(val->xmpArrayType()) | xmpArrayOptionBits(val->xmpStruct()); - if ( i->typeId() == xmpBag - || i->typeId() == xmpSeq - || i->typeId() == xmpAlt) { - printNode(ns, i->tagName(), "", options); - meta.SetProperty(ns.c_str(), i->tagName().c_str(), 0, options); - for (int idx = 0; idx < i->count(); ++idx) { - const std::string item = i->tagName() + "[" + toString(idx + 1) + "]"; - printNode(ns, item, i->toString(idx), 0); - meta.SetProperty(ns.c_str(), item.c_str(), i->toString(idx).c_str()); + if (i.typeId() == xmpBag || i.typeId() == xmpSeq || i.typeId() == xmpAlt) { + printNode(ns, i.tagName(), "", options); + meta.SetProperty(ns.c_str(), i.tagName().c_str(), 0, options); + for (int idx = 0; idx < i.count(); ++idx) { + const std::string item = i.tagName() + "[" + toString(idx + 1) + "]"; + printNode(ns, item, i.toString(idx), 0); + meta.SetProperty(ns.c_str(), item.c_str(), i.toString(idx).c_str()); } continue; } - if (i->typeId() == xmpText) { - if (i->count() == 0) { - printNode(ns, i->tagName(), "", options); - meta.SetProperty(ns.c_str(), i->tagName().c_str(), 0, options); - } - else { - printNode(ns, i->tagName(), i->toString(0), options); - meta.SetProperty(ns.c_str(), i->tagName().c_str(), i->toString(0).c_str(), options); + if (i.typeId() == xmpText) { + if (i.count() == 0) { + printNode(ns, i.tagName(), "", options); + meta.SetProperty(ns.c_str(), i.tagName().c_str(), 0, options); + } else { + printNode(ns, i.tagName(), i.toString(0), options); + meta.SetProperty(ns.c_str(), i.tagName().c_str(), i.toString(0).c_str(), options); } continue; } // Don't let any Xmpdatum go by unnoticed - throw Error(kerUnhandledXmpdatum, i->tagName(), i->typeName()); + throw Error(kerUnhandledXmpdatum, i.tagName(), i.typeName()); } std::string tmpPacket; meta.SerializeToBuffer(&tmpPacket, xmpFormatOptionBits(static_cast(formatFlags)), padding); // throws diff --git a/src/xmpsidecar.cpp b/src/xmpsidecar.cpp index 01def479..ebc1872c 100644 --- a/src/xmpsidecar.cpp +++ b/src/xmpsidecar.cpp @@ -99,10 +99,10 @@ namespace Exiv2 { } // #1112 - store dates to deal with loss of TZ information during conversions - for (Exiv2::XmpData::const_iterator it = xmpData_.begin(); it != xmpData_.end(); ++it) { - std::string key(it->key()); + for (auto&& it : xmpData_) { + std::string key(it.key()); if ( key.find("Date") != std::string::npos ) { - std::string value(it->value().toString()); + std::string value(it.value().toString()); dates_[key] = value; } } @@ -112,13 +112,11 @@ namespace Exiv2 { } // XmpSidecar::readMetadata // lower case string - static std::string toLowerCase(std::string a) + static std::string toLowerCase(const std::string& a) { - for(size_t i=0 ; i < a.length() ; i++) - { - a[i]=tolower(a[i]); - } - return a; + std::string b = a; + std::transform(a.begin(), a.end(), b.begin(), ::tolower); + return b; } static bool matchi(const std::string key,const char* substr) @@ -137,9 +135,9 @@ namespace Exiv2 { if (writeXmpFromPacket() == false) { // #589 copy XMP tags Exiv2::XmpData copy ; - for (Exiv2::XmpData::const_iterator it = xmpData_.begin(); it != xmpData_.end(); ++it) { - if ( !matchi(it->key(),"exif") && !matchi(it->key(),"iptc") ) { - copy[it->key()] = it->value(); + for (auto&& it : xmpData_) { + if (!matchi(it.key(), "exif") && !matchi(it.key(), "iptc")) { + copy[it.key()] = it.value(); } } @@ -148,16 +146,16 @@ namespace Exiv2 { copyIptcToXmp(iptcData_, xmpData_); // #589 - restore tags which were modified by the convertors - for (Exiv2::XmpData::const_iterator it = copy.begin(); it != copy.end(); ++it) { - xmpData_[it->key()] = it->value() ; + for (auto&& it : copy) { + xmpData_[it.key()] = it.value(); } // #1112 - restore dates if they lost their TZ info - for ( auto it = dates_.begin() ; it != dates_.end() ; ++it ) { - std::string sKey = it->first; + for (auto&& date : dates_) { + std::string sKey = date.first; Exiv2::XmpKey key(sKey); if ( xmpData_.findKey(key) != xmpData_.end() ) { - std::string value_orig(it->second); + std::string value_orig(date.second); std::string value_now(xmpData_[sKey].value().toString()); // std::cout << key << " -> " << value_now << " => " << value_orig << std::endl; if ( value_orig.find(value_now.substr(0,10)) != std::string::npos ) {