From 56a7b11072099ce6be3d8c162a6472d3ef422af7 Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Thu, 21 Apr 2005 07:21:53 +0000 Subject: [PATCH] Fixed situation where an Error with a confusing message was thrown. Added BasicIo::path(). Further code and doc clean-up. --- src/basicio.cpp | 15 +++++++++++++-- src/basicio.hpp | 16 +++++++++++++--- src/error.cpp | 2 +- src/image.cpp | 16 ++++++++-------- src/image.hpp | 16 +++++++++------- src/iotest.cpp | 6 +++--- src/jpgimage.cpp | 20 ++++++++------------ src/jpgimage.hpp | 4 +--- 8 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/basicio.cpp b/src/basicio.cpp index 4e156284..b09ec603 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -163,7 +163,7 @@ namespace Exiv2 { throw Error(10, path_, "w+b", strError()); } if (src.open() != 0) { - throw Error(9, strError()); + throw Error(9, src.path(), strError()); } write(src); src.close(); @@ -305,6 +305,10 @@ namespace Exiv2 { return feof(fp_) != 0; } + std::string FileIo::path() const + { + return path_; + } MemIo::MemIo(const byte* data, long size) { @@ -349,7 +353,9 @@ namespace Exiv2 { // Generic reopen to reset position to start data_.clear(); idx_ = 0; - if (src.open() != 0) throw Error(9, strError()); + if (src.open() != 0) { + throw Error(9, src.path(), strError()); + } write(src); src.close(); } @@ -461,4 +467,9 @@ namespace Exiv2 { return idx_ == data_.size(); } + std::string MemIo::path() const + { + return "MemIo"; + } + } // namespace Exiv2 diff --git a/src/basicio.hpp b/src/basicio.hpp index 71e64f9e..24636c88 100644 --- a/src/basicio.hpp +++ b/src/basicio.hpp @@ -194,6 +194,12 @@ namespace Exiv2 { virtual int error() const = 0; //!Returns true if the IO position has reach the end, otherwise false. virtual bool eof() const = 0; + /*! + @brief Return the path to the IO resource. Often used to form + comprehensive error messages where only a BasicIo instance is + available. + */ + virtual std::string path() const =0; /*! @brief Returns a temporary data storage location. This is often needed to rewrite an IO source. @@ -404,12 +410,14 @@ namespace Exiv2 { -1 if failure; */ virtual long size() const; - //!Returns true if the file is open, otherwise false. + //! Returns true if the file is open, otherwise false. virtual bool isopen() const; - //!Returns 0 if the file is in a valid state, otherwise nonzero. + //! Returns 0 if the file is in a valid state, otherwise nonzero. virtual int error() const; - //!Returns true if the file position has reach the end, otherwise false. + //! Returns true if the file position has reach the end, otherwise false. virtual bool eof() const; + //! Returns the path of the file + virtual std::string path() const; /*! @brief Returns a temporary data storage location. The actual type returned depends upon the size of the file represented a FileIo @@ -590,6 +598,8 @@ namespace Exiv2 { virtual int error() const; //!Returns true if the IO position has reach the end, otherwise false. virtual bool eof() const; + //! Returns a dummy path, indicating that memory access is used + virtual std::string path() const; /*! @brief Returns a temporary data storage location. Currently returns an empty MemIo object, but callers should not rely on this diff --git a/src/error.cpp b/src/error.cpp index 2d2768e1..60111dd6 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -52,7 +52,7 @@ namespace Exiv2 { ErrMsg( 6, "Invalid key `%1'"), // %1=key ErrMsg( 7, "Invalid tag name or ifdId `%1', ifdId %2"), // %1=tag name, %2=ifdId ErrMsg( 8, "Value not set"), - ErrMsg( 9, "Failed to open the data source: %1"), // %1=strerror + ErrMsg( 9, "%1: Failed to open the data source: %2"), // %1=path, %2=strerror ErrMsg( 10, "%1: Failed to open file (%2): %3"), // %1=path, %2=mode, %3=strerror ErrMsg( 11, "%1: The file contains data of an unknown image type"), // %1=path ErrMsg( 12, "The memory contains data of an unknown image type"), diff --git a/src/image.cpp b/src/image.cpp index cd4a8316..64595955 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -115,7 +115,7 @@ namespace Exiv2 { Image::AutoPtr ImageFactory::open(const std::string& path) { BasicIo::AutoPtr io(new FileIo(path)); - Image::AutoPtr image(open(io)); + Image::AutoPtr image = open(io); // may throw if (image.get() == 0) throw Error(11, path); return image; } @@ -123,20 +123,20 @@ namespace Exiv2 { Image::AutoPtr ImageFactory::open(const byte* data, long size) { BasicIo::AutoPtr io(new MemIo(data, size)); - Image::AutoPtr image(open(io)); + Image::AutoPtr image = open(io); // may throw if (image.get() == 0) throw Error(12); return image; } Image::AutoPtr ImageFactory::open(BasicIo::AutoPtr io) { + if (io->open() != 0) { + throw Error(9, io->path(), strError()); + } Image::AutoPtr image; - if (io->open() != 0) return image; - IoCloser closer(*io); Registry::const_iterator b = registry_->begin(); Registry::const_iterator e = registry_->end(); - for (Registry::const_iterator i = b; i != e; ++i) - { + for (Registry::const_iterator i = b; i != e; ++i) { if (i->second.isThisType(*io, false)) { image = i->second.newInstance(io, false); break; @@ -155,7 +155,7 @@ namespace Exiv2 { } fileIo->close(); BasicIo::AutoPtr io(fileIo); - Image::AutoPtr image(create(type, io)); + Image::AutoPtr image = create(type, io); if (image.get() == 0) throw Error(13, type); return image; } @@ -163,7 +163,7 @@ namespace Exiv2 { Image::AutoPtr ImageFactory::create(Image::Type type) { BasicIo::AutoPtr io(new MemIo); - Image::AutoPtr image(create(type, io)); + Image::AutoPtr image = create(type, io); if (image.get() == 0) throw Error(13, type); return image; } diff --git a/src/image.hpp b/src/image.hpp index eead267d..ae7a43bb 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -302,13 +302,15 @@ namespace Exiv2 { (re)opened by this method. @param io An auto-pointer that owns a BasicIo instance that provides image data. The contents of the image data are tested to determine - the type. \b Important: This method takes ownership of the passed + the type. + @note This method takes ownership of the passed in BasicIo instance through the auto-pointer. Callers should not continue to use the BasicIo instance after it is passed to this method. - Use theImage::io() method to get a temporary reference. + Use the Image::io() method to get a temporary reference. @return An auto-pointer that owns an Image instance whose type matches that of the \em io data. If no image type could be determined, the pointer is 0. + @throw Error If opening the BasicIo fails */ static Image::AutoPtr open(BasicIo::AutoPtr io); /*! @@ -336,11 +338,11 @@ namespace Exiv2 { contains data, it will be overwritten. @param type Type of the image to be created. @param io An auto-pointer that owns a BasicIo instance that will - be written to when creating a new image. \b Important: This - method takes ownership of the passed in BasicIo instance through - the auto-pointer. Callers should not continue to use the BasicIo - instance after it is passed to this method. Use theImage::io() - method to get a temporary reference. + be written to when creating a new image. + @note This method takes ownership of the passed in BasicIo instance + through the auto-pointer. Callers should not continue to use the + BasicIo instance after it is passed to this method. Use the + Image::io() method to get a temporary reference. @return An auto-pointer that owns an Image instance of the requested type. If the image type is not supported, the pointer is 0. */ diff --git a/src/iotest.cpp b/src/iotest.cpp index 6b0d2444..ac82a607 100644 --- a/src/iotest.cpp +++ b/src/iotest.cpp @@ -58,7 +58,7 @@ try { FileIo fileIn(argv[1]); if (fileIn.open() != 0) { - throw Error(9, strError()); + throw Error(9, fileIn.path(), strError()); } FileIo fileOut1(argv[2]); @@ -139,7 +139,7 @@ int WriteReadSeek(BasicIo &io) const long len2 = (long)strlen(tester2) + 1; if (io.open() != 0) { - throw Error(9, strError()); + throw Error(9, io.path(), strError()); } IoCloser closer(io); if (io.write((byte*)tester1, len1) != len1) { @@ -202,7 +202,7 @@ int WriteReadSeek(BasicIo &io) // open should seek to beginning if (io.open() != 0) { - throw Error(9, strError()); + throw Error(9, io.path(), strError()); } memset(buf, -1, sizeof(buf)); if (io.read(buf, sizeof(buf)) != insert + len2) { diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index e10f1bb5..ede59122 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -86,7 +86,7 @@ namespace Exiv2 { : io_(io) { if (create) { - initImage(initData, dataSize); // may throw + initImage(initData, dataSize); } } @@ -171,7 +171,7 @@ namespace Exiv2 { void JpegBase::readMetadata() { if (io_->open() != 0) { - throw Error(9, strError()); + throw Error(9, io_->path(), strError()); } IoCloser closer(*io_); // Ensure that this is the correct image type @@ -298,7 +298,9 @@ namespace Exiv2 { void JpegBase::writeMetadata() { - if (io_->open() != 0) throw Error(9, strError()); + if (io_->open() != 0) { + throw Error(9, io_->path(), strError()); + } IoCloser closer(*io_); BasicIo::AutoPtr tempIo(io_->temporary()); // may throw assert (tempIo.get() != 0); @@ -574,13 +576,7 @@ namespace Exiv2 { Image::AutoPtr newJpegInstance(BasicIo::AutoPtr io, bool create) { - Image::AutoPtr image; - if (create) { - image = Image::AutoPtr(new JpegImage(io, true)); - } - else { - image = Image::AutoPtr(new JpegImage(io, false)); - } + Image::AutoPtr image = Image::AutoPtr(new JpegImage(io, create)); if (!image->good()) { image.reset(); } @@ -654,8 +650,8 @@ namespace Exiv2 { iIo.read(tmpBuf, 7); if (iIo.error() || iIo.eof()) return false; - if (0xff!=tmpBuf[0] || 0x01!=tmpBuf[1] || - memcmp(tmpBuf + 2, ExvImage::exiv2Id_, 5) != 0) { + if ( 0xff != tmpBuf[0] || 0x01 != tmpBuf[1] + || memcmp(tmpBuf + 2, ExvImage::exiv2Id_, 5) != 0) { result = false; } if (!advance || !result ) iIo.seek(-7, BasicIo::cur); diff --git a/src/jpgimage.hpp b/src/jpgimage.hpp index 8e591b7d..f4897a69 100644 --- a/src/jpgimage.hpp +++ b/src/jpgimage.hpp @@ -235,9 +235,7 @@ namespace Exiv2 { writing all buffered metadata to the provided BasicIo. @param oIo BasicIo instance to write to (a temporary location). - @throw Error if reading from input file failed, the output file - can not be written to, or the input file does not contain - a valid image. + @return 4 if opening or writing to the associated BasicIo fails */ void doWriteMetadata(BasicIo& oIo);