using System; using System.IO; using System.Text; namespace SabreTools.IO.Writers { public class IniWriter : IDisposable { /// /// Internal stream writer for outputting /// private readonly StreamWriter? sw; /// /// Constructor for writing to a file /// public IniWriter(string filename) { sw = new StreamWriter(filename); } /// /// Consturctor for writing to a stream /// public IniWriter(Stream stream, Encoding encoding) { sw = new StreamWriter(stream, encoding); } /// /// Write a section tag /// public void WriteSection(string? value) { if (sw?.BaseStream == null) return; if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Section tag cannot be null or empty", nameof(value)); sw.WriteLine($"[{value.TrimStart('[').TrimEnd(']')}]"); } /// /// Write a key value pair /// public void WriteKeyValuePair(string key, string? value) { if (sw?.BaseStream == null) return; if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("Key cannot be null or empty", nameof(key)); value ??= string.Empty; sw.WriteLine($"{key}={value}"); } /// /// Write a comment /// public void WriteComment(string? value) { if (sw?.BaseStream == null) return; value ??= string.Empty; sw.WriteLine($";{value}"); } /// /// Write a generic string /// public void WriteString(string? value) { if (sw?.BaseStream == null) return; value ??= string.Empty; sw.Write(value); } /// /// Write a newline /// public void WriteLine() { if (sw?.BaseStream == null) return; sw.WriteLine(); } /// /// Flush the underlying writer /// public void Flush() { sw?.Flush(); } /// /// Dispose of the underlying writer /// public void Dispose() { sw?.Dispose(); } } }