update version number
[mcs.git] / tools / misc / GenerateDelegate.cs
blob0ba8e78c0a010580ac9eec5b2c8bcfd86945ca2b
1 /**
2 * Namespace: com.mastergaurav.utils
3 * Class: GenerateDelegate
5 * Author: Gaurav Vaish
6 * Maintainer: gvaish@iitk.ac.in
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
8 * Implementation: yes
9 * Status: 100%
11 * (C) Gaurav Vaish (2001)
14 using System;
15 using System.IO;
17 namespace com.mastergaurav.Utils
19 public class GenerateDelegate
21 public static string TargetDirectory = String.Empty;
22 public static string NamespaceName = String.Empty;
24 public static readonly string PROLOGUE = "/**\n * Namespace: ";
25 public static readonly string DETAILS = " *\n * Author: Gaurav Vaish\n" +
26 " * Maintainer: gvaish@iitk.ac.in\n" +
27 " * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\n" +
28 " * Implementation: yes\n" +
29 " * Status: 100%\n" +
30 " *\n" +
31 " * (C) Gaurav Vaish (2002)\n" +
32 " */\n\n";
33 public static readonly string USING = "using System;\n" +
34 "using System.Web;\n" +
35 "using System.Web.UI;\n\n";
37 public static readonly string NAMESPACE = "namespace ";
39 public static string AskForNamespace()
41 string nm = String.Empty;
42 System.Console.Write("Enter the name of the namespace: ");
43 nm = System.Console.ReadLine();
44 return nm;
47 public static string AskForMore()
49 string del = String.Empty;
50 System.Console.Write("Enter the name of the delegate (end to stop): ");
51 del = System.Console.ReadLine();
52 if(del == String.Empty || del == "end")
53 return String.Empty;
54 return del;
57 public static void Generate(string delName)
59 string fileName = TargetDirectory + "\\" + delName + "EventHandler.cs";
60 System.Console.Write("File: ");//, fileName);
61 System.Console.Write(fileName);
62 System.Console.Write("\tGenerating");
64 StreamWriter writer;
65 try
67 Stream stream = new FileStream(fileName, FileMode.Truncate, FileAccess.Write);
68 writer = new StreamWriter(stream);
69 } catch(FileNotFoundException)
71 writer = File.CreateText(fileName);
73 if(writer == null)
75 System.Console.WriteLine("Null writer...\n");
76 return;
78 writer.Write(PROLOGUE);
79 writer.Write(NamespaceName + "\n");
80 writer.Write(DETAILS);
81 writer.Write(NAMESPACE);
82 writer.Write(NamespaceName + "\n");
83 writer.Write("{\n");
84 writer.Write("\tpublic delegate void ");
85 writer.Write(delName);
86 writer.Write("EventHandler(object sender, ");
87 writer.Write(delName);
88 writer.Write("EventArgs e);\n");
89 writer.Write("}");
91 writer.Flush();
92 writer.Close();
94 System.Console.WriteLine("\tGenerated\n");
97 public static string GetTargetDir()
99 System.Console.Write("Enter target directory: ");
100 return System.Console.ReadLine();
103 public static void Usage(bool wrong)
105 if(wrong)
107 System.Console.WriteLine("Wrong # arguments.");
109 System.Console.WriteLine("Usage: GenerateDelegate [target-dir] [namespace] [delegate1 [delegate2 [...]]]");
112 public static bool IsHelp(string arg)
114 return (arg == "-h" || arg == "--help");
117 public static bool IsDirectory(string dirName)
119 FileAttributes attrs;
122 attrs = File.GetAttributes(dirName);
123 if( (attrs & FileAttributes.Directory) != FileAttributes.Directory)
125 Usage(true);
126 return false;
128 }catch(Exception e)
130 System.Console.WriteLine("Exception: {0}", e.ToString());
131 return false;
133 return true;
136 public static void Main(string[] args)
138 if(args.Length == 1 && IsHelp(args[0]))
140 Usage(false);
141 return;
144 if(args.Length == 0)
146 TargetDirectory = GetTargetDir();
147 while(TargetDirectory.EndsWith("\\"))
149 TargetDirectory = TargetDirectory.Substring(0, TargetDirectory.Length - 1);
151 } else
153 while(args[0].EndsWith("\\"))
155 args[0] = args[0].Substring(0, args[0].Length - 1);
157 TargetDirectory = args[0];
160 if(!IsDirectory(TargetDirectory))
161 return;
163 if(args.Length > 1)
165 NamespaceName = args[1];
166 } else
168 NamespaceName = AskForNamespace();
171 if(args.Length > 2)
173 int i=0;
174 foreach(string currArg in args)
176 if(i != 0)
178 Generate(currArg);
180 i++;
183 string delegateName = String.Empty;
184 while((delegateName = AskForMore()) != String.Empty)
186 Generate(delegateName);