/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ namespace OmiLAXR.Utils { /// /// Utility class for writing CSV files with automatic formatting and optional auto-flushing. /// Extends BufferedUtf8Writer to provide CSV-specific functionality like header management /// and row writing with proper CSV formatting and escaping. /// public class CsvFile : BufferedUtf8Writer { /// /// Array of column headers for the CSV file. /// Set during initialization or via SetHeaders method. /// public string[] Headers { get; private set; } /// /// Controls whether the file buffer is automatically flushed after each write operation. /// When true, ensures immediate disk writes but may impact performance for frequent writes. /// public bool AutoFlush { get; set; } /// /// Initializes a new CSV file with optional headers and configuration. /// /// File path where the CSV will be written /// Optional array of column headers to write immediately /// Whether to flush after each write operation /// Size of the internal buffer in bytes public CsvFile(string path, string[] headers = null, bool autoFlush = false, int bufferSize = 8192) : base(path, bufferSize) { // Write headers immediately if provided if (headers != null) SetHeaders(headers); AutoFlush = autoFlush; } /// /// Sets the column headers and writes them as the first row of the CSV file. /// Overwrites any existing headers and writes the header row immediately. /// /// Array of header names to set and write public void SetHeaders(params string[] headers) { Headers = headers; // Write headers as comma-separated values WriteLine(string.Join(",", headers)); // Flush immediately if auto-flush is enabled if (AutoFlush) Flush(); } /// /// Writes a data row to the CSV file with automatic comma separation and optional flushing. /// Converts all values to strings and joins them with commas. /// /// Array of values to write as a CSV row public void WriteRow(params object[] values) { // Convert all values to strings and join with commas WriteLine(string.Join(",", values)); // Flush immediately if auto-flush is enabled if (AutoFlush) Flush(); } } }