From 961bf05e7e71a4dbfd1ed1641afbed4e90c10464 Mon Sep 17 00:00:00 2001 From: MichaƂ Klichowicz Date: Fri, 18 Sep 2026 10:16:07 +0200 Subject: * ignoring multiple/leading empty lines * error readability improvements --- Program.cs | 1 + src/BCDDFile.cs | 22 ++++++++++++++++++---- src/PBNBoard.cs | 2 +- src/PBNFile.cs | 39 ++++++++++++++++++++++++++------------- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/Program.cs b/Program.cs index aacf8e1..9e766d4 100644 --- a/Program.cs +++ b/Program.cs @@ -53,6 +53,7 @@ namespace BCDD errors.AddRange(w.errors); } if (errors.Count > 0) { + Console.WriteLine(); Console.WriteLine("Following ERRORs occured:"); foreach (String error in errors) { Console.WriteLine(error); diff --git a/src/BCDDFile.cs b/src/BCDDFile.cs index 36a0872..03d0af2 100644 --- a/src/BCDDFile.cs +++ b/src/BCDDFile.cs @@ -9,6 +9,7 @@ namespace BCDD public List errors; private String filename; private PBNFile file; + private String shortname; public static int filesCounter; public static ManualResetEvent filesCountdown = new ManualResetEvent(false); @@ -21,17 +22,30 @@ namespace BCDD this.errors = new List(); this.filename = filename; this.file = new PBNFile(filename); + this.shortname = Path.GetFileName(this.filename); + if (this.file.ParseErrors.Count > 0) + { + this.error("PBN format errors encountered"); + foreach (KeyValuePair kv in this.file.ParseErrors) + { + this.error(kv.Value, "", kv.Key); + } + } } - private void error(String message, String boardNo = "") + private void error(String message, String boardNo = "", int lineNo = -1) { if (!"".Equals(boardNo)) { - message = String.Format("[{0}:{1}] {2}", this.filename, boardNo, message); + message = String.Format("[{0}:B#{1}] {2}", this.shortname, boardNo, message); + } + else if (lineNo > -1) + { + message = String.Format("[{0}:L#{1}] {2}", this.shortname, lineNo, message); } else { - message = String.Format("[{0}] {1}", this.filename, message); + message = String.Format("[{0}] {1}", this.shortname, message); } errors.Add(message); Console.WriteLine("ERROR: " + message); @@ -39,7 +53,7 @@ namespace BCDD private void info(String boardNo, String ddTable, ParContract contract) { - Console.WriteLine(String.Format("[{0}:{1}] {2} {3}", this.filename, boardNo, ddTable, contract)); + Console.WriteLine(String.Format("[{0}:{1}] {2} {3}", this.shortname, boardNo, ddTable, contract)); } private void processBoard(object state) diff --git a/src/PBNBoard.cs b/src/PBNBoard.cs index 15acbe0..2ecaca8 100644 --- a/src/PBNBoard.cs +++ b/src/PBNBoard.cs @@ -60,7 +60,7 @@ namespace BCDD this.Fields.Add(field); } if (!this.HasField("Deal")) { - throw new FieldNotFoundException("'Deal' field not found"); + throw new FieldNotFoundException("PBN section doesn't contain 'Deal' field"); } if (!this.HasField("Dealer")) { diff --git a/src/PBNFile.cs b/src/PBNFile.cs index b26f84c..f2a1465 100644 --- a/src/PBNFile.cs +++ b/src/PBNFile.cs @@ -9,6 +9,7 @@ namespace BCDD class PBNFile { public List Boards; + public Dictionary ParseErrors; private String filename; private String tmpFileName; @@ -18,28 +19,40 @@ namespace BCDD public PBNFile(String filename) { this.filename = filename; + this.ParseErrors = new Dictionary(); this.Boards = new List(); - String[] contents = File.ReadAllLines(this.filename).Select(l => l.Trim()).ToArray(); + List contents = File.ReadAllLines(this.filename).Select(l => l.Trim()).ToList(); + if (!contents.Last().Equals("")) + { + contents.Add(""); + } List lines = new List(); - foreach (String line in contents) + int lineNo = 0; + for (int l = 0; l < contents.Count; l++) { - if (line.Length == 0) + String line = contents[l]; + if (line.Length != 0) { - if (lines.Count > 0) - { - this.Boards.Add(new PBNBoard(lines)); - lines = new List(); - } + lines.Add(line); + continue; } else { - lines.Add(line); + if (lines.Count > 0) // ignore leading or multiple empty lines + { + try + { + this.Boards.Add(new PBNBoard(lines)); + } + catch (Exception ex) + { + this.ParseErrors[lineNo+1] = ex.Message; + } + lines = new List(); + lineNo = l; + } } } - if (lines.Count > 0) - { - this.Boards.Add(new PBNBoard(lines)); - } if (!this.Boards[0].HasField("Event")) { this.Boards[0].WriteEvent(""); -- cgit v1.3.1