aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-15 17:41:14 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-15 17:41:14 +0000
commitf1e413e914b9f03607194757848bab1ed4f401a3 (patch)
tree0cc0dbba61295b47bf343d439a4234123c4369a3
parent63628550708a616c5c58bc5c707db1e7fd9cd7c2 (diff)
downloadlibgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.gz
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.bz2
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.xz
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.zip
Accidentally included a dll, oops. Also, gats null, and gats float work
correctly now and are tested against the python implementation.
-rw-r--r--cs-dotnet/src/gatsfloat.cs124
-rw-r--r--cs-dotnet/src/gatsinteger.dllbin3072 -> 0 bytes
-rw-r--r--cs-dotnet/src/gatsnull.cs26
-rw-r--r--cs-dotnet/src/gatsobject.cs6
-rw-r--r--cs-dotnet/src/tests/floats.cs39
-rw-r--r--cs-dotnet/src/tests/ints.cs3
6 files changed, 195 insertions, 3 deletions
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 @@
1using System.IO;
2using System;
3
4namespace Com.Xagasoft.Gats
5{
6 public class GatsFloat : GatsObject
7 {
8 private static readonly double Log256 = Math.Log( 256.0 );
9 public double Value { get; set; }
10
11 public GatsFloat()
12 {
13 Value = 0.0;
14 }
15
16 public GatsFloat( double val )
17 {
18 Value = val;
19 }
20
21 public override string ToString()
22 {
23 return Value.ToString();
24 }
25
26 public override void Read( Stream s, char type )
27 {
28 if( type == 'F' )
29 {
30 int subType = s.ReadByte();
31 if( subType == -1 )
32 throw new GatsException( GatsException.Type.PrematureEnd );
33 switch( (char)subType )
34 {
35 case 'N': Value = -Double.NaN; break;
36 case 'n': Value = Double.NaN; break;
37 case 'I': Value = Double.NegativeInfinity; break;
38 case 'i': Value = Double.PositiveInfinity; break;
39 case 'Z': Value = -0.0; break;
40 case 'z': Value = 0.0; break;
41 }
42 }
43 else if( type == 'f' )
44 {
45 int len = (int)GatsInteger.ReadPackedInt( s );
46 bool neg = false;
47 if( len < 0 )
48 {
49 neg = true;
50 len = -len;
51 }
52 int[] buf = new int[len];
53 for( int j = 0; j < len; j++ )
54 {
55 buf[j] = s.ReadByte();
56 }
57 Value = 0.0;
58 for( int j = len-1; j > 0; j-- )
59 {
60 Value = (Value+(double)buf[j]) * (1.0/256.0);
61 }
62 Value += buf[0];
63 long scale = GatsInteger.ReadPackedInt( s );
64 Value *= Math.Pow( 256.0, scale );
65 if( neg )
66 Value = -Value;
67 }
68 }
69
70 public override void Write( Stream s )
71 {
72 if( Value == 0.0 )
73 {
74 s.WriteByte( (int)'F' );
75 s.WriteByte( (int)'z' );
76 }
77 else if( Double.IsInfinity( Value ) )
78 {
79 s.WriteByte( (int)'F' );
80 if( Double.IsNegativeInfinity( Value ) )
81 s.WriteByte( (int)'I' );
82 else
83 s.WriteByte( (int)'i' );
84 }
85 else if( Double.IsNaN( Value ) )
86 {
87 s.WriteByte( (int)'F' );
88 s.WriteByte( (int)'n' );
89 }
90 else
91 {
92 s.WriteByte( (int)'f' );
93 double d = Value;
94 bool neg = false;
95 if( d < 0.0 )
96 {
97 neg = true;
98 d = -d;
99 }
100
101 MemoryStream ms = new MemoryStream();
102 long scale = (long)(Math.Log( d ) / Log256);
103 if( scale < 0 ) scale--;
104 d /= Math.Pow( 256.0, scale );
105 ms.WriteByte( (byte)d );
106 d -= (int)d;
107 for( int j = 0; j < 150 && d != 0.0; j++ )
108 {
109 d = d*256.0;
110 ms.WriteByte( (byte)d );
111 d -= (int)d;
112 }
113 byte[] msbuf = ms.ToArray();
114 if( neg )
115 GatsInteger.WritePackedInt( s, -msbuf.Length );
116 else
117 GatsInteger.WritePackedInt( s, msbuf.Length );
118 s.Write( msbuf, 0, msbuf.Length );
119 GatsInteger.WritePackedInt( s, scale );
120 }
121 }
122 }
123}
124
diff --git a/cs-dotnet/src/gatsinteger.dll b/cs-dotnet/src/gatsinteger.dll
deleted file mode 100644
index d3c8e79..0000000
--- a/cs-dotnet/src/gatsinteger.dll
+++ /dev/null
Binary files 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 @@
1using System.IO;
2
3namespace Com.Xagasoft.Gats
4{
5 public class GatsNull : GatsObject
6 {
7 public GatsNull()
8 {
9 }
10
11 public override string ToString()
12 {
13 return "(null)";
14 }
15
16 public override void Read( Stream s, char type )
17 {
18 }
19
20 public override void Write( Stream s )
21 {
22 s.WriteByte( (int)'n' );
23 }
24 }
25}
26
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
4{ 4{
5 public abstract class GatsObject 5 public abstract class GatsObject
6 { 6 {
7 public abstract void Read( Stream s, char cType ); 7 public abstract void Read( Stream s, char type );
8 public abstract void Write( Stream s ); 8 public abstract void Write( Stream s );
9 9
10 public static GatsObject Read( Stream s ) 10 public static GatsObject Read( Stream s )
@@ -39,11 +39,11 @@ namespace Com.Xagasoft.Gats
39 39
40 case 'f': 40 case 'f':
41 case 'F': 41 case 'F':
42// goRet = new GatsFloat(); 42 goRet = new GatsFloat();
43 break; 43 break;
44 44
45 case 'n': 45 case 'n':
46// goRet = new GatsNull(); 46 goRet = new GatsNull();
47 break; 47 break;
48 48
49 case 'e': 49 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 @@
1using System;
2using System.IO;
3using Com.Xagasoft.Gats;
4
5public class Test
6{
7 public static void Write( double d, Stream s )
8 {
9 GatsFloat gf = new GatsFloat( d );
10 gf.Write( s );
11 Console.WriteLine("Wrote: " + d );
12 }
13
14 public static void Read( Stream s )
15 {
16 GatsObject o = GatsObject.Read( s );
17 Console.WriteLine("Read type: " + o.GetType() );
18 Console.WriteLine("Read vlaue: " + o );
19 }
20
21 public static void Main()
22 {
23 MemoryStream ms = new MemoryStream();
24 Write( Math.PI, ms );
25 ms.Seek( 0, SeekOrigin.Begin );
26 Read( ms );
27
28 try
29 {
30 FileStream fs = new FileStream("float.gats", FileMode.Open,
31 FileAccess.Read );
32 Read( fs );
33 }
34 catch( Exception e )
35 {
36 Console.WriteLine("Can't test files: " + e.Message );
37 }
38 }
39}
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 @@
1using System; 1using System;
2using System.Globalization;
2using System.IO; 3using System.IO;
3using Com.Xagasoft.Gats; 4using Com.Xagasoft.Gats;
4 5
@@ -11,5 +12,7 @@ class Ints
11 GatsObject obj = GatsObject.Read( file ); 12 GatsObject obj = GatsObject.Read( file );
12 Console.WriteLine("Read type: " + obj.GetType() ); 13 Console.WriteLine("Read type: " + obj.GetType() );
13 Console.WriteLine("Read int: " + ((GatsInteger)obj).Value ); 14 Console.WriteLine("Read int: " + ((GatsInteger)obj).Value );
15
16 Console.WriteLine("Float? " + double.Parse("0x1.62e42fefa39efp+2") );
14 } 17 }
15} 18}