From 63628550708a616c5c58bc5c707db1e7fd9cd7c2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 14 Nov 2012 23:55:29 +0000 Subject: Strings, bools, and ints all seem fine, the more complex container types are coming next, and will implement the full range of appropriate interfaces. --- cs-dotnet/src/gatsboolean.cs | 40 +++++++++++++++++++++++++++++++ cs-dotnet/src/gatsexception.cs | 16 +++++++++++-- cs-dotnet/src/gatsinteger.cs | 6 ++--- cs-dotnet/src/gatsobject.cs | 53 +++++++++++++++++++++++++++++++++++++++++- cs-dotnet/src/gatsstring.cs | 49 ++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/tests/ints.cs | 9 ++++--- 6 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 cs-dotnet/src/gatsboolean.cs create mode 100644 cs-dotnet/src/gatsstring.cs (limited to 'cs-dotnet') diff --git a/cs-dotnet/src/gatsboolean.cs b/cs-dotnet/src/gatsboolean.cs new file mode 100644 index 0000000..e7e07e6 --- /dev/null +++ b/cs-dotnet/src/gatsboolean.cs @@ -0,0 +1,40 @@ +using System.IO; + +namespace Com.Xagasoft.Gats +{ + public class GatsBoolean : GatsObject + { + public bool Value { get; set; } + + public GatsBoolean() + { + } + + public GatsBoolean( bool val ) + { + Value = val; + } + + public override string ToString() + { + return Value.ToString(); + } + + public override void Read( Stream s, char type ) + { + if( type == '0' ) + Value = false; + else + Value = true; + } + + public override void Write( Stream s ) + { + if( Value ) + s.WriteByte( (int)'1' ); + else + s.WriteByte( (int)'0' ); + } + } +} + diff --git a/cs-dotnet/src/gatsexception.cs b/cs-dotnet/src/gatsexception.cs index ea665d0..c6b2690 100644 --- a/cs-dotnet/src/gatsexception.cs +++ b/cs-dotnet/src/gatsexception.cs @@ -4,9 +4,21 @@ namespace Com.Xagasoft.Gats { public class GatsException : Exception { - public GatsException( String sName ) : - base( sName ) + public enum Type { + PrematureEnd = 1, + InvalidType = 2 + }; + + private Type _Reason; + public Type Reason + { + get { return this._Reason; } + } + + public GatsException( Type reason ) + { + _Reason = reason; } } } diff --git a/cs-dotnet/src/gatsinteger.cs b/cs-dotnet/src/gatsinteger.cs index cbb552e..9a9abbe 100644 --- a/cs-dotnet/src/gatsinteger.cs +++ b/cs-dotnet/src/gatsinteger.cs @@ -16,7 +16,7 @@ namespace Com.Xagasoft.Gats return Value.ToString(); } - public override void Read( Stream s, byte cType ) + public override void Read( Stream s, char cType ) { Value = ReadPackedInt( s ); } @@ -35,7 +35,7 @@ namespace Com.Xagasoft.Gats b = s.ReadByte(); if( b == -1 ) - throw new GatsException("Premature end of stream encountered."); + throw new GatsException( GatsException.Type.PrematureEnd ); bNeg = (b&0x40) == 0x40; rOut |= ((long)b)&0x3F; int c = 0; @@ -43,7 +43,7 @@ namespace Com.Xagasoft.Gats { b = s.ReadByte(); if( b == -1 ) - throw new GatsException("Premature end of stream encountered."); + throw new GatsException( GatsException.Type.PrematureEnd ); rOut |= (long)(b&0x7F) << (6+7*(c++)); } if( bNeg ) diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs index f782f95..a0b9276 100644 --- a/cs-dotnet/src/gatsobject.cs +++ b/cs-dotnet/src/gatsobject.cs @@ -4,7 +4,58 @@ namespace Com.Xagasoft.Gats { public abstract class GatsObject { - public abstract void Read( Stream s, byte cType ); + public abstract void Read( Stream s, char cType ); public abstract void Write( Stream s ); + + public static GatsObject Read( Stream s ) + { + int b = s.ReadByte(); + if( b == -1 ) + throw new GatsException( GatsException.Type.PrematureEnd ); + char type = (char)b; + GatsObject goRet = null; + switch( type ) + { + case 'i': + goRet = new GatsInteger(); + break; + + case 's': + goRet = new GatsString(); + break; + + case '0': + case '1': + goRet = new GatsBoolean(); + break; + + case 'l': +// goRet = new GatsList(); + break; + + case 'd': +// goRet = new GatsDictionary(); + break; + + case 'f': + case 'F': +// goRet = new GatsFloat(); + break; + + case 'n': +// goRet = new GatsNull(); + break; + + case 'e': + return null; + + default: + throw new GatsException( GatsException.Type.InvalidType ); + } + + goRet.Read( s, type ); + + return goRet; + } } } diff --git a/cs-dotnet/src/gatsstring.cs b/cs-dotnet/src/gatsstring.cs new file mode 100644 index 0000000..30b0afb --- /dev/null +++ b/cs-dotnet/src/gatsstring.cs @@ -0,0 +1,49 @@ +using System.IO; + +namespace Com.Xagasoft.Gats +{ + public class GatsString : GatsObject + { + public byte[] Value { get; set; } + + public GatsString() + { + } + + public GatsString( byte[] val ) + { + Value = val; + } + + public override string ToString() + { + return Value.ToString(); + } + + public override void Read( Stream s, char cType ) + { + int Size = (int)GatsInteger.ReadPackedInt( s ); + Value = new byte[Size]; + int SoFar = 0; + do + { + SoFar += s.Read( Value, SoFar, Size-SoFar ); + } while( SoFar < Size ); + } + + public override void Write( Stream s ) + { + s.WriteByte( (byte)'s' ); + if( Value == null ) + { + GatsInteger.WritePackedInt( s, 0 ); + } + else + { + GatsInteger.WritePackedInt( s, Value.Length ); + s.Write( Value, 0, Value.Length ); + } + } + }; +} + diff --git a/cs-dotnet/src/tests/ints.cs b/cs-dotnet/src/tests/ints.cs index ea97820..7099995 100644 --- a/cs-dotnet/src/tests/ints.cs +++ b/cs-dotnet/src/tests/ints.cs @@ -7,10 +7,9 @@ class Ints static void Main() { FileStream file = new FileStream("test.gats", FileMode.Open, - FileAccess.Write ); - long iVal = 0xfffffe; - GatsInteger i = new GatsInteger( iVal ); - i.Write( file ); - Console.WriteLine("Read int: " + i.Value ); + FileAccess.Read ); + GatsObject obj = GatsObject.Read( file ); + Console.WriteLine("Read type: " + obj.GetType() ); + Console.WriteLine("Read int: " + ((GatsInteger)obj).Value ); } } -- cgit v1.2.3