From 0827a76b6a1f4dc2818c4a87a1fe6357a129c18f Mon Sep 17 00:00:00 2001 From: postscript-dev Date: Fri, 16 Jul 2021 11:04:13 +0100 Subject: [PATCH] Update Sony2Fp makernote functions Minor changes using suggestions in #1777: + Simplify loop conditions + Add startsWith() in SonyMakerNote class + Change `if`/`else` to `switch` --- src/makernote_int.cpp | 4 +--- src/sonymn_int.cpp | 39 ++++++++++++++++++++++++++------------- src/sonymn_int.hpp | 5 +++++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/makernote_int.cpp b/src/makernote_int.cpp index e21fa105..1598503a 100644 --- a/src/makernote_int.cpp +++ b/src/makernote_int.cpp @@ -1201,10 +1201,8 @@ namespace Exiv2 { int sony2FpSelector(uint16_t /*tag*/, const byte* /*pData*/, uint32_t /*size*/, TiffComponent* const pRoot) { // Not valid for models beginning - const char* models[] = { "SLT-", "HV", "ILCA-" }; - std::string model = getExifModel(pRoot); - for (auto&& m : models) { + for (auto& m : { "SLT-", "HV", "ILCA-" }) { if (startsWith(model, m)) return -1; } diff --git a/src/sonymn_int.cpp b/src/sonymn_int.cpp index a682ab68..4719892c 100644 --- a/src/sonymn_int.cpp +++ b/src/sonymn_int.cpp @@ -814,7 +814,7 @@ namespace Exiv2 { { 0x17, "AFAreaMode" , N_("AF area mode") , N_("Auto focus area mode"), sony2FpId, makerTags, unsignedByte, 1, EXV_PRINT_TAG(sony2FpAFAreaMode)}, { 0x2d, "FocusPosition2" , N_("Focus position 2") , N_("Focus position 2") , sony2FpId, makerTags, unsignedByte, 1, printSony2FpFocusPosition2}, // End of list marker - {0xffff, "(UnknownSony2FpTag)", "(UnknownSony2FpTag)" , "(UnknownSony2FpTag)" , sony2FpId, makerTags, unsignedByte, 1, printValue}, + {0xffff, "(UnknownSony2FpTag)", "(Unknown Sony2Fp tag)" , "(Unknown Sony2Fp tag)" , sony2FpId, makerTags, unsignedByte, 1, printValue}, }; const TagInfo* SonyMakerNote::tagListFp() @@ -828,18 +828,25 @@ namespace Exiv2 { os << value; else { long val = (value.toLong() & 0x7F); - if (val == 0) + switch (val) { + case 0: os << N_("Manual"); - else if (val == 2) + break; + case 2: os << N_("AF-S"); - else if (val == 3) + break; + case 3: os << N_("AF-C"); - else if (val == 4) + break; + case 4: os << N_("AF-A"); - else if (val == 6) + break; + case 6: os << N_("DMF"); - else + break; + default: os << "(" << val << ")"; + } } return os; @@ -855,20 +862,21 @@ namespace Exiv2 { return os << "(" << value << ")"; // Ranges of models that do not support this tag - const char* models[] = { "DSC-", "Stellar" }; - std::string model = pos->toString(); - for (auto&& m : models) { - if (model.find(m) == 0) { + for (auto& m : { "DSC-", "Stellar" }) { + if (startsWith(model, m)) { os << N_("n/a"); return os; } } long val = value.toLong(); - if (val == 255) + switch (val) { + case 255: os << N_("Infinity"); - else + break; + default: os << val; + } } return os; } @@ -947,6 +955,11 @@ namespace Exiv2 { return tagInfo2010e_; } + bool SonyMakerNote::startsWith(const std::string& s, const std::string& start) + { + return s.size() >= start.size() && std::memcmp(s.data(), start.data(), start.size()) == 0; + } + // https://github.com/Exiv2/exiv2/pull/906#issuecomment-504338797 static DataBuf sonyTagCipher(uint16_t /* tag */, const byte* bytes, uint32_t size, TiffComponent* const /*object*/, bool bDecipher) { diff --git a/src/sonymn_int.hpp b/src/sonymn_int.hpp index 71764bd2..58768fe7 100644 --- a/src/sonymn_int.hpp +++ b/src/sonymn_int.hpp @@ -75,6 +75,11 @@ namespace Exiv2 { static const TagInfo tagInfoFp_[]; static const TagInfo tagInfoSonyMisc1_[]; static const TagInfo tagInfo2010e_[]; + + // TODO: When moving to C++20, this can be replace with + // std::basic_string::starts_with() . + // Suggested in https://github.com/Exiv2/exiv2/pull/1777 . + static bool startsWith(const std::string& s, const std::string& start); }; // class SonyMakerNote DataBuf sonyTagDecipher(uint16_t, const byte*, uint32_t, TiffComponent* const);