1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include "PbbamInternalConfig.h"
#include "FileProducer.h"
#include <stdexcept>
#include <cstdio>
namespace PacBio {
namespace BAM {
FileProducer::FileProducer(std::string targetFilename)
: FileProducer(targetFilename, targetFilename + ".tmp")
{}
FileProducer::FileProducer(std::string targetFilename, std::string tempFilename)
: targetFilename_{std::move(targetFilename)}, tempFilename_{std::move(tempFilename)}
{
if (targetFilename_.empty()) {
throw std::runtime_error{
"[pbbam] temp file producer ERROR: cannot write to file with empty filename"};
}
// override renaming if writing to stdout
//
// setting temp filename to '-' keeps consistent interfaces
// for derived classes to actually operate on temp filename
if (targetFilename_ == "-") {
tempFilename_ = "-";
}
}
FileProducer::~FileProducer()
{
// skip renaming if there is a 'live' exception
// or if writing to stdout
if ((std::current_exception() == nullptr) && (tempFilename_ != "-")) {
std::rename(tempFilename_.c_str(), targetFilename_.c_str());
}
}
} // namespace BAM
} // namespace PacBio
|