Getter für Block, Compilefehler entfernt

This commit is contained in:
2014-04-04 08:59:28 +02:00
parent 709b088a22
commit a09f936de6
2 changed files with 60 additions and 32 deletions

View File

@@ -17,10 +17,10 @@ namespace WorldOfPeacecraft
{ {
ParserThread = new Thread (new ThreadStart (this.RunParser)); ParserThread = new Thread (new ThreadStart (this.RunParser));
message = new LinkedList<string> (); message = new LinkedList<string> ();
LastLineRegex = new Regex("^end:[0-9]+$"); LastLineRegex = new Regex ("^end:[0-9]+$");
} }
public void RunParser () private void RunParser ()
{ {
while (true) { while (true) {
bool waitRequired = false; 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 package is not complete wait for more lines
if (!IsCompletePackage()) if (!IsCompletePackage ())
return; return;
Block mainBlock = new Block(message, 0, message.Count - 1); Block mainBlock = new Block (message, 0, message.Count - 1);
MapData(mainBlock); MapData (mainBlock);
} }
public void MapData (Block block) private void MapData (Block block)
{ {
// TODO Implement // TODO Implement
} }
public bool IsCompletePackage() private bool IsCompletePackage ()
{ {
string lastLine = message.Last.Value; string lastLine = message.Last.Value;
return LastLineRegex.IsMatch(lastLine); return LastLineRegex.IsMatch (lastLine);
} }
public void Stop () public void Stop ()
@@ -74,44 +74,64 @@ namespace WorldOfPeacecraft
private class Block private class Block
{ {
private string Name; private string Name;
private ISet<Block> Blocks = new HashSet<Block>(); private ISet<Block> Blocks = new HashSet<Block> ();
private Dictionary<String, String> Values = new Dictionary<String, String>(); private Dictionary<String, String> Values = new Dictionary<String, String> ();
public Block (String[] message, int start, int end) public Block (String[] message, int start, int end)
{ {
int pos = start; int pos = start;
Name = StringUtils.SubstringAfter(message[pos], ":"); Name = StringUtils.SubstringAfter (message [pos], ":");
pos++; pos++;
while (pos < end) while (pos < end) {
{
// Is the next element a block or a value? // Is the next element a block or a value?
if (message[pos].StartsWith("begin:")) if (message [pos].StartsWith ("begin:")) {
{
// It's a block // It's a block
int blockstart = pos; int blockstart = pos;
int begins = 1; int begins = 1;
while (begins > 0) while (begins > 0) {
{
pos++; pos++;
if (pos >= end) if (pos >= end)
throw new ParsingException("The message is missing end:-lines"); throw new ParsingException ("The message is missing end:-lines");
if (message[pos].StartsWith("end:")) if (message [pos].StartsWith ("end:"))
begins--; begins--;
else if(message[pos].StartsWith("begin:")) else if (message [pos].StartsWith ("begin:"))
begins++; begins++;
} }
Blocks.Add(new Block(message, blockstart, pos)); Blocks.Add (new Block (message, blockstart, pos));
} } else {
else
{
// It's a value // It's a value
string name = StringUtils.SubstringBefore(message[pos], ":"); string name = StringUtils.SubstringBefore (message [pos], ":");
string val = StringUtils.SubstringAfter(message[pos], ":"); string val = StringUtils.SubstringAfter (message [pos], ":");
Values[name] = val; Values [name] = val;
pos++; pos++;
} }
} }
} }
public string GetName ()
{
return Name;
}
public ISet<Block> 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]);
}
} }
} }
} }

View File

@@ -5,12 +5,20 @@ namespace WorldOfPeacecraft
{ {
public class ParsingException : Exception 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)
{
}
} }
} }