diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-11-15 20:59:11 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-11-15 20:59:11 +0000 |
commit | 862c485d46f3f8e992a9ca9fea40fb33f0b43959 (patch) | |
tree | 118e251b2867ed41e0825a573838160357b0eed5 /cs-dotnet/src | |
parent | b9b5a5fc16e21387b3350260d2139b293bbcd2b0 (diff) | |
download | libgats-862c485d46f3f8e992a9ca9fea40fb33f0b43959.tar.gz libgats-862c485d46f3f8e992a9ca9fea40fb33f0b43959.tar.bz2 libgats-862c485d46f3f8e992a9ca9fea40fb33f0b43959.tar.xz libgats-862c485d46f3f8e992a9ca9fea40fb33f0b43959.zip |
Dictionaries are implemented now. Everything tests out against python.
The next step is the packet stream interface.
Diffstat (limited to 'cs-dotnet/src')
-rw-r--r-- | cs-dotnet/src/gatsdictionary.cs | 149 | ||||
-rw-r--r-- | cs-dotnet/src/gatsexception.cs | 3 | ||||
-rw-r--r-- | cs-dotnet/src/gatsobject.cs | 20 | ||||
-rw-r--r-- | cs-dotnet/src/tests/dictionary.cs | 48 | ||||
-rw-r--r-- | cs-dotnet/src/tests/lists.cs | 5 |
5 files changed, 211 insertions, 14 deletions
diff --git a/cs-dotnet/src/gatsdictionary.cs b/cs-dotnet/src/gatsdictionary.cs new file mode 100644 index 0000000..0122c3a --- /dev/null +++ b/cs-dotnet/src/gatsdictionary.cs | |||
@@ -0,0 +1,149 @@ | |||
1 | using System.Text; | ||
2 | using System.IO; | ||
3 | using System.Linq; | ||
4 | using System.Collections; | ||
5 | using System.Collections.Generic; | ||
6 | |||
7 | namespace Com.Xagasoft.Gats | ||
8 | { | ||
9 | public class GatsDictionary : GatsObject, IDictionary<string, GatsObject> | ||
10 | // ICollection<KeyValuePair<string, GatsObject>>, | ||
11 | // IEnumerable<KeyValuePair<string, GatsObject>> | ||
12 | { | ||
13 | private Dictionary<string, GatsObject> Value = | ||
14 | new Dictionary<string, GatsObject>(); | ||
15 | |||
16 | public GatsDictionary() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | public override string ToString() | ||
21 | { | ||
22 | StringBuilder bld = new StringBuilder(); | ||
23 | bld.Append("{"); | ||
24 | bool begin = true; | ||
25 | foreach( KeyValuePair<string, GatsObject> j in Value ) | ||
26 | { | ||
27 | if( begin ) | ||
28 | begin = false; | ||
29 | else | ||
30 | bld.Append(", "); | ||
31 | bld.Append( j.Key ); | ||
32 | bld.Append(" = "); | ||
33 | bld.Append( j.Value ); | ||
34 | } | ||
35 | bld.Append("}"); | ||
36 | return bld.ToString(); | ||
37 | } | ||
38 | |||
39 | public override void Read( Stream s, char type ) | ||
40 | { | ||
41 | for(;;) | ||
42 | { | ||
43 | GatsObject key = GatsObject.Read( s ); | ||
44 | if( key == null ) | ||
45 | break; | ||
46 | if( key.GetType() != typeof(GatsString) ) | ||
47 | throw new GatsException( GatsException.Type.InvalidFormat ); | ||
48 | Value.Add( key.ToString(), GatsObject.Read( s ) ); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | public override void Write( Stream s ) | ||
53 | { | ||
54 | s.WriteByte( (int)'d' ); | ||
55 | foreach( KeyValuePair<string, GatsObject> j in Value ) | ||
56 | { | ||
57 | new GatsString( j.Key ).Write( s ); | ||
58 | j.Value.Write( s ); | ||
59 | } | ||
60 | s.WriteByte( (int)'e' ); | ||
61 | } | ||
62 | |||
63 | // | ||
64 | // List interface overrides under here. | ||
65 | // | ||
66 | public void Add( string key, GatsObject obj ) | ||
67 | { | ||
68 | Value.Add( key, obj ); | ||
69 | } | ||
70 | |||
71 | public bool Remove( string key ) | ||
72 | { | ||
73 | return Value.Remove( key ); | ||
74 | } | ||
75 | |||
76 | public bool ContainsKey( string key ) | ||
77 | { | ||
78 | return Value.ContainsKey( key ); | ||
79 | } | ||
80 | |||
81 | public bool TryGetValue( string key, out GatsObject obj ) | ||
82 | { | ||
83 | return Value.TryGetValue( key, out obj ); | ||
84 | } | ||
85 | |||
86 | public GatsObject this[string key] | ||
87 | { | ||
88 | get { return this.Value[key]; } | ||
89 | set { this.Value[key] = value; } | ||
90 | } | ||
91 | |||
92 | public ICollection<string> Keys | ||
93 | { | ||
94 | get { return this.Value.Keys; } | ||
95 | } | ||
96 | |||
97 | public ICollection<GatsObject> Values | ||
98 | { | ||
99 | get { return this.Value.Values; } | ||
100 | } | ||
101 | |||
102 | public int Count | ||
103 | { | ||
104 | get { return this.Value.Count; } | ||
105 | } | ||
106 | |||
107 | public bool IsReadOnly | ||
108 | { | ||
109 | get { return false; } // return this.Value.IsReadOnly; } | ||
110 | } | ||
111 | |||
112 | public void Add( KeyValuePair<string,GatsObject> pair ) | ||
113 | { | ||
114 | ((IDictionary<string,GatsObject>)Value).Add( pair ); | ||
115 | } | ||
116 | |||
117 | public void Clear() | ||
118 | { | ||
119 | Value.Clear(); | ||
120 | } | ||
121 | |||
122 | public bool Contains( KeyValuePair<string,GatsObject> pair ) | ||
123 | { | ||
124 | return ((IDictionary<string,GatsObject>)Value).Contains( pair ); | ||
125 | } | ||
126 | |||
127 | public void CopyTo( KeyValuePair<string,GatsObject>[] result, | ||
128 | int count ) | ||
129 | { | ||
130 | ((IDictionary<string,GatsObject>)Value).CopyTo( result, count ); | ||
131 | } | ||
132 | |||
133 | public bool Remove( KeyValuePair<string,GatsObject> pair ) | ||
134 | { | ||
135 | return ((IDictionary<string,GatsObject>)Value).Remove( pair ); | ||
136 | } | ||
137 | |||
138 | IEnumerator<KeyValuePair<string,GatsObject> > IEnumerable<KeyValuePair<string,GatsObject> >.GetEnumerator() | ||
139 | { | ||
140 | return Value.GetEnumerator(); | ||
141 | } | ||
142 | |||
143 | IEnumerator IEnumerable.GetEnumerator() | ||
144 | { | ||
145 | return Value.GetEnumerator(); | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | |||
diff --git a/cs-dotnet/src/gatsexception.cs b/cs-dotnet/src/gatsexception.cs index c6b2690..2dc49eb 100644 --- a/cs-dotnet/src/gatsexception.cs +++ b/cs-dotnet/src/gatsexception.cs | |||
@@ -7,7 +7,8 @@ namespace Com.Xagasoft.Gats | |||
7 | public enum Type | 7 | public enum Type |
8 | { | 8 | { |
9 | PrematureEnd = 1, | 9 | PrematureEnd = 1, |
10 | InvalidType = 2 | 10 | InvalidType = 2, |
11 | InvalidFormat = 3, | ||
11 | }; | 12 | }; |
12 | 13 | ||
13 | private Type _Reason; | 14 | private Type _Reason; |
diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs index 5e76a6c..792db6a 100644 --- a/cs-dotnet/src/gatsobject.cs +++ b/cs-dotnet/src/gatsobject.cs | |||
@@ -13,37 +13,37 @@ namespace Com.Xagasoft.Gats | |||
13 | if( b == -1 ) | 13 | if( b == -1 ) |
14 | throw new GatsException( GatsException.Type.PrematureEnd ); | 14 | throw new GatsException( GatsException.Type.PrematureEnd ); |
15 | char type = (char)b; | 15 | char type = (char)b; |
16 | GatsObject goRet = null; | 16 | GatsObject ret = null; |
17 | switch( type ) | 17 | switch( type ) |
18 | { | 18 | { |
19 | case 'i': | 19 | case 'i': |
20 | goRet = new GatsInteger(); | 20 | ret = new GatsInteger(); |
21 | break; | 21 | break; |
22 | 22 | ||
23 | case 's': | 23 | case 's': |
24 | goRet = new GatsString(); | 24 | ret = new GatsString(); |
25 | break; | 25 | break; |
26 | 26 | ||
27 | case '0': | 27 | case '0': |
28 | case '1': | 28 | case '1': |
29 | goRet = new GatsBoolean(); | 29 | ret = new GatsBoolean(); |
30 | break; | 30 | break; |
31 | 31 | ||
32 | case 'l': | 32 | case 'l': |
33 | goRet = new GatsList(); | 33 | ret = new GatsList(); |
34 | break; | 34 | break; |
35 | 35 | ||
36 | case 'd': | 36 | case 'd': |
37 | // goRet = new GatsDictionary(); | 37 | ret = new GatsDictionary(); |
38 | break; | 38 | break; |
39 | 39 | ||
40 | case 'f': | 40 | case 'f': |
41 | case 'F': | 41 | case 'F': |
42 | goRet = new GatsFloat(); | 42 | ret = new GatsFloat(); |
43 | break; | 43 | break; |
44 | 44 | ||
45 | case 'n': | 45 | case 'n': |
46 | goRet = new GatsNull(); | 46 | ret = new GatsNull(); |
47 | break; | 47 | break; |
48 | 48 | ||
49 | case 'e': | 49 | case 'e': |
@@ -53,9 +53,9 @@ namespace Com.Xagasoft.Gats | |||
53 | throw new GatsException( GatsException.Type.InvalidType ); | 53 | throw new GatsException( GatsException.Type.InvalidType ); |
54 | } | 54 | } |
55 | 55 | ||
56 | goRet.Read( s, type ); | 56 | ret.Read( s, type ); |
57 | 57 | ||
58 | return goRet; | 58 | return ret; |
59 | } | 59 | } |
60 | } | 60 | } |
61 | } | 61 | } |
diff --git a/cs-dotnet/src/tests/dictionary.cs b/cs-dotnet/src/tests/dictionary.cs new file mode 100644 index 0000000..a22acad --- /dev/null +++ b/cs-dotnet/src/tests/dictionary.cs | |||
@@ -0,0 +1,48 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using Com.Xagasoft.Gats; | ||
4 | |||
5 | public class Test | ||
6 | { | ||
7 | public static void Write( GatsObject o, Stream s ) | ||
8 | { | ||
9 | o.Write( s ); | ||
10 | Console.WriteLine("Wrote: " + o ); | ||
11 | } | ||
12 | |||
13 | public static void Read( Stream s ) | ||
14 | { | ||
15 | GatsObject o = GatsObject.Read( s ); | ||
16 | Console.WriteLine("Read type: " + o.GetType() ); | ||
17 | Console.WriteLine("Read vlaue: " + o ); | ||
18 | } | ||
19 | |||
20 | public static void Main() | ||
21 | { | ||
22 | MemoryStream ms = new MemoryStream(); | ||
23 | GatsDictionary d = new GatsDictionary(); | ||
24 | GatsList l = new GatsList(); | ||
25 | l.Add( new GatsFloat( Math.PI ) ); | ||
26 | l.Add( new GatsInteger( 1337 ) ); | ||
27 | l.Add( new GatsString("Hello") ); | ||
28 | d.Add("list", l ); | ||
29 | d.Add("int", new GatsInteger( 998877 ) ); | ||
30 | d.Add("float", new GatsFloat( 87.332 ) ); | ||
31 | d.Add("string", new GatsString("Yup, a string") ); | ||
32 | d.Add("bool", new GatsBoolean( false ) ); | ||
33 | Write( d, ms ); | ||
34 | ms.Seek( 0, SeekOrigin.Begin ); | ||
35 | Read( ms ); | ||
36 | |||
37 | try | ||
38 | { | ||
39 | FileStream fs = new FileStream("dictionary.gats", FileMode.Open, | ||
40 | FileAccess.Read ); | ||
41 | Read( fs ); | ||
42 | } | ||
43 | catch( Exception e ) | ||
44 | { | ||
45 | Console.WriteLine("Can't test files: " + e.Message ); | ||
46 | } | ||
47 | } | ||
48 | } | ||
diff --git a/cs-dotnet/src/tests/lists.cs b/cs-dotnet/src/tests/lists.cs index 8d12bf9..09ad931 100644 --- a/cs-dotnet/src/tests/lists.cs +++ b/cs-dotnet/src/tests/lists.cs | |||
@@ -27,10 +27,10 @@ public class Test | |||
27 | Write( l, ms ); | 27 | Write( l, ms ); |
28 | ms.Seek( 0, SeekOrigin.Begin ); | 28 | ms.Seek( 0, SeekOrigin.Begin ); |
29 | Read( ms ); | 29 | Read( ms ); |
30 | /* | 30 | |
31 | try | 31 | try |
32 | { | 32 | { |
33 | FileStream fs = new FileStream("float.gats", FileMode.Open, | 33 | FileStream fs = new FileStream("list.gats", FileMode.Open, |
34 | FileAccess.Read ); | 34 | FileAccess.Read ); |
35 | Read( fs ); | 35 | Read( fs ); |
36 | } | 36 | } |
@@ -38,6 +38,5 @@ public class Test | |||
38 | { | 38 | { |
39 | Console.WriteLine("Can't test files: " + e.Message ); | 39 | Console.WriteLine("Can't test files: " + e.Message ); |
40 | } | 40 | } |
41 | */ | ||
42 | } | 41 | } |
43 | } | 42 | } |