diff --git a/include/exiv2/image.hpp b/include/exiv2/image.hpp index 97f1900f..83d15340 100644 --- a/include/exiv2/image.hpp +++ b/include/exiv2/image.hpp @@ -72,9 +72,9 @@ namespace Exiv2 { typedef std::vector NativePreviewList; /*! - @brief options for printStructure + @brief Options for printStructure */ - typedef enum { kpsNone, kpsBasic, kpsXMP } printStructureOption_e ; + typedef enum { kpsNone, kpsBasic, kpsXMP } PrintStructureOption; /*! @brief Abstract base class defining the interface for an image. This is @@ -113,7 +113,7 @@ namespace Exiv2 { not valid (does not look like data of the specific image type). @caution This function is not thread safe and intended for exiv2 -pS for debugging. */ - virtual void printStructure(std::ostream& out,printStructureOption_e option=kpsNone); + virtual void printStructure(std::ostream& out, PrintStructureOption option =kpsNone); /*! @brief Read all metadata supported by a specific image format from the image. Before this method is called, the image metadata will be @@ -412,16 +412,6 @@ namespace Exiv2 { bool writeXmpFromPacket() const; //! Return list of native previews. This is meant to be used only by the PreviewManager. const NativePreviewList& nativePreviews() const; - /*! - @brief format a string in the pattern of \em sprintf \em . - */ - std::string stringFormat(const char* format, ...) const; - - /*! - @brief format binary for display in \em printStructure() \em . - */ - std::string binaryToString(DataBuf& buf,size_t size,size_t start=0) const; - //@} protected: diff --git a/include/exiv2/jpgimage.hpp b/include/exiv2/jpgimage.hpp index 9731a784..50eaffd0 100644 --- a/include/exiv2/jpgimage.hpp +++ b/include/exiv2/jpgimage.hpp @@ -159,7 +159,7 @@ namespace Exiv2 { not valid (does not look like data of the specific image type). @caution This function is not thread safe and intended for exiv2 -pS for debugging. */ - void printStructure(std::ostream& out,Exiv2::printStructureOption_e option); + void printStructure(std::ostream& out, PrintStructureOption option); //@} protected: diff --git a/include/exiv2/pngimage.hpp b/include/exiv2/pngimage.hpp index c668ae04..2fa940d8 100644 --- a/include/exiv2/pngimage.hpp +++ b/include/exiv2/pngimage.hpp @@ -93,7 +93,7 @@ namespace Exiv2 not valid (does not look like data of the specific image type). @caution This function is not thread safe and intended for exiv2 -pS for debugging. */ - void printStructure(std::ostream& out,Exiv2::printStructureOption_e option); + void printStructure(std::ostream& out, PrintStructureOption option); //@} //! @name Accessors diff --git a/include/exiv2/tiffimage.hpp b/include/exiv2/tiffimage.hpp index 4b2a756f..7ca67ade 100644 --- a/include/exiv2/tiffimage.hpp +++ b/include/exiv2/tiffimage.hpp @@ -93,7 +93,7 @@ namespace Exiv2 { not valid (does not look like data of the specific image type). @caution This function is not thread safe and intended for exiv2 -pS for debugging. */ - void printStructure(std::ostream& out,Exiv2::printStructureOption_e option); + void printStructure(std::ostream& out, PrintStructureOption option); /*! @brief Not supported. TIFF format does not contain a comment. diff --git a/src/actions.cpp b/src/actions.cpp index 6d8806ee..45687647 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -252,7 +252,7 @@ namespace Action { return 1; } // Print::run - int Print::printStructure(std::ostream& out,Exiv2::printStructureOption_e option) + int Print::printStructure(std::ostream& out, Exiv2::PrintStructureOption option) { if (!Exiv2::fileExists(path_, true)) { std::cerr << path_ << ": " diff --git a/src/image.cpp b/src/image.cpp index f134aca4..e443da7d 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -36,6 +36,7 @@ EXIV2_RCSID("@(#) $Id$") #include "config.h" #include "image.hpp" +#include "image_int.hpp" #include "error.hpp" #include "futils.hpp" @@ -167,7 +168,8 @@ namespace Exiv2 { Image::~Image() { } - void Image::printStructure(std::ostream&, printStructureOption_e) + + void Image::printStructure(std::ostream&, PrintStructureOption) { throw Error(13, io_->path()); } @@ -356,50 +358,6 @@ namespace Exiv2 { return ImageFactory::checkMode(imageType_, metadataId); } - std::string Image::stringFormat(const char* format, ...) const - { - std::string result; - - int need = (int) std::strlen(format)*2; // initial guess - char* buffer = NULL; - int again = 4; - int rc = -1; - - while (rc < 0 && again--) { - if ( buffer ) delete[] buffer; - need *= 2 ; - buffer = new char[need]; - if ( buffer ) { - va_list args; // variable arg list - va_start(args, format); // args start after format - rc=vsnprintf(buffer,(unsigned int)need, format, args); - va_end(args); // free the args - } - } - - if ( rc > 0 ) result = std::string(buffer) ; - if ( buffer ) delete[] buffer; // free buffer - return result; - } - - std::string Image::binaryToString(DataBuf& buf,size_t size,size_t start /* = 0 */) const - { - std::string result = ""; - byte* buff = buf.pData_; - - size += start; - - while (start < size) { - int c = (int) buff[start++] ; - bool bTrailingNull = c == 0 && start == size; - if ( !bTrailingNull ) { - if (c < ' ' || c > 127) c = '.' ; - result += (char) c ; - } - } - return result; - } - AccessMode ImageFactory::checkMode(int type, MetadataId metadataId) { const Registry* r = find(registry, type); @@ -619,3 +577,52 @@ namespace Exiv2 { } // append } // namespace Exiv2 + +namespace Exiv2 { + namespace Internal { + + std::string stringFormat(const char* format, ...) + { + std::string result; + + int need = (int) std::strlen(format)*2; // initial guess + char* buffer = NULL; + int again = 4; + int rc = -1; + + while (rc < 0 && again--) { + if ( buffer ) delete[] buffer; + need *= 2 ; + buffer = new char[need]; + if ( buffer ) { + va_list args; // variable arg list + va_start(args, format); // args start after format + rc=vsnprintf(buffer,(unsigned int)need, format, args); + va_end(args); // free the args + } + } + + if ( rc > 0 ) result = std::string(buffer) ; + if ( buffer ) delete[] buffer; // free buffer + return result; + } + + std::string binaryToString(DataBuf& buf, size_t size, size_t start /*=0*/) + { + std::string result = ""; + byte* buff = buf.pData_; + + size += start; + + while (start < size) { + int c = (int) buff[start++] ; + bool bTrailingNull = c == 0 && start == size; + if ( !bTrailingNull ) { + if (c < ' ' || c > 127) c = '.' ; + result += (char) c ; + } + } + return result; + } + +}} // namespace Internal, Exiv2 diff --git a/src/image_int.hpp b/src/image_int.hpp new file mode 100644 index 00000000..fbc54b8e --- /dev/null +++ b/src/image_int.hpp @@ -0,0 +1,59 @@ +// ***************************************************************** -*- C++ -*- +/* + * Copyright (C) 2004-2015 Andreas Huggel + * + * This program is part of the Exiv2 distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA. + */ +/*! + @file image_int.hpp + @brief Internal image helpers + @version $Rev: 3777 $ + @author Andreas Huggel (ahu) + ahuggel@gmx.net + @date 10-May-15, ahu: created + */ +#ifndef IMAGE_INT_HPP_ +#define IMAGE_INT_HPP_ + +// ***************************************************************************** +// included header files +#include "types.hpp" + +// + standard includes +#include + +// ***************************************************************************** +// namespace extensions +namespace Exiv2 { + namespace Internal { + +// ***************************************************************************** +// class definitions + + /*! + @brief format a string in the pattern of \em sprintf \em . + */ + std::string stringFormat(const char* format, ...); + + /*! + @brief format binary for display in \em printStructure() \em . + */ + std::string binaryToString(DataBuf& buf, size_t size, size_t start =0); + +}} // namespace Internal, Exiv2 + +#endif // #ifndef IMAGE_INT_HPP_ diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index e76b7831..37213901 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -35,6 +35,7 @@ EXIV2_RCSID("@(#) $Id$") #include "config.h" #include "jpgimage.hpp" +#include "image_int.hpp" #include "error.hpp" #include "futils.hpp" @@ -515,9 +516,9 @@ namespace Exiv2 { return true ; } -#define REPORT_MARKER if ( option == kpsBasic ) out << stringFormat("%8ld | %#02x %-5s",io_->tell(), marker,nm[marker].c_str()) +#define REPORT_MARKER if ( option == kpsBasic ) out << Internal::stringFormat("%8ld | %#02x %-5s",io_->tell(), marker,nm[marker].c_str()) - void JpegBase::printStructure(std::ostream& out,printStructureOption_e option) + void JpegBase::printStructure(std::ostream& out, PrintStructureOption option) { if (io_->open() != 0) throw Error(9, io_->path(), strError()); // Ensure that this is the correct image type @@ -589,7 +590,7 @@ namespace Exiv2 { ){ size = getUShort(buf.pData_, bigEndian); } - if ( option == kpsBasic ) out << stringFormat(" | %7d ", size); + if ( option == kpsBasic ) out << Internal::stringFormat(" | %7d ", size); // only print the signature for appn if (marker >= app0_ && marker <= (app0_ | 0x0F)) { @@ -624,7 +625,7 @@ namespace Exiv2 { bufRead = size; } } else if ( option == kpsBasic ) { - out << "| " << binaryToString(buf,32,size>0?2:0); + out << "| " << Internal::binaryToString(buf,32,size>0?2:0); } } diff --git a/src/pngimage.cpp b/src/pngimage.cpp index df30d5d9..78d6ba45 100644 --- a/src/pngimage.cpp +++ b/src/pngimage.cpp @@ -37,6 +37,7 @@ EXIV2_RCSID("@(#) $Id$") #include "pngimage.hpp" #include "jpgimage.hpp" #include "image.hpp" +#include "image_int.hpp" #include "basicio.hpp" #include "error.hpp" #include "futils.hpp" @@ -91,7 +92,7 @@ namespace Exiv2 { return "image/png"; } - void PngImage::printStructure(std::ostream& out,printStructureOption_e option) + void PngImage::printStructure(std::ostream& out, PrintStructureOption option) { if (io_->open() != 0) { throw Error(9, io_->path(), strError()); @@ -145,10 +146,10 @@ namespace Exiv2 { DataBuf buff(blen+1); io_->read(buff.pData_,blen); dataOffset -= blen ; - dataString = binaryToString(buff,blen); + dataString = Internal::binaryToString(buff, blen); } - if ( option == kpsBasic ) out << stringFormat("%8d | %5d | %10s |%8d | ",(uint32_t)address, index++,chType,dOff) << dataString << std::endl; + if ( option == kpsBasic ) out << Internal::stringFormat("%8d | %5d | %10s |%8d | ",(uint32_t)address, index++,chType,dOff) << dataString << std::endl; // for XMP, back up and read the whole block const char* key = "XML:com.adobe.xmp" ; size_t start = ::strlen(key); diff --git a/src/tiffimage.cpp b/src/tiffimage.cpp index 3d4ec323..cb848b22 100644 --- a/src/tiffimage.cpp +++ b/src/tiffimage.cpp @@ -38,6 +38,7 @@ EXIV2_RCSID("@(#) $Id$") #include "tiffvisitor_int.hpp" #include "makernote_int.hpp" #include "image.hpp" +#include "image_int.hpp" #include "error.hpp" #include "futils.hpp" #include "types.hpp" @@ -438,14 +439,14 @@ namespace Exiv2 { || isRationalType(type) ; } - static bool isPrintXMP(uint16_t type,Exiv2::printStructureOption_e option) + static bool isPrintXMP(uint16_t type, Exiv2::PrintStructureOption option) { return type == 700 && option == kpsXMP; } #define MIN(a,b) ((a)<(b))?(b):(a) - void TiffImage::printStructure(std::ostream& out,Exiv2::printStructureOption_e option) + void TiffImage::printStructure(std::ostream& out, Exiv2::PrintStructureOption option) { if (io_->open() != 0) throw Error(9, io_->path(), strError()); // Ensure that this is the correct image type @@ -468,13 +469,13 @@ namespace Exiv2 { ; if ( option == kpsBasic ) { - out << stringFormat("STRUCTURE OF TIFF FILE (%c%c): ",c,c) << io_->path() << std::endl; + out << Internal::stringFormat("STRUCTURE OF TIFF FILE (%c%c): ",c,c) << io_->path() << std::endl; out << " address | tag | type | count | offset | value\n"; } uint32_t start = byteSwap4(dir,4,bSwap); while ( start ) { - // if ( option == kpsBasic ) out << stringFormat("bSwap, start = %d %u\n",bSwap,offset); + // if ( option == kpsBasic ) out << Internal::stringFormat("bSwap, start = %d %u\n",bSwap,offset); // Read top of directory io_->seek(start,BasicIo::beg); @@ -516,7 +517,7 @@ namespace Exiv2 { if ( option == kpsBasic ) { uint32_t address = start + 2 + i*12 ; - out << stringFormat("%8u | %#06x %-25s |%10s |%9u |%9u | ",address,tag,tagName(tag,25),typeName(type),count,offset); + out << Internal::stringFormat("%8u | %#06x %-25s |%10s |%9u |%9u | ",address,tag,tagName(tag,25),typeName(type),count,offset); if ( isShortType(type) ){ for ( uint16_t k = 0 ; k < kount ; k++ ) { @@ -542,7 +543,7 @@ namespace Exiv2 { sp = " "; } } else if ( isStringType(type) ) { - out << sp << binaryToString(buf,kount); + out << sp << Internal::binaryToString(buf, kount); } sp = kount == count ? "" : " ..."; out << sp << std::endl;