Fixed situation where an Error with a confusing message was thrown. Added BasicIo::path(). Further code and doc clean-up.

v0.27.3
Andreas Huggel 20 years ago
parent c99597ccd7
commit 56a7b11072

@ -163,7 +163,7 @@ namespace Exiv2 {
throw Error(10, path_, "w+b", strError()); throw Error(10, path_, "w+b", strError());
} }
if (src.open() != 0) { if (src.open() != 0) {
throw Error(9, strError()); throw Error(9, src.path(), strError());
} }
write(src); write(src);
src.close(); src.close();
@ -305,6 +305,10 @@ namespace Exiv2 {
return feof(fp_) != 0; return feof(fp_) != 0;
} }
std::string FileIo::path() const
{
return path_;
}
MemIo::MemIo(const byte* data, long size) MemIo::MemIo(const byte* data, long size)
{ {
@ -349,7 +353,9 @@ namespace Exiv2 {
// Generic reopen to reset position to start // Generic reopen to reset position to start
data_.clear(); data_.clear();
idx_ = 0; idx_ = 0;
if (src.open() != 0) throw Error(9, strError()); if (src.open() != 0) {
throw Error(9, src.path(), strError());
}
write(src); write(src);
src.close(); src.close();
} }
@ -461,4 +467,9 @@ namespace Exiv2 {
return idx_ == data_.size(); return idx_ == data_.size();
} }
std::string MemIo::path() const
{
return "MemIo";
}
} // namespace Exiv2 } // namespace Exiv2

@ -194,6 +194,12 @@ namespace Exiv2 {
virtual int error() const = 0; virtual int error() const = 0;
//!Returns true if the IO position has reach the end, otherwise false. //!Returns true if the IO position has reach the end, otherwise false.
virtual bool eof() const = 0; 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 @brief Returns a temporary data storage location. This is often
needed to rewrite an IO source. needed to rewrite an IO source.
@ -404,12 +410,14 @@ namespace Exiv2 {
-1 if failure; -1 if failure;
*/ */
virtual long size() const; 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; 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; 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; 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 @brief Returns a temporary data storage location. The actual type
returned depends upon the size of the file represented a FileIo returned depends upon the size of the file represented a FileIo
@ -590,6 +598,8 @@ namespace Exiv2 {
virtual int error() const; virtual int error() const;
//!Returns true if the IO position has reach the end, otherwise false. //!Returns true if the IO position has reach the end, otherwise false.
virtual bool eof() const; 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 @brief Returns a temporary data storage location. Currently returns
an empty MemIo object, but callers should not rely on this an empty MemIo object, but callers should not rely on this

@ -52,7 +52,7 @@ namespace Exiv2 {
ErrMsg( 6, "Invalid key `%1'"), // %1=key ErrMsg( 6, "Invalid key `%1'"), // %1=key
ErrMsg( 7, "Invalid tag name or ifdId `%1', ifdId %2"), // %1=tag name, %2=ifdId ErrMsg( 7, "Invalid tag name or ifdId `%1', ifdId %2"), // %1=tag name, %2=ifdId
ErrMsg( 8, "Value not set"), 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( 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( 11, "%1: The file contains data of an unknown image type"), // %1=path
ErrMsg( 12, "The memory contains data of an unknown image type"), ErrMsg( 12, "The memory contains data of an unknown image type"),

@ -115,7 +115,7 @@ namespace Exiv2 {
Image::AutoPtr ImageFactory::open(const std::string& path) Image::AutoPtr ImageFactory::open(const std::string& path)
{ {
BasicIo::AutoPtr io(new FileIo(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); if (image.get() == 0) throw Error(11, path);
return image; return image;
} }
@ -123,20 +123,20 @@ namespace Exiv2 {
Image::AutoPtr ImageFactory::open(const byte* data, long size) Image::AutoPtr ImageFactory::open(const byte* data, long size)
{ {
BasicIo::AutoPtr io(new MemIo(data, 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); if (image.get() == 0) throw Error(12);
return image; return image;
} }
Image::AutoPtr ImageFactory::open(BasicIo::AutoPtr io) Image::AutoPtr ImageFactory::open(BasicIo::AutoPtr io)
{ {
if (io->open() != 0) {
throw Error(9, io->path(), strError());
}
Image::AutoPtr image; Image::AutoPtr image;
if (io->open() != 0) return image;
IoCloser closer(*io);
Registry::const_iterator b = registry_->begin(); Registry::const_iterator b = registry_->begin();
Registry::const_iterator e = registry_->end(); 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)) { if (i->second.isThisType(*io, false)) {
image = i->second.newInstance(io, false); image = i->second.newInstance(io, false);
break; break;
@ -155,7 +155,7 @@ namespace Exiv2 {
} }
fileIo->close(); fileIo->close();
BasicIo::AutoPtr io(fileIo); BasicIo::AutoPtr io(fileIo);
Image::AutoPtr image(create(type, io)); Image::AutoPtr image = create(type, io);
if (image.get() == 0) throw Error(13, type); if (image.get() == 0) throw Error(13, type);
return image; return image;
} }
@ -163,7 +163,7 @@ namespace Exiv2 {
Image::AutoPtr ImageFactory::create(Image::Type type) Image::AutoPtr ImageFactory::create(Image::Type type)
{ {
BasicIo::AutoPtr io(new MemIo); 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); if (image.get() == 0) throw Error(13, type);
return image; return image;
} }

@ -302,13 +302,15 @@ namespace Exiv2 {
(re)opened by this method. (re)opened by this method.
@param io An auto-pointer that owns a BasicIo instance that provides @param io An auto-pointer that owns a BasicIo instance that provides
image data. The contents of the image data are tested to determine 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 in BasicIo instance through the auto-pointer. Callers should not
continue to use the BasicIo instance after it is passed to this method. 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 @return An auto-pointer that owns an Image instance whose type
matches that of the \em io data. If no image type could be matches that of the \em io data. If no image type could be
determined, the pointer is 0. determined, the pointer is 0.
@throw Error If opening the BasicIo fails
*/ */
static Image::AutoPtr open(BasicIo::AutoPtr io); static Image::AutoPtr open(BasicIo::AutoPtr io);
/*! /*!
@ -336,11 +338,11 @@ namespace Exiv2 {
contains data, it will be overwritten. contains data, it will be overwritten.
@param type Type of the image to be created. @param type Type of the image to be created.
@param io An auto-pointer that owns a BasicIo instance that will @param io An auto-pointer that owns a BasicIo instance that will
be written to when creating a new image. \b Important: This be written to when creating a new image.
method takes ownership of the passed in BasicIo instance through @note This method takes ownership of the passed in BasicIo instance
the auto-pointer. Callers should not continue to use the BasicIo through the auto-pointer. Callers should not continue to use the
instance after it is passed to this method. Use theImage::io() BasicIo instance after it is passed to this method. Use the
method to get a temporary reference. Image::io() method to get a temporary reference.
@return An auto-pointer that owns an Image instance of the requested @return An auto-pointer that owns an Image instance of the requested
type. If the image type is not supported, the pointer is 0. type. If the image type is not supported, the pointer is 0.
*/ */

@ -58,7 +58,7 @@ try {
FileIo fileIn(argv[1]); FileIo fileIn(argv[1]);
if (fileIn.open() != 0) { if (fileIn.open() != 0) {
throw Error(9, strError()); throw Error(9, fileIn.path(), strError());
} }
FileIo fileOut1(argv[2]); FileIo fileOut1(argv[2]);
@ -139,7 +139,7 @@ int WriteReadSeek(BasicIo &io)
const long len2 = (long)strlen(tester2) + 1; const long len2 = (long)strlen(tester2) + 1;
if (io.open() != 0) { if (io.open() != 0) {
throw Error(9, strError()); throw Error(9, io.path(), strError());
} }
IoCloser closer(io); IoCloser closer(io);
if (io.write((byte*)tester1, len1) != len1) { if (io.write((byte*)tester1, len1) != len1) {
@ -202,7 +202,7 @@ int WriteReadSeek(BasicIo &io)
// open should seek to beginning // open should seek to beginning
if (io.open() != 0) { if (io.open() != 0) {
throw Error(9, strError()); throw Error(9, io.path(), strError());
} }
memset(buf, -1, sizeof(buf)); memset(buf, -1, sizeof(buf));
if (io.read(buf, sizeof(buf)) != insert + len2) { if (io.read(buf, sizeof(buf)) != insert + len2) {

@ -86,7 +86,7 @@ namespace Exiv2 {
: io_(io) : io_(io)
{ {
if (create) { if (create) {
initImage(initData, dataSize); // may throw initImage(initData, dataSize);
} }
} }
@ -171,7 +171,7 @@ namespace Exiv2 {
void JpegBase::readMetadata() void JpegBase::readMetadata()
{ {
if (io_->open() != 0) { if (io_->open() != 0) {
throw Error(9, strError()); throw Error(9, io_->path(), strError());
} }
IoCloser closer(*io_); IoCloser closer(*io_);
// Ensure that this is the correct image type // Ensure that this is the correct image type
@ -298,7 +298,9 @@ namespace Exiv2 {
void JpegBase::writeMetadata() void JpegBase::writeMetadata()
{ {
if (io_->open() != 0) throw Error(9, strError()); if (io_->open() != 0) {
throw Error(9, io_->path(), strError());
}
IoCloser closer(*io_); IoCloser closer(*io_);
BasicIo::AutoPtr tempIo(io_->temporary()); // may throw BasicIo::AutoPtr tempIo(io_->temporary()); // may throw
assert (tempIo.get() != 0); assert (tempIo.get() != 0);
@ -574,13 +576,7 @@ namespace Exiv2 {
Image::AutoPtr newJpegInstance(BasicIo::AutoPtr io, bool create) Image::AutoPtr newJpegInstance(BasicIo::AutoPtr io, bool create)
{ {
Image::AutoPtr image; Image::AutoPtr image = Image::AutoPtr(new JpegImage(io, create));
if (create) {
image = Image::AutoPtr(new JpegImage(io, true));
}
else {
image = Image::AutoPtr(new JpegImage(io, false));
}
if (!image->good()) { if (!image->good()) {
image.reset(); image.reset();
} }
@ -654,8 +650,8 @@ namespace Exiv2 {
iIo.read(tmpBuf, 7); iIo.read(tmpBuf, 7);
if (iIo.error() || iIo.eof()) return false; if (iIo.error() || iIo.eof()) return false;
if (0xff!=tmpBuf[0] || 0x01!=tmpBuf[1] || if ( 0xff != tmpBuf[0] || 0x01 != tmpBuf[1]
memcmp(tmpBuf + 2, ExvImage::exiv2Id_, 5) != 0) { || memcmp(tmpBuf + 2, ExvImage::exiv2Id_, 5) != 0) {
result = false; result = false;
} }
if (!advance || !result ) iIo.seek(-7, BasicIo::cur); if (!advance || !result ) iIo.seek(-7, BasicIo::cur);

@ -235,9 +235,7 @@ namespace Exiv2 {
writing all buffered metadata to the provided BasicIo. writing all buffered metadata to the provided BasicIo.
@param oIo BasicIo instance to write to (a temporary location). @param oIo BasicIo instance to write to (a temporary location).
@throw Error if reading from input file failed, the output file @return 4 if opening or writing to the associated BasicIo fails
can not be written to, or the input file does not contain
a valid image.
*/ */
void doWriteMetadata(BasicIo& oIo); void doWriteMetadata(BasicIo& oIo);

Loading…
Cancel
Save