aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src
diff options
context:
space:
mode:
Diffstat (limited to 'cs-dotnet/src')
-rw-r--r--cs-dotnet/src/gatsexception.cs12
-rw-r--r--cs-dotnet/src/gatsinteger.cs86
-rw-r--r--cs-dotnet/src/gatsinteger.dllbin0 -> 3072 bytes
-rw-r--r--cs-dotnet/src/gatsobject.cs10
-rw-r--r--cs-dotnet/src/tests/ints.cs16
5 files changed, 124 insertions, 0 deletions
diff --git a/cs-dotnet/src/gatsexception.cs b/cs-dotnet/src/gatsexception.cs
new file mode 100644
index 0000000..ea665d0
--- /dev/null
+++ b/cs-dotnet/src/gatsexception.cs
@@ -0,0 +1,12 @@
1using System;
2
3namespace Com.Xagasoft.Gats
4{
5 public class GatsException : Exception
6 {
7 public GatsException( String sName ) :
8 base( sName )
9 {
10 }
11 }
12}
diff --git a/cs-dotnet/src/gatsinteger.cs b/cs-dotnet/src/gatsinteger.cs
new file mode 100644
index 0000000..cbb552e
--- /dev/null
+++ b/cs-dotnet/src/gatsinteger.cs
@@ -0,0 +1,86 @@
1using System.IO;
2
3namespace Com.Xagasoft.Gats
4{
5 public class GatsInteger : GatsObject
6 {
7 public long Value { get; set; }
8
9 public GatsInteger( long val=0 )
10 {
11 Value = val;
12 }
13
14 public override string ToString()
15 {
16 return Value.ToString();
17 }
18
19 public override void Read( Stream s, byte cType )
20 {
21 Value = ReadPackedInt( s );
22 }
23
24 public override void Write( Stream s )
25 {
26 s.WriteByte( (int)'i' );
27 WritePackedInt( s, Value );
28 }
29
30 public static long ReadPackedInt( Stream s )
31 {
32 int b;
33 long rOut = 0;
34 bool bNeg;
35
36 b = s.ReadByte();
37 if( b == -1 )
38 throw new GatsException("Premature end of stream encountered.");
39 bNeg = (b&0x40) == 0x40;
40 rOut |= ((long)b)&0x3F;
41 int c = 0;
42 while( (b&0x80) == 0x80 )
43 {
44 b = s.ReadByte();
45 if( b == -1 )
46 throw new GatsException("Premature end of stream encountered.");
47 rOut |= (long)(b&0x7F) << (6+7*(c++));
48 }
49 if( bNeg )
50 return -rOut;
51 return rOut;
52 }
53
54 public static void WritePackedInt( Stream s, long iIn )
55 {
56 byte b;
57
58 if( iIn < 0 )
59 {
60 iIn = -iIn;
61 b = (byte)(iIn&0x3F);
62 if( iIn > b )
63 b |= 0x80 | 0x40;
64 else
65 b |= 0x40;
66 }
67 else
68 {
69 b = (byte)(iIn&0x3F);
70 if( iIn > b )
71 b |= 0x80;
72 }
73 s.WriteByte( b );
74 iIn = iIn >> 6;
75
76 while( iIn > 0 )
77 {
78 b = (byte)(iIn&0x7F);
79 if( iIn > b )
80 b |= 0x80;
81 s.WriteByte( b );
82 iIn = iIn >> 7;
83 }
84 }
85 };
86}
diff --git a/cs-dotnet/src/gatsinteger.dll b/cs-dotnet/src/gatsinteger.dll
new file mode 100644
index 0000000..d3c8e79
--- /dev/null
+++ b/cs-dotnet/src/gatsinteger.dll
Binary files differ
diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs
new file mode 100644
index 0000000..f782f95
--- /dev/null
+++ b/cs-dotnet/src/gatsobject.cs
@@ -0,0 +1,10 @@
1using System.IO;
2
3namespace Com.Xagasoft.Gats
4{
5 public abstract class GatsObject
6 {
7 public abstract void Read( Stream s, byte cType );
8 public abstract void Write( Stream s );
9 }
10}
diff --git a/cs-dotnet/src/tests/ints.cs b/cs-dotnet/src/tests/ints.cs
new file mode 100644
index 0000000..ea97820
--- /dev/null
+++ b/cs-dotnet/src/tests/ints.cs
@@ -0,0 +1,16 @@
1using System;
2using System.IO;
3using Com.Xagasoft.Gats;
4
5class Ints
6{
7 static void Main()
8 {
9 FileStream file = new FileStream("test.gats", FileMode.Open,
10 FileAccess.Write );
11 long iVal = 0xfffffe;
12 GatsInteger i = new GatsInteger( iVal );
13 i.Write( file );
14 Console.WriteLine("Read int: " + i.Value );
15 }
16}