aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src/gatsdictionary.cs
diff options
context:
space:
mode:
Diffstat (limited to 'cs-dotnet/src/gatsdictionary.cs')
-rw-r--r--cs-dotnet/src/gatsdictionary.cs149
1 files changed, 149 insertions, 0 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 @@
1using System.Text;
2using System.IO;
3using System.Linq;
4using System.Collections;
5using System.Collections.Generic;
6
7namespace 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