From 211428ebea89e08111354e6956e075967ee4f4af Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 10 Apr 2018 02:18:46 +0200 Subject: Initial .NET codebase --- src/PBNFile.cs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/PBNFile.cs (limited to 'src/PBNFile.cs') diff --git a/src/PBNFile.cs b/src/PBNFile.cs new file mode 100644 index 0000000..b26f84c --- /dev/null +++ b/src/PBNFile.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace BCDD +{ + class PBNFile + { + public List Boards; + + private String filename; + private String tmpFileName; + + StreamWriter outputFile; + + public PBNFile(String filename) + { + this.filename = filename; + this.Boards = new List(); + String[] contents = File.ReadAllLines(this.filename).Select(l => l.Trim()).ToArray(); + List lines = new List(); + foreach (String line in contents) + { + if (line.Length == 0) + { + if (lines.Count > 0) + { + this.Boards.Add(new PBNBoard(lines)); + lines = new List(); + } + } + else + { + lines.Add(line); + } + } + if (lines.Count > 0) + { + this.Boards.Add(new PBNBoard(lines)); + } + if (!this.Boards[0].HasField("Event")) + { + this.Boards[0].WriteEvent(""); + } + } + + public void WriteBoard(PBNBoard board) + { + if (this.outputFile == null) + { + this.tmpFileName = Path.GetTempFileName(); + this.outputFile = new StreamWriter(new FileStream(this.tmpFileName, FileMode.Create), Encoding.UTF8); + } + foreach (PBNField field in board.Fields) + { + this.outputFile.WriteLine(field.RawField); + } + this.outputFile.WriteLine(); + } + + public void Save() + { + if (this.outputFile == null) + { + throw new IOException("No boards written to PBN file, unable to save it."); + } + this.outputFile.Flush(); + this.outputFile.Close(); + File.Delete(this.filename); + File.Move(this.tmpFileName, this.filename); + } + } +} -- cgit v1.2.3