diff options
author | emkael <emkael@tlen.pl> | 2016-05-23 14:56:43 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-05-23 14:56:43 +0200 |
commit | 22ff6c7e6ca9f697e97a1b1b43c461821286e63f (patch) | |
tree | 63171347f840adf3a2761a45447d636f76322a7b /PBNFile.cs | |
parent | 71fc38dd129db3a43e0bb6c894aaea1d14e17286 (diff) |
* PBN board/field structures with file operation mechanisms
Diffstat (limited to 'PBNFile.cs')
-rw-r--r-- | PBNFile.cs | 48 |
1 files changed, 48 insertions, 0 deletions
@@ -8,12 +8,60 @@ namespace BCDD {
class PBNFile
{
+ public List<PBNBoard> Boards;
+
private String filename;
+ private String tmpFileName;
+
+ StreamWriter outputFile;
public PBNFile(String filename)
{
this.filename = filename;
+ this.Boards = new List<PBNBoard>();
+ String[] contents = File.ReadAllLines(this.filename).Select(l => l.Trim()).ToArray();
+ List<String> lines = new List<String>();
+ foreach (String line in contents)
+ {
+ if (line.Length == 0)
+ {
+ if (lines.Count > 0)
+ {
+ this.Boards.Add(new PBNBoard(lines));
+ lines = new List<String>();
+ }
+ }
+ else
+ {
+ lines.Add(line);
+ }
+ }
+ if (lines.Count > 0)
+ {
+ this.Boards.Add(new PBNBoard(lines));
+ }
}
+ 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()
+ {
+ this.outputFile.Flush();
+ this.outputFile.Close();
+ File.Delete(this.filename);
+ File.Move(this.tmpFileName, this.filename);
+ }
}
}
|