Blame view

src/TimeTableCatalog/AsciiWriter.cc 5.23 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * AsciiWriter.cc
 *
 *  Created on: 6 août 2013
 *      Author: CS
 */

#include <iostream>

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

cea3f975   Benjamin Renard   Fix TT & catalog ...
15
16
#include <boost/algorithm/string.hpp>

fbe3c2bb   Benjamin Renard   First commit
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
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) {
d1774421   Hacene SI HADJ MOHAND   Kernek ok
68
69
70
            
	// -- write name
	pOut << "# " << AsciiData::NAME_KEYWORD << " " << pTT._name << ";" << std::endl;
fbe3c2bb   Benjamin Renard   First commit
71
	// -- write description
d1774421   Hacene SI HADJ MOHAND   Kernek ok
72
                    pOut << "# " << AsciiData::DESCRIPTION_KEYWORD <<std::endl ;
fbe3c2bb   Benjamin Renard   First commit
73
74
75
	for (auto line : pTT._description) {
		pOut << "# " << line << ";" << std::endl;
	}
d1774421   Hacene SI HADJ MOHAND   Kernek ok
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
                    pOut << "# " <<std::endl;
                    //ListStartDate
                   pOut << "# " << AsciiData::LIST_START_DATE_KEYWORD << " ";
                   if(pTT._listStartDate == 0){
                           pOut <<"0001-01-01T00:00:00:00.000Z";
                   }else{ 
                           writeISOTime(pTT._listStartDate, pTT._timeFormat, pOut);
                   }
	pOut << std::endl;
        
                   //ListStopDate
                   pOut << "# " << AsciiData::LIST_STOP_DATE_KEYWORD << " ";
                   if(pTT._listStopDate == 0){
                           pOut <<"0001-01-01T00:00:00:00.000Z";
                   }else{ 
                           writeISOTime(pTT._listStopDate, pTT._timeFormat, pOut);
                   }
	pOut <<  std::endl;
        
                    // Contact:
                    pOut << "# " << AsciiData::CONTACT_KEYWORD << " " << pTT._contact << std::endl;
fbe3c2bb   Benjamin Renard   First commit
97
98

	// -- write historic
cea3f975   Benjamin Renard   Fix TT & catalog ...
99
100
101
	std::string history = pTT._history;
	boost::replace_all(history, "\n", "\n# ");
	pOut << "# " << AsciiData::HISTORIC_KEYWORD << " " << history << ";" << std::endl;
fbe3c2bb   Benjamin Renard   First commit
102
103
104
105
106
107
108
109
110
111
112

	// -- 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) << ": ";
4de2b52c   Benjamin Renard   Fix some tests + ...
113
114
115
		pOut << AsciiData::ATTRIB_ID << ":" << parameterDescription.getId() << "; ";
		pOut << AsciiData::ATTRIB_NAME << ":" << parameterDescription.getName() << "; ";
		pOut << AsciiData::ATTRIB_SIZE << ":" << parameterDescription.getSize() << "; ";
68c29629   Benjamin Renard   Fix in TT/Catalog...
116
		pOut << AsciiData::ATTRIB_TYPE << ":" << getTypeAsString(parameterDescription.getType()) << "; ";
4de2b52c   Benjamin Renard   Fix some tests + ...
117
118
119
		pOut << AsciiData::ATTRIB_UNIT << ":" << parameterDescription.getUnit() << "; ";
		pOut << AsciiData::ATTRIB_DESCRITION << ":" << parameterDescription.getDescription() << "; ";
		pOut << AsciiData::ATTRIB_UCD << ":" << parameterDescription.getUcd() << "; ";
fbe3c2bb   Benjamin Renard   First commit
120
121
122
123
124
125
126
127
128
		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()) {
5b387233   Vincent Cephirins   RM7926 - Evol - C...
129
		writeTimeData(interval._startTime, pTT._extTimeFormat, pOut);
fbe3c2bb   Benjamin Renard   First commit
130
		pOut << AsciiData::SEPARATOR;
5b387233   Vincent Cephirins   RM7926 - Evol - C...
131
		writeTimeData(interval._stopTime, pTT._extTimeFormat, pOut);
fbe3c2bb   Benjamin Renard   First commit
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

		// 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;
	}
}

68c29629   Benjamin Renard   Fix in TT/Catalog...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
std::string AsciiWriter::getTypeAsString(ParameterDescription::ParameterType type) {
	switch (type) {
		case ParameterDescription::ParameterType::Unknown:
			return "string";
		case ParameterDescription::ParameterType::Double:
			return "double";
		case ParameterDescription::ParameterType::Date:
			return "date";
		case ParameterDescription::ParameterType::String:
			return "string";
		case ParameterDescription::ParameterType::Integer:
			return "integer";
	}
	return "string";
}

fbe3c2bb   Benjamin Renard   First commit
165
} /* namespace TimeTableCatalog */