2002-01-07 Gaurav Vaish <gvaish@iitk.ac.in>
[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 baseDir = @"C:\WINNT\Microsoft.NET\Framework\v1.0.2914";
23                 private static readonly string[] assemblies = {
24                         "mscorlib",
25                         "System",
26                         "System.Drawing",
27                         "System.Web",
28                         "System.Security",
29                         "System.Windows.Forms",
30                         "System.XML",
31                         "System.Data",
32                         "System.Design",
33                         "System.Enterpriseservices",
34                         "System.Management",
35                         "System.Messaging",
36                         "System.Runtime.Remoting",
37                         "System.ServiceProcess",
38                         "System.Web.RegularExpressions",
39                         "System.Web.Services"
40                 };
41
42                 public EnumCheck(string className)
43                 {
44                         this.className = className;
45                 }
46
47                 public void Display()
48                 {
49                         LoadType();
50                         if(type == null || !type.IsEnum)
51                         {
52                                 System.Console.Write("-->Failed to load the enumeration: " + className);
53                                 return;
54                         }
55                         Array ar = Enum.GetValues(type);
56                         System.Console.WriteLine("-->Enumeration: {0}", type.ToString());
57                         for(int i=0; i < ar.Length; i++)
58                         {
59                                 Enum b = (Enum)ar.GetValue(i);
60                                 System.Console.Write(" {0}", Enum.Format(type, b, "G"));
61                                 System.Console.WriteLine(" ({0}) ", Enum.Format(type, b, "D"));
62                         }
63                 }
64
65                 private void LoadType()
66                 {
67                         type = null;
68                         foreach(string assemblyName in assemblies)
69                         {
70                                 string fileName = baseDir + "\\" + assemblyName + ".dll";
71                                 try
72                                 {
73                                         Assembly assembly = Assembly.Load(AssemblyName.GetAssemblyName(fileName));
74                                         foreach(Type t in assembly.GetTypes())
75                                         {
76                                                 if(!t.IsEnum)
77                                                         continue;
78                                                 string name = null;
79                                                 if(Type.GetType(className + "," + assemblyName) != null)
80                                                         name = Type.GetType(className + "," + assemblyName).ToString();
81                                                 if(name == t.ToString())
82                                                 {
83                                                         type = t;
84                                                         break;
85                                                 }
86                                         }
87                                 } catch(BadImageFormatException)
88                                 {
89                                 } catch(ReflectionTypeLoadException)
90                                 {
91                                 } catch(ArgumentException)
92                                 {
93                                 }
94                                 if(type != null)
95                                         return;
96                         }
97                 }
98
99                 public static void PrintUsage()
100                 {
101                         System.Console.WriteLine("Usage:");
102                         System.Console.WriteLine("EnumCheck [<enum> [<enum> [... ] ] ]");
103                         System.Console.WriteLine("");
104                         System.Console.WriteLine("enum := <namespace>[.<subnamespace>[...]].enum_name");
105                         System.Console.WriteLine("");
106                 }
107
108                 public static void Main(string[] args)
109                 {
110                         if(args.Length > 0 && (args[0] == "--help" || args[0] == "-h"))
111                         {
112                                 PrintUsage();
113                                 return;
114                         }
115                         EnumCheck check = null;
116                         if(args.Length != 0)
117                         {
118                                 foreach(string clName in args)
119                                 {
120                                         check = new EnumCheck(clName);
121                                         check.Display();
122                                         System.Console.WriteLine("\n");
123                                 }
124                         }
125                         while(true)
126                         {
127                                 System.Console.Write("Enter the name of the Enumeration (end to stop): ");
128                                 string clName = System.Console.ReadLine();
129                                 if(clName == "stop" || clName.Length == 0)
130                                         break;
131                                 check = new EnumCheck(clName);
132                                 check.Display();
133                                 System.Console.WriteLine("\n");
134                         }
135                 }
136         }
137 }