aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src/gatsdictionary.cs
blob: af7f72a48af9f4cee07f4b9b8a67100d0d08c30f (plain)
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
/*
 * Copyright (C) 2007-2012 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
{
    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' );
        }

        //
        // 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; } // return this.Value.IsReadOnly; }
        }

        public void Add( KeyValuePair<string,GatsObject> pair )
        {
            ((IDictionary<string,GatsObject>)Value).Add( pair );
        }

        public void Clear()
        {
            Value.Clear();
        }

        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();
        }
    }
}