Opentk/Source/Bind/BindStreamWriter.cs
Stefanos A. 6022ea9fbd Improved multiline output
This lets us compose and print multiline strings without manually
adjusting indentation on each line.
2013-11-01 08:57:44 +01:00

76 lines
2.2 KiB
C#

#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.IO;
using System.Text.RegularExpressions;
using Bind.Structures;
using Enum=Bind.Structures.Enum;
namespace Bind
{
class BindStreamWriter : StreamWriter
{
int indent_level = 0;
Regex splitLines = new Regex(Environment.NewLine, RegexOptions.Compiled);
//Regex splitLines = new Regex("(\r\n|\n\r|\n|\r)", RegexOptions.Compiled);
public readonly string File;
public BindStreamWriter(string file)
: base(file)
{
File = file;
}
public void Indent()
{
++indent_level;
}
public void Unindent()
{
if (indent_level > 0)
--indent_level;
}
public override void Write(string value)
{
bool is_multiline = false;
foreach (var line in splitLines.Split(value))
{
WriteLine(line);
is_multiline = true;
}
if (!is_multiline)
{
for (int i = indent_level; i > 0; i--)
base.Write(" ");
base.Write(value);
}
}
public override void WriteLine(string value)
{
// Todo: it seems that spacing is not correct if this code
// is enabled on Linux/Mono. However, it works as it should on Windows/.Net.
// This could be related to line-ending differences, but I haven't been able to
// find the cause yet.
// This ugly workaround should work until the real cause is found.
if (Environment.OSVersion.Platform == PlatformID.Win32Windows ||
Environment.OSVersion.Platform == PlatformID.Win32NT ||
Environment.OSVersion.Platform == PlatformID.Win32S ||
Environment.OSVersion.Platform == PlatformID.WinCE)
{
for (int i = indent_level; i > 0; i--)
base.Write(" ");
}
base.WriteLine(value);
}
}
}