File: FileProducer.cpp

package info (click to toggle)
pbbam 2.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,144 kB
  • sloc: cpp: 60,214; xml: 2,908; ansic: 660; sh: 275; python: 203; makefile: 187
file content (43 lines) | stat: -rw-r--r-- 1,160 bytes parent folder | download | duplicates (3)
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