From b9b5a5fc16e21387b3350260d2139b293bbcd2b0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 15 Nov 2012 19:22:26 +0000 Subject: Much overriding was done, but GatsList works great. And just like a standard .net list. --- cs-dotnet/src/gatslist.cs | 135 +++++++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/gatsobject.cs | 2 +- cs-dotnet/src/gatsstring.cs | 7 ++- cs-dotnet/src/tests/lists.cs | 43 ++++++++++++++ 4 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 cs-dotnet/src/gatslist.cs create mode 100644 cs-dotnet/src/tests/lists.cs diff --git a/cs-dotnet/src/gatslist.cs b/cs-dotnet/src/gatslist.cs new file mode 100644 index 0000000..d111bac --- /dev/null +++ b/cs-dotnet/src/gatslist.cs @@ -0,0 +1,135 @@ +using System; +using System.IO; +using System.Text; +using System.Collections; +using System.Collections.Generic; + +namespace Com.Xagasoft.Gats +{ + public class GatsList : GatsObject, IList, + ICollection, IEnumerable +// IReadOnlyList, IReadOnlyCollection, +// These two are in .net 4.5, not 4.0, mono can't use them. + { + private List Value = new List(); + + public GatsList() + { + } + + public override string ToString() + { + StringBuilder bld = new StringBuilder(); + bld.Append("["); + for( int j = 0; j < Value.Count-1; j++ ) + { + bld.Append( Value[j] ); + bld.Append(", "); + } + bld.Append( Value[Value.Count-1] ); + bld.Append("]"); + return bld.ToString(); + } + + public override void Read( Stream s, char type ) + { + for(;;) + { + GatsObject obj = GatsObject.Read( s ); + if( obj == null ) + break; + Value.Add( obj ); + } + } + + public override void Write( Stream s ) + { + s.WriteByte( (int)'l' ); + foreach( GatsObject obj in Value ) + { + obj.Write( s ); + } + s.WriteByte( (int)'e' ); + } + + // + // List interface overrides under here. + // + public int IndexOf( GatsObject obj ) + { + return Value.IndexOf( obj ); + } + + public void Insert( int idx, GatsObject obj ) + { + Value.Insert( idx, obj ); + } + + public void RemoveAt( int idx ) + { + Value.RemoveAt( idx ); + } + + public GatsObject this[int idx] { + get { return this.Value[idx]; } + set { this.Value[idx] = value; } + } + + public int Count { + get { return this.Value.Count; } + } + + public bool IsReadOnly { + get { return false; } // this.Value.IsReadOnly; } + } + + public void Add( GatsObject obj ) + { + Value.Add( obj ); + } + + public void Clear() + { + Value.Clear(); + } + + public bool Contains( GatsObject obj ) + { + return Value.Contains( obj ); + } + + public void CopyTo( GatsObject[] result, int count ) + { + Value.CopyTo( result, count ); + } + + public bool Remove( GatsObject obj ) + { + return Value.Remove( obj ); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return Value.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return Value.GetEnumerator(); + } + + public bool IsFixedSize { + get { return false; } // this.Value.IsFixedSize; } + } + + public bool IsSynchronized { + get { return false; } // this.Value.IsSynchronized; } + } + + public object SyncRoot { + get { return null; } // this.Value.SyncRoot; } + } + + } +} + diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs index a48a464..5e76a6c 100644 --- a/cs-dotnet/src/gatsobject.cs +++ b/cs-dotnet/src/gatsobject.cs @@ -30,7 +30,7 @@ namespace Com.Xagasoft.Gats break; case 'l': -// goRet = new GatsList(); + goRet = new GatsList(); break; case 'd': diff --git a/cs-dotnet/src/gatsstring.cs b/cs-dotnet/src/gatsstring.cs index 30b0afb..47b63ed 100644 --- a/cs-dotnet/src/gatsstring.cs +++ b/cs-dotnet/src/gatsstring.cs @@ -15,9 +15,14 @@ namespace Com.Xagasoft.Gats Value = val; } + public GatsString( string val ) + { + Value = System.Text.Encoding.UTF8.GetBytes( val ); + } + public override string ToString() { - return Value.ToString(); + return System.Text.Encoding.UTF8.GetString( Value ); } public override void Read( Stream s, char cType ) diff --git a/cs-dotnet/src/tests/lists.cs b/cs-dotnet/src/tests/lists.cs new file mode 100644 index 0000000..8d12bf9 --- /dev/null +++ b/cs-dotnet/src/tests/lists.cs @@ -0,0 +1,43 @@ +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(); + GatsList l = new GatsList(); + l.Add( new GatsFloat( Math.PI ) ); + l.Add( new GatsInteger( 1337 ) ); + l.Add( new GatsString("Hello") ); + Write( l, ms ); + ms.Seek( 0, SeekOrigin.Begin ); + Read( ms ); +/* + try + { + FileStream fs = new FileStream("float.gats", FileMode.Open, + FileAccess.Read ); + Read( fs ); + } + catch( Exception e ) + { + Console.WriteLine("Can't test files: " + e.Message ); + } + */ + } +} -- cgit v1.2.3