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