Files
MMPS_DZ_1/plugins/csv_output_plugin.cpp
T

48 lines
1.1 KiB
C++

#include "api.hpp"
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <locale>
PLUGIN_EXPORT std::uint32_t plugin_api_version(){
return PLUGIN_API_VERSION;
}
PLUGIN_EXPORT PluginType plugin_type(){
return PluginType::Output;
}
PLUGIN_EXPORT const char* plugin_name(){
return "CSV output";
}
PLUGIN_EXPORT const char* plugin_description(){
return "Writes calculated solution points to a CSV file";
}
PLUGIN_EXPORT int write_output(const OutputParameters* parameters){
if (parameters == nullptr || parameters->destination_path == nullptr ||
parameters->points == nullptr)
{
return 1;
}
std::ofstream stream(parameters->destination_path);
if (!stream)
return 2;
stream.imbue(std::locale::classic());
stream << std::setprecision(17);
stream << "t,x1,x2,x3\n";
for (size_t index = 0; index < parameters->point_count; ++index){
const SolutionPoint& point = parameters->points[index];
stream << point.t << ',' << point.x1 << ','
<< point.x2 << ',' << point.x3 << '\n';
}
return stream ? 0 : 3;
}