diff options
Diffstat (limited to '')
-rw-r--r-- | cs-dotnet/src/gatsboolean.cs | 40 | ||||
-rw-r--r-- | cs-dotnet/src/gatsexception.cs | 16 | ||||
-rw-r--r-- | cs-dotnet/src/gatsinteger.cs | 6 | ||||
-rw-r--r-- | cs-dotnet/src/gatsobject.cs | 53 | ||||
-rw-r--r-- | cs-dotnet/src/gatsstring.cs | 49 | ||||
-rw-r--r-- | cs-dotnet/src/tests/ints.cs | 9 |
6 files changed, 162 insertions, 11 deletions
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 @@ | |||
1 | using System.IO; | ||
2 | |||
3 | namespace Com.Xagasoft.Gats | ||
4 | { | ||
5 | public class GatsBoolean : GatsObject | ||
6 | { | ||
7 | public bool Value { get; set; } | ||
8 | |||
9 | public GatsBoolean() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | public GatsBoolean( bool val ) | ||
14 | { | ||
15 | Value = val; | ||
16 | } | ||
17 | |||
18 | public override string ToString() | ||
19 | { | ||
20 | return Value.ToString(); | ||
21 | } | ||
22 | |||
23 | public override void Read( Stream s, char type ) | ||
24 | { | ||
25 | if( type == '0' ) | ||
26 | Value = false; | ||
27 | else | ||
28 | Value = true; | ||
29 | } | ||
30 | |||
31 | public override void Write( Stream s ) | ||
32 | { | ||
33 | if( Value ) | ||
34 | s.WriteByte( (int)'1' ); | ||
35 | else | ||
36 | s.WriteByte( (int)'0' ); | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
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 | |||
4 | { | 4 | { |
5 | public class GatsException : Exception | 5 | public class GatsException : Exception |
6 | { | 6 | { |
7 | public GatsException( String sName ) : | 7 | public enum Type |
8 | base( sName ) | ||
9 | { | 8 | { |
9 | PrematureEnd = 1, | ||
10 | InvalidType = 2 | ||
11 | }; | ||
12 | |||
13 | private Type _Reason; | ||
14 | public Type Reason | ||
15 | { | ||
16 | get { return this._Reason; } | ||
17 | } | ||
18 | |||
19 | public GatsException( Type reason ) | ||
20 | { | ||
21 | _Reason = reason; | ||
10 | } | 22 | } |
11 | } | 23 | } |
12 | } | 24 | } |
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 | |||
16 | return Value.ToString(); | 16 | return Value.ToString(); |
17 | } | 17 | } |
18 | 18 | ||
19 | public override void Read( Stream s, byte cType ) | 19 | public override void Read( Stream s, char cType ) |
20 | { | 20 | { |
21 | Value = ReadPackedInt( s ); | 21 | Value = ReadPackedInt( s ); |
22 | } | 22 | } |
@@ -35,7 +35,7 @@ namespace Com.Xagasoft.Gats | |||
35 | 35 | ||
36 | b = s.ReadByte(); | 36 | b = s.ReadByte(); |
37 | if( b == -1 ) | 37 | if( b == -1 ) |
38 | throw new GatsException("Premature end of stream encountered."); | 38 | throw new GatsException( GatsException.Type.PrematureEnd ); |
39 | bNeg = (b&0x40) == 0x40; | 39 | bNeg = (b&0x40) == 0x40; |
40 | rOut |= ((long)b)&0x3F; | 40 | rOut |= ((long)b)&0x3F; |
41 | int c = 0; | 41 | int c = 0; |
@@ -43,7 +43,7 @@ namespace Com.Xagasoft.Gats | |||
43 | { | 43 | { |
44 | b = s.ReadByte(); | 44 | b = s.ReadByte(); |
45 | if( b == -1 ) | 45 | if( b == -1 ) |
46 | throw new GatsException("Premature end of stream encountered."); | 46 | throw new GatsException( GatsException.Type.PrematureEnd ); |
47 | rOut |= (long)(b&0x7F) << (6+7*(c++)); | 47 | rOut |= (long)(b&0x7F) << (6+7*(c++)); |
48 | } | 48 | } |
49 | if( bNeg ) | 49 | 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 | |||
4 | { | 4 | { |
5 | public abstract class GatsObject | 5 | public abstract class GatsObject |
6 | { | 6 | { |
7 | public abstract void Read( Stream s, byte cType ); | 7 | public abstract void Read( Stream s, char cType ); |
8 | public abstract void Write( Stream s ); | 8 | public abstract void Write( Stream s ); |
9 | |||
10 | public static GatsObject Read( Stream s ) | ||
11 | { | ||
12 | int b = s.ReadByte(); | ||
13 | if( b == -1 ) | ||
14 | throw new GatsException( GatsException.Type.PrematureEnd ); | ||
15 | char type = (char)b; | ||
16 | GatsObject goRet = null; | ||
17 | switch( type ) | ||
18 | { | ||
19 | case 'i': | ||
20 | goRet = new GatsInteger(); | ||
21 | break; | ||
22 | |||
23 | case 's': | ||
24 | goRet = new GatsString(); | ||
25 | break; | ||
26 | |||
27 | case '0': | ||
28 | case '1': | ||
29 | goRet = new GatsBoolean(); | ||
30 | break; | ||
31 | |||
32 | case 'l': | ||
33 | // goRet = new GatsList(); | ||
34 | break; | ||
35 | |||
36 | case 'd': | ||
37 | // goRet = new GatsDictionary(); | ||
38 | break; | ||
39 | |||
40 | case 'f': | ||
41 | case 'F': | ||
42 | // goRet = new GatsFloat(); | ||
43 | break; | ||
44 | |||
45 | case 'n': | ||
46 | // goRet = new GatsNull(); | ||
47 | break; | ||
48 | |||
49 | case 'e': | ||
50 | return null; | ||
51 | |||
52 | default: | ||
53 | throw new GatsException( GatsException.Type.InvalidType ); | ||
54 | } | ||
55 | |||
56 | goRet.Read( s, type ); | ||
57 | |||
58 | return goRet; | ||
59 | } | ||
9 | } | 60 | } |
10 | } | 61 | } |
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 @@ | |||
1 | using System.IO; | ||
2 | |||
3 | namespace Com.Xagasoft.Gats | ||
4 | { | ||
5 | public class GatsString : GatsObject | ||
6 | { | ||
7 | public byte[] Value { get; set; } | ||
8 | |||
9 | public GatsString() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | public GatsString( byte[] val ) | ||
14 | { | ||
15 | Value = val; | ||
16 | } | ||
17 | |||
18 | public override string ToString() | ||
19 | { | ||
20 | return Value.ToString(); | ||
21 | } | ||
22 | |||
23 | public override void Read( Stream s, char cType ) | ||
24 | { | ||
25 | int Size = (int)GatsInteger.ReadPackedInt( s ); | ||
26 | Value = new byte[Size]; | ||
27 | int SoFar = 0; | ||
28 | do | ||
29 | { | ||
30 | SoFar += s.Read( Value, SoFar, Size-SoFar ); | ||
31 | } while( SoFar < Size ); | ||
32 | } | ||
33 | |||
34 | public override void Write( Stream s ) | ||
35 | { | ||
36 | s.WriteByte( (byte)'s' ); | ||
37 | if( Value == null ) | ||
38 | { | ||
39 | GatsInteger.WritePackedInt( s, 0 ); | ||
40 | } | ||
41 | else | ||
42 | { | ||
43 | GatsInteger.WritePackedInt( s, Value.Length ); | ||
44 | s.Write( Value, 0, Value.Length ); | ||
45 | } | ||
46 | } | ||
47 | }; | ||
48 | } | ||
49 | |||
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 | |||
7 | static void Main() | 7 | static void Main() |
8 | { | 8 | { |
9 | FileStream file = new FileStream("test.gats", FileMode.Open, | 9 | FileStream file = new FileStream("test.gats", FileMode.Open, |
10 | FileAccess.Write ); | 10 | FileAccess.Read ); |
11 | long iVal = 0xfffffe; | 11 | GatsObject obj = GatsObject.Read( file ); |
12 | GatsInteger i = new GatsInteger( iVal ); | 12 | Console.WriteLine("Read type: " + obj.GetType() ); |
13 | i.Write( file ); | 13 | Console.WriteLine("Read int: " + ((GatsInteger)obj).Value ); |
14 | Console.WriteLine("Read int: " + i.Value ); | ||
15 | } | 14 | } |
16 | } | 15 | } |