From 66f1a27bc0ef29d43c98c02b20d0b7786bb56ce8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 13 Jun 2020 23:37:15 -0700 Subject: [PATCH] Add IniWriter --- SabreTools.Library/Writers/IniWriter.cs | 101 ++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 SabreTools.Library/Writers/IniWriter.cs diff --git a/SabreTools.Library/Writers/IniWriter.cs b/SabreTools.Library/Writers/IniWriter.cs new file mode 100644 index 00000000..a2e3d1b4 --- /dev/null +++ b/SabreTools.Library/Writers/IniWriter.cs @@ -0,0 +1,101 @@ +using System; +using System.IO; +using System.Text; + +namespace SabreTools.Library.Writers +{ + 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(); + } + } +}