Blame view

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

#include "Catalog.hh"
#include "AsciiData.hh"
#include "AsciiReader.hh"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
1bcc7b53   Erdogan Furkan   #11651 - Done.
13
#include <boost/tokenizer.hpp>
fbe3c2bb   Benjamin Renard   First commit
14
15
16
17
18
19
20
21
22
23
24
#include <boost/lexical_cast.hpp>
#include <fstream>
#include "TimeTableCatalogUtil.hh"
#include <algorithm>
#include <cctype>

namespace TimeTableCatalog {

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

AsciiReader::AsciiReader(const std::string& pPath) :
818d2ea6   Benjamin Renard   Support new time ...
25
		AbstractReader(pPath), timeSize(0) {
fbe3c2bb   Benjamin Renard   First commit
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

}

AsciiReader::~AsciiReader() {

}

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

/**
 * Checks the TT format to know if the reader is the good one.
 */
bool AsciiReader::canRead(const std::string& pPath){
	std::string tmpPath(pPath);
	std::transform(tmpPath.begin(), tmpPath.end(), tmpPath.begin(), ::tolower);
	return boost::algorithm::ends_with(tmpPath, ".txt");
}

void AsciiReader::read(TimeTable& ptt) {
	std::ifstream ttfile(getLocalPath(), std::ios::in);

	// -- open file
	if (ttfile) {
		std::string line;
		// -- read line
		int crtIndex = 0;
		while (getline(ttfile, line)) {
			boost::algorithm::trim(line);
			if (boost::starts_with(line, "#")) {
				// -- it is a metadata
				readMetadata(line, ptt);
			} else {
				// -- add it to TimeTable
818d2ea6   Benjamin Renard   Support new time ...
59
60
61
				if (line.empty()) {
					continue;
				}
fbe3c2bb   Benjamin Renard   First commit
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
				ptt.addInterval(*readInterval(line, ptt,crtIndex));
				++crtIndex;
			}
		}
		// -- close file
		ttfile.close();
	} else {
		LOG4CXX_INFO(_logger,
				"TimeTable file Not Found or unreadable (" + getPath() + ")");
	}
}

std::unique_ptr<AbstractReader> AsciiReader::createInstance(
		const std::string& pPath) {
	return std::unique_ptr < AbstractReader > (new AsciiReader(pPath));
}

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

std::unique_ptr<TimeInterval> AsciiReader::readInterval(
		const std::string& pline, TimeTable& pTT, int pcrtIndex) {
fbe3c2bb   Benjamin Renard   First commit
83
84
	// set date time format if not set (on first interval)
	if (pTT._timeFormat == TimeTable::TIME_FORMAT::UNKNOWN) {
818d2ea6   Benjamin Renard   Support new time ...
85
86
87
88
		pTT._timeFormat = getTimeFormat(pline, this->timeSize);
		if (pTT._timeFormat == TimeTable::TIME_FORMAT::UNKNOWN) {
			std::unique_ptr <TimeInterval> ti (new TimeInterval(0, 0, pcrtIndex));
		}
fbe3c2bb   Benjamin Renard   First commit
89
90
	}

818d2ea6   Benjamin Renard   Support new time ...
91
92
93
94
95
96
97
98
99
100
101
102
	std::string line = pline;
	boost::algorithm::trim(line);
	// read start time
	double startTime = readISOTime(line, pTT._timeFormat);
	line.erase(0, this->timeSize);
	boost::algorithm::trim(line);

	// read stop time
	double stopTime = readISOTime(line, pTT._timeFormat);
	line.erase(0, this->timeSize);
	boost::algorithm::trim(line);

fbe3c2bb   Benjamin Renard   First commit
103
	// -- create time interval
818d2ea6   Benjamin Renard   Support new time ...
104
105
	std::unique_ptr <TimeInterval> ti (new TimeInterval(startTime, stopTime, pcrtIndex));

fbe3c2bb   Benjamin Renard   First commit
106

1bcc7b53   Erdogan Furkan   #11651 - Done.
107
108
	std::vector<std::string> params = splitLine(line);
	
fbe3c2bb   Benjamin Renard   First commit
109
	// If parameters value found, set them for the interval
818d2ea6   Benjamin Renard   Support new time ...
110
	if (!params.empty()) {
fbe3c2bb   Benjamin Renard   First commit
111
112
		// Starts setting parameter values with the third field of the
		// intervalFields vector (The first and second one are strat and stop dates)
fbe3c2bb   Benjamin Renard   First commit
113
114
115
116
		ParameterDescriptionList pdl = pTT.getParameterDescritptions();

		std::vector<std::string> paremeterValues;

818d2ea6   Benjamin Renard   Support new time ...
117
		int paramPos = 0;
fbe3c2bb   Benjamin Renard   First commit
118
119
120
		for (auto parameterDesc : pdl) {
			paremeterValues.clear();
			for (int paramIdx=0; paramIdx< parameterDesc.getSizeAsInt(); paramIdx++) {
818d2ea6   Benjamin Renard   Support new time ...
121
122
123
124
				if (paramPos < (int)params.size()) {
					paremeterValues.push_back(params[paramPos]);
					++paramPos;
				}
fbe3c2bb   Benjamin Renard   First commit
125
126
127
128
129
130
131
132
			}
			ti->addParameterData(parameterDesc.getId(), paremeterValues);
		}
	}

	return ti;
}

1bcc7b53   Erdogan Furkan   #11651 - Done.
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

std::vector<std::string> AsciiReader::splitLine(const std::string& line) {
    std::vector<std::string> result;
    std::string token;
    bool inQuotes = false;

    for (char c : line) {
        if (c == '"') {
            inQuotes = !inQuotes;
        } else if (c == ' ' && !inQuotes) {
            if (!token.empty()) {
                result.push_back(token);
                token.clear();
            }
        } else {
            token += c;
        }
    }

    // Add the last token
    if (!token.empty()) 
        result.push_back(token);
    return result;
}


fbe3c2bb   Benjamin Renard   First commit
159
160
161
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
void AsciiReader::readMetadata(const std::string& pline,
		TimeTable& ptimeTable) {
	// try to find string like "Prop:xxxx" and set _prop attribute to "xxxx"

	// -- here try to find "Name:"
	if (contains(pline, AsciiData::NAME_KEYWORD, "#")) {
		extractvalue(pline, ptimeTable._name);
	}
	// -- here try to find "Historic:"
	else if (contains(pline, AsciiData::HISTORIC_KEYWORD, "#")) {
		extractvalue(pline, ptimeTable._history);
	}
	// -- here try to find "CreationDate:"
	// and try to set it as unix time
	// if impossible, add the line to the description
	else if (contains(pline, AsciiData::CREATION_DATE_KEYWORD, "#")) {
		// convert creation date string to char*
		std::string creationDate;
		extractvalue(pline, creationDate);
		// try to read date
		try {
			ptimeTable._creationDate = TimeTableCatalog::readISOTime(creationDate);
		} catch (...) {
			addDescription(pline, ptimeTable);
		}

	} // -- every other lines are added to description field*/
	// -- here try to find "Parameter NN :"
	else if (contains(pline, AsciiData::PARAMETER_KEYWORD, "#")) {
		addParameter(pline, ptimeTable);
	}
	// -- here try to find "CreationDate:"
	// and try to set it as unix time
	// if impossible, add the line to the description
	else {
		addDescription(pline, ptimeTable);
	}

}

void AsciiReader::addDescription(const std::string & pline, TimeTable& ptt) {
	// get value after "#"
	std::string value = pline.substr(1);
	// remove ; character at the end of the line
	boost::algorithm::replace_last(value, ";", "");
	ptt._description.push_back(value);
}

void AsciiReader::addParameter(const std::string & pline, TimeTable& ptt) {
	// get value after "Parameter NN:"
	size_t pos = pline.find(": ");
	if (pos!=std::string::npos) {
		// Extract parameter informations (after ": ")
		std::string paramLine = pline.substr(pos+2);

		// remove ; character at the end of the line
		boost::algorithm::replace_last(paramLine, ";", "");

		ParameterDescription pd;

		// Split line using coma separator
		std::stringstream ss(paramLine);
		std::string paramField;
24ca3eda   Benjamin Renard   Fix catalog heade...
222
		while (std::getline(ss, paramField, ';')) {
fbe3c2bb   Benjamin Renard   First commit
223
224
225
226
227

			boost::algorithm::trim(paramField);
			std::vector<std::string> fields;
			boost::split(fields,paramField,boost::is_any_of(":"));

906b7c37   Benjamin Renard   Fix ASCII catalog...
228
229
230
231
232
233
			std::string value = "";
			if (fields.size() > 1) {
				std::vector<std::string> values(fields.begin()+1, fields.end());
				value = boost::algorithm::join(values, ":");
			}

fbe3c2bb   Benjamin Renard   First commit
234
235
			// Depending on the fiield [0] value, sets the corresponding parameter attribute
			if (fields [0] == AsciiData::ATTRIB_ID) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
236
				pd.setId(value);
fbe3c2bb   Benjamin Renard   First commit
237
238
			}
			if (fields [0] == AsciiData::ATTRIB_NAME) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
239
				pd.setName(value);
fbe3c2bb   Benjamin Renard   First commit
240
241
			}
			else if (fields [0] == AsciiData::ATTRIB_SIZE) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
242
				pd.setSize(value);
fbe3c2bb   Benjamin Renard   First commit
243
244
			}
			else if (fields [0] == AsciiData::ATTRIB_TYPE) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
245
				pd.setType(getTypeFromString(value));
fbe3c2bb   Benjamin Renard   First commit
246
247
			}
			else if (fields [0] == AsciiData::ATTRIB_UNIT) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
248
				pd.setUnit(value);
fbe3c2bb   Benjamin Renard   First commit
249
250
			}
			else if (fields [0] == AsciiData::ATTRIB_DESCRITION) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
251
				pd.setDescription(value);
fbe3c2bb   Benjamin Renard   First commit
252
			}
0b2d2cab   Erdogan Furkan   Kernel part for c...
253
254
255
			else if (fields [0] == AsciiData::ATTRIB_STATUS) {
				pd.setStatus(value);
			}
fbe3c2bb   Benjamin Renard   First commit
256
			else if (fields [0] == AsciiData::ATTRIB_UCD) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
257
				pd.setUcd(value);
fbe3c2bb   Benjamin Renard   First commit
258
259
			}
			else if (fields [0] == AsciiData::ATTRIB_UTYPE) {
906b7c37   Benjamin Renard   Fix ASCII catalog...
260
				pd.setUtype(value);
fbe3c2bb   Benjamin Renard   First commit
261
262
263
264
265
266
267
			}
		}

		((Catalog *) &ptt)->addParameterDescription(pd);
	}
}

68c29629   Benjamin Renard   Fix in TT/Catalog...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
ParameterDescription::ParameterType AsciiReader::getTypeFromString(std::string type) {
	boost::algorithm::trim(type);
	boost::algorithm::to_lower(type);

	if ((type.compare("string") == 0) || (type.compare("char") == 0)) {
		return ParameterDescription::ParameterType::String;
	}
	else if ((type.compare("integer") == 0) || (type.compare("int") == 0)) {
		return ParameterDescription::ParameterType::Integer;
	}
	else if ((type.compare("float") == 0) || (type.compare("double") == 0)) {
		return ParameterDescription::ParameterType::Double;
	}
	else if (type.compare("date") == 0) {
		return ParameterDescription::ParameterType::Date;
	}
	return ParameterDescription::ParameterType::String;
}

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