Extract out common code for two switch-cases into a function to fix warning

The warning was caused by -Wimplicit-fallthrough:
https://developers.redhat.com/blog/2017/03/10/wimplicit-fallthrough-in-gcc-7/

I also realized that the method printStructure was duplicated in the
classes Print and Erase. By moving the implementation to a free function
into the cpp file, I could remove the duplication in the code.
This commit is contained in:
Luis Díaz Más
2018-05-25 15:10:16 +02:00
parent 335607054c
commit 88ba920962
2 changed files with 27 additions and 39 deletions
+27 -33
View File
@@ -158,6 +158,9 @@ namespace {
os << make_pair( myString, width)
*/
std::ostream& operator<<( std::ostream& os, std::pair<std::string, int> strAndWidth);
//! Print image Structure information
int printStructure(std::ostream& out, Exiv2::PrintStructureOption option, const std::string &path);
}
// *****************************************************************************
@@ -232,6 +235,12 @@ namespace Action {
{
}
int setModeAndPrintStructure(Exiv2::PrintStructureOption option, const std::string& path)
{
_setmode(_fileno(stdout),O_BINARY);
return printStructure(std::cout, option, path);
}
int Print::run(const std::string& path)
{
try {
@@ -243,18 +252,17 @@ namespace Action {
case Params::pmList: rc = printList(); break;
case Params::pmComment: rc = printComment(); break;
case Params::pmPreview: rc = printPreviewList(); break;
case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic) ; break;
case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive) ; break;
case Params::pmStructure: rc = printStructure(std::cout,Exiv2::kpsBasic, path_) ; break;
case Params::pmRecursive: rc = printStructure(std::cout,Exiv2::kpsRecursive, path_) ; break;
case Params::pmXMP:
if (option == Exiv2::kpsNone)
option = Exiv2::kpsXMP;
// drop
rc = setModeAndPrintStructure(option, path_);
break;
case Params::pmIccProfile:
if (option == Exiv2::kpsNone)
option = Exiv2::kpsIccProfile;
_setmode(_fileno(stdout),O_BINARY);
rc = printStructure(std::cout,option);
rc = setModeAndPrintStructure(option, path_);
break;
}
return rc;
@@ -271,19 +279,6 @@ namespace Action {
}
}
int Print::printStructure(std::ostream& out, Exiv2::PrintStructureOption option)
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_ << ": "
<< _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->printStructure(out,option);
return 0;
}
int Print::printSummary()
{
if (!Exiv2::fileExists(path_, true)) {
@@ -962,7 +957,7 @@ namespace Action {
rc = eraseIccProfile(image.get());
}
if (0 == rc && Params::instance().target_ & Params::ctIptcRaw) {
rc = printStructure(std::cout,Exiv2::kpsIptcErase);
rc = printStructure(std::cout,Exiv2::kpsIptcErase,path_);
}
if (0 == rc) {
@@ -979,19 +974,6 @@ namespace Action {
return 1;
} // Erase::run
int Erase::printStructure(std::ostream& out, Exiv2::PrintStructureOption option)
{
if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_ << ": "
<< _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0);
image->printStructure(out,option);
return 0;
}
int Erase::eraseThumbnail(Exiv2::Image* image) const
{
Exiv2::ExifThumb exifThumb(image->exifData());
@@ -2379,4 +2361,16 @@ namespace {
return os << std::setw( minChCount) << str;
}
int printStructure(std::ostream& out, Exiv2::PrintStructureOption option, const std::string &path)
{
if (!Exiv2::fileExists(path, true)) {
std::cerr << path << ": "
<< _("Failed to open the file\n");
return -1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
assert(image.get() != 0);
image->printStructure(out,option);
return 0;
}
}
-6
View File
@@ -179,8 +179,6 @@ namespace Action {
bool printMetadatum(const Exiv2::Metadatum& md, const Exiv2::Image* image);
//! Print the label for a summary line
void printLabel(const std::string& label) const;
//! Print image Structure information
int printStructure(std::ostream& out, Exiv2::PrintStructureOption option);
/*!
@brief Print one summary line with a label (if provided) and requested
data. A line break is printed only if a label is provided.
@@ -277,10 +275,6 @@ namespace Action {
@brief Erase ICCProfile from the file.
*/
int eraseIccProfile(Exiv2::Image* image) const;
/*!
@brief Print image Structure information (used by ctIptcRaw/kpsIptcErase)
*/
int printStructure(std::ostream& out, Exiv2::PrintStructureOption option);
private: