Newer
Older
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
#include "ToJSON.h"
#include "Event.h"
#include <fstream>
using namespace std;
string JSON::Error = "";
string JSON::SeparateRows = "}, \n";
string JSON::SeparateSubRows = ", \n";
string JSON::FinishRows = "}\n\t}";
string JSON::FinishSubRows = "}";
string JSON::Finish = "}\n";
string JSON::StartRow(const string& name)
{
string row = "\n{ \"" + name + "\" : ";
return row;
}
string JSON::StartSubRow(const string& name)
{
string row = "\t\t\"" + name + "\" : ";
return row;
}
string JSON::StartTypedBlock(const string& name)
{
string row = "\n\t\"" + name + "\" : \n\t{ \n";
return row;
}
bool JSON::GenerateRow(EventData Event, string &ResRow, const string& EndRowJSON)
{
string EventModeName = JSON::StartSubRow("Event mode") + "\"" + Event.GetEventModeName() + "\"" + JSON::SeparateSubRows;
char AccurateValue[256];
sprintf(AccurateValue, "%.10e", Event.elapsed_time);
string FullElapsedTime = JSON::StartSubRow(string("Full elapsed time")) + string(AccurateValue) + JSON::SeparateSubRows;
AccurateValue[0] = '\0';
double mean_elapsed = Event.GetMeanElapsedTime();
sprintf(AccurateValue, "%.10e", mean_elapsed);
string MeanElapsedTime = JSON::StartSubRow(string("Mean elapsed time")) + string(AccurateValue) + JSON::SeparateSubRows;
string TimeSeries = JSON::StartSubRow(string("Time series")) + "[";
int TimeSeriesLen = Event.time_series.size();
for (int i = 0; i < TimeSeriesLen - 1; i++)
{
AccurateValue[0] = '\0';
sprintf(AccurateValue, "%.10e", Event.time_series[i]);
TimeSeries += string(AccurateValue) + ", ";
}
if(TimeSeriesLen != 0)
{
AccurateValue[0] = '\0';
sprintf(AccurateValue, "%.10e", Event.time_series[TimeSeriesLen - 1]);
TimeSeries += string(AccurateValue);
}
TimeSeries += "]" ;
string JSONrow = JSON::StartSubRow(Event.event_name) + "{" + EventModeName + FullElapsedTime + MeanElapsedTime + TimeSeries + EndRowJSON;
ResRow = JSONrow;
return true;
}
bool JSON::WriteOutput(const string& Filename, const string& OutputData)
{
ofstream file;
file.open(Filename);
if (!file)
{
string error_definition = "Can't open file " + Filename + "\n";
JSON::Error = error_definition;
return false;
}
file << OutputData;
file.close();
return true;
}