From f1e413e914b9f03607194757848bab1ed4f401a3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 15 Nov 2012 17:41:14 +0000 Subject: Accidentally included a dll, oops. Also, gats null, and gats float work correctly now and are tested against the python implementation. --- cs-dotnet/src/gatsfloat.cs | 124 ++++++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/gatsinteger.dll | Bin 3072 -> 0 bytes cs-dotnet/src/gatsnull.cs | 26 +++++++++ cs-dotnet/src/gatsobject.cs | 6 +- cs-dotnet/src/tests/floats.cs | 39 +++++++++++++ cs-dotnet/src/tests/ints.cs | 3 + 6 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 cs-dotnet/src/gatsfloat.cs delete mode 100644 cs-dotnet/src/gatsinteger.dll create mode 100644 cs-dotnet/src/gatsnull.cs create mode 100644 cs-dotnet/src/tests/floats.cs (limited to 'cs-dotnet/src') diff --git a/cs-dotnet/src/gatsfloat.cs b/cs-dotnet/src/gatsfloat.cs new file mode 100644 index 0000000..7161975 --- /dev/null +++ b/cs-dotnet/src/gatsfloat.cs @@ -0,0 +1,124 @@ +using System.IO; +using System; + +namespace Com.Xagasoft.Gats +{ + public class GatsFloat : GatsObject + { + private static readonly double Log256 = Math.Log( 256.0 ); + public double Value { get; set; } + + public GatsFloat() + { + Value = 0.0; + } + + public GatsFloat( double val ) + { + Value = val; + } + + public override string ToString() + { + return Value.ToString(); + } + + public override void Read( Stream s, char type ) + { + if( type == 'F' ) + { + int subType = s.ReadByte(); + if( subType == -1 ) + throw new GatsException( GatsException.Type.PrematureEnd ); + switch( (char)subType ) + { + case 'N': Value = -Double.NaN; break; + case 'n': Value = Double.NaN; break; + case 'I': Value = Double.NegativeInfinity; break; + case 'i': Value = Double.PositiveInfinity; break; + case 'Z': Value = -0.0; break; + case 'z': Value = 0.0; break; + } + } + else if( type == 'f' ) + { + int len = (int)GatsInteger.ReadPackedInt( s ); + bool neg = false; + if( len < 0 ) + { + neg = true; + len = -len; + } + int[] buf = new int[len]; + for( int j = 0; j < len; j++ ) + { + buf[j] = s.ReadByte(); + } + Value = 0.0; + for( int j = len-1; j > 0; j-- ) + { + Value = (Value+(double)buf[j]) * (1.0/256.0); + } + Value += buf[0]; + long scale = GatsInteger.ReadPackedInt( s ); + Value *= Math.Pow( 256.0, scale ); + if( neg ) + Value = -Value; + } + } + + public override void Write( Stream s ) + { + if( Value == 0.0 ) + { + s.WriteByte( (int)'F' ); + s.WriteByte( (int)'z' ); + } + else if( Double.IsInfinity( Value ) ) + { + s.WriteByte( (int)'F' ); + if( Double.IsNegativeInfinity( Value ) ) + s.WriteByte( (int)'I' ); + else + s.WriteByte( (int)'i' ); + } + else if( Double.IsNaN( Value ) ) + { + s.WriteByte( (int)'F' ); + s.WriteByte( (int)'n' ); + } + else + { + s.WriteByte( (int)'f' ); + double d = Value; + bool neg = false; + if( d < 0.0 ) + { + neg = true; + d = -d; + } + + MemoryStream ms = new MemoryStream(); + long scale = (long)(Math.Log( d ) / Log256); + if( scale < 0 ) scale--; + d /= Math.Pow( 256.0, scale ); + ms.WriteByte( (byte)d ); + d -= (int)d; + for( int j = 0; j < 150 && d != 0.0; j++ ) + { + d = d*256.0; + ms.WriteByte( (byte)d ); + d -= (int)d; + } + byte[] msbuf = ms.ToArray(); + if( neg ) + GatsInteger.WritePackedInt( s, -msbuf.Length ); + else + GatsInteger.WritePackedInt( s, msbuf.Length ); + s.Write( msbuf, 0, msbuf.Length ); + GatsInteger.WritePackedInt( s, scale ); + } + } + } +} + diff --git a/cs-dotnet/src/gatsinteger.dll b/cs-dotnet/src/gatsinteger.dll deleted file mode 100644 index d3c8e79..0000000 Binary files a/cs-dotnet/src/gatsinteger.dll and /dev/null differ diff --git a/cs-dotnet/src/gatsnull.cs b/cs-dotnet/src/gatsnull.cs new file mode 100644 index 0000000..353a30f --- /dev/null +++ b/cs-dotnet/src/gatsnull.cs @@ -0,0 +1,26 @@ +using System.IO; + +namespace Com.Xagasoft.Gats +{ + public class GatsNull : GatsObject + { + public GatsNull() + { + } + + public override string ToString() + { + return "(null)"; + } + + public override void Read( Stream s, char type ) + { + } + + public override void Write( Stream s ) + { + s.WriteByte( (int)'n' ); + } + } +} + diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs index a0b9276..a48a464 100644 --- a/cs-dotnet/src/gatsobject.cs +++ b/cs-dotnet/src/gatsobject.cs @@ -4,7 +4,7 @@ namespace Com.Xagasoft.Gats { public abstract class GatsObject { - public abstract void Read( Stream s, char cType ); + public abstract void Read( Stream s, char type ); public abstract void Write( Stream s ); public static GatsObject Read( Stream s ) @@ -39,11 +39,11 @@ namespace Com.Xagasoft.Gats case 'f': case 'F': -// goRet = new GatsFloat(); + goRet = new GatsFloat(); break; case 'n': -// goRet = new GatsNull(); + goRet = new GatsNull(); break; case 'e': diff --git a/cs-dotnet/src/tests/floats.cs b/cs-dotnet/src/tests/floats.cs new file mode 100644 index 0000000..b64402c --- /dev/null +++ b/cs-dotnet/src/tests/floats.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using Com.Xagasoft.Gats; + +public class Test +{ + public static void Write( double d, Stream s ) + { + GatsFloat gf = new GatsFloat( d ); + gf.Write( s ); + Console.WriteLine("Wrote: " + d ); + } + + 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(); + Write( Math.PI, 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 ); + } + } +} diff --git a/cs-dotnet/src/tests/ints.cs b/cs-dotnet/src/tests/ints.cs index 7099995..766b667 100644 --- a/cs-dotnet/src/tests/ints.cs +++ b/cs-dotnet/src/tests/ints.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.IO; using Com.Xagasoft.Gats; @@ -11,5 +12,7 @@ class Ints GatsObject obj = GatsObject.Read( file ); Console.WriteLine("Read type: " + obj.GetType() ); Console.WriteLine("Read int: " + ((GatsInteger)obj).Value ); + + Console.WriteLine("Float? " + double.Parse("0x1.62e42fefa39efp+2") ); } } -- cgit v1.2.3