mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
IO namespace
This commit is contained in:
101
SabreTools.Library/IO/IniWriter.cs
Normal file
101
SabreTools.Library/IO/IniWriter.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Library.IO
|
||||
{
|
||||
public class IniWriter : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal stream writer for outputting
|
||||
/// </summary>
|
||||
private StreamWriter sw;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for writing to a file
|
||||
/// </summary>
|
||||
public IniWriter(string filename)
|
||||
{
|
||||
sw = new StreamWriter(filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consturctor for writing to a stream
|
||||
/// </summary>
|
||||
public IniWriter(Stream stream, Encoding encoding)
|
||||
{
|
||||
sw = new StreamWriter(stream, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a section tag
|
||||
/// </summary>
|
||||
public void WriteSection(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
throw new ArgumentException(nameof(value));
|
||||
|
||||
sw.WriteLine($"[{value.TrimStart('[').TrimEnd(']')}]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a key value pair
|
||||
/// </summary>
|
||||
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}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a comment
|
||||
/// </summary>
|
||||
public void WriteComment(string value)
|
||||
{
|
||||
if (value == null)
|
||||
value = string.Empty;
|
||||
|
||||
sw.WriteLine($";{value}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a generic string
|
||||
/// </summary>
|
||||
public void WriteString(string value)
|
||||
{
|
||||
if (value == null)
|
||||
value = string.Empty;
|
||||
|
||||
sw.Write(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a newline
|
||||
/// </summary>
|
||||
public void WriteLine()
|
||||
{
|
||||
sw.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the underlying writer
|
||||
/// </summary>
|
||||
public void Flush()
|
||||
{
|
||||
sw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose of the underlying writer
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
sw.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user