3 // Adam Treat (manyoso@yahoo.com)
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
13 using System.Text.RegularExpressions;
14 using System.Collections.Specialized;
21 StringCollection filebuffer;
22 bool linespace = true;
27 "monostyle -f file.cs -l <true|false> > output.cs\n\n" +
28 " -f || /-f || --file file.cs The csharp source file to parse.\n\n" +
29 " -l || /-l || --line <true|false> Specifies wether to use line spacing.\n\n");
32 public static void Main (string[] args)
34 MonoStyle style = new MonoStyle(args);
37 public MonoStyle (string[] args)
39 int argc = args.Length;
40 for(int i = 0; i < argc; i++) {
42 // The "/" switch is there for wine users, like me ;-)
43 if(arg.StartsWith("-") || arg.StartsWith("/")) {
45 case "-l": case "/-l": case "--line":
50 if (args[i++] == "false") {
54 case "-f": case "/-f": case "--file":
71 filebuffer = new StringCollection();
72 StreamReader sr = new StreamReader(file);
78 public void FillBuffer(StreamReader sr)
80 sr.BaseStream.Seek(0, SeekOrigin.Begin);
81 while (sr.Peek() > -1) {
82 filebuffer.Add(sr.ReadLine());
87 public void FixMonoStyle()
89 for (int i=0; i < filebuffer.Count; i++) {
90 IsBadMonoStyle(filebuffer[i]);
94 public void PrintToConsole()
96 for (int i=0; i < filebuffer.Count; i++) {
97 Console.WriteLine(filebuffer[i]);
101 public void IsBadMonoStyle(String str)
103 if (IsBadMonoType(str)) {
104 FixHangingBrace(str);
105 } else if(IsBadMonoFlow(str)) {
106 FixHangingBrace(str);
107 } else if(IsBadMonoFunction(str)) {
109 } else if(IsBadMonoProperty(str)) {
110 FixHangingBrace(str);
115 public void FixHangingBrace(String str)
117 int strloc = filebuffer.IndexOf(str);
118 int brcloc = FindHangingBrace(strloc);
119 int diff = brcloc - strloc;
121 for (int i = 0; i < diff+1; i++) {
122 filebuffer.RemoveAt(strloc);
124 filebuffer.Insert(strloc, str + " {");
126 filebuffer.Insert(strloc+1, "");
131 public int FindHangingBrace(int strloc)
137 string str = filebuffer[strloc++];
138 found = IsHangingBrace(str);
139 if (!found && !IsBlankLine(str)) {
142 } catch (Exception) {
149 public void FixEndBrace(String str)
151 int strloc = filebuffer.IndexOf(str);
152 filebuffer.RemoveAt(strloc);
153 filebuffer.Insert(strloc, RemoveEndBrace(str));
154 filebuffer.Insert(strloc+1, AddHangingBrace(str));
157 public static bool IsBadMonoType(String str)
159 if ( IsType(str) && !EndWithBrace(str)) {
166 public static bool IsBadMonoFlow(String str)
168 if (IsFlow(str) && !EndWithBrace(str)) {
175 public static bool IsBadMonoFunction(String str)
177 if (IsFunction(str) && EndWithBrace(str)) {
184 public static bool IsBadMonoProperty(String str)
186 if (IsProperty(str) && !EndWithBrace(str)) {
193 public static bool IsType(String str)
195 if ( !IsComment(str) && (
206 public static bool IsFlow(String str)
208 if ( !IsComment(str) && (
226 public static bool IsFunction(String str)
228 if ( Regex.IsMatch(str, @"^\s*(\w+)\s+(\w+).*\(+") &&
229 !IsDeclaration(str) &&
239 public static bool IsProperty(String str)
241 if ( Regex.IsMatch(str, @"^\s*(\w+)\s+(\w+).*") &&
242 !IsDeclaration(str) &&
253 public static string RemoveEndBrace(String str)
255 Regex rg = new Regex(@"\{\s*$");
256 return rg.Replace(str, "");
259 public static string AddHangingBrace(String str)
261 Regex rg = new Regex(@"\S+\s*");
262 string blank = rg.Replace(str,"");
266 public static bool IsDeclaration(String str)
268 return Regex.IsMatch(str, @"\;\s*$");
271 public static bool IsComment(String str)
273 return Regex.IsMatch(str, @"^(\s*\/+|\s*\*+|\s*\#+)");
276 public static bool EndWithBrace(String str)
278 return Regex.IsMatch(str, @"\{\s*$");
281 public static bool IsHangingBrace(String str)
283 return Regex.IsMatch(str, @"(^|\s+)\{");
286 public static bool IsBlankLine(String str)
288 return Regex.IsMatch(str, @"^\s*$");
291 public static bool IsNameSpace(String str)
293 return Regex.IsMatch(str, @"(^|\s+)namespace\s+");
296 public static bool IsClass(String str)
298 return Regex.IsMatch(str, @"\s+class\s+");
301 public static bool IsStruct(String str)
303 return Regex.IsMatch(str, @"\s+struct\s+");
306 public static bool IsEnum(String str)
308 return Regex.IsMatch(str, @"\s+enum\s+");
311 public static bool IsIf(String str)
313 return Regex.IsMatch(str, @"(^|\s+|\}+)if(\s+|\(+|$)");
316 public static bool IsElse(String str)
318 return Regex.IsMatch(str, @"(^|\s+|\}+)else(\s+|\{+|$)");
321 public static bool IsElseIf(String str)
323 return Regex.IsMatch(str, @"(^|\s+|\}+)else if(\s+|\(+|$)");
326 public static bool IsTry(String str)
328 return Regex.IsMatch(str, @"(^|\s+|\}+)try(\s+|\(+|$)");
331 public static bool IsCatch(String str)
333 return Regex.IsMatch(str, @"(^|\s+|\}+)catch(\s+|\(+|$)");
336 public static bool IsFinally(String str)
338 return Regex.IsMatch(str, @"(^|\s+|\}+)finally(\s+|\{+|$)");
341 public static bool IsFor(String str)
343 return Regex.IsMatch(str, @"(^|\s+|\}+)for(\s+|\(+|$)");
346 public static bool IsForEach(String str)
348 return Regex.IsMatch(str, @"(^|\s+|\}+)foreach(\s+|\(+|$)");
351 public static bool IsWhile(String str)
353 return Regex.IsMatch(str, @"(^|\s+|\}+)while(\s+|\(+|$)");
356 public static bool IsSwitch(String str)
358 return Regex.IsMatch(str, @"(^|\s+|\}+)switch(\s+|\(+|$)");
361 public static bool IsCase(String str)
363 return Regex.IsMatch(str, @"(^|\s+|\}+)case(\s+|\(+|$)");