#include "ratio.hpp" #include #include namespace ratio { void print_configuration(std::ostream& output, char kind, int slot, const Configuration& value) { output << kind << ' ' << slot << ' ' << static_cast(value.result) << ' ' << value.generation << '\n'; } void print_feed(std::ostream& output, int slot, const Feed& value) { output << 'F' << ' ' << slot << ' ' << static_cast(value.result) << ' ' << value.mapped << ' ' << value.weight << '\n'; } void print_snapshot(std::ostream& output, int slot, const Snapshot& value) { const Statistics& stats = value.stats; output << 'S' << ' ' << slot << ' ' << static_cast(value.result) << ' ' << value.generation << ' ' << stats.samples << ' ' << stats.weight << ' ' << stats.raw_total << ' ' << stats.mapped_total << ' ' << stats.raw_mean() << ' ' << stats.mapped_mean() << ' ' << stats.minimum << ' ' << stats.maximum << ' ' << stats.clipped << '\n'; } void print_totals(std::ostream& output, const Totals& value) { output << 'T' << ' ' << value.configured << ' ' << value.stats.samples << ' ' << value.stats.weight << ' ' << value.stats.raw_total << ' ' << value.stats.mapped_total << ' ' << value.stats.clipped << '\n'; } int run(std::istream& input, std::ostream& output) { Ledger ledger; Parser parser; std::string line; while (std::getline(input, line)) { const Command command = parser.parse(line); switch (command.verb) { case Verb::empty: break; case Verb::invalid: output << "E 1\n"; break; case Verb::configure: print_configuration(output, 'C', command.slot, ledger.configure(command.slot, command.profile)); break; case Verb::feed: print_feed(output, command.slot, ledger.feed(command.slot, command.raw, command.weight)); break; case Verb::snapshot: print_snapshot(output, command.slot, ledger.snapshot(command.slot)); break; case Verb::drop: print_configuration(output, 'D', command.slot, ledger.drop(command.slot)); break; case Verb::clear: print_configuration(output, 'Z', command.slot, ledger.clear(command.slot)); break; case Verb::totals: print_totals(output, ledger.totals()); break; } } return input.bad() || !output ? 2 : 0; } }