Blame view

src/ParamOutputImpl/Download/FileWriterASCIIJson.cc 5.63 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
272
/**
 * FileWriterASCIIJson.cc
 *  Created on: 21 oct. 2014
 *      Author: AKKA
 */

#include "FileWriterASCIIJson.hh"

#include <boost/algorithm/string.hpp>

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

FileWriterASCIIJson::FileWriterASCIIJson(AMDA::Parameters::ParameterManager& pParameterManager) :
		FileWriterASCIIAbstract(pParameterManager)
{
}

FileWriterASCIIJson::~FileWriterASCIIJson(void)
{
	if (_outputFile.is_open())
		writeEndFile();
}

std::string FileWriterASCIIJson::getExtension(void)
{
	return "json";
}

std::string FileWriterASCIIJson::getDataFillCharacter(void)
{
	return "";
}

void FileWriterASCIIJson::writeBeginFile(void)
{
	isFirstInfo.clear();
	if (!_outputFile.is_open())
		return;
	_outputFile << "{";
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndFile(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "}";
	isFirstInfo.clear();
}

void FileWriterASCIIJson::writeBeginGeneralDescription(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "\"infos\" : {";
	isFirstInfo.back() = false;
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndGeneralDescription(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "}";
	isFirstInfo.pop_back();
}

void FileWriterASCIIJson::writeErrorInfo(std::string msg)
{
	if (!_outputFile.is_open())
		return;
	std::string msgCorrection = msg;
	boost::replace_all(msgCorrection, "\n", " ");
	boost::replace_all(msgCorrection, "\"", " ");
	writeSingleInfo("error", msgCorrection, 0);
}

void FileWriterASCIIJson::writeBeginInfoGroup(std::string title, int /*level*/)
{
	if (!_outputFile.is_open())
			return;
	if (!isFirstInfo.back())
		_outputFile << ", ";
	if (title.empty())
		_outputFile << "{";
	else
	{
		std::string titleWithoutSpace = title;
		boost::replace_all(titleWithoutSpace, " ", "_");
		_outputFile << "\"" << titleWithoutSpace << "\": {";
	}
	isFirstInfo.back() = false;
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndInfoGroup(int /*level*/)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "}";
	isFirstInfo.pop_back();
}

void FileWriterASCIIJson::writeSingleInfo(std::string key, std::string value, int /*level*/)
{
	if (!_outputFile.is_open())
		return;
	if (!isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "\"" << key << "\" : \"" << value << "\"";
	isFirstInfo.back() = false;
}

void FileWriterASCIIJson::writeBeginInfoList(std::string title, int /*level*/)
{
	if (!_outputFile.is_open())
		return;
	if (!isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "\"" << title << "\" : [";
	isFirstInfo.back() = false;
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndInfoList(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "]";
	isFirstInfo.pop_back();
}

void FileWriterASCIIJson::writeBeginFieldsDescription(void)
{
	if (!_outputFile.is_open())
		return;
	if (!isFirstInfo.empty() && !isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "\"fields\" : [";
	isFirstInfo.back() = false;
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndFieldsDescription(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "]";
	isFirstInfo.pop_back();
}

void FileWriterASCIIJson::writeFieldDescription(std::string paramId)
{
	if (!_outputFile.is_open())
		return;
	if(!isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "{";

	_outputFile << "\"id\" : \"" << paramId << "\"";

	_outputFile << ", \"name\" : \"" << _fieldInfoMap[paramId].name << "\"";

	_outputFile << ", \"ucd\" : \"" << _fieldInfoMap[paramId].ucd << "\"";

	_outputFile << ", \"type\" : \"";
	switch (_fieldInfoMap[paramId].type)
	{
	case DT_FLOAT:
		_outputFile << "float";
		break;
	case DT_SHORT:
		_outputFile << "short";
		break;
	case DT_INT:
		_outputFile << "int";
		break;
	case DT_DOUBLE:
		_outputFile << "double";
		break;
	case DT_LONGDOUBLE:
		_outputFile << "long double";
		break;
	case DT_LOGICAL:
		_outputFile << "logical";
		break;
	case DT_TIME:
		_outputFile << "time";
		break;
	default:
		_outputFile << "unknown";
	}

	std::stringstream size;
	if ((_fieldInfoMap[paramId].type != DT_TIME) &&
		!_fieldInfoMap[paramId].componentsList.empty() &&
		(_fieldInfoMap[paramId].dimSize1 * _fieldInfoMap[paramId].dimSize2 != (int)_fieldInfoMap[paramId].componentsList.size()))
	{
		//some components are not write
		size << _fieldInfoMap[paramId].componentsList.size();
	}
	else
	{
		//all components are write
		size << _fieldInfoMap[paramId].dimSize1;
		if (_fieldInfoMap[paramId].dimSize2 > 1)
		{
			size << "*";
			size << _fieldInfoMap[paramId].dimSize2;
		}
	}

	_outputFile << "\", \"size\" : \"" << size.str() << "\"";

	_outputFile << "}";

	isFirstInfo.back() = false;
}

void FileWriterASCIIJson::writeBeginData(void)
{
	if (!_outputFile.is_open())
		return;
	if (!isFirstInfo.empty() && !isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "\"datas\" : [";
	isFirstInfo.back() = false;
	isFirstInfo.push_back(true);
}

void FileWriterASCIIJson::writeEndData(void)
{
	if (!_outputFile.is_open())
		return;
	_outputFile << "]";
	isFirstInfo.pop_back();
}

void FileWriterASCIIJson::writeDataRecord(std::string record)
{
	if (!_outputFile.is_open())
		return;
	if (!isFirstInfo.back())
		_outputFile << ", ";
	_outputFile << "[" << record << "]";
	isFirstInfo.back() = false;
}

std::string FileWriterASCIIJson::getDataStartTag(bool isTimeData)
{
	if (isTimeData)
		return "\"";
	return ", \"";
}

std::string FileWriterASCIIJson::getDataStopTag(void)
{
	return "\"";
}

std::string FileWriterASCIIJson::getDataValueSeparator(void)
{
	return ", ";
}

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