Blame view

src/ParamGetImpl/LocalFileInterface/FileReaderASCII.cc 8.22 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
/*
 * FileReaderASCII.cc
 *
 *  Created on: Nov 24, 2014
 *  Author: AKKA
 */

#include <boost/algorithm/string/trim_all.hpp>
#include <boost/algorithm/string.hpp>

#include "FileReaderASCII.hh"
f5c53312   Benjamin Renard   Get calib info in...
12
#include "TimeUtil.hh"
fbe3c2bb   Benjamin Renard   First commit
13
14
15
16
17
18
19

namespace AMDA {
namespace LocalFileInterface {

// 32 KB is the maximum supported line size
int FileReaderASCII::MaxLineSize = 32 * 1024;

f5c53312   Benjamin Renard   Get calib info in...
20
21
std::string FileReaderASCII::ClbKey = "CALIB_INFO";

fbe3c2bb   Benjamin Renard   First commit
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
FileReaderASCII::FileReaderASCII() : _asciiStream()
{
}

FileReaderASCII::~FileReaderASCII()
{
}

bool FileReaderASCII::open(std::string filePath)
{
	if (isOpened())
	{
		LOG4CXX_ERROR(gLogger, "FileReaderASCII::open - A file is already opened");
		return false;
	}
	// Open ascii stream
	_asciiStream.open (filePath, std::ios::in);

	return (_asciiStream.is_open());
}

bool FileReaderASCII::close(void)
{
	if (isOpened())
	{
		_asciiStream.close();
	}

	return (_asciiStream.is_open() == false);
}

bool FileReaderASCII::isOpened(void)
{
	return (_asciiStream.is_open());
}

std::string FileReaderASCII::getTimeParamId(void)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderASCII::getTimeParamId");

	// Time parameter id is always at the first position in text files
	return std::string("0");
}

/*
f5c53312   Benjamin Renard   Get calib info in...
67
 * @brief Split line
fbe3c2bb   Benjamin Renard   First commit
68
 */
f5c53312   Benjamin Renard   Get calib info in...
69
bool FileReaderASCII::split (const char *line, std::vector<std::string> &lineFields)
fbe3c2bb   Benjamin Renard   First commit
70
71
72
73
74
75
76
77
78
{
	std::string sLine(line);

	// Remove all spaces in the given string (even multiple spaces between fields)
	boost::algorithm::trim_all(sLine);

	if ((sLine.find(" ") != std::string::npos) ||
	    (sLine.find("\t") != std::string::npos)) {

fbe3c2bb   Benjamin Renard   First commit
79
80
		boost::split(lineFields,sLine,boost::is_any_of(" \t"));

f5c53312   Benjamin Renard   Get calib info in...
81
		return true;
fbe3c2bb   Benjamin Renard   First commit
82

fbe3c2bb   Benjamin Renard   First commit
83
	}
f5c53312   Benjamin Renard   Get calib info in...
84
85

	return (false);
fbe3c2bb   Benjamin Renard   First commit
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
}

bool FileReaderASCII::getParamInfo(std::string& /*paramId*/, LocalParamType& /*paramType*/, int& /*dim1Size*/, int& /*dim2Size*/)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderASCII::getParamInfo");

	// Parameter types and size can't be determined by the reader
	return true;
}

int FileReaderASCII::getRecordIndex(std::string& /*timeId*/, double time)
{
	LOG4CXX_DEBUG(gLogger, "FileReaderASCII::getRecordIndex");

	char line [MaxLineSize];
	int lineCount = 0;

	// Set current stream position at the beginning of the file
	_asciiStream.seekg (0, _asciiStream.beg);

	while(_asciiStream.eof() == false) {
		if (_asciiStream.getline (line, MaxLineSize)) {
			// Comments in the file are not considered
			if (line [0] != '#') {
f5c53312   Benjamin Renard   Get calib info in...
110
111
112
113
114
115
				std::vector<std::string> lineFields;
				if (split (line, lineFields))
				{
					if (!lineFields.empty())
						if (getTime(lineFields[0]) >= time)
							return (lineCount);
fbe3c2bb   Benjamin Renard   First commit
116
117
118
119
				}
				lineCount++;
			}
		}
f5c53312   Benjamin Renard   Get calib info in...
120
		else if (_asciiStream.rdstate() == std::ios_base::failbit) {
fbe3c2bb   Benjamin Renard   First commit
121
122
123
124
125
126
127
128
129
130
131
132
			LOG4CXX_ERROR(gLogger, "FileReaderASCII::getRecordIndex - Error reading line content, maxSize is " << MaxLineSize);
			return -1;
		}
	}

	LOG4CXX_ERROR(gLogger, "FileReaderASCII::FileReaderASCII - Cannot get record index");
	return -1;
}

FileReaderStatus FileReaderASCII::getParamPacketData(std::string& /*timeId*/, std::string& paramId,
		int recordIndex, double stopTime, LocalParamDataPacket *packet)
{
f5c53312   Benjamin Renard   Get calib info in...
133
	LOG4CXX_DEBUG(gLogger, "FileReaderASCII::getParamPacketData - " << recordIndex);
fbe3c2bb   Benjamin Renard   First commit
134
135
136

	char line [MaxLineSize];
	int lineCount = 0;
fbe3c2bb   Benjamin Renard   First commit
137
138
139
	int colStart = std::stoi(paramId);
	int colStop = colStart + packet->getDimsSize();
	bool packetFull;
f5c53312   Benjamin Renard   Get calib info in...
140
	double crtTime;
fbe3c2bb   Benjamin Renard   First commit
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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

	float 	*dataFloat = NULL;
	double 	*dataDouble = NULL;
	short 	*dataShort = NULL;
	int		*dataInt = NULL;
	void 	*data;

	if ((packet->getType() != TYPE_FLOAT) &&
		(packet->getType() != TYPE_DOUBLE) &&
		(packet->getType() != TYPE_SHORT) &&
		(packet->getType() != TYPE_INT)	) {
		LOG4CXX_ERROR(gLogger, "FileReaderASCII::getParamPacketData - Not supported packet type");
		return FRS_ERROR;
	}

	if (packet->getType() == TYPE_FLOAT) {
		dataFloat = new float [packet->getDimsSize()];
		data = dataFloat;
	}
	else if (packet->getType() == TYPE_DOUBLE) {
		dataDouble = new double [packet->getDimsSize()];
		data = dataDouble;
	}
	else if (packet->getType() == TYPE_SHORT) {
		dataShort = new short [packet->getDimsSize()];
		data = dataShort;
	}
	else if (packet->getType() == TYPE_INT) {
		dataInt = new int [packet->getDimsSize()];
		data = dataInt;
	}

	// Set current stream position at the beginning of the file
	_asciiStream.seekg (0, _asciiStream.beg);

	while (_asciiStream.getline (line, MaxLineSize)) {
		// Comments in the file are not considered
		if (line [0] == '#')
			continue;

		lineCount++;

		// recordIndex reached ?
		if (lineCount <= recordIndex)
			continue;

f5c53312   Benjamin Renard   Get calib info in...
187
188
189
190
191
192
		//split line
		std::vector<std::string> lineFields;
		if (split (line, lineFields) && !lineFields.empty())
		{
			crtTime = getTime(lineFields[0]);

fbe3c2bb   Benjamin Renard   First commit
193
			// stopTime overtaken ?
f5c53312   Benjamin Renard   Get calib info in...
194
			if (crtTime > stopTime) {
fbe3c2bb   Benjamin Renard   First commit
195
196
197
198
199
200
201
202
203
204
205
206
207
				if (dataFloat != NULL)
					delete [] dataFloat;
				if (dataDouble != NULL)
					delete [] dataDouble;
				if (dataShort != NULL)
					delete [] dataShort;
				if (dataInt != NULL)
					delete [] dataInt;
				return FRS_FINISH;
			}

			// Extract values on the line depending on their type
			for (int col=colStart; col<colStop; col++) {
5c4d205f   Benjamin Renard   Fix some minor vi...
208
				if (col < (int)lineFields.size()) {
fbe3c2bb   Benjamin Renard   First commit
209
					if (packet->getType() == TYPE_FLOAT)
f5c53312   Benjamin Renard   Get calib info in...
210
						dataFloat [col-colStart] = std::stof (lineFields[col]);
fbe3c2bb   Benjamin Renard   First commit
211
					else if (packet->getType() == TYPE_DOUBLE)
f5c53312   Benjamin Renard   Get calib info in...
212
						dataDouble [col-colStart] = std::stod (lineFields[col]);
fbe3c2bb   Benjamin Renard   First commit
213
					else if (packet->getType() == TYPE_SHORT)
f5c53312   Benjamin Renard   Get calib info in...
214
						dataShort [col-colStart] = (short) std::stof (lineFields[col]);
fbe3c2bb   Benjamin Renard   First commit
215
					else if (packet->getType() == TYPE_INT)
f5c53312   Benjamin Renard   Get calib info in...
216
						dataInt [col-colStart] = std::stoi (lineFields[col]);
fbe3c2bb   Benjamin Renard   First commit
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
				} else {
					LOG4CXX_ERROR(gLogger, "FileReaderASCII::getParamPacketData - Error retrieving column " << col);
					if (dataFloat != NULL)
						delete [] dataFloat;
					if (dataDouble != NULL)
						delete [] dataDouble;
					if (dataShort != NULL)
						delete [] dataShort;
					if (dataInt != NULL)
						delete [] dataInt;
					return FRS_ERROR;
				}
			}

			//add data record in the packet
f5c53312   Benjamin Renard   Get calib info in...
232
			if (!packet->addData(crtTime, data, packetFull))
fbe3c2bb   Benjamin Renard   First commit
233
234
235
236
237
238
239
240
241
242
243
244
245
246
			{
				if (dataFloat != NULL)
					delete [] dataFloat;
				if (dataDouble != NULL)
					delete [] dataDouble;
				if (dataShort != NULL)
					delete [] dataShort;
				if (dataInt != NULL)
					delete [] dataInt;
				if (packetFull)
					return FRS_MORE;
				LOG4CXX_ERROR(gLogger, "FileReaderASCII::getParamPacketData - Error to add data in packet");
				return FRS_ERROR;
			}
fbe3c2bb   Benjamin Renard   First commit
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
		} else {
			LOG4CXX_ERROR(gLogger, "FileReaderASCII::getParamPacketData - Error retrieving time on line " << lineCount);
			if (dataFloat != NULL)
				delete [] dataFloat;
			if (dataDouble != NULL)
				delete [] dataDouble;
			if (dataShort != NULL)
				delete [] dataShort;
			if (dataInt != NULL)
				delete [] dataInt;
			return  FRS_ERROR;
		}
	}

	if (dataFloat != NULL)
		delete [] dataFloat;
	if (dataDouble != NULL)
		delete [] dataDouble;
	if (dataShort != NULL)
		delete [] dataShort;
	if (dataInt != NULL)
		delete [] dataInt;
	return FRS_EOF;
}

f5c53312   Benjamin Renard   Get calib info in...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * @brief Get an information
 */
bool FileReaderASCII::getInfo(const char* pInfoName, std::vector<double>& res)
{
	res.clear();

	// Set current stream position at the beginning of the file
	_asciiStream.seekg (0, _asciiStream.beg);

	char line [MaxLineSize];
	while (_asciiStream.getline (line, MaxLineSize)) {
		std::string lineStr = std::string(line);
		boost::algorithm::trim_all(lineStr);
		if (lineStr[0] != '#')
			continue;
		lineStr = lineStr.substr(1,lineStr.size()-1);
		boost::algorithm::trim_all(lineStr);
5c4d205f   Benjamin Renard   Fix some minor vi...
290
		if (!boost::starts_with(lineStr, FileReaderASCII::ClbKey))
f5c53312   Benjamin Renard   Get calib info in...
291
292
293
294
295
296
297
298
299
300
301
			continue;
		lineStr = lineStr.substr(FileReaderASCII::ClbKey.size(),lineStr.size()-FileReaderASCII::ClbKey.size());
		std::vector<std::string> lineValues;
		boost::split(lineValues,lineStr,boost::is_any_of(","));
		if (lineValues.empty())
			continue;
		for (auto &value : lineValues)
			boost::algorithm::trim_all(value);
		if (lineValues[0].compare(pInfoName) != 0)
			continue;

5c4d205f   Benjamin Renard   Fix some minor vi...
302
		for (int i = 1; i < (int)lineValues.size(); ++i)
f5c53312   Benjamin Renard   Get calib info in...
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
			res.push_back(std::stof(lineValues[i]));
		return true;
	}

	return false;
}

double FileReaderASCII::getTime(std::string& timeString) {
	switch (_timeFormat)
	{
		case LocalTimeFormat::DOUBLE :
			return std::stod(timeString);
		case LocalTimeFormat::ISO :
			return TimeUtil::readTimeInIso((char*)timeString.c_str());
		default:
			LOG4CXX_WARN(gLogger, "FileReaderASCII::getTime - Time format not implemented");
			return 0.;
	}
}

fbe3c2bb   Benjamin Renard   First commit
323
324
} /* LocalFileInterface */
} /* AMDA */