1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* Copyright (C) 2007-2013 Xagasoft, All rights reserved.
*
* This file is part of the libgats library and is released under the
* terms of the license contained in the file LICENSE.
*/
using System.Text;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace Com.Xagasoft.Gats
{
/// <summary>
/// Encapsulates a single dictionary of GatsObjects.
/// </summary>
/// <remarks>
/// All keys are strings, and are encoded UTF-8. At the moment it is
/// advisable to stick to 7-bit ASCII or LATIN-1 compatible strings for
/// interoperability. If you want full unicode for keys, be sure you
/// handle it correctly with all libraries.
///
/// Values can be any valid GatsObject.
///
/// Just like dictionaries in memory, order is not important and is
/// considered random in encoded GATS. This means that dictionary items
/// may be in a different order after writing them and reading them back
/// again. Do not rely on a specific order, if you need ordered data use
/// a GatsList.
///
/// All standard dictionary interface methods are implemented, so you can
/// use a GatsDictionary just like the .NET standard Dictionary.
/// </remarks>
public class GatsDictionary : GatsObject, IDictionary<string, GatsObject>
// ICollection<KeyValuePair<string, GatsObject>>,
// IEnumerable<KeyValuePair<string, GatsObject>>
{
private Dictionary<string, GatsObject> Value =
new Dictionary<string, GatsObject>();
public GatsDictionary()
{
}
public override string ToString()
{
StringBuilder bld = new StringBuilder();
bld.Append("{");
bool begin = true;
foreach( KeyValuePair<string, GatsObject> j in Value )
{
if( begin )
begin = false;
else
bld.Append(", ");
bld.Append( j.Key );
bld.Append(" = ");
bld.Append( j.Value );
}
bld.Append("}");
return bld.ToString();
}
public override void Read( Stream s, char type )
{
for(;;)
{
GatsObject key = GatsObject.Read( s );
if( key == null )
break;
if( key.GetType() != typeof(GatsString) )
throw new GatsException( GatsException.Type.InvalidFormat );
Value.Add( key.ToString(), GatsObject.Read( s ) );
}
}
public override void Write( Stream s )
{
s.WriteByte( (int)'d' );
foreach( KeyValuePair<string, GatsObject> j in Value )
{
new GatsString( j.Key ).Write( s );
j.Value.Write( s );
}
s.WriteByte( (int)'e' );
}
//
// Extra helper functions for making life with GATS easier
//
/// <summary>
/// Helper function to add a new GatsString to the dictionary from a
/// byte array.
/// </summary>
/// <param name="key">The key to insert the new value with</param>
/// <param name="val">The byte array to be inserted as a string</param>
public void Add( string key, byte[] val )
{
Add( key, new GatsString( val ) );
}
/// <summary>
/// Helper function to add a new GatsString to the dictionary from a
/// string.
/// </summary>
/// <remarks>
/// The string is encoded UTF-8 by .NETs internal facilities.
/// </remarks>
/// <param name="key">The key to insert the new value with</param>
/// <param name="val">The string to be inserted as a string</param>
public void Add( string key, string val )
{
Add( key, new GatsString( val ) );
}
/// <summary>
/// Helper function to add a new GatsInteger to the dictionary.
/// </summary>
/// <remarks>
/// Implicit upcasting should allow all integer types (byte, short,
/// int, long), and automatic unboxing should allow all object variants
/// to be passed into this method without problem.
/// </remarks>
/// <param name="key">The key to insert the new value with</param>
/// <param name="val">The long to be inserted</param>
public void Add( string key, long val )
{
Add( key, new GatsInteger( val ) );
}
/// <summary>
/// Helper function to add a new GatsFloat to the dictionary.
/// </summary>
/// <remarks>
/// Implicit upcasting should allow floats and doubles to both be
/// accepted by this method. Please note that decimal types are not
/// strictly compatible, please see GatsFloat for more details.
/// </remarks>
/// <param name="key">The key to insert the new value with</param>
/// <param name="val">The double to be inserted</param>
public void Add( string key, double val )
{
Add( key, new GatsFloat( val ) );
}
/// <summary>
/// Helper function to add a new GatsBoolean to the dictionary.
/// </summary>
/// <param name="key">The key to insert the new value with</param>
/// <param name="val">The boolean value to be inserted</param>
public void Add( string key, bool val )
{
Add( key, new GatsBoolean( val ) );
}
/// <summary>
/// Helper function to add a new GatsNull to the dictionary.
/// </summary>
/// <param name="key">The key to insert the new null value with</param>
public void AddNull( string key )
{
Add( key, new GatsNull() );
}
/// <summary>
/// Helper function to add a new GatsDictionary to the dictionary.
/// </summary>
/// <remarks>
/// A new GatsDictionary is created, added to the dictionary, and
/// returned.
/// </remarks>
/// <param name="key">The key to insert the new value with</param>
/// <returns>A new, empty dictionary that is already added</returns>
public GatsDictionary AddDict( string key )
{
GatsDictionary dict = new GatsDictionary();
Add( key, dict );
return dict;
}
/// <summary>
/// Helper function to add a new GatsList to the dictionary.
/// </summary>
/// <remarks>
/// A new GatsList is created, added to the dictionary, and returned.
/// </remarks>
/// <param name="key">The key to insert the new value with</param>
/// <returns>A new, empty list that is already added</returns>
public GatsList AddList( string key )
{
GatsList list = new GatsList();
Add( key, list );
return list;
}
//
// List interface overrides under here.
//
public void Add( string key, GatsObject obj )
{
Value.Add( key, obj );
}
public bool Remove( string key )
{
return Value.Remove( key );
}
public bool ContainsKey( string key )
{
return Value.ContainsKey( key );
}
public bool TryGetValue( string key, out GatsObject obj )
{
return Value.TryGetValue( key, out obj );
}
public GatsObject this[string key]
{
get { return this.Value[key]; }
set { this.Value[key] = value; }
}
public ICollection<string> Keys
{
get { return this.Value.Keys; }
}
public ICollection<GatsObject> Values
{
get { return this.Value.Values; }
}
public int Count
{
get { return this.Value.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public void Add( KeyValuePair<string,GatsObject> pair )
{
((IDictionary<string,GatsObject>)Value).Add( pair );
}
public void Clear()
{
Value.Clear();
}
public GatsDictionary GetDict( string Key )
{
return this[Key] as GatsDictionary;
}
public GatsList GetList( string Key )
{
return this[Key] as GatsList;
}
public long GetInt( string Key )
{
return (this[Key] as GatsInteger).Value;
}
public bool GetBool( string Key )
{
return (this[Key] as GatsBoolean).Value;
}
public double GetFloat( string Key )
{
return (this[Key] as GatsFloat).Value;
}
public byte[] GetString( string Key )
{
return (this[Key] as GatsString).Value;
}
public string GetUtf8String( string Key )
{
return (this[Key] as GatsString).ToString();
}
public bool Contains( KeyValuePair<string,GatsObject> pair )
{
return ((IDictionary<string,GatsObject>)Value).Contains( pair );
}
public void CopyTo( KeyValuePair<string,GatsObject>[] result,
int count )
{
((IDictionary<string,GatsObject>)Value).CopyTo( result, count );
}
public bool Remove( KeyValuePair<string,GatsObject> pair )
{
return ((IDictionary<string,GatsObject>)Value).Remove( pair );
}
IEnumerator<KeyValuePair<string,GatsObject> > IEnumerable<KeyValuePair<string,GatsObject> >.GetEnumerator()
{
return Value.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Value.GetEnumerator();
}
}
}
|