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-03-12 17:13:05 +00:00
|
|
|
|
static readonly char[] SplitCharacters = new char[] { '\r', '\n' };
|
|
|
|
|
readonly StreamWriter sw;
|
2010-12-06 22:32:11 +00:00
|
|
|
|
public readonly string File;
|
|
|
|
|
|
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-03-31 07:50:51 +00:00
|
|
|
|
public void Write(WriteOptions options, string value)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2014-03-12 17:13:05 +00:00
|
|
|
|
var lines = value.Split(SplitCharacters,
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
2013-11-01 13:00:46 +00:00
|
|
|
|
bool is_multiline = lines.Length > 1;
|
|
|
|
|
if (is_multiline)
|
2013-11-01 07:57:44 +00:00
|
|
|
|
{
|
2013-11-01 13:00:46 +00:00
|
|
|
|
// Write all internal lines
|
|
|
|
|
for (int i = 0; i < lines.Length - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
var line = lines[i];
|
2014-03-31 07:50:51 +00:00
|
|
|
|
WriteIndentations(options);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
sw.Write(line);
|
|
|
|
|
sw.Write(System.Environment.NewLine);
|
2013-11-01 13:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
// Write the last line without appending a newline
|
2014-03-31 07:50:51 +00:00
|
|
|
|
WriteIndentations(options);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
sw.Write(lines[lines.Length - 1]);
|
2013-11-01 07:57:44 +00:00
|
|
|
|
}
|
2013-11-01 13:00:46 +00:00
|
|
|
|
else
|
2013-11-01 07:57:44 +00:00
|
|
|
|
{
|
2014-03-31 07:50:51 +00:00
|
|
|
|
WriteIndentations(options);
|
2014-03-12 17:13:05 +00:00
|
|
|
|
sw.Write(value);
|
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()
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|