From 862c485d46f3f8e992a9ca9fea40fb33f0b43959 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 15 Nov 2012 20:59:11 +0000 Subject: Dictionaries are implemented now. Everything tests out against python. The next step is the packet stream interface. --- cs-dotnet/src/gatsdictionary.cs | 149 ++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/gatsexception.cs | 3 +- cs-dotnet/src/gatsobject.cs | 20 ++--- cs-dotnet/src/tests/dictionary.cs | 48 ++++++++++++ cs-dotnet/src/tests/lists.cs | 5 +- 5 files changed, 211 insertions(+), 14 deletions(-) create mode 100644 cs-dotnet/src/gatsdictionary.cs create mode 100644 cs-dotnet/src/tests/dictionary.cs diff --git a/cs-dotnet/src/gatsdictionary.cs b/cs-dotnet/src/gatsdictionary.cs new file mode 100644 index 0000000..0122c3a --- /dev/null +++ b/cs-dotnet/src/gatsdictionary.cs @@ -0,0 +1,149 @@ +using System.Text; +using System.IO; +using System.Linq; +using System.Collections; +using System.Collections.Generic; + +namespace Com.Xagasoft.Gats +{ + public class GatsDictionary : GatsObject, IDictionary +// ICollection>, +// IEnumerable> + { + private Dictionary Value = + new Dictionary(); + + public GatsDictionary() + { + } + + public override string ToString() + { + StringBuilder bld = new StringBuilder(); + bld.Append("{"); + bool begin = true; + foreach( KeyValuePair j in Value ) + { + if( begin ) + begin = false; + else + bld.Append(", "); + bld.Append( j.Key ); + bld.Append(" = "); + bld.Append( j.Value ); + } + bld.Append("}"); + return bld.ToString(); + } + + public override void Read( Stream s, char type ) + { + for(;;) + { + GatsObject key = GatsObject.Read( s ); + if( key == null ) + break; + if( key.GetType() != typeof(GatsString) ) + throw new GatsException( GatsException.Type.InvalidFormat ); + Value.Add( key.ToString(), GatsObject.Read( s ) ); + } + } + + public override void Write( Stream s ) + { + s.WriteByte( (int)'d' ); + foreach( KeyValuePair j in Value ) + { + new GatsString( j.Key ).Write( s ); + j.Value.Write( s ); + } + s.WriteByte( (int)'e' ); + } + + // + // List interface overrides under here. + // + public void Add( string key, GatsObject obj ) + { + Value.Add( key, obj ); + } + + public bool Remove( string key ) + { + return Value.Remove( key ); + } + + public bool ContainsKey( string key ) + { + return Value.ContainsKey( key ); + } + + public bool TryGetValue( string key, out GatsObject obj ) + { + return Value.TryGetValue( key, out obj ); + } + + public GatsObject this[string key] + { + get { return this.Value[key]; } + set { this.Value[key] = value; } + } + + public ICollection Keys + { + get { return this.Value.Keys; } + } + + public ICollection Values + { + get { return this.Value.Values; } + } + + public int Count + { + get { return this.Value.Count; } + } + + public bool IsReadOnly + { + get { return false; } // return this.Value.IsReadOnly; } + } + + public void Add( KeyValuePair pair ) + { + ((IDictionary)Value).Add( pair ); + } + + public void Clear() + { + Value.Clear(); + } + + public bool Contains( KeyValuePair pair ) + { + return ((IDictionary)Value).Contains( pair ); + } + + public void CopyTo( KeyValuePair[] result, + int count ) + { + ((IDictionary)Value).CopyTo( result, count ); + } + + public bool Remove( KeyValuePair pair ) + { + return ((IDictionary)Value).Remove( pair ); + } + + IEnumerator > IEnumerable >.GetEnumerator() + { + return Value.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return Value.GetEnumerator(); + } + } +} + diff --git a/cs-dotnet/src/gatsexception.cs b/cs-dotnet/src/gatsexception.cs index c6b2690..2dc49eb 100644 --- a/cs-dotnet/src/gatsexception.cs +++ b/cs-dotnet/src/gatsexception.cs @@ -7,7 +7,8 @@ namespace Com.Xagasoft.Gats public enum Type { PrematureEnd = 1, - InvalidType = 2 + InvalidType = 2, + InvalidFormat = 3, }; private Type _Reason; diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs index 5e76a6c..792db6a 100644 --- a/cs-dotnet/src/gatsobject.cs +++ b/cs-dotnet/src/gatsobject.cs @@ -13,37 +13,37 @@ namespace Com.Xagasoft.Gats if( b == -1 ) throw new GatsException( GatsException.Type.PrematureEnd ); char type = (char)b; - GatsObject goRet = null; + GatsObject ret = null; switch( type ) { case 'i': - goRet = new GatsInteger(); + ret = new GatsInteger(); break; case 's': - goRet = new GatsString(); + ret = new GatsString(); break; case '0': case '1': - goRet = new GatsBoolean(); + ret = new GatsBoolean(); break; case 'l': - goRet = new GatsList(); + ret = new GatsList(); break; case 'd': -// goRet = new GatsDictionary(); + ret = new GatsDictionary(); break; case 'f': case 'F': - goRet = new GatsFloat(); + ret = new GatsFloat(); break; case 'n': - goRet = new GatsNull(); + ret = new GatsNull(); break; case 'e': @@ -53,9 +53,9 @@ namespace Com.Xagasoft.Gats throw new GatsException( GatsException.Type.InvalidType ); } - goRet.Read( s, type ); + ret.Read( s, type ); - return goRet; + return ret; } } } diff --git a/cs-dotnet/src/tests/dictionary.cs b/cs-dotnet/src/tests/dictionary.cs new file mode 100644 index 0000000..a22acad --- /dev/null +++ b/cs-dotnet/src/tests/dictionary.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using Com.Xagasoft.Gats; + +public class Test +{ + public static void Write( GatsObject o, Stream s ) + { + o.Write( s ); + Console.WriteLine("Wrote: " + o ); + } + + public static void Read( Stream s ) + { + GatsObject o = GatsObject.Read( s ); + Console.WriteLine("Read type: " + o.GetType() ); + Console.WriteLine("Read vlaue: " + o ); + } + + public static void Main() + { + MemoryStream ms = new MemoryStream(); + GatsDictionary d = new GatsDictionary(); + GatsList l = new GatsList(); + l.Add( new GatsFloat( Math.PI ) ); + l.Add( new GatsInteger( 1337 ) ); + l.Add( new GatsString("Hello") ); + d.Add("list", l ); + d.Add("int", new GatsInteger( 998877 ) ); + d.Add("float", new GatsFloat( 87.332 ) ); + d.Add("string", new GatsString("Yup, a string") ); + d.Add("bool", new GatsBoolean( false ) ); + Write( d, ms ); + ms.Seek( 0, SeekOrigin.Begin ); + Read( ms ); + + try + { + FileStream fs = new FileStream("dictionary.gats", FileMode.Open, + FileAccess.Read ); + Read( fs ); + } + catch( Exception e ) + { + Console.WriteLine("Can't test files: " + e.Message ); + } + } +} diff --git a/cs-dotnet/src/tests/lists.cs b/cs-dotnet/src/tests/lists.cs index 8d12bf9..09ad931 100644 --- a/cs-dotnet/src/tests/lists.cs +++ b/cs-dotnet/src/tests/lists.cs @@ -27,10 +27,10 @@ public class Test Write( l, ms ); ms.Seek( 0, SeekOrigin.Begin ); Read( ms ); -/* + try { - FileStream fs = new FileStream("float.gats", FileMode.Open, + FileStream fs = new FileStream("list.gats", FileMode.Open, FileAccess.Read ); Read( fs ); } @@ -38,6 +38,5 @@ public class Test { Console.WriteLine("Can't test files: " + e.Message ); } - */ } } -- cgit v1.2.3