File: GraphDefaultAttributes.cs

package info (click to toggle)
graphviz 2.26.3-5%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 63,032 kB
  • ctags: 25,930
  • sloc: ansic: 212,134; sh: 20,316; cpp: 7,239; makefile: 4,211; yacc: 3,335; xml: 2,450; tcl: 1,900; cs: 1,890; objc: 1,149; perl: 829; lex: 363; awk: 171; python: 41; ruby: 35; php: 26
file content (254 lines) | stat: -rwxr-xr-x 7,598 bytes parent folder | download | duplicates (3)
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
/* $Id: GraphDefaultAttributes.cs,v 1.3 2009/06/03 01:10:59 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
*      This software is part of the graphviz package      *
*                http://www.graphviz.org/                 *
*                                                         *
*            Copyright (c) 1994-2008 AT&T Corp.           *
*                and is licensed under the                *
*            Common Public License, Version 1.0           *
*                      by AT&T Corp.                      *
*                                                         *
*        Information and Software Systems Research        *
*              AT&T Research, Florham Park NJ             *
**********************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Graphviz
{
	public class GraphDefaultAttributes : IDictionary<string, string>
	{
		public GraphDefaultAttributes(Graph graph, IntPtr proto)
		{
			_graph = graph;
			_proto = proto;
		}
		
		void IDictionary<string, string>.Add(string key, string value)
		{
			SetValue(key, value);
		}

		bool IDictionary<string, string>.ContainsKey(string key)
		{
			return !string.IsNullOrEmpty(GetValue(key));
		}

		ICollection<string> IDictionary<string, string>.Keys
		{
			get
			{
				/* return all keys with non-null, non-empty values */
				List<string> keys = new List<string>();
				IEnumerator<Agsym_t> attributes = EnumAttributes();
				while (attributes.MoveNext())
					if (attributes.Current.value != IntPtr.Zero && Marshal.ReadByte(attributes.Current.value) != 0)
						keys.Add(MarshalBytes(attributes.Current.name));
				return keys;
			}
		}

		bool IDictionary<string, string>.Remove(string key)
		{
			/* if key exists, overwrite with empty */
			if (agfindattr(_proto, key) != IntPtr.Zero) {
				SetValue(key, string.Empty);
				return true;
			}
			else
				return false;
		}

		bool IDictionary<string, string>.TryGetValue(string key, out string value)
		{
			/* grab value for key, but if it's empty return null instead */
			value = GetValue(key);
			if (value == string.Empty) {
				value = null;
				return false;
			}
			else
				return value != null;
		}

		ICollection<string> IDictionary<string, string>.Values
		{
			get
			{
				/* return all non-null, non-empty values */
				List<string> values = new List<string>();
				IEnumerator<Agsym_t> attributes = EnumAttributes();
				while (attributes.MoveNext()) {
					string attrValue = MarshalBytes(attributes.Current.value);
					if (!string.IsNullOrEmpty(attrValue))
						values.Add(attrValue);
				}
				return values;
			}
		}

		string IDictionary<string, string>.this[string key]
		{
			get
			{
				/* return non-null, non-empty value */
				string value = GetValue(key);
				if (string.IsNullOrEmpty(value))
					throw new KeyNotFoundException();
				else
					return value;
			}
			set
			{
				SetValue(key, value);
			}
		}

		void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item)
		{
			SetValue(item.Key, item.Value);
		}

		void ICollection<KeyValuePair<string, string>>.Clear()
		{
			IEnumerator<Agsym_t> attributes = EnumAttributes();
			while (attributes.MoveNext())
				SetValue(MarshalBytes(attributes.Current.name), string.Empty);
		}

		bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item)
		{
			return GetValue(item.Key) == item.Value;
		}

		void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		int ICollection<KeyValuePair<string, string>>.Count
		{
			get
			{
				/* count all non-null, non-empty values */
				int count = 0;
				IEnumerator<Agsym_t> attributes = EnumAttributes();
				while (attributes.MoveNext())
					if (attributes.Current.value != IntPtr.Zero && Marshal.ReadByte(attributes.Current.value) != 0)
						++count;
				return count;
			}
		}

		bool ICollection<KeyValuePair<string, string>>.IsReadOnly
		{
			get { return false; }
		}

		bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item)
		{
			/* if key + value are present, overwrite value with empty */
			if (GetValue(item.Key) == item.Value) {
				SetValue(item.Key, string.Empty);
				return true;
			}
			else
				return false;
		} 

		IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
		{
			/* return all keys with values that are non-null and non-empty */
			IEnumerator<Agsym_t> attributes = EnumAttributes();
			while (attributes.MoveNext()) {
				string attrValue = MarshalBytes(attributes.Current.value);
				if (!string.IsNullOrEmpty(attrValue))
					yield return new KeyValuePair<string, string>(
						MarshalBytes(attributes.Current.value),
						attrValue);
			}
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			/* return all keys with values that are non-null and non-empty */
			IEnumerator<Agsym_t> attributes = EnumAttributes();
			while (attributes.MoveNext()) {
				string attrValue = MarshalBytes(attributes.Current.value);
				if (!string.IsNullOrEmpty(attrValue))
					yield return new KeyValuePair<string, string>(
						MarshalBytes(attributes.Current.value),
						attrValue);
			}
		}
		
		private string GetValue(string key)
		{
			/* try to find the value for the key */
			IntPtr attrPtr = agfindattr(_proto, key);
			if (attrPtr != IntPtr.Zero) {
				Agsym_t attr = (Agsym_t)Marshal.PtrToStructure(attrPtr, typeof(Agsym_t));
				return MarshalBytes(attr.value);
			}
			else
				return null;
		}
		
		private void SetValue(string key, string value)
		{
			/* change the attribute value and notify the parent graph */
			agattr(_proto, key, value);
			_graph.NoteChanged(true);
		}

		private IEnumerator<Agsym_t> EnumAttributes()
		{
			/* go through each attribute symbol */
			for (IntPtr nextAttrPtr = agfstattr(_proto); nextAttrPtr != IntPtr.Zero; nextAttrPtr = agnxtattr(_proto, nextAttrPtr))
				yield return (Agsym_t)Marshal.PtrToStructure(nextAttrPtr, typeof(Agsym_t));
		}
		
		private static string MarshalBytes(IntPtr bytes)
		{
			return (string)UTF8Marshaler.GetInstance(null).MarshalNativeToManaged(bytes);
		}
		
		[DllImport("libgraph-4.dll")]
		private static extern IntPtr agattr(
			IntPtr obj,
			[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))] string name,
			[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))] string value);
		
		[DllImport("libgraph-4.dll")]
		private static extern IntPtr agfindattr(
			IntPtr obj,
			[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))] string name);
		
		[DllImport("libgraph-4.dll")]
		private static extern IntPtr agfstattr(IntPtr obj);
		
		[DllImport("libgraph-4.dll")]
		private static extern IntPtr agnxtattr(IntPtr obj, IntPtr attr);

		[StructLayout(LayoutKind.Sequential)]
		private struct Agsym_t
		{
			public IntPtr name;
			public IntPtr value;
			
			public int index;
			public byte printed;
			public byte fix;
		};

		private readonly Graph _graph;
		private readonly IntPtr _proto;
		
		
	}
}