2013-11-01 13:00:46 +00:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006 - 2013 Stefanos Apostolopoulos for the Open Toolkit Library
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2009-08-21 20:28:14 +00:00
|
|
|
|
using Bind.Structures;
|
|
|
|
|
using Enum=Bind.Structures.Enum;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
namespace Bind
|
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
enum WriteOptions
|
|
|
|
|
{
|
|
|
|
|
Default = 0,
|
|
|
|
|
NoIndent = 1
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 17:13:05 +00:00
|
|
|
|
class BindStreamWriter : IDisposable
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-06-20 19:58:02 +00:00
|
|
|
|
static readonly string[] SplitStrings = new string[] { System.Environment.NewLine };
|
2014-03-12 17:13:05 +00:00
|
|
|
|
readonly StreamWriter sw;
|
2010-12-06 22:32:11 +00:00
|
|
|
|
public readonly string File;
|
|
|
|
|
|
2014-06-20 19:58:02 +00:00
|
|
|
|
bool newline = true;
|
2014-03-12 17:13:05 +00:00
|
|
|
|
int indent_level = 0;
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public BindStreamWriter(string file)
|
|
|
|
|
{
|
2010-12-06 22:32:11 +00:00
|
|
|
|
File = file;
|
2014-03-12 17:13:05 +00:00
|
|
|
|
sw = new StreamWriter(file);
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Indent()
|
|
|
|
|
{
|
|
|
|
|
++indent_level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unindent()
|
|
|
|
|
{
|
|
|
|
|
if (indent_level > 0)
|
|
|
|
|
--indent_level;
|
|
|
|
|
}
|
2014-06-20 19:58:02 +00:00
|
|
|
|
|
2014-03-31 07:50:51 +00:00
|
|
|
|
public void Write(WriteOptions options, string value)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-06-20 19:58:02 +00:00
|
|
|
|
var lines = value.Split(SplitStrings, StringSplitOptions.None);
|
|
|
|
|
for (int i = 0; i < lines.Length; ++i)
|
2013-11-01 07:57:44 +00:00
|
|
|
|
{
|
2014-06-20 19:58:02 +00:00
|
|
|
|
if (lines[i].Length != 0)
|
|
|
|
|
{
|
|
|
|
|
if (newline)
|
|
|
|
|
{
|
|
|
|
|
WriteIndentations(options);
|
|
|
|
|
}
|
|
|
|
|
newline = false;
|
|
|
|
|
sw.Write(lines[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we're going to write another line add a line break string
|
|
|
|
|
if (i + 1 < lines.Length)
|
2013-11-01 13:00:46 +00:00
|
|
|
|
{
|
2014-03-12 17:13:05 +00:00
|
|
|
|
sw.Write(System.Environment.NewLine);
|
2014-06-20 19:58:02 +00:00
|
|
|
|
newline = true;
|
2013-11-01 13:00:46 +00:00
|
|
|
|
}
|
2013-11-01 07:57:44 +00:00
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
2009-08-11 14:17:27 +00:00
|
|
|
|
|
2014-03-31 07:50:51 +00:00
|
|
|
|
public void Write(WriteOptions options, string format, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
Write(options, String.Format(format, args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(string value)
|
|
|
|
|
{
|
|
|
|
|
Write(WriteOptions.Default, value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 17:13:05 +00:00
|
|
|
|
public void Write(string format, params object[] args)
|
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
Write(WriteOptions.Default, format, args);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteLine()
|
|
|
|
|
{
|
2014-06-20 19:58:02 +00:00
|
|
|
|
Write(System.Environment.NewLine);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 07:50:51 +00:00
|
|
|
|
public void WriteLine(WriteOptions options, string value)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
Write(options, value);
|
2014-03-13 17:44:36 +00:00
|
|
|
|
WriteLine();
|
2014-03-12 17:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 07:50:51 +00:00
|
|
|
|
public void WriteLine(WriteOptions options, string format, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
WriteLine(options, String.Format(format, args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteLine(string value)
|
|
|
|
|
{
|
|
|
|
|
WriteLine(WriteOptions.Default, value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 17:13:05 +00:00
|
|
|
|
public void WriteLine(string format, params object[] args)
|
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
WriteLine(WriteOptions.Default, format, args);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Flush()
|
|
|
|
|
{
|
|
|
|
|
sw.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
sw.Close();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
2013-11-01 13:00:46 +00:00
|
|
|
|
|
2014-03-31 07:50:51 +00:00
|
|
|
|
void WriteIndentations(WriteOptions options)
|
2013-11-01 13:00:46 +00:00
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
if (options != WriteOptions.NoIndent)
|
|
|
|
|
{
|
|
|
|
|
for (int i = indent_level; i > 0; i--)
|
|
|
|
|
sw.Write(" ");
|
|
|
|
|
}
|
2014-03-12 17:13:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
sw.Dispose();
|
2013-11-01 13:00:46 +00:00
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|