File: AliasText.cs

package info (click to toggle)
nini 1.1.0%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,776 kB
  • ctags: 882
  • sloc: cs: 7,649; xml: 2,945; makefile: 62; ansic: 7
file content (148 lines) | stat: -rw-r--r-- 3,901 bytes parent folder | download | duplicates (7)
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
#region Copyright
//
// Nini Configuration Project.
// Copyright (C) 2006 Brent R. Matzelle.  All rights reserved.
//
// This software is published under the terms of the MIT X11 license, a copy of 
// which has been included with this distribution in the LICENSE.txt file.
// 
#endregion

using System;
using System.Collections;

namespace Nini.Config
{
	/// <include file='AliasText.xml' path='//Class[@name="AliasText"]/docs/*' />
	public class AliasText
	{
		#region Private variables
		Hashtable intAlias = null;
		Hashtable booleanAlias = null;
		#endregion

		#region Constructors
		/// <include file='AliasText.xml' path='//Constructor[@name="AliasText"]/docs/*' />
		public AliasText ()
		{
			intAlias = InsensitiveHashtable ();
			booleanAlias = InsensitiveHashtable ();
			DefaultAliasLoad ();
		}
		#endregion
		
		#region Public methods
		/// <include file='AliasText.xml' path='//Method[@name="AddAliasInt"]/docs/*' />
		public void AddAlias (string key, string alias, int value)
		{
			if (intAlias.Contains (key)) {
				Hashtable keys = (Hashtable)intAlias[key];
				
				keys[alias] = value;
			} else {
				Hashtable keys = InsensitiveHashtable ();
				keys[alias] = value;
				intAlias.Add (key, keys);
			}
		}
		
		/// <include file='AliasText.xml' path='//Method[@name="AddAliasBoolean"]/docs/*' />
		public void AddAlias (string alias, bool value)
		{
			booleanAlias[alias] = value;
		}
		
#if (NET_COMPACT_1_0)
#else
		/// <include file='AliasText.xml' path='//Method[@name="AddAliasEnum"]/docs/*' />
		public void AddAlias (string key, Enum enumAlias)
		{
			SetAliasTypes (key, enumAlias);
		}
#endif
		
		/// <include file='AliasText.xml' path='//Method[@name="ContainsBoolean"]/docs/*' />
		public bool ContainsBoolean (string key)
		{
			return booleanAlias.Contains (key);
		}
		
		/// <include file='AliasText.xml' path='//Method[@name="ContainsInt"]/docs/*' />
		public bool ContainsInt (string key, string alias)
		{
			bool result = false;

			if (intAlias.Contains (key)) {
				Hashtable keys = (Hashtable)intAlias[key];
				result = (keys.Contains (alias));
			}
			
			return result;
		}
		
		/// <include file='AliasText.xml' path='//Method[@name="GetBoolean"]/docs/*' />
		public bool GetBoolean (string key)
		{
			if (!booleanAlias.Contains (key)) {
				throw new ArgumentException ("Alias does not exist for text");
			}
			
			return (bool)booleanAlias[key];
		}
		
		/// <include file='AliasText.xml' path='//Method[@name="GetInt"]/docs/*' />
		public int GetInt (string key, string alias)
		{
			if (!intAlias.Contains (key)) {
				throw new ArgumentException ("Alias does not exist for key");
			}

			Hashtable keys = (Hashtable)intAlias[key];

			if (!keys.Contains (alias)) {
				throw new ArgumentException ("Config value does not match a " +
											 "supplied alias");
			}
			
			return (int)keys[alias];
		}
		#endregion
		
		#region Private methods
		/// <summary>
		/// Loads the default alias values.
		/// </summary>
		private void DefaultAliasLoad ()
		{
			AddAlias("true", true);
			AddAlias("false", false);
		}

#if (NET_COMPACT_1_0)
#else
		/// <summary>
		/// Extracts and sets the alias types from an enumeration.
		/// </summary>
		private void SetAliasTypes (string key, Enum enumAlias)
		{
			string[] names = Enum.GetNames (enumAlias.GetType ());
			int[] values = (int[])Enum.GetValues (enumAlias.GetType ());
			
			for (int i = 0; i < names.Length; i++)
			{
				AddAlias (key, names[i], values[i]);
			}
		}
#endif
		
		/// <summary>
		/// Returns a case insensitive hashtable.
		/// </summary>
		private Hashtable InsensitiveHashtable ()
		{
			return new Hashtable (CaseInsensitiveHashCodeProvider.Default, 
								  CaseInsensitiveComparer.Default);
		}
		#endregion
	}
}