Fix discrepancies between mono and .net

This commit is contained in:
thefiddler 2014-03-12 18:13:05 +01:00
parent 1c6cfc390b
commit 13e80a7c06

View file

@ -35,18 +35,18 @@ using Enum=Bind.Structures.Enum;
namespace Bind namespace Bind
{ {
class BindStreamWriter : StreamWriter class BindStreamWriter : IDisposable
{ {
int indent_level = 0; static readonly char[] SplitCharacters = new char[] { '\r', '\n' };
Regex splitLines = new Regex(Environment.NewLine, RegexOptions.Compiled); readonly StreamWriter sw;
//Regex splitLines = new Regex("(\r\n|\n\r|\n|\r)", RegexOptions.Compiled);
public readonly string File; public readonly string File;
int indent_level = 0;
public BindStreamWriter(string file) public BindStreamWriter(string file)
: base(file)
{ {
File = file; File = file;
sw = new StreamWriter(file);
} }
public void Indent() public void Indent()
@ -60,9 +60,10 @@ namespace Bind
--indent_level; --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; bool is_multiline = lines.Length > 1;
if (is_multiline) if (is_multiline)
{ {
@ -71,21 +72,31 @@ namespace Bind
{ {
var line = lines[i]; var line = lines[i];
WriteIndentations(); WriteIndentations();
base.Write(line); sw.Write(line);
base.Write(System.Environment.NewLine); sw.Write(System.Environment.NewLine);
} }
// Write the last line without appending a newline // Write the last line without appending a newline
WriteIndentations(); WriteIndentations();
base.Write(lines[lines.Length - 1]); sw.Write(lines[lines.Length - 1]);
} }
else else
{ {
WriteIndentations(); 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 Mono implementation of WriteLine calls Write internally.
// The .Net implementation does not. // The .Net implementation does not.
@ -97,13 +108,33 @@ namespace Bind
{ {
WriteIndentations(); 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() void WriteIndentations()
{ {
for (int i = indent_level; i > 0; i--) for (int i = indent_level; i > 0; i--)
base.Write(" "); sw.Write(" ");
}
public void Dispose()
{
sw.Dispose();
} }
} }
} }