summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-05-23 14:56:43 +0200
committeremkael <emkael@tlen.pl>2016-05-23 14:56:43 +0200
commit22ff6c7e6ca9f697e97a1b1b43c461821286e63f (patch)
tree63171347f840adf3a2761a45447d636f76322a7b
parent71fc38dd129db3a43e0bb6c894aaea1d14e17286 (diff)
* PBN board/field structures with file operation mechanisms
-rw-r--r--BCDD.csproj1
-rw-r--r--PBNBoard.cs98
-rw-r--r--PBNFile.cs48
3 files changed, 147 insertions, 0 deletions
diff --git a/BCDD.csproj b/BCDD.csproj
index 4814531..915d67c 100644
--- a/BCDD.csproj
+++ b/BCDD.csproj
@@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BCalcWrapper.cs" />
+ <Compile Include="PBNBoard.cs" />
<Compile Include="PBNFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
diff --git a/PBNBoard.cs b/PBNBoard.cs
new file mode 100644
index 0000000..259c728
--- /dev/null
+++ b/PBNBoard.cs
@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace BCDD
+{
+ class PBNField
+ {
+ public String Key;
+ public String Value;
+ public String RawField;
+
+ public PBNField() { }
+
+ public PBNField(String key, String value)
+ {
+ this.Key = key;
+ this.Value = value;
+ this.RawField = String.Format("[{0} \"{1}\"]", this.Key, this.Value);
+ }
+
+ public PBNField(String rawData)
+ {
+ this.RawField = rawData;
+ }
+ }
+
+ class FieldNotFoundException : Exception
+ {
+ public FieldNotFoundException() : base() { }
+ public FieldNotFoundException(String msg) : base(msg) { }
+ }
+
+ class PBNBoard
+ {
+ public List<PBNField> Fields;
+
+ public PBNBoard(List<string> lines)
+ {
+ this.Fields = new List<PBNField>();
+ Regex linePattern = new Regex(@"\[(.*) ""(.*)""\]");
+ foreach (String line in lines)
+ {
+ PBNField field = new PBNField();
+ field.RawField = line;
+ Match lineParse = linePattern.Match(line);
+ if (lineParse.Success)
+ {
+ field.Key = lineParse.Groups[1].Value;
+ field.Value = lineParse.Groups[2].Value;
+ }
+ this.Fields.Add(field);
+ }
+ }
+
+ public bool HasField(String key)
+ {
+ foreach (PBNField field in this.Fields)
+ {
+ if (key.Equals(field.Key))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String GetField(String key)
+ {
+ foreach (PBNField field in this.Fields)
+ {
+ if (key.Equals(field.Key))
+ {
+ return field.Value;
+ }
+ }
+ throw new FieldNotFoundException(key + " field not found");
+ }
+
+ public void DeleteField(String key)
+ {
+ List<PBNField> toRemove = new List<PBNField>();
+ foreach (PBNField field in this.Fields)
+ {
+ if (key.Equals(field.Key))
+ {
+ toRemove.Add(field);
+ }
+ }
+ foreach (PBNField remove in toRemove)
+ {
+ this.Fields.Remove(remove);
+ }
+ }
+
+ }
+}
diff --git a/PBNFile.cs b/PBNFile.cs
index 3f68756..bec7fcd 100644
--- a/PBNFile.cs
+++ b/PBNFile.cs
@@ -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);
+ }
}
}