From 13e80a7c067a4f40c721bfbe5dd7aae0d5a1283d Mon Sep 17 00:00:00 2001 From: thefiddler Date: Wed, 12 Mar 2014 18:13:05 +0100 Subject: [PATCH] Fix discrepancies between mono and .net --- Source/Bind/BindStreamWriter.cs | 61 +++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/Source/Bind/BindStreamWriter.cs b/Source/Bind/BindStreamWriter.cs index 7f6ac02..f627ef0 100644 --- a/Source/Bind/BindStreamWriter.cs +++ b/Source/Bind/BindStreamWriter.cs @@ -35,18 +35,18 @@ using Enum=Bind.Structures.Enum; namespace Bind { - class BindStreamWriter : StreamWriter + class BindStreamWriter : IDisposable { - int indent_level = 0; - Regex splitLines = new Regex(Environment.NewLine, RegexOptions.Compiled); - //Regex splitLines = new Regex("(\r\n|\n\r|\n|\r)", RegexOptions.Compiled); - + static readonly char[] SplitCharacters = new char[] { '\r', '\n' }; + readonly StreamWriter sw; public readonly string File; + int indent_level = 0; + public BindStreamWriter(string file) - : base(file) { File = file; + sw = new StreamWriter(file); } public void Indent() @@ -60,9 +60,10 @@ namespace Bind --indent_level; } - public override void Write(string value) + public void Write(string value) { - var lines = splitLines.Split(value); + var lines = value.Split(SplitCharacters, + StringSplitOptions.RemoveEmptyEntries); bool is_multiline = lines.Length > 1; if (is_multiline) { @@ -71,21 +72,31 @@ namespace Bind { var line = lines[i]; WriteIndentations(); - base.Write(line); - base.Write(System.Environment.NewLine); + sw.Write(line); + sw.Write(System.Environment.NewLine); } // Write the last line without appending a newline WriteIndentations(); - base.Write(lines[lines.Length - 1]); + sw.Write(lines[lines.Length - 1]); } else { WriteIndentations(); - base.Write(value); + sw.Write(value); } } - public override void WriteLine(string value) + public void Write(string format, params object[] args) + { + Write(String.Format(format, args)); + } + + public void WriteLine() + { + sw.WriteLine(); + } + + public void WriteLine(string value) { // The Mono implementation of WriteLine calls Write internally. // The .Net implementation does not. @@ -97,13 +108,33 @@ namespace Bind { WriteIndentations(); } - base.WriteLine(value); + sw.WriteLine(value); + } + + public void WriteLine(string format, params object[] args) + { + WriteLine(String.Format(format, args)); + } + + public void Flush() + { + sw.Flush(); + } + + public void Close() + { + sw.Close(); } void WriteIndentations() { for (int i = indent_level; i > 0; i--) - base.Write(" "); + sw.Write(" "); + } + + public void Dispose() + { + sw.Dispose(); } } } -- 2.7.4