GzipPostProcessing.cc 2.03 KB
/*
 * 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>
#include <boost/filesystem.hpp>

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();
	boost::filesystem::path path(pFile);
	boost::filesystem::remove(path);

	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 */