iced/Generator/IO/FileWriter.cs

154 lines
4.6 KiB
C#

/*
Copyright (C) 2018-2019 de4dot@gmail.com
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.
*/
using System;
using System.IO;
namespace Generator.IO {
sealed class FileWriter : IDisposable {
const string NumberPrefix = "0x";
const string NumberByteFormat = "X2";
const string CommentPrefix = "// ";
readonly TextWriter writer;
bool needSpace;
bool needIndent;
int indentCount;
string indentString;
public FileWriter(TextWriter writer) {
this.writer = writer;
needIndent = true;
InitializeIndent();
}
void InitializeIndent() => indentString = GetIndentString(indentCount);
static string GetIndentString(int indentCount) {
switch (indentCount) {
case 1: return "\t";
case 2: return "\t\t";
case 3: return "\t\t\t";
case 4: return "\t\t\t\t";
case 5: return "\t\t\t\t\t";
case 6: return "\t\t\t\t\t\t";
case 7: return "\t\t\t\t\t\t\t";
case 8: return "\t\t\t\t\t\t\t\t";
default: return new string('\t', indentCount);
}
}
public void Indent() {
indentCount++;
InitializeIndent();
}
public void Unindent() {
indentCount--;
InitializeIndent();
}
void WriteIndent() {
if (needIndent) {
needIndent = false;
writer.Write(indentString);
}
}
public void WriteHeader() {
WriteLine("/*");
WriteLine("Copyright (C) 2018-2019 de4dot@gmail.com");
WriteLine();
WriteLine("Permission is hereby granted, free of charge, to any person obtaining");
WriteLine("a copy of this software and associated documentation files (the");
WriteLine("\"Software\"), to deal in the Software without restriction, including");
WriteLine("without limitation the rights to use, copy, modify, merge, publish,");
WriteLine("distribute, sublicense, and/or sell copies of the Software, and to");
WriteLine("permit persons to whom the Software is furnished to do so, subject to");
WriteLine("the following conditions:");
WriteLine();
WriteLine("The above copyright notice and this permission notice shall be");
WriteLine("included in all copies or substantial portions of the Software.");
WriteLine();
WriteLine("THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,");
WriteLine("EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF");
WriteLine("MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.");
WriteLine("IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY");
WriteLine("CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,");
WriteLine("TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE");
WriteLine("SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.");
WriteLine("*/");
WriteLine();
WriteLine("// This file was generated by the Generator project");
WriteLine();
}
public void WriteLine(string s) {
Write(s);
WriteLine();
}
public void WriteByte(byte value) => WriteNumberComma(value.ToString(NumberByteFormat));
void WriteNumberComma(string number) {
if (needSpace)
Write(" ");
needSpace = true;
Write(NumberPrefix);
Write(number);
Write(",");
}
public void WriteCompressedUInt32(uint value) {
for (;;) {
uint v = value;
if (v < 0x80)
WriteByte((byte)value);
else
WriteByte((byte)(value | 0x80));
value >>= 7;
if (value == 0)
break;
}
}
public void WriteCommentLine(string s) {
Write(CommentPrefix);
Write(s);
WriteLine();
}
void Write(string s) {
WriteIndent();
writer.Write(s);
}
public void WriteLine() {
writer.WriteLine();
needSpace = false;
needIndent = true;
}
public void Dispose() => writer.Dispose();
}
}