// ***************************************************************************** // Main int main(int argc, char* const argv[]) { try { // Handle command line arguments Params params; if (params.getopt(argc, argv)) { params.usage(); return 1; } if (params.help_) { params.help(); return 2; } // Use MemIo to increase test coverage. Exiv2::BasicIo::AutoPtr fileIo(new Exiv2::FileIo(params.read_)); Exiv2::BasicIo::AutoPtr memIo(new Exiv2::MemIo); memIo->transfer(*fileIo); Exiv2::Image::AutoPtr readImg = Exiv2::ImageFactory::open(memIo); assert(readImg.get() != 0); readImg->readMetadata(); Exiv2::Image::AutoPtr writeImg = Exiv2::ImageFactory::open(params.write_); assert(writeImg.get() != 0); if (params.preserve_) writeImg->readMetadata(); if (params.iptc_) { writeImg->setIptcData(readImg->iptcData()); } if (params.exif_) { writeImg->setExifData(readImg->exifData()); } if (params.comment_) { writeImg->setComment(readImg->comment()); } if (params.xmp_) { writeImg->setXmpData(readImg->xmpData()); } try { writeImg->writeMetadata(); } catch (const Exiv2::AnyError&) { std::cerr << params.progname() << ": Could not write metadata to (" << params.write_ << ")\n"; return 8; } return 0; } catch (Exiv2::AnyError& e) { std::cerr << "Caught Exiv2 exception '" << e << "'\n"; return 10; } }
Image::AutoPtr ImageFactory::create(int type, const std::string& path) { std::auto_ptr<FileIo> fileIo(new FileIo(path)); // Create or overwrite the file, then close it if (fileIo->open("w+b") != 0) { throw Error(10, path, "w+b", strError()); } fileIo->close(); BasicIo::AutoPtr io(fileIo); Image::AutoPtr image = create(type, io); if (image.get() == 0) throw Error(13, type); return image; }
BasicIo::AutoPtr FileIo::temporary() const { BasicIo::AutoPtr basicIo; struct stat buf; int ret = stat(path_.c_str(), &buf); // If file is > 1MB then use a file, otherwise use memory buffer if (ret != 0 || buf.st_size > 1048576) { pid_t pid = ::getpid(); std::string tmpname = path_ + toString(pid); std::auto_ptr<FileIo> fileIo(new FileIo(tmpname)); if (fileIo->open("w+b") != 0) { throw Error(10, path_, "w+b", strError()); } basicIo = fileIo; } else { basicIo.reset(new MemIo); } return basicIo; }
int ImageFactory::getType(const std::string& path) { FileIo fileIo(path); return getType(fileIo); }