| #include "ctxstream.hpp" |
|
|
| #include <algorithm> |
| #include <cctype> |
| #include <map> |
| #include <sstream> |
|
|
| namespace ctxstream { |
|
|
| static std::string trim(const std::string& s) { |
| std::size_t a = 0, b = s.size(); |
| while (a < b && std::isspace(static_cast<unsigned char>(s[a]))) ++a; |
| while (b > a && std::isspace(static_cast<unsigned char>(s[b - 1]))) --b; |
| return s.substr(a, b - a); |
| } |
|
|
| static bool to_number(const std::string& s, double* out) { |
| if (s.empty()) return false; |
| std::string clean; |
| for (char c : s) { |
| if (c != ',' && c != '_') clean += c; |
| } |
| try { |
| std::size_t used = 0; |
| const double v = std::stod(clean, &used); |
| if (used != clean.size()) return false; |
| *out = v; |
| return true; |
| } catch (...) { |
| return false; |
| } |
| } |
|
|
| std::vector<Record> parse_records(const std::string& raw) { |
| std::vector<Record> out; |
| std::istringstream in(raw); |
| std::string line; |
| while (std::getline(in, line)) { |
| const std::string t = trim(line); |
| if (t.empty() || t == "NONE") continue; |
|
|
| |
| |
| |
| std::size_t cut = t.rfind('\t'); |
| if (cut == std::string::npos) { |
| const std::size_t colon = t.rfind(':'); |
| const std::size_t space = t.find_last_of(" "); |
| cut = (colon != std::string::npos && colon > space) ? colon : space; |
| } |
| if (cut == std::string::npos || cut == 0) continue; |
|
|
| double v = 0.0; |
| if (!to_number(trim(t.substr(cut + 1)), &v)) continue; |
| const std::string key = trim(t.substr(0, cut)); |
| if (key.empty()) continue; |
| out.push_back({key, v}); |
| } |
| return out; |
| } |
|
|
| Tally tally(const std::vector<SegmentResult>& results) { |
| Tally out; |
| std::map<std::string, double> sums; |
| for (const SegmentResult& r : results) { |
| if (!r.ok) continue; |
| |
| const std::vector<Record> recs = parse_records(r.raw); |
| out.parsed_records += static_cast<int>(recs.size()); |
| |
| |
| int lines = 0; |
| std::istringstream in(r.raw); |
| std::string l; |
| while (std::getline(in, l)) { |
| if (!trim(l).empty() && trim(l) != "NONE") ++lines; |
| } |
| out.unparsed_lines += std::max(0, lines - static_cast<int>(recs.size())); |
| for (const Record& rec : recs) { |
| sums[rec.key] += rec.value; |
| out.total += rec.value; |
| } |
| } |
| out.sorted.assign(sums.begin(), sums.end()); |
| std::sort(out.sorted.begin(), out.sorted.end(), |
| [](const auto& a, const auto& b) { |
| if (a.second != b.second) return a.second < b.second; |
| return a.first < b.first; |
| }); |
| return out; |
| } |
|
|
| static std::string fmt(double v) { |
| std::ostringstream o; |
| if (v == static_cast<long long>(v)) o << static_cast<long long>(v); |
| else o << v; |
| return o.str(); |
| } |
|
|
| std::string construct(const Tally& t, Answer how, const std::string& label) { |
| if (t.sorted.empty()) return label + ": NONE"; |
| switch (how) { |
| case Answer::Least: |
| return label + ": " + t.sorted.front().first; |
| case Answer::Most: |
| return label + ": " + t.sorted.back().first; |
| case Answer::Total: |
| return label + ": " + fmt(t.total); |
| case Answer::List: { |
| std::ostringstream o; |
| for (const auto& [k, v] : t.sorted) o << k << "\t" << fmt(v) << "\n"; |
| return o.str(); |
| } |
| } |
| return label + ": NONE"; |
| } |
|
|
| } |
|
|