Fix discrepancies between mono and .net
authorthefiddler <stapostol@gmail.com>
Wed, 12 Mar 2014 17:13:05 +0000 (18:13 +0100)
committerthefiddler <stapostol@gmail.com>
Wed, 12 Mar 2014 17:13:05 +0000 (18:13 +0100)
Source/Bind/BindStreamWriter.cs

index 7f6ac02..f627ef0 100644 (file)
@@ -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();
         }
     }
 }