Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EnumCheck.cs « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c564cc79564e77a17bb36c6a33a3f410aa60d62 (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
/**
 * Namespace: System.Web
 * Class:     EnumCheck
 *
 * Author:  Gaurav Vaish
 * Contact: <gvaish@iitk.ac.in>
 * Status:  100%
 *
 * (C) Gaurav Vaish (2002)
 */

using System;
using System.Xml;
using System.Collections;
using System.Reflection;

namespace Mono.Enumerations
{
	public class EnumCheck
	{
		private string className;
		private Type   type;
		private EnumCheckAssemblyCollection ecac = new EnumCheckAssemblyCollection();
		
		public static string confFile = "assemblies.xml";

		public EnumCheck(string className)
		{
			this.className = className;
			ecac.Parse();
		}

		public void Display()
		{
			ecac.ConfigFile = confFile;
			LoadType();
			if(type == null || !type.IsEnum)
			{
				System.Console.Write("-->Failed to load the enumeration: " + className);
				return;
			}
			Array ar = Enum.GetValues(type);
			System.Console.WriteLine("-->Enumeration: {0}", type.ToString());
			for(int i=0; i < ar.Length; i++)
			{
				Enum b = (Enum)ar.GetValue(i);
				System.Console.Write(" {0}", Enum.Format(type, b, "G"));
				System.Console.WriteLine(" ({0}) ", Enum.Format(type, b, "D"));
			}
		}

		private void LoadType()
		{
			type = null;
			foreach(string url in ecac)
			{
				try
				{
					Assembly assembly = Assembly.LoadFrom(url);
					foreach(Type t in assembly.GetTypes())
					{
						if(!t.IsEnum)
							continue;
						if(className == t.ToString())
						{
							type = t;
							break;
						}
					}
				} catch(BadImageFormatException)
				{
				} catch(ReflectionTypeLoadException)
				{
				} catch(ArgumentException)
				{
				}
				if(type != null)
					return;
			}
		}

		public static void PrintUsage()
		{
			System.Console.WriteLine("Usage:");
			System.Console.WriteLine("EnumCheck [<enum> [<enum> [... ] ] ]");
			System.Console.WriteLine("");
			System.Console.WriteLine("enum := <namespace>[.<subnamespace>[...]].enum_name");
			System.Console.WriteLine("");
		}

		public static void Main(string[] args)
		{
			if(args.Length > 0 && (args[0] == "--help" || args[0] == "-h"))
			{
				PrintUsage();
				return;
			}
			EnumCheck check = null;
			string bdir;
			System.Console.Write("Enter assembly configuration file [{0}]:", confFile);
			//System.Console.Write("[{0}]: ", confFile);
			bdir = System.Console.ReadLine();
			while(bdir.EndsWith("/") || bdir.EndsWith("\\"))
			{
				bdir = bdir.Substring(0, bdir.Length - 1);
			}
			if(bdir != "")
			{
				confFile = bdir;
			}
			if(args.Length != 0)
			{
				foreach(string clName in args)
				{
					check = new EnumCheck(clName);
					check.Display();
					System.Console.WriteLine("\n");
				}
			}
			while(true)
			{
				System.Console.Write("Enter the name of the Enumeration (end to stop): ");
				string clName = System.Console.ReadLine();
				if(clName == "stop" || clName == "end" || clName.Length == 0)
					break;
				check = new EnumCheck(clName);
				check.Display();
				System.Console.WriteLine("\n");
			}
		}
	}
}