AsciiWriter.cc 3.6 KB
/*
 * AsciiWriter.cc
 *
 *  Created on: 6 août 2013
 *      Author: CS
 */

#include <iostream>

#include "AsciiData.hh"
#include "AsciiWriter.hh"
#include <fstream>
#include "TimeTableCatalogUtil.hh"

namespace TimeTableCatalog {

const std::string AsciiWriter::FORMAT = "ASCII";

AsciiWriter::AsciiWriter(const std::string& pPath, const std::string& pName) :
		AbstractWriter(pPath, pName) {

}

AsciiWriter::~AsciiWriter() {
}

// ----------------- PUBLIC --------------------------

std::string AsciiWriter::write(const TimeTable& pTT) {
	LOG4CXX_DEBUG(logger, "Opening file " << getFile(pTT));
	std::ofstream ttfile(getFile(pTT), std::ios::out);

	// -- open file
	if (ttfile) {
		LOG4CXX_DEBUG(logger, "Writing metadata");
		writeMetaData(pTT, ttfile);
		LOG4CXX_DEBUG(logger, "Writing intervals");
		writeIntervals(pTT, ttfile);
	}
	LOG4CXX_DEBUG(logger, "End Of writing");
	// -- close file
	ttfile.close();

	return getFile(pTT);
}

/**
 * Creates an instance of this.
 */
std::unique_ptr<AbstractWriter> AsciiWriter::createInstance(
		const std::string& pPath, const std::string& pName) {
	return std::unique_ptr < AbstractWriter > (new AsciiWriter(pPath, pName));
}

/**
 * Gets the tt file expected extension, starting with .
 */
const std::string AsciiWriter::getExtension() const {
	return ".txt";
}

// ----------------- PRIVATE --------------------------

void AsciiWriter::writeMetaData(const TimeTable& pTT,
		std::ostream& pOut) {
	// -- write description
	for (auto line : pTT._description) {
		pOut << "# " << line << ";" << std::endl;
	}
	// -- write name
	pOut << "# " << AsciiData::NAME_KEYWORD << " " << pTT._name << ";" << std::endl;

	// -- write historic
	pOut << "# " << AsciiData::HISTORIC_KEYWORD << " " << pTT._history << ";" << std::endl;

	// -- write creation date
	pOut << "# " << AsciiData::CREATION_DATE_KEYWORD << " ";
	writeISOTime(pTT._creationDate, pTT._timeFormat, pOut);
	pOut << ";" << std::endl;

	// Write parameter description if defined (Catalog ONLY)
	ParameterDescriptionList pdl = pTT.getParameterDescritptions();
	int pCount=0;
	for (auto parameterDescription : pdl) {
		pOut << "# " << AsciiData::PARAMETER_KEYWORD << " " << (pCount+1) << ": ";
		pOut << AsciiData::ATTRIB_ID << ":" << parameterDescription.getId() << ", ";
		pOut << AsciiData::ATTRIB_NAME << ":" << parameterDescription.getName() << ", ";
		pOut << AsciiData::ATTRIB_SIZE << ":" << parameterDescription.getSize() << ", ";
		pOut << AsciiData::ATTRIB_TYPE << ":" << parameterDescription.getTypeAsString() << ", ";
		pOut << AsciiData::ATTRIB_UNIT << ":" << parameterDescription.getUnit() << ", ";
		pOut << AsciiData::ATTRIB_DESCRITION << ":" << parameterDescription.getDescription() << ", ";
		pOut << AsciiData::ATTRIB_UCD << ":" << parameterDescription.getUcd() << ", ";
		pOut << AsciiData::ATTRIB_UTYPE << ":" << parameterDescription.getUtype() << ";" << std::endl;
		pCount++;
	}
}

void AsciiWriter::writeIntervals(const TimeTable& pTT, std::ostream& pOut) {
	ParameterDescriptionList pdl = pTT.getParameterDescritptions();

	for (TimeInterval interval : pTT.getIntervals()) {
		writeISOTime(interval._startTime, pTT._timeFormat, pOut);
		pOut << AsciiData::SEPARATOR;
		writeISOTime(interval._stopTime, pTT._timeFormat, pOut);

		// If the TT has a parameter description list, write parameter data
		if (pdl.empty() == false) {
			for (auto parameterDescription : pdl) {
				std::vector<std::string> dataValues = interval.getParameterData (parameterDescription.getId());

				for (auto dataValue : dataValues) {
					pOut << AsciiData::SEPARATOR << dataValue;
				}
			}
		}

		// skip to next line
		pOut << std::endl;
	}
}

} /* namespace TimeTableCatalog */