Blame view

src/TimeTableCatalog/SpaceWriter.cc 6.92 KB
04d31e39   Hacene SI HADJ MOHAND   new format ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * SpaceWriter.cc
 *
 *  Created on: 31/05/2021
 *      Author: akka
 */

#include <iostream>

#include "SpaceData.hh"
#include "SpaceWriter.hh"
#include <fstream>
#include "TimeTableCatalogUtil.hh"

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

04d31e39   Hacene SI HADJ MOHAND   new format ok
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
namespace TimeTableCatalog {

const std::string SpaceWriter::FORMAT = "SPACE";

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

}

SpaceWriter::~SpaceWriter() {
}

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

std::string SpaceWriter::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> SpaceWriter::createInstance(
		const std::string& pPath, const std::string& pName) {
	return std::unique_ptr < AbstractWriter > (new SpaceWriter(pPath, pName));
}

/**
 * Gets the tt file expected extension, starting with .
 */
const std::string SpaceWriter::getExtension() const {
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
61
	return ".txt";
04d31e39   Hacene SI HADJ MOHAND   new format ok
62
63
64
65
66
67
}

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

void SpaceWriter::writeMetaData(const TimeTable& pTT,
		std::ostream& pOut) {
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
68
                    // write version 
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
69
                     pOut << "# " <<SpaceData::EVENT_TABLE_VERSION_KEYWORD << " " << pTT._eventTableVersion << std::endl;
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
70
71
72
                     	// -- write ListTitle
	pOut << "# " << SpaceData::LIST_TITLE_KEYWORD<< " " << pTT._name << std::endl;
                          	// -- write ListID
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
73
	pOut << "# " << SpaceData::LIST_ID_KEYWORD<< " " << pTT._listID << std::endl;
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
74
75
        
        	// -- write creation date
04d31e39   Hacene SI HADJ MOHAND   new format ok
76
	pOut << "# " << SpaceData::CREATION_DATE_KEYWORD << " ";
b3096d5d   Hacene SI HADJ MOHAND   correcting time i...
77
	writeTimeData(pTT._creationDate, pTT._extTimeFormat, pOut);
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
78
79
80
81
	pOut << std::endl;
        
        	// -- write modify date
	pOut << "# " << SpaceData::MODIFY_DATE_KEYWORD << " ";
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
82
83
84
                    if(pTT._modificationDate == 0){
                             pOut <<"0001-01-01T00:00:00:00.000Z";
                    }else{
b3096d5d   Hacene SI HADJ MOHAND   correcting time i...
85
                            writeTimeData(pTT._modificationDate, pTT._extTimeFormat, pOut);
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
86
                    }
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
87
88
89
90
	pOut << std::endl;
        
                    ParameterDescriptionList pdl = pTT.getParameterDescritptions();
                    // FieldNames
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
91
                   pOut << "# " << SpaceData::FIELD_NAMES_KEYWORD << " "<< "t_begin" <<" "<< "t_end"<<" ";
04d31e39   Hacene SI HADJ MOHAND   new format ok
92
	for (auto parameterDescription : pdl) {
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
93
94
95
96
97
		pOut << parameterDescription.getId() << " ";
	}
                   pOut <<std::endl;
                   
                     // FieldUnits
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
98
99
                   pOut << "# " << SpaceData::FIELD_UNIT_KEYWORD << " "<< "DateTime" <<" "<< "DateTime"<<" ";
                    std::string unit;
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
100
                   for (auto parameterDescription : pdl) {
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
101
102
                                        unit = parameterDescription.getUnit().empty() ? "None" :  parameterDescription.getUnit();
		pOut << unit << " ";
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
103
104
105
106
	}
                   pOut <<std::endl;
                   
                   // FieldTypes
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
107
                   pOut << "# " << SpaceData::FIELD_TYPES_KEYWORD << " "<< "char" <<" "<< "char"<<" ";
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
108
109
110
111
112
113
                   for (auto parameterDescription : pdl) {
		pOut << getTypeAsString(parameterDescription.getType()) << " ";
	}
                   pOut <<std::endl;
                   
                   // # FieldNulls
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
114
                    pOut << "# " << SpaceData::FIELD_NULLS_KEYWORD << " "<< " "<< "0001-01-01T00:00:00:00.000Z" <<" "<< "0001-01-01T00:00:00:00.000Z"<<" ";;
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
115
116
117
118
119
120
121
                   for (auto parameterDescription : pdl) {
		pOut << parameterDescription.getNull() << " ";
	}
                   pOut <<std::endl;                  
                   
                   //ListStartDate
                   pOut << "# " << SpaceData::LIST_START_DATE_KEYWORD << " ";
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
122
123
124
                   if(pTT._listStartDate == 0){
                           pOut <<"0001-01-01T00:00:00:00.000Z";
                   }else{ 
b3096d5d   Hacene SI HADJ MOHAND   correcting time i...
125
                           writeTimeData(pTT._listStartDate, pTT._extTimeFormat, pOut);
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
126
                   }
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
127
128
129
130
	pOut << std::endl;
        
                   //ListStopDate
                   pOut << "# " << SpaceData::LIST_STOP_DATE_KEYWORD << " ";
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
131
132
133
                   if(pTT._listStopDate == 0){
                           pOut <<"0001-01-01T00:00:00:00.000Z";
                   }else{ 
b3096d5d   Hacene SI HADJ MOHAND   correcting time i...
134
                           writeTimeData(pTT._listStopDate, pTT._extTimeFormat, pOut);
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
135
                   }
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	pOut <<  std::endl;
        
                    // Contact:
                    pOut << "# " << SpaceData::CONTACT_KEYWORD << " " << pTT._contact << std::endl;
                    
                    // Contact:
                    pOut << "# " << SpaceData::CONTACT_ID_KEYWORD << " " << pTT._contactID << std::endl;
        
        
	// -- write description
                    pOut << "# " << SpaceData::DESCRIPTION_KEYWORD <<std::endl;
	for (auto line : pTT._description) {
		pOut << "# " << line << std::endl;
	}
cea3f975   Benjamin Renard   Fix TT & catalog ...
150
151
152
153
154
                    if(! pTT._history.empty()) {
                             std::string history = pTT._history;
                             boost::replace_all(history, "\n", "\n# ");
                             pOut << "# " << history << std::endl;
                    }
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
155
156
        
                    // repeat variable names
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
157
                    pOut << "# " << "t_begin" <<" "<< "t_end"<<" ";
a1184f24   Hacene SI HADJ MOHAND   HPEvent in progress
158
159
                     for (auto parameterDescription : pdl) {
		pOut << parameterDescription.getId() << " ";
04d31e39   Hacene SI HADJ MOHAND   new format ok
160
	}
91128aae   Hacene SI HADJ MOHAND   kernel arguments ok
161
                    pOut <<  std::endl;
04d31e39   Hacene SI HADJ MOHAND   new format ok
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
}

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

	for (TimeInterval interval : pTT.getIntervals()) {
		writeTimeData(interval._startTime, pTT._extTimeFormat, pOut);
		pOut << SpaceData::SEPARATOR;
		writeTimeData(interval._stopTime, pTT._extTimeFormat, 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 << SpaceData::SEPARATOR << dataValue;
				}
			}
		}

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

std::string SpaceWriter::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";
}

} /* namespace TimeTableCatalog */