FileWriterASCIIAbstract.hh 9.98 KB
/**
 * FileWriterASCIIAbstract.hh
 *
 *  Created on: 21 oct. 2014
 *      Author: AKKA
 */

#ifndef FILEWRITERASCIIABSTRACT_HH_
#define FILEWRITERASCIIABSTRACT_HH_

#include "FileWriter.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "InstrumentMgr.hh"
#include "MissionMgr.hh"

#include <fstream>
#include <map>
#include <list>

namespace AMDA
{
	namespace ParamOutputImpl
	{
		namespace Download
		{
			namespace FileWriter
			{

				using namespace AMDA::Info;

				/**
				 * @class FileWriterASCIIAbstract
				 * @brief Abstract class use to define a file writer for a ASCII file.
				 * @details
				 */
				class FileWriterASCIIAbstract : public FileWriter
				{
				public:
					/*
					 * @brief Working structure used to define a field for ASCII formats
					 */
					typedef struct
					{
						std::string name;
						std::string ucd;
						std::string unit;
						FileDataType type;
						int dimSize1;
						int dimSize2;
						AMDA::Common::ParameterIndexComponentList componentsList;
						std::string filePath;
						std::fstream *file;
					} ASCIIFieldInfo;

					/*
					 * @brief Constructor
					 */
					FileWriterASCIIAbstract(AMDA::Parameters::ParameterManager &pParameterManager);

					/*
					 * @brief Destructor
					 */
					virtual ~FileWriterASCIIAbstract(void);

					/*
					 * @overload FileWriter::getExtension - Abstract method to get file format extension
					 */
					virtual std::string getExtension(void) = 0;

					/*
					 * @brief Abstract method to get fill character to use
					 */
					virtual std::string getDataFillCharacter(void) = 0;

					/*
					 * @brief Abstract method to get string representation of a Not A Number
					 */
					virtual std::string getNanString(void) = 0;

					/*
					 * @brief Abstract method to write begin file tag
					 */
					virtual void writeBeginFile(void) = 0;

					/*
					 * @brief Abstract method to write end file tag
					 */
					virtual void writeEndFile(void) = 0;

					/*
					 * @brief Abstract method to write begin description tag
					 */
					virtual void writeBeginGeneralDescription(void) = 0;

					/*
					 * @brief Abstract method to write end description tag
					 */
					virtual void writeEndGeneralDescription(void) = 0;

					/*
					 * @brief Abstract method to write an error message
					 */
					virtual void writeErrorInfo(std::string msg) = 0;

					/*
					 * @brief Abstract method to write begin info group tag with a title
					 */
					virtual void writeBeginInfoGroup(std::string title, int level) = 0;

					/*
					 * @brief Abstract method to write end info group tag
					 */
					virtual void writeEndInfoGroup(int level) = 0;

					/*
					 * @brief Abstract method to write an info designated by a key and a value
					 */
					virtual void writeSingleInfo(std::string key, std::string value, int level) = 0;

					/*
					 * @brief Abstract method to write begin info list tag with a title
					 */
					virtual void writeBeginInfoList(std::string title, int level) = 0;

					/*
					 * @brief Abstract method to write end info list tag
					 */
					virtual void writeEndInfoList(void) = 0;

					/*
					 * @brief Abstract method to write begin info fields description tag
					 */
					virtual void writeBeginFieldsDescription(void) = 0;

					/*
					 * @brief Abstract method to write end info fields description tag
					 */
					virtual void writeEndFieldsDescription(void) = 0;

					/*
					 * @brief Abstract method to write a field description by parameter id
					 */
					virtual void writeFieldDescription(std::string paramId) = 0;

					/*
					 * @brief Abstract method to write begin data tag
					 */
					virtual void writeBeginData(void) = 0;

					/*
					 * @brief Abstract method to write end data tag
					 */
					virtual void writeEndData(void) = 0;

					/*
					 * @brief Abstract method to write a data record
					 */
					virtual void writeDataRecord(std::string record) = 0;

					/*
					 * @brief Abstract method to get data start tag
					 */
					virtual std::string getDataStartTag(bool isTimeData) = 0;

					/*
					 * @brief Abstract method to get data stop tag
					 */
					virtual std::string getDataStopTag(void) = 0;

					/*
					 * @brief Abstract method to get data value separator
					 */
					virtual std::string getDataValueSeparator(void) = 0;

					/*
					 * @brief Abstract method used to know if data must be trimmed
					 */
					virtual bool trimData(void) = 0;

					/*
					 * @overload FileWriter::createNewFile - Create a new ASCII file
					 */
					virtual bool createNewFile(std::string filePath, std::string filePrefix="");

					/*
					 * @overload FileWriter::closeFile - Close current opened ASCII file
					 */
					virtual void closeFile(void);

					/*
					 * @overload FileWriter::addParameter - Add an output parameter in ASCII file
					 */
					virtual bool addParameter(ParamProperties *paramProp, AMDA::Common::ParameterIndexComponentList &indexList,
											  FileDataType type, bool isFirstParam, int dim1Size = 1, int dim2Size = 1, bool isTableParam = false);

					/*
					 * @overload FileWriter::isInfoInSeparateFile - Function use to know if info must be write in a separate file
					 */
					virtual bool isInfoInSeparateFile(bool separateInfoFile, bool onlyOneInterval, OutputStructure outputStructure);

					/*
					 * @overload FileWriter::writeError - Write an error message in the ASCII file
					 */
					virtual bool writeError(std::string msg);

					/*
					 * @overload FileWriter::writeAMDAInfo - Write an AMDA info bloc for ASCII file
					 */
					virtual bool writeAMDAInfo(std::string version, std::string createdby, std::string acknowledgement);

					/*
					 * @overload FileWriter::writeRequestInfo - Write a request info bloc for ASCII file
					 */
					virtual bool writeRequestInfo(std::string structure, std::string timeFormat,
												  int timeResolution, std::string outputParams, std::string ttName);

					/*
					 * @overload FileWriter::writeParamsInfo - Write a parameter info bloc for ASCII file
					 */
					virtual bool writeParamsInfo(ParamPropertiesList &paramPropertiesList, OutputStructure outputStructure,
												 std::string currentParamId = "");

					virtual bool writeTableParamsInfo(std::vector<std::string> tableParams, int level);

					/*
					 * @overload FileWriter::writeIntervalInfo - Write an interval info bloc for ASCII file
					 */
					virtual bool writeIntervalInfo(std::string startTime, std::string stopTime, bool isSeveralIntervals);

					/*
					 * @overload FileWriter::writeTimeData - Write a time data in the ASCII file
					 */
					virtual bool writeTimeData(std::string paramId, double data, OutputFormatTime timeFormat, bool isFirstParam);

					/*
					 * @overload FileWriter::writeFloatData - Write a float data in the ASCII file
					 */
					virtual bool writeFloatData(std::string paramId, int varIndex, float data, bool precision);

					/*
					 * @overload FileWriter::writeShortData - Write a short data in the ASCII file
					 */
					virtual bool writeShortData(std::string paramId, int varIndex, short data, bool precision);

					/*
					 * @overload FileWriter::writeIntData - Write a int data in the ASCII file
					 */
					virtual bool writeIntData(std::string paramId, int varIndex, int data, bool precision);

					/*
					 * @overload FileWriter::writeDoubleData - Write a double data in the ASCII file
					 */
					virtual bool writeDoubleData(std::string paramId, int varIndex, double data, bool precision);

					/*
					 * @overload FileWriter::writeLongDoubleData - Write a long double data in the ASCII file
					 */
					virtual bool writeLongDoubleData(std::string paramId, int varIndex, long double data, bool precision);

					/*
					 * @overload FileWriter::writeLogicalData - Write a logical data in the ASCII file
					 */
					virtual bool writeLogicalData(std::string paramId, int varIndex, AMDA::Parameters::LogicalData data, bool precision);

					/*
					 * @overload FileWriter::goToNextRecord - Prepare ASCII writer to write the next record
					 */
					virtual bool goToNextRecord(std::string paramId, bool isFirstParam);

					/*
					 * @brief Flush data in current opened file
					 */
					virtual bool flushData(std::string paramId, bool isFirstParam);

					/*
					 * @overload FileWriter::finalize - Finalize ASCII file write.
					 * This function merge all temporary files in the final resulted file
					 */
					virtual bool finalize(bool isInfoFile = false);

				protected:
					/**
					 * @brief output stream file
					 */
					std::ofstream _outputFile;

					/**
					 * @brief Map of fields working structure
					 */
					std::map<std::string, ASCIIFieldInfo> _fieldInfoMap;

				private:
					/*
					 * @brief Clean output temporary file used for each output parameters
					 */
					void cleanTempFiles();

					/*
					 * @brief Add a a parameter info
					 */
					bool writeParameterInfo(ParamInfoSPtr parameterInfo, int level);

					/*
					 * brief Add a dataset info
					 */
					bool writeDatasetInfo(DataSetInfoSPtr datasetInfo,
										  std::set<std::pair<std::string, ParamInfoSPtr>, FileWriter::cmpParamInfoStruct> &baseParamsInfoList);

					/*
					 * brief Add an instrument info
					 */
					bool writeInstrumentInfo(InstrumentInfoSPtr instrumentInfo,
											 std::set<std::pair<std::string, ParamInfoSPtr>, FileWriter::cmpParamInfoStruct> &baseParamsInfoList);

					/*
					 * brief Add a mission info
					 */
					bool writeMissionInfo(MissionInfoSPtr missionInfo,
										  std::set<std::pair<std::string, ParamInfoSPtr>, FileWriter::cmpParamInfoStruct> &baseParamsInfoList);

					std::string getOutputTimeFormatStr();

					/*
					 * @brief current output file path
					 */
					std::string _outputFilePath;

					/*
					 * @brief Ordered list of output parameters
					 */
					std::list<std::string> _outputParamsList;

					/*
					 * @brief Flag to force to not write all info blocs
					 */
					bool _writeInfo;
				};

			} /* namespace FileWriter */
		}	  /* namespace Download */
	}		  /* namespace ParamOutputImpl */
} /* namespace AMDA */

#endif /* FILEWRITERASCIIABSTRACT_HH_ */