Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for countcharpairs.cs

(view source code of countcharpairs.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class CountCharPairs
  11. 	{
  12. 		static string progver = "1.00";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			if ( args.Length > 1 )
  18. 			{
  19. 				int rc = 0;
  20. 				string file = String.Empty;
  21. 				bool fileset = false;
  22. 				List<string> testchars = new List<string>( );
  23. 				string[] allowed = { "[]", "][", "()", ")(", "{}", "}{", "<>", "><" };
  24.  
  25. 				foreach ( string arg in args )
  26. 				{
  27. 					if ( arg.Length == 2 && allowed.Contains( arg ) )
  28. 					{
  29. 						testchars.Add( arg );
  30. 					}
  31. 					else if ( !fileset && File.Exists( arg ) )
  32. 					{
  33. 						file = arg;
  34. 						fileset = true;
  35. 					}
  36. 					else
  37. 					{
  38. 						return ShowHelp( );
  39. 					}
  40. 				}
  41. 				if ( testchars.Count == 0 )
  42. 				{
  43. 					return ShowHelp( );
  44. 				}
  45. 				StreamReader sr = new StreamReader( file );
  46. 				string text = sr.ReadToEnd( );
  47. 				sr.Close( );
  48. 				Regex regex;
  49. 				foreach ( string testpair in testchars )
  50. 				{
  51. 					regex = new Regex( Regex.Escape( testpair[0].ToString( ) ) );
  52. 					int openchars = regex.Matches( text ).Count;
  53. 					regex = new Regex( Regex.Escape( testpair[1].ToString( ) ) );
  54. 					int closechars = regex.Matches( text ).Count;
  55. 					rc += Math.Abs( openchars - closechars );
  56. 					if ( openchars == closechars )
  57. 					{
  58. 						Console.WriteLine( "{0}\tOK\t({1} pairs)", testpair, openchars );
  59. 					}
  60. 					else
  61. 					{
  62. 						Console.ForegroundColor = ConsoleColor.Red;
  63. 						Console.WriteLine( "{0}\tERROR\t{1} x {2}, {3} x {4}", testpair, openchars, testpair[0], closechars, testpair[1] );
  64. 						Console.ResetColor( );
  65. 					}
  66. 				}
  67. 				return rc;
  68. 			}
  69. 			else
  70. 			{
  71. 				return ShowHelp( );
  72. 			}
  73. 		}
  74.  
  75. 		static int ShowHelp( params string[] errmsg )
  76. 		{
  77. 			/*
  78. 			CountCharPairs.exe,  Version 1.00
  79. 			Count character pairs in source files, e.g. {} in a C# source file
  80.  
  81. 			Usage:    CountCharPairs  sourcefile charpair  [ charpair  [ ... ] ]
  82.  
  83. 			Where:    sourcefile      is the source file to be investigated
  84. 			          charpair        is a pair of characters to search for in the
  85. 			                          source code (allowed: "[]", "()", "{}" and "<>")
  86.  
  87. 			The program will count the number of opening characters and the number of
  88. 			closing characters for each specified character pair.
  89. 			If the number of opening characters equals the number of closing characters,
  90. 			the program returns an "OK" message; if not, the numbers of occurences for
  91. 			each character will be shown.
  92. 			The program's return code equals the total number of mismatches, or 1 if
  93. 			this error screen is shown.
  94.  
  95. 			Written by Rob van der Woude
  96. 			http://www.robvanderwoude.com
  97. 			*/
  98.  
  99. 			if ( errmsg.Length > 0 )
  100. 			{
  101. 				List<string> errargs = new List<string>( errmsg );
  102. 				errargs.RemoveAt( 0 );
  103. 				Console.Error.WriteLine( );
  104. 				Console.ForegroundColor = ConsoleColor.Red;
  105. 				Console.Error.Write( "ERROR:\t" );
  106. 				Console.ForegroundColor = ConsoleColor.White;
  107. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  108. 				Console.ResetColor( );
  109. 			}
  110.  
  111. 			Console.Error.WriteLine( );
  112.  
  113. 			Console.Error.WriteLine( "CountCharPairs.exe,  Version {0}", progver );
  114.  
  115. 			Console.Error.WriteLine( "Count character pairs in source files, e.g. {} in a C# source file" );
  116.  
  117. 			Console.Error.WriteLine( );
  118.  
  119. 			Console.Error.Write( "Usage:    " );
  120. 			Console.ForegroundColor = ConsoleColor.White;
  121. 			Console.Error.WriteLine( "CountCharPairs  sourcefile charpair  [ charpair  [ ... ] ]" );
  122. 			Console.ResetColor( );
  123.  
  124. 			Console.Error.WriteLine( );
  125.  
  126. 			Console.Error.Write( "Where:    " );
  127. 			Console.ForegroundColor = ConsoleColor.White;
  128. 			Console.Error.Write( "sourcefile" );
  129. 			Console.ResetColor( );
  130. 			Console.Error.WriteLine( "      is the source file to be investigated" );
  131.  
  132. 			Console.ForegroundColor = ConsoleColor.White;
  133. 			Console.Error.Write( "          charpair" );
  134. 			Console.ResetColor( );
  135. 			Console.Error.WriteLine( "        is a pair of characters to search for in the" );
  136.  
  137. 			Console.Error.WriteLine( "                          source code (allowed: \"[]\", \"()\", \"{}\" and \"<>\")" );
  138.  
  139. 			Console.Error.WriteLine( );
  140.  
  141. 			Console.Error.WriteLine( "The program will count the number of opening characters and the number of" );
  142.  
  143. 			Console.Error.WriteLine( "closing characters for each specified character pair." );
  144.  
  145. 			Console.Error.WriteLine( "If the number of opening characters equals the number of closing characters," );
  146.  
  147. 			Console.Error.WriteLine( "the program returns an \"OK\" message; if not, the numbers of occurences for" );
  148.  
  149. 			Console.Error.WriteLine( "each character will be shown." );
  150.  
  151. 			Console.Error.WriteLine( "The program's return code equals the total number of mismatches, or 1 if" );
  152.  
  153. 			Console.Error.WriteLine( "this error screen is shown." );
  154.  
  155. 			Console.Error.WriteLine( );
  156.  
  157. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  158.  
  159. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  160.  
  161. 			return 1;
  162. 		}
  163.  
  164. 	}
  165. }
  166.  

page last modified: 2024-04-16; loaded in 0.0062 seconds