From a09f936de67c30bc8b53ac8a5ddf528741e18f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 4 Apr 2014 08:59:28 +0200 Subject: [PATCH] =?UTF-8?q?Getter=20f=C3=BCr=20Block,=20Compilefehler=20en?= =?UTF-8?q?tfernt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Parser.cs | 76 ++++++++++++++++++++++++++--------------- src/ParsingException.cs | 16 ++++++--- 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/Parser.cs b/src/Parser.cs index 7d76635..ab68042 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -17,10 +17,10 @@ namespace WorldOfPeacecraft { ParserThread = new Thread (new ThreadStart (this.RunParser)); message = new LinkedList (); - LastLineRegex = new Regex("^end:[0-9]+$"); + LastLineRegex = new Regex ("^end:[0-9]+$"); } - public void RunParser () + private void RunParser () { while (true) { bool waitRequired = false; @@ -38,24 +38,24 @@ namespace WorldOfPeacecraft } } - public void Parse () + private void Parse () { // If package is not complete wait for more lines - if (!IsCompletePackage()) + if (!IsCompletePackage ()) return; - Block mainBlock = new Block(message, 0, message.Count - 1); - MapData(mainBlock); + Block mainBlock = new Block (message, 0, message.Count - 1); + MapData (mainBlock); } - public void MapData (Block block) + private void MapData (Block block) { // TODO Implement } - public bool IsCompletePackage() + private bool IsCompletePackage () { string lastLine = message.Last.Value; - return LastLineRegex.IsMatch(lastLine); + return LastLineRegex.IsMatch (lastLine); } public void Stop () @@ -74,44 +74,64 @@ namespace WorldOfPeacecraft private class Block { private string Name; - private ISet Blocks = new HashSet(); - private Dictionary Values = new Dictionary(); + private ISet Blocks = new HashSet (); + private Dictionary Values = new Dictionary (); public Block (String[] message, int start, int end) { int pos = start; - Name = StringUtils.SubstringAfter(message[pos], ":"); + Name = StringUtils.SubstringAfter (message [pos], ":"); pos++; - while (pos < end) - { + while (pos < end) { // Is the next element a block or a value? - if (message[pos].StartsWith("begin:")) - { + if (message [pos].StartsWith ("begin:")) { // It's a block int blockstart = pos; int begins = 1; - while (begins > 0) - { + while (begins > 0) { pos++; if (pos >= end) - throw new ParsingException("The message is missing end:-lines"); - if (message[pos].StartsWith("end:")) + throw new ParsingException ("The message is missing end:-lines"); + if (message [pos].StartsWith ("end:")) begins--; - else if(message[pos].StartsWith("begin:")) + else if (message [pos].StartsWith ("begin:")) begins++; } - Blocks.Add(new Block(message, blockstart, pos)); - } - else - { + Blocks.Add (new Block (message, blockstart, pos)); + } else { // It's a value - string name = StringUtils.SubstringBefore(message[pos], ":"); - string val = StringUtils.SubstringAfter(message[pos], ":"); - Values[name] = val; + string name = StringUtils.SubstringBefore (message [pos], ":"); + string val = StringUtils.SubstringAfter (message [pos], ":"); + Values [name] = val; pos++; } } } + + public string GetName () + { + return Name; + } + + public ISet GetBlocks () + { + return Blocks; + } + + public string GetStringValue (string name) + { + return Values [name]; + } + + public int GetIntValue (string name) + { + return int.Parse (Values [name]); + } + + public long GetLongValue (string name) + { + return long.Parse (Values [name]); + } } } } diff --git a/src/ParsingException.cs b/src/ParsingException.cs index fb97636..e0538b7 100644 --- a/src/ParsingException.cs +++ b/src/ParsingException.cs @@ -5,12 +5,20 @@ namespace WorldOfPeacecraft { public class ParsingException : Exception { - public ParsingException () : base(); + public ParsingException () : base() + { + } - public ParsingException (string message) : base(message); + public ParsingException (string message) : base(message) + { + } - public ParsingException (SerializationInfo info, StreamingContext context) : base(info, context); + public ParsingException (SerializationInfo info, StreamingContext context) : base(info, context) + { + } - public ParsingException (string message, Exception innerException) : base(message, innerException); + public ParsingException (string message, Exception innerException) : base(message, innerException) + { + } } }