Blame view

src/PostProcessing/GzipPostProcessing.cc 2.03 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * GzipPostProcessing.cc
 *
 *  Created on: 25 sept. 2013
 *      Author: CS
 */

#include "GzipPostProcessing.hh"
#include "GzipNode.hh"
#include "PostProcessingRegistry.hh"

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/filesystem/path.hpp>
05f2d856   Benjamin Renard   Remove original f...
19
#include <boost/filesystem.hpp>
fbe3c2bb   Benjamin Renard   First commit
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

namespace postprocessing {

const std::string GzipPostProcessing::EXTENSION = ".gz";

/**
 * Self-registering to post-processing factory
 */

std::string GzipPostProcessing::_key =
		PostProcessingRegistry::getInstance().addElement(EXTENSION,
				new GzipPostProcessing());

GzipPostProcessing::GzipPostProcessing() : PostProcessing() {

}

GzipPostProcessing::~GzipPostProcessing() {

}

/**
 * Applies post-processing.
 */

void GzipPostProcessing::apply(PostProcessingAble* pOutput) {
	_outputDestination = pOutput->getDestination();
	for (auto file : pOutput->getOutputs()) {
		_files.push_back(gzip(file, pOutput->getDestination()));
	}
}

std::string GzipPostProcessing::gzip(std::string& pFile,
		const std::string& pOutDir) {
	std::string outFile = pFile + EXTENSION;
	if (!pOutDir.empty()) {
		boost::filesystem::path filePath(pFile);
		outFile = pOutDir + "/" + filePath.filename().string() + EXTENSION;
	}

	std::ofstream file(outFile, std::ios_base::out | std::ios_base::binary);
	std::ifstream in(pFile, std::ios_base::in);
	boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
	out.push(boost::iostreams::gzip_compressor());
	out.push(file);
	boost::iostreams::copy(in, out);
	file.close();
05f2d856   Benjamin Renard   Remove original f...
67
68
	boost::filesystem::path path(pFile);
	boost::filesystem::remove(path);
fbe3c2bb   Benjamin Renard   First commit
69
70
71
72
73
74
75
76
77
78
79
80
81
82

	return outFile;
}

/**
 * Registers related node and process.
 */
void GzipPostProcessing::registerChildList(
		std::map<std::string, boost::shared_ptr<AMDA::XMLConfigurator::NodeCfg>>& childList) {
	childList["gzip"] = boost::shared_ptr<AMDA::XMLConfigurator::NodeCfg>(
			new GzipNode());
}

} /* namespace postprocessing */