using System; using System.IO; using System.Text; namespace SabreTools.Library.IO { public class IniWriter : IDisposable { /// /// Internal stream writer for outputting /// private 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 (string.IsNullOrWhiteSpace(value)) throw new ArgumentException(nameof(value)); sw.WriteLine($"[{value.TrimStart('[').TrimEnd(']')}]"); } /// /// Write a key value pair /// public void WriteKeyValuePair(string key, string value) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException(nameof(key)); if (value == null) value = string.Empty; sw.WriteLine($"{key}={value}"); } /// /// Write a comment /// public void WriteComment(string value) { if (value == null) value = string.Empty; sw.WriteLine($";{value}"); } /// /// Write a generic string /// public void WriteString(string value) { if (value == null) value = string.Empty; sw.Write(value); } /// /// Write a newline /// public void WriteLine() { sw.WriteLine(); } /// /// Flush the underlying writer /// public void Flush() { sw.Flush(); } /// /// Dispose of the underlying writer /// public void Dispose() { sw.Dispose(); } } }