/
ExportStream.h
118 lines (97 loc) · 3.31 KB
/
ExportStream.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include <string>
#include <fstream>
#include <stdexcept>
#include <fmt/format.h>
#include "itextstream.h"
#include "os/fs.h"
#include "os/path.h"
namespace stream
{
/**
* Stream object used to write data to the a given target directory and filename.
* To prevent corruption of a possibly existing target file, it will open a stream
* to a temporary file for writing first. On calling close(), the temporary stream
* will be finalised and the temporary file will be moved over to the target file,
* which in turn will be renamed to .bak first.
*/
class ExportStream
{
private:
fs::path _tempFile;
std::ofstream _tempStream;
std::string _outputDirectory;
std::string _filename;
public:
// Output stream mode
enum class Mode
{
Text,
Binary,
};
ExportStream(const std::string& outputDirectory, const std::string& filename, Mode mode = Mode::Text) :
ExportStream(outputDirectory, filename, mode == Mode::Binary ? std::ios::out | std::ios::binary : std::ios::out)
{}
ExportStream(const std::string& outputDirectory, const std::string& filename, std::ios::openmode mode) :
_outputDirectory(outputDirectory),
_filename(filename)
{
if (!path_is_absolute(_outputDirectory.c_str()))
{
throw std::runtime_error(fmt::format(_("Path is not absolute: {0}"), _outputDirectory));
}
fs::path targetPath = _outputDirectory;
if (!fs::exists(targetPath))
{
rMessage() << "Creating directory: " << targetPath << std::endl;
fs::create_directories(targetPath);
}
// Open a temporary file (leading underscore)
_tempFile = targetPath / ("_" + _filename);
_tempStream = std::ofstream(_tempFile.string(), mode);
if (!_tempStream.is_open())
{
throw std::runtime_error(
fmt::format(_("Cannot open file for writing: {0}"), _tempFile.string()));
}
}
// Returns the stream for writing the export data
std::ofstream& getStream()
{
return _tempStream;
}
void close()
{
_tempStream.close();
// The full OS path to the output file
fs::path targetPath = _outputDirectory;
targetPath /= _filename;
if (fs::exists(targetPath))
{
try
{
// Move the old target file to .bak (overwriting any existing .bak file)
fs::rename(targetPath, targetPath.string() + ".bak");
}
catch (fs::filesystem_error& e)
{
rError() << "Could not rename the existing file to .bak: " << targetPath.string() << std::endl
<< e.what() << std::endl;
throw std::runtime_error(
fmt::format(_("Could not rename the existing file to .bak: {0}"), targetPath.string()));
}
}
try
{
fs::rename(_tempFile, targetPath);
}
catch (fs::filesystem_error& e)
{
rError() << "Could not rename the temporary file " << _tempFile.string() << std::endl
<< e.what() << std::endl;
throw std::runtime_error(
fmt::format(_("Could not rename the temporary file: {0}"), _tempFile.string()));
}
}
};
}