diff options
author | Michał Klichowicz <emkael@tlen.pl> | 2023-11-05 14:16:25 +0100 |
---|---|---|
committer | Michał Klichowicz <emkael@tlen.pl> | 2023-11-05 14:16:25 +0100 |
commit | 86b5e6fab4f7d223d30f0b370020a6ba42cb8d05 (patch) | |
tree | 9c6a84ebe11e7ddff8e06bd665f3146821091f64 /src/BCDDFile.cs | |
parent | bc32d973874ff5cd1d495395f9f8489e95b9bb0f (diff) |
Refactoring separate files into separate threads
Diffstat (limited to 'src/BCDDFile.cs')
-rw-r--r-- | src/BCDDFile.cs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/BCDDFile.cs b/src/BCDDFile.cs new file mode 100644 index 0000000..a37ead6 --- /dev/null +++ b/src/BCDDFile.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace BCDD +{ + public class BCDDFile + { + public List<String> errors; + private String filename; + + public static int filesCounter; + public static ManualResetEvent filesCountdown = new ManualResetEvent(false); + + public BCDDFile(String filename) + { + this.errors = new List<String>(); + this.filename = filename; + } + + private void error(String message, String boardNo = "") + { + if (!"".Equals(boardNo)) + { + message = String.Format("[{0}:{1}] {2}", this.filename, boardNo, message); + } + else + { + message = String.Format("[{0}] {1}", this.filename, message); + } + errors.Add(message); + Console.WriteLine("ERROR: " + message); + } + + public void analyze(object state) + { + try + { + PBNFile file = new PBNFile(filename); + foreach (PBNBoard board in file.Boards) + { + DDTable table = new DDTable(board); + String boardNo; + try + { + boardNo = board.GetNumber(); + } + catch (FieldNotFoundException) + { + boardNo = "?"; + } + try + { + int[,] ddTable = table.GetDDTable(); + if (ddTable != null) + { + ParScore par = new ParScore(board); + ParContract contract = par.GetParContract(ddTable); + board.SaveDDTable(ddTable); + board.SaveParContract(contract); + Console.WriteLine("Board " + boardNo); + DDTable.PrintTable(ddTable); + Console.WriteLine(contract); + Console.WriteLine(); + } + else + { + this.error("unable to determine DD table for board " + boardNo, boardNo); + } + } + catch (DllNotFoundException) + { + throw; + } + catch (Exception e) + { + this.error(e.Message, boardNo); + } + file.WriteBoard(board); + } + file.Save(); + } + catch (DllNotFoundException) + { + this.error("libbcalcdds.dll could not be loaded - make sure it's present in application directory!"); + return; + } + catch (Exception e) + { + this.error(e.Message); + } + finally + { + if (Interlocked.Decrement(ref BCDDFile.filesCounter) == 0) + { + BCDDFile.filesCountdown.Set(); + } + } + } + } +} |