Skip to content

File column_width_tracker.cpp

File List > benchmark > exporter > column_width_tracker.cpp

Go to the documentation of this file

#include "column_width_tracker.h"

#include <algorithm>
#include <cstdio>
#include <cstring>

namespace Argos {

void ColumnWidthTracker::ensureCol(int col) {
    if (col >= static_cast<int>(widths.size())) {
        widths.resize(col + 1, 0);
    }
}

void ColumnWidthTracker::measureString(int col, const std::string &text) {
    ensureCol(col);
    widths[col] = std::max(widths[col], text.length());
}

void ColumnWidthTracker::measureInt(int col, int64_t value) {
    ensureCol(col);
    widths[col] = std::max(widths[col], std::to_string(value).length());
}

void ColumnWidthTracker::measureDouble(int col, double value, const char *fmt) {
    char buf[64];
    std::snprintf(buf, sizeof(buf), fmt, value);
    ensureCol(col);
    widths[col] = std::max(widths[col], std::strlen(buf));
}

void ColumnWidthTracker::applyToWorksheet(lxw_worksheet *ws) const {
    for (size_t col = 0; col < widths.size(); ++col) {
        double width = static_cast<double>(std::max(widths[col], MIN_WIDTH)) + PADDING;
        worksheet_set_column(ws, col, col, width, NULL);
    }
}

} // namespace Argos