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/gatsdictionary.cs | |
| 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/gatsdictionary.cs')
| -rw-r--r-- | cs-dotnet/src/gatsdictionary.cs | 149 |
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 @@ | |||
| 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 | |||
