From b72e4b0e44c06854e80143e8432491fbff500904 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:12:57 -0700 Subject: [PATCH 01/13] clang-tidy: use auto Signed-off-by: Rosen Penev --- src/bmffimage.cpp | 2 +- src/crwimage_int.cpp | 2 +- src/pgfimage.cpp | 2 +- src/tiffcomposite_int.cpp | 2 +- src/value.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bmffimage.cpp b/src/bmffimage.cpp index b71dbad0..152c1f91 100644 --- a/src/bmffimage.cpp +++ b/src/bmffimage.cpp @@ -527,7 +527,7 @@ void BmffImage::parseXmp(uint64_t length, uint64_t start) { long restore = io_->tell(); io_->seek(static_cast(start), BasicIo::beg); - size_t lengthSizeT = static_cast(length); + auto lengthSizeT = static_cast(length); DataBuf xmp(lengthSizeT + 1); xmp.write_uint8(lengthSizeT, 0); // ensure xmp is null terminated! if (io_->read(xmp.data(), lengthSizeT) != lengthSizeT) diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index 1774941a..6010ac0f 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -350,7 +350,7 @@ size_t CiffDirectory::doWrite(Blob& blob, ByteOrder byteOrder, size_t offset) { for (auto&& component : components_) { dirOffset = component->write(blob, byteOrder, dirOffset); } - const uint32_t dirStart = static_cast(dirOffset); + const auto dirStart = static_cast(dirOffset); // Number of directory entries byte buf[4]; diff --git a/src/pgfimage.cpp b/src/pgfimage.cpp index 7de1aa66..3f5049d9 100644 --- a/src/pgfimage.cpp +++ b/src/pgfimage.cpp @@ -178,7 +178,7 @@ void PgfImage::doWriteMetadata(BasicIo& outIo) { throw Error(ErrorCode::kerImageWriteFailed); // Write new Header size. - uint32_t newHeaderSize = static_cast(header.size() + imgSize); + auto newHeaderSize = static_cast(header.size() + imgSize); DataBuf buffer(4); std::copy_n(&newHeaderSize, 4, buffer.data()); byteSwap_(buffer, 0, bSwap_); diff --git a/src/tiffcomposite_int.cpp b/src/tiffcomposite_int.cpp index 54a76e2c..66e913ad 100644 --- a/src/tiffcomposite_int.cpp +++ b/src/tiffcomposite_int.cpp @@ -1073,7 +1073,7 @@ uint32_t TiffIfdMakernote::doWrite(IoWrapper& ioWrapper, ByteOrder byteOrder, in uint32_t /*dataIdx*/, uint32_t& imageIdx) { mnOffset_ = static_cast(offset); setImageByteOrder(byteOrder); - uint32_t len = static_cast(writeHeader(ioWrapper, this->byteOrder())); + auto len = static_cast(writeHeader(ioWrapper, this->byteOrder())); len += ifd_.write(ioWrapper, this->byteOrder(), offset - baseOffset() + len, uint32_t(-1), uint32_t(-1), imageIdx); return len; } // TiffIfdMakernote::doWrite diff --git a/src/value.cpp b/src/value.cpp index ab1ce47f..5ef99ebb 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -843,7 +843,7 @@ size_t DateValue::copy(byte* buf, ByteOrder /*byteOrder*/) const { // sprintf wants to add the null terminator, so use oversized buffer char temp[9]; - size_t wrote = static_cast(snprintf(temp, sizeof(temp), "%04d%02d%02d", date_.year, date_.month, date_.day)); + auto wrote = static_cast(snprintf(temp, sizeof(temp), "%04d%02d%02d", date_.year, date_.month, date_.day)); std::memcpy(buf, temp, wrote); return wrote; } From 9f676205031db9e8a371bc5f7ad09bfa2723129e Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:16:53 -0700 Subject: [PATCH 02/13] clang-tidy: add more nodiscard Signed-off-by: Rosen Penev --- samples/geotag.cpp | 14 +++++++------- src/basicio.cpp | 8 ++++---- src/convert.cpp | 2 +- src/exif.cpp | 18 +++++++++--------- src/preview.cpp | 26 +++++++++++++------------- src/tags.cpp | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/samples/geotag.cpp b/samples/geotag.cpp index 8c8bdae2..b45099fd 100644 --- a/samples/geotag.cpp +++ b/samples/geotag.cpp @@ -123,7 +123,7 @@ class Position { virtual ~Position() = default; // instance methods - bool good() const { + [[nodiscard]] bool good() const { return time_ || lon_ || lat_ || ele_; } std::string getTimeString() { @@ -131,22 +131,22 @@ class Position { times_ = getExifTime(time_); return times_; } - time_t getTime() const { + [[nodiscard]] time_t getTime() const { return time_; } - std::string toString() const; + [[nodiscard]] std::string toString() const; // getters/setters - double lat() const { + [[nodiscard]] double lat() const { return lat_; } - double lon() const { + [[nodiscard]] double lon() const { return lon_; } - double ele() const { + [[nodiscard]] double ele() const { return ele_; } - int delta() const { + [[nodiscard]] int delta() const { return delta_; } void delta(int delta) { diff --git a/src/basicio.cpp b/src/basicio.cpp index da320f89..97f226c4 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -619,19 +619,19 @@ class EXIV2API BlockMap { size_ = num; } - bool isNone() const { + [[nodiscard]] bool isNone() const { return type_ == bNone; } - bool isKnown() const { + [[nodiscard]] bool isKnown() const { return type_ == bKnown; } - byte* getData() const { + [[nodiscard]] byte* getData() const { return data_; } - size_t getSize() const { + [[nodiscard]] size_t getSize() const { return size_; } diff --git a/src/convert.cpp b/src/convert.cpp index fa5fa739..61e632a6 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -262,7 +262,7 @@ class Converter { //! @name Accessors //@{ //! Get the value of the erase flag, see also setErase(bool on). - bool erase() const { + [[nodiscard]] bool erase() const { return erase_; } //@} diff --git a/src/exif.cpp b/src/exif.cpp index 77e194ff..26b562ad 100644 --- a/src/exif.cpp +++ b/src/exif.cpp @@ -76,17 +76,17 @@ class Thumbnail { @brief Return the thumbnail image in a %DataBuf. The caller owns the data buffer and %DataBuf ensures that it will be deleted. */ - virtual Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const = 0; + [[nodiscard]] virtual Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const = 0; /*! @brief Return the MIME type of the thumbnail ("image/tiff" or "image/jpeg"). */ - virtual const char* mimeType() const = 0; + [[nodiscard]] virtual const char* mimeType() const = 0; /*! @brief Return the file extension for the format of the thumbnail (".tif", ".jpg"). */ - virtual const char* extension() const = 0; + [[nodiscard]] virtual const char* extension() const = 0; //@} }; // class Thumbnail @@ -100,9 +100,9 @@ class TiffThumbnail : public Thumbnail { //! @name Accessors //@{ - Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const override; - const char* mimeType() const override; - const char* extension() const override; + [[nodiscard]] Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const override; + [[nodiscard]] const char* mimeType() const override; + [[nodiscard]] const char* extension() const override; //@} }; // class TiffThumbnail @@ -116,9 +116,9 @@ class JpegThumbnail : public Thumbnail { //! @name Accessors //@{ - Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const override; - const char* mimeType() const override; - const char* extension() const override; + [[nodiscard]] Exiv2::DataBuf copy(const Exiv2::ExifData& exifData) const override; + [[nodiscard]] const char* mimeType() const override; + [[nodiscard]] const char* extension() const override; //@} }; // class JpegThumbnail diff --git a/src/preview.cpp b/src/preview.cpp index e1faecd4..82d9899c 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -58,15 +58,15 @@ class Loader { static UniquePtr create(PreviewId id, const Image& image); //! Check if a preview image with given params exists in the image - virtual bool valid() const { + [[nodiscard]] virtual bool valid() const { return valid_; } //! Get properties of a preview image with given params - virtual PreviewProperties getProperties() const; + [[nodiscard]] virtual PreviewProperties getProperties() const; //! Get a buffer that contains the preview image - virtual DataBuf getData() const = 0; + [[nodiscard]] virtual DataBuf getData() const = 0; //! Read preview image dimensions when they are not available directly virtual bool readDimensions() { @@ -119,10 +119,10 @@ class LoaderNative : public Loader { LoaderNative(PreviewId id, const Image& image, int parIdx); //! Get properties of a preview image with given params - PreviewProperties getProperties() const override; + [[nodiscard]] PreviewProperties getProperties() const override; //! Get a buffer that contains the preview image - DataBuf getData() const override; + [[nodiscard]] DataBuf getData() const override; //! Read preview image dimensions bool readDimensions() override; @@ -142,10 +142,10 @@ class LoaderExifJpeg : public Loader { LoaderExifJpeg(PreviewId id, const Image& image, int parIdx); //! Get properties of a preview image with given params - PreviewProperties getProperties() const override; + [[nodiscard]] PreviewProperties getProperties() const override; //! Get a buffer that contains the preview image - DataBuf getData() const override; + [[nodiscard]] DataBuf getData() const override; //! Read preview image dimensions bool readDimensions() override; @@ -175,10 +175,10 @@ class LoaderExifDataJpeg : public Loader { LoaderExifDataJpeg(PreviewId id, const Image& image, int parIdx); //! Get properties of a preview image with given params - PreviewProperties getProperties() const override; + [[nodiscard]] PreviewProperties getProperties() const override; //! Get a buffer that contains the preview image - DataBuf getData() const override; + [[nodiscard]] DataBuf getData() const override; //! Read preview image dimensions bool readDimensions() override; @@ -207,10 +207,10 @@ class LoaderTiff : public Loader { LoaderTiff(PreviewId id, const Image& image, int parIdx); //! Get properties of a preview image with given params - PreviewProperties getProperties() const override; + [[nodiscard]] PreviewProperties getProperties() const override; //! Get a buffer that contains the preview image - DataBuf getData() const override; + [[nodiscard]] DataBuf getData() const override; protected: //! Name of the group that contains the preview image @@ -243,10 +243,10 @@ class LoaderXmpJpeg : public Loader { LoaderXmpJpeg(PreviewId id, const Image& image, int parIdx); //! Get properties of a preview image with given params - PreviewProperties getProperties() const override; + [[nodiscard]] PreviewProperties getProperties() const override; //! Get a buffer that contains the preview image - DataBuf getData() const override; + [[nodiscard]] DataBuf getData() const override; //! Read preview image dimensions bool readDimensions() override; diff --git a/src/tags.cpp b/src/tags.cpp index 679ee9fa..50939665 100644 --- a/src/tags.cpp +++ b/src/tags.cpp @@ -168,7 +168,7 @@ struct ExifKey::Impl { //! @name Accessors //@{ //! Return the name of the tag - std::string tagName() const; + [[nodiscard]] std::string tagName() const; //@} // DATA From 06d977d7bc35227cdc608a9eeb67366de7a0dd24 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:28:21 -0700 Subject: [PATCH 03/13] clang-tidy: use default Signed-off-by: Rosen Penev --- include/exiv2/error.hpp | 2 +- src/error.cpp | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/include/exiv2/error.hpp b/include/exiv2/error.hpp index 408eef0a..8ef0f841 100644 --- a/include/exiv2/error.hpp +++ b/include/exiv2/error.hpp @@ -262,7 +262,7 @@ class EXIV2API Error : public std::exception { } //! Virtual destructor. (Needed because of throw()) - ~Error() noexcept override; + ~Error() noexcept override = default; //@} //! @name Accessors diff --git a/src/error.cpp b/src/error.cpp index dd2ed5c2..4a53e751 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -153,9 +153,6 @@ Error::Error(ErrorCode code) : code_(code) { setMsg(0); } -Error::~Error() noexcept { -} - ErrorCode Error::code() const noexcept { return code_; } From aa41d557b276a03223cb103a94c16e85910261a4 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:31:05 -0700 Subject: [PATCH 04/13] function move Signed-off-by: Rosen Penev --- src/xmp.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/xmp.cpp b/src/xmp.cpp index cf88221f..fd1f9cd8 100644 --- a/src/xmp.cpp +++ b/src/xmp.cpp @@ -232,7 +232,8 @@ Exiv2::XmpKey::UniquePtr makeXmpKey(const std::string& schemaNs, const std::stri //! Helper class used to serialize critical sections class AutoLock { public: - AutoLock(Exiv2::XmpParser::XmpLockFct xmpLockFct, void* pLockData) : xmpLockFct_(xmpLockFct), pLockData_(pLockData) { + AutoLock(Exiv2::XmpParser::XmpLockFct xmpLockFct, void* pLockData) : + xmpLockFct_(std::move(xmpLockFct)), pLockData_(pLockData) { if (xmpLockFct_) xmpLockFct_(pLockData_, true); } @@ -510,7 +511,7 @@ void* XmpParser::pLockData_ = nullptr; #ifdef EXV_HAVE_XMP_TOOLKIT bool XmpParser::initialize(XmpParser::XmpLockFct xmpLockFct, void* pLockData) { if (!initialized_) { - xmpLockFct_ = xmpLockFct; + xmpLockFct_ = std::move(xmpLockFct); pLockData_ = pLockData; initialized_ = SXMPMeta::Initialize(); #ifdef EXV_ADOBE_XMPSDK From dfb93dd8cdf6d21fb529c77a8a6fce87da27df84 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:35:29 -0700 Subject: [PATCH 05/13] clang-tidy: use C++ headers Signed-off-by: Rosen Penev --- src/convert.cpp | 2 +- src/http.cpp | 2 +- unitTests/test_slice.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/convert.cpp b/src/convert.cpp index 61e632a6..11aae7b6 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -26,8 +26,8 @@ #endif #ifdef EXV_HAVE_ICONV -#include #include +#include #endif // Adobe XMP Toolkit diff --git a/src/http.cpp b/src/http.cpp index 0e32ab3c..04e83e74 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -23,12 +23,12 @@ #define closesocket close #include -#include #include #include #include #include #include +#include #define fopen_S(f, n, o) f = fopen(n, o) #define WINAPI diff --git a/unitTests/test_slice.cpp b/unitTests/test_slice.cpp index ac9b90f3..575a2bcc 100644 --- a/unitTests/test_slice.cpp +++ b/unitTests/test_slice.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include -#include +#include #include #include "slice.hpp" #include "types.hpp" From b25e75dd7bd82df451e451be5dc77019effab854 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:38:39 -0700 Subject: [PATCH 06/13] replace Sleep with sleep_for Signed-off-by: Rosen Penev --- src/http.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index 04e83e74..fc7c244e 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -11,6 +11,8 @@ #include "http.hpp" #include +#include +#include //////////////////////////////////////// // platform specific code @@ -41,13 +43,6 @@ using DWORD = unsigned long; static int WSAGetLastError() { return errno; } - -static void Sleep(int millisecs) { - const struct timespec rqtp = {0, millisecs * 1000000}; - struct timespec rmtp; - nanosleep(&rqtp, &rmtp); -} - #endif //////////////////////////////////////// @@ -246,7 +241,7 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st //////////////////////////////////// // send the header (we'll have to wait for the connection by the non-blocking socket) while (sleep_ >= 0 && send(sockfd, buffer, n, 0) == SOCKET_ERROR /* && WSAGetLastError() == WSAENOTCONN */) { - Sleep(snooze); + std::this_thread::sleep_for(std::chrono::milliseconds(snooze)); sleep_ -= snooze; } @@ -330,7 +325,7 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st } n = forgive(recv(sockfd, buffer + end, static_cast(buff_l - end), 0), err); if (!n) { - Sleep(snooze); + std::this_thread::sleep_for(std::chrono::milliseconds(snooze)); sleep_ -= snooze; if (sleep_ < 0) n = FINISH; From 3298fcf656c9618a11a57d1ef57a9c4e50f0426b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 29 Mar 2022 23:46:14 -0700 Subject: [PATCH 07/13] some chrono Signed-off-by: Rosen Penev --- src/http.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index fc7c244e..e8882ded 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -12,6 +12,7 @@ #include #include +#include #include //////////////////////////////////////// @@ -65,8 +66,8 @@ static constexpr std::array blankLines{ "\n\n", // this is commonly sent by CGI scripts }; -static constexpr int snooze = 0; -static int sleep_ = 1000; +static constexpr auto snooze = std::chrono::milliseconds::zero(); +static auto sleep_ = std::chrono::milliseconds(1000); static int forgive(int n, int& err) { err = WSAGetLastError(); @@ -240,12 +241,13 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st //////////////////////////////////// // send the header (we'll have to wait for the connection by the non-blocking socket) - while (sleep_ >= 0 && send(sockfd, buffer, n, 0) == SOCKET_ERROR /* && WSAGetLastError() == WSAENOTCONN */) { - std::this_thread::sleep_for(std::chrono::milliseconds(snooze)); + while (sleep_ >= std::chrono::milliseconds::zero() && + send(sockfd, buffer, n, 0) == SOCKET_ERROR /* && WSAGetLastError() == WSAENOTCONN */) { + std::this_thread::sleep_for(snooze); sleep_ -= snooze; } - if (sleep_ < 0) + if (sleep_ < std::chrono::milliseconds::zero()) return error(errors, "error - timeout connecting to server = %s port = %s wsa_error = %d", servername, port, WSAGetLastError()); @@ -325,16 +327,16 @@ int Exiv2::http(Exiv2::Dictionary& request, Exiv2::Dictionary& response, std::st } n = forgive(recv(sockfd, buffer + end, static_cast(buff_l - end), 0), err); if (!n) { - std::this_thread::sleep_for(std::chrono::milliseconds(snooze)); + std::this_thread::sleep_for(snooze); sleep_ -= snooze; - if (sleep_ < 0) + if (sleep_ < std::chrono::milliseconds::zero()) n = FINISH; } } if (n != FINISH || !OK(status)) { - snprintf(buffer, sizeof buffer, "wsa_error = %d,n = %d,sleep_ = %d status = %d", WSAGetLastError(), n, sleep_, - status); + snprintf(buffer, sizeof buffer, "wsa_error = %d,n = %d,sleep_ = %d status = %d", WSAGetLastError(), n, + int(sleep_.count()), status); error(errors, buffer, nullptr, nullptr, 0); } else if (bSearching && OK(status)) { if (end) { From c35fb7104b850806fcc9b9949a554416bc604404 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 31 Mar 2022 17:55:27 -0700 Subject: [PATCH 08/13] clang-tidy: use any_of Signed-off-by: Rosen Penev --- src/jpgimage.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index 13e59471..01b22f33 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -38,11 +38,7 @@ static inline bool inRange2(int value, int lo1, int hi1, int lo2, int hi2) { bool Photoshop::isIrb(const byte* pPsData, size_t sizePsData) { if (sizePsData < 4) return false; - for (auto&& i : irbId_) { - if (memcmp(pPsData, i, 4) == 0) - return true; - } - return false; + return std::any_of(irbId_.begin(), irbId_.end(), [pPsData](auto id) { return memcmp(pPsData, id, 4) == 0; }); } bool Photoshop::valid(const byte* pPsData, size_t sizePsData) { From 8810b0b630e2e3576b86f769eac0bcca5d6339f2 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 31 Mar 2022 17:58:23 -0700 Subject: [PATCH 09/13] clang-tidy: no else after return Signed-off-by: Rosen Penev --- samples/ini-test.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/samples/ini-test.cpp b/samples/ini-test.cpp index 06085483..122c8cb2 100644 --- a/samples/ini-test.cpp +++ b/samples/ini-test.cpp @@ -25,16 +25,15 @@ int main() { if (reader.ParseError() < 0) { std::cerr << "Can't load '" << ini << "'" << std::endl; return EXIT_FAILURE; - } else { - std::cout << "Config loaded from : '" << ini << "' " - << "version=" << reader.GetInteger("protocol", "version", -1) - << ", name=" << reader.Get("user", "name", "UNKNOWN") - << ", email=" << reader.Get("user", "email", "UNKNOWN") << ", pi=" << reader.GetReal("user", "pi", -1) - << ", active=" << reader.GetBoolean("user", "active", true) << std::endl; - - std::cout << "169=" << reader.Get("canon", "169", "UNDEFINED") - << ", 170=" << reader.Get("canon", "170", "UNDEFINED") << std::endl; } + std::cout << "Config loaded from : '" << ini << "' " + << "version=" << reader.GetInteger("protocol", "version", -1) + << ", name=" << reader.Get("user", "name", "UNKNOWN") + << ", email=" << reader.Get("user", "email", "UNKNOWN") << ", pi=" << reader.GetReal("user", "pi", -1) + << ", active=" << reader.GetBoolean("user", "active", true) << std::endl; + + std::cout << "169=" << reader.Get("canon", "169", "UNDEFINED") << ", 170=" << reader.Get("canon", "170", "UNDEFINED") + << std::endl; return EXIT_SUCCESS; } From d190baac6cbfec367a3eca18b9c1dcbd2a784d6c Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 31 Mar 2022 18:06:26 -0700 Subject: [PATCH 10/13] clang-tidy: do not use using namespace Found with google-build-using-namespace Signed-off-by: Rosen Penev --- src/cr2image.cpp | 25 ++++++++++++------------- src/crwimage.cpp | 11 +++++------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/cr2image.cpp b/src/cr2image.cpp index 00624ad9..79814811 100644 --- a/src/cr2image.cpp +++ b/src/cr2image.cpp @@ -17,7 +17,6 @@ // ***************************************************************************** // class member definitions namespace Exiv2 { -using namespace Internal; Cr2Image::Cr2Image(BasicIo::UniquePtr io, bool /*create*/) : Image(ImageType::cr2, mdExif | mdIptc | mdXmp, std::move(io)) { @@ -87,7 +86,7 @@ void Cr2Image::writeMetadata() { if (isCr2Type(*io_, false)) { pData = io_->mmap(true); size = io_->size(); - Cr2Header cr2Header; + Internal::Cr2Header cr2Header; if (0 == cr2Header.read(pData, 16)) { bo = cr2Header.byteOrder(); } @@ -101,9 +100,9 @@ void Cr2Image::writeMetadata() { } // Cr2Image::writeMetadata ByteOrder Cr2Parser::decode(ExifData& exifData, IptcData& iptcData, XmpData& xmpData, const byte* pData, size_t size) { - Cr2Header cr2Header; - return TiffParserWorker::decode(exifData, iptcData, xmpData, pData, size, Tag::root, TiffMapping::findDecoder, - &cr2Header); + Internal::Cr2Header cr2Header; + return Internal::TiffParserWorker::decode(exifData, iptcData, xmpData, pData, size, Internal::Tag::root, + Internal::TiffMapping::findDecoder, &cr2Header); } WriteMethod Cr2Parser::encode(BasicIo& io, const byte* pData, size_t size, ByteOrder byteOrder, @@ -113,20 +112,20 @@ WriteMethod Cr2Parser::encode(BasicIo& io, const byte* pData, size_t size, ByteO // Delete IFDs which do not occur in TIFF images static constexpr auto filteredIfds = std::array{ - panaRawId, + Internal::panaRawId, }; for (auto&& filteredIfd : filteredIfds) { #ifdef EXIV2_DEBUG_MESSAGES std::cerr << "Warning: Exif IFD " << filteredIfd << " not encoded\n"; #endif - ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); + ed.erase(std::remove_if(ed.begin(), ed.end(), Internal::FindExifdatum(filteredIfd)), ed.end()); } - Cr2Header header(byteOrder); - OffsetWriter offsetWriter; - offsetWriter.setOrigin(OffsetWriter::cr2RawIfdOffset, Cr2Header::offset2addr(), byteOrder); - return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder, &header, - &offsetWriter); + auto header = Internal::Cr2Header(byteOrder); + Internal::OffsetWriter offsetWriter; + offsetWriter.setOrigin(Internal::OffsetWriter::cr2RawIfdOffset, Internal::Cr2Header::offset2addr(), byteOrder); + return Internal::TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Internal::Tag::root, + Internal::TiffMapping::findEncoder, &header, &offsetWriter); } // ************************************************************************* @@ -146,7 +145,7 @@ bool isCr2Type(BasicIo& iIo, bool advance) { if (iIo.error() || iIo.eof()) { return false; } - Cr2Header header; + Internal::Cr2Header header; bool rc = header.read(buf, len); if (!advance || !rc) { iIo.seek(-len, BasicIo::cur); diff --git a/src/crwimage.cpp b/src/crwimage.cpp index 84bc9442..ff217c7a 100644 --- a/src/crwimage.cpp +++ b/src/crwimage.cpp @@ -18,7 +18,6 @@ // ***************************************************************************** // class member definitions namespace Exiv2 { -using namespace Internal; CrwImage::CrwImage(BasicIo::UniquePtr io, bool /*create*/) : Image(ImageType::crw, mdExif | mdComment, std::move(io)) { } // CrwImage::CrwImage @@ -102,12 +101,12 @@ void CrwImage::writeMetadata() { void CrwParser::decode(CrwImage* pCrwImage, const byte* pData, size_t size) { // Parse the image, starting with a CIFF header component - CiffHeader header; + Internal::CiffHeader header; header.read(pData, size); header.decode(*pCrwImage); // a hack to get absolute offset of preview image inside CRW structure - CiffComponent* preview = header.findComponent(0x2007, 0x0000); + auto preview = header.findComponent(0x2007, 0x0000); if (preview) { (pCrwImage->exifData())["Exif.Image2.JPEGInterchangeFormat"] = uint32_t(preview->pData() - pData); (pCrwImage->exifData())["Exif.Image2.JPEGInterchangeFormatLength"] = static_cast(preview->size()); @@ -116,14 +115,14 @@ void CrwParser::decode(CrwImage* pCrwImage, const byte* pData, size_t size) { void CrwParser::encode(Blob& blob, const byte* pData, size_t size, const CrwImage* pCrwImage) { // Parse image, starting with a CIFF header component - CiffHeader header; + Internal::CiffHeader header; if (size != 0) { header.read(pData, size); } // Encode Exif tags from image into the CRW parse tree and write the // structure to the binary image blob - CrwMap::encode(&header, *pCrwImage); + Internal::CrwMap::encode(&header, *pCrwImage); header.write(blob); } @@ -147,7 +146,7 @@ bool isCrwType(BasicIo& iIo, bool advance) { if (!(('I' == tmpBuf[0] && 'I' == tmpBuf[1]) || ('M' == tmpBuf[0] && 'M' == tmpBuf[1]))) { result = false; } - if (result && std::memcmp(tmpBuf + 6, CiffHeader::signature(), 8) != 0) { + if (result && std::memcmp(tmpBuf + 6, Internal::CiffHeader::signature(), 8) != 0) { result = false; } if (!advance || !result) From e9896d1f9945942a0f144b9262409c77d5810082 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 1 Apr 2022 17:33:13 -0700 Subject: [PATCH 11/13] clang-tidy: fix inconsistent decleration Signed-off-by: Rosen Penev --- src/basicio.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basicio.cpp b/src/basicio.cpp index 97f226c4..d5e0f836 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -1525,7 +1525,7 @@ HttpIo::HttpIo(const std::string& url, size_t blockSize) { class CurlIo::CurlImpl : public Impl { public: //! Constructor - CurlImpl(const std::string& path, size_t blockSize); + CurlImpl(const std::string& url, size_t blockSize); //! Destructor. Cleans up the curl pointer and releases all managed memory. ~CurlImpl() override; From e82be13b73927221eaba71c80d45596b95a11f4e Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 1 Apr 2022 17:38:06 -0700 Subject: [PATCH 12/13] C to std::function Signed-off-by: Rosen Penev --- src/convert.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.cpp b/src/convert.cpp index 11aae7b6..8bbdfd38 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -1527,7 +1527,7 @@ bool asciiToUtf8(std::string& /*str*/) { return true; } -using ConvFct = bool (*)(std::string& str); +using ConvFct = std::function; struct ConvFctList { bool operator==(const std::pair& fromTo) const { From 262d0ee4b360d470d8ac25480fe3776b398af152 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 1 Apr 2022 18:00:04 -0700 Subject: [PATCH 13/13] struct to std::pair conversions Signed-off-by: Rosen Penev --- src/crwimage_int.cpp | 27 ++++++++++++--------------- src/crwimage_int.hpp | 8 +------- src/tags_int.hpp | 19 ++++++++----------- src/tiffimage_int.hpp | 23 ++++++----------------- 4 files changed, 27 insertions(+), 50 deletions(-) diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index 6010ac0f..01a39c55 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -22,10 +22,7 @@ class RotationMap { private: //! Helper structure for the mapping list - struct OmList { - uint16_t orientation; //!< Exif orientation value - int32_t degrees; //!< CRW Rotation degrees - }; + using OmList = std::pair; // DATA static const OmList omList_[]; }; // class RotationMap @@ -520,7 +517,7 @@ CiffComponent* CiffDirectory::doFindComponent(uint16_t crwTagId, uint16_t crwDir void CiffHeader::add(uint16_t crwTagId, uint16_t crwDir, DataBuf&& buf) { CrwDirs crwDirs; CrwMap::loadStack(crwDirs, crwDir); - [[maybe_unused]] uint16_t rootDirectory = crwDirs.top().crwDir_; + [[maybe_unused]] auto [rootDirectory, _] = crwDirs.top(); crwDirs.pop(); if (!pRootDir_) { pRootDir_ = std::make_unique(); @@ -553,18 +550,18 @@ CiffComponent* CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) { set value */ if (!crwDirs.empty()) { - CrwSubDir csd = crwDirs.top(); + auto [dir, parent] = crwDirs.top(); crwDirs.pop(); // Find the directory for (auto&& component : components_) { - if (component->tag() == csd.crwDir_) { + if (component->tag() == dir) { cc_ = component; break; } } if (!cc_) { // Directory doesn't exist yet, add it - m_ = std::make_unique(csd.crwDir_, csd.parent_); + m_ = std::make_unique(dir, parent); cc_ = m_.get(); add(std::move(m_)); } @@ -592,7 +589,6 @@ void CiffHeader::remove(uint16_t crwTagId, uint16_t crwDir) { if (pRootDir_) { CrwDirs crwDirs; CrwMap::loadStack(crwDirs, crwDir); - [[maybe_unused]] uint16_t rootDirectory = crwDirs.top().crwDir_; crwDirs.pop(); pRootDir_->remove(crwDirs, crwTagId); } @@ -608,11 +604,11 @@ void CiffComponent::doRemove(CrwDirs& /*crwDirs*/, uint16_t /*crwTagId*/) { void CiffDirectory::doRemove(CrwDirs& crwDirs, uint16_t crwTagId) { if (!crwDirs.empty()) { - CrwSubDir csd = crwDirs.top(); + auto [dir, _] = crwDirs.top(); crwDirs.pop(); // Find the directory for (auto i = components_.begin(); i != components_.end(); ++i) { - if ((*i)->tag() == csd.crwDir_) { + if ((*i)->tag() == dir) { // Recursive call to next lower level directory (*i)->remove(crwDirs, crwTagId); if ((*i)->empty()) @@ -743,8 +739,8 @@ void CrwMap::decodeArray(const CiffComponent& ciffComponent, const CrwMapping* p if (ifdId == canonSiId) { // Exif.Photo.FNumber float f = fnumber(canonEv(aperture)); - Rational r = floatToRationalCast(f); - URational ur(r.first, r.second); + auto [r, s] = floatToRationalCast(f); + auto ur = URational(r, s); URationalValue fn; fn.value_.push_back(ur); image.exifData().add(ExifKey("Exif.Photo.FNumber"), &fn); @@ -835,9 +831,10 @@ void CrwMap::decodeBasic(const CiffComponent& ciffComponent, const CrwMapping* p void CrwMap::loadStack(CrwDirs& crwDirs, uint16_t crwDir) { for (auto&& crw : crwSubDir_) { - if (crw.crwDir_ == crwDir) { + auto&& [dir, parent] = crw; + if (dir == crwDir) { crwDirs.push(crw); - crwDir = crw.parent_; + crwDir = parent; } } } // CrwMap::loadStack diff --git a/src/crwimage_int.hpp b/src/crwimage_int.hpp index fd5ce6a0..621ad74c 100644 --- a/src/crwimage_int.hpp +++ b/src/crwimage_int.hpp @@ -20,7 +20,7 @@ namespace Exiv2::Internal { class CiffHeader; class CiffComponent; struct CrwMapping; -struct CrwSubDir; +using CrwSubDir = std::pair; // ***************************************************************************** // type definitions @@ -486,12 +486,6 @@ class CiffHeader { }; // class CiffHeader -//! Structure for the CIFF directory hierarchy -struct CrwSubDir { - uint16_t crwDir_; //!< Directory tag - uint16_t parent_; //!< Parent directory tag -}; // struct CrwSubDir - /*! @brief Structure for a mapping table for conversion of CIFF entries to image metadata and vice versa. diff --git a/src/tags_int.hpp b/src/tags_int.hpp index 0e95ed5e..ae4a54c7 100644 --- a/src/tags_int.hpp +++ b/src/tags_int.hpp @@ -213,10 +213,7 @@ struct TagDetails { @brief Helper structure for lookup tables for translations of bitmask values to human readable labels. */ -struct TagDetailsBitmask { - uint32_t mask_; //!< Bitmask value - const char* label_; //!< Description of the tag value -}; // struct TagDetailsBitmask +using TagDetailsBitmask = std::pair; /*! @brief Helper structure for lookup tables for translations of controlled @@ -271,20 +268,20 @@ template std::ostream& printTagBitmask(std::ostream& os, const Value& value, const ExifData*) { const auto val = value.toUint32(); if (val == 0 && N > 0) { - const TagDetailsBitmask* td = *(&array); - if (td->mask_ == 0) - return os << exvGettext(td->label_); + auto [mask, label] = *array; + if (mask == 0) + return os << exvGettext(label); } bool sep = false; for (int i = 0; i < N; ++i) { // *& acrobatics is a workaround for a MSVC 7.1 bug - const TagDetailsBitmask* td = *(&array) + i; + auto [mask, label] = *(array + i); - if (val & td->mask_) { + if (val & mask) { if (sep) { - os << ", " << exvGettext(td->label_); + os << ", " << exvGettext(label); } else { - os << exvGettext(td->label_); + os << exvGettext(label); sep = true; } } diff --git a/src/tiffimage_int.hpp b/src/tiffimage_int.hpp index 58d4c124..a34da015 100644 --- a/src/tiffimage_int.hpp +++ b/src/tiffimage_int.hpp @@ -134,17 +134,11 @@ class TiffHeader : public TiffHeaderBase { */ struct TiffImgTagStruct { //! Search key for TIFF image tag structure. - struct Key { - //! Constructor - Key(uint16_t t, IfdId g) : t_(t), g_(g) { - } - uint16_t t_; //!< %Tag - IfdId g_; //!< %Group - }; - + using Key = std::pair; //! Comparison operator to compare a TiffImgTagStruct with a TiffImgTagStruct::Key bool operator==(const Key& key) const { - return key.g_ == group_ && key.t_ == tag_; + auto [t, g] = key; + return g == group_ && t == tag_; } // DATA @@ -158,17 +152,12 @@ struct TiffImgTagStruct { */ struct TiffGroupStruct { //! Search key for TIFF group structure. - struct Key { - //! Constructor - Key(uint32_t e, IfdId g) : e_(e), g_(g) { - } - uint32_t e_; //!< Extended tag - IfdId g_; //!< %Group - }; + using Key = std::pair; //! Comparison operator to compare a TiffGroupStruct with a TiffGroupStruct::Key bool operator==(const Key& key) const { - return key.g_ == group_ && (Tag::all == extendedTag_ || key.e_ == extendedTag_); + auto [e, g] = key; + return g == group_ && (Tag::all == extendedTag_ || e == extendedTag_); } //! Return the tag corresponding to the extended tag [[nodiscard]] uint16_t tag() const {