2002-1-18 Duncan Mak <duncan@192.168.10-120.masq>
[mono.git] / mcs / tools / EnumCheck.cs
1 /**
2  * Namespace: System.Web
3  * Class:     EnumCheck
4  *
5  * Author:  Gaurav Vaish
6  * Contact: <gvaish@iitk.ac.in>
7  * Status:  100%
8  *
9  * (C) Gaurav Vaish (2002)
10  */
11
12 using System;
13 using System.Reflection;
14
15 namespace Mono.Enumerations
16 {
17         public class EnumCheck
18         {
19                 private string className;
20                 private Type   type;
21
22                 private static readonly string[] assemblies = {
23                         "mscorlib",
24                         "System",
25                         "System.Drawing",
26                         "System.Web",
27                         "System.Security",
28                         "System.Windows.Forms",
29                         "System.XML",
30                         "System.Data",
31                         "System.Design",
32                         "System.Enterpriseservices",
33                         "System.Management",
34                         "System.Messaging",
35                         "System.Runtime.Remoting",
36                         "System.ServiceProcess",
37                         "System.Web.RegularExpressions",
38                         "System.Web.Services"
39                 };
40
41                 public EnumCheck(string className)
42                 {
43                         this.className = className;
44                 }
45
46                 public void Display()
47                 {
48                         LoadType();
49                         if(type == null || !type.IsEnum)
50                         {
51                                 System.Console.Write("-->Failed to load the enumeration: " + className);
52                                 return;
53                         }
54                         Array ar = Enum.GetValues(type);
55                         System.Console.WriteLine("-->Enumeration: {0}", type.ToString());
56                         for(int i=0; i < ar.Length; i++)
57                         {
58                                 Enum b = (Enum)ar.GetValue(i);
59                                 System.Console.Write(" {0}", Enum.Format(type, b, "G"));
60                                 System.Console.WriteLine(" ({0}) ", Enum.Format(type, b, "D"));
61                         }
62                 }
63
64                 private void LoadType()
65                 {
66                         type = null;
67                         foreach(string assemblyName in assemblies)
68                         {
69                                 try
70                                 {
71                                         Assembly assembly = Assembly.Load(assemblyName + ".dll");
72                                         foreach(Type t in assembly.GetTypes())
73                                         {
74                                                 if(!t.IsEnum)
75                                                         continue;
76                                                 string name = null;
77                                                 if(Type.GetType(className + "," + assemblyName) != null)
78                                                         name = Type.GetType(className + "," + assemblyName).ToString();
79                                                 if(name == t.ToString())
80                                                 {
81                                                         type = t;
82                                                         break;
83                                                 }
84                                         }
85                                 } catch(BadImageFormatException)
86                                 {
87                                 } catch(ReflectionTypeLoadException)
88                                 {
89                                 } catch(ArgumentException)
90                                 {
91                                 }
92                                 if(type != null)
93                                         return;
94                         }
95                 }
96
97                 public static void PrintUsage()
98                 {
99                         System.Console.WriteLine("Usage:");
100                         System.Console.WriteLine("EnumCheck [<enum> [<enum> [... ] ] ]");
101                         System.Console.WriteLine("");
102                         System.Console.WriteLine("enum := <namespace>[.<subnamespace>[...]].enum_name");
103                         System.Console.WriteLine("");
104                 }
105
106                 public static void Main(string[] args)
107                 {
108                         if(args.Length > 0 && (args[0] == "--help" || args[0] == "-h"))
109                         {
110                                 PrintUsage();
111                                 return;
112                         }
113                         EnumCheck check = null;
114                         if(args.Length != 0)
115                         {
116                                 foreach(string clName in args)
117                                 {
118                                         check = new EnumCheck(clName);
119                                         check.Display();
120                                         System.Console.WriteLine("\n");
121                                 }
122                         }
123                         while(true)
124                         {
125                                 System.Console.Write("Enter the name of the Enumeration (end to stop): ");
126                                 string clName = System.Console.ReadLine();
127                                 if(clName == "stop" || clName.Length == 0)
128                                         break;
129                                 check = new EnumCheck(clName);
130                                 check.Display();
131                                 System.Console.WriteLine("\n");
132                         }
133                 }
134         }
135 }