Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for masktext.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class MaskText
  11. 	{
  12. 		static string progver = "1.00";
  13. 		static bool oddrow = true;
  14.  
  15.  
  16. 		static int Main( string[] args )
  17. 		{
  18. 			string text = String.Empty;
  19. 			string mask = String.Empty;
  20.  
  21. 			#region Command Line Arguments
  22.  
  23. 			if ( args.Length == 0 )
  24. 			{
  25. 				return ShowHelp( );
  26. 			}
  27. 			if ( args.Length > 0 )
  28. 			{
  29. 				if ( args[0] == "/?" )
  30. 				{
  31. 					return ShowHelp( );
  32. 				}
  33. 				if ( args[0].ToUpper( ) == "/M" )
  34. 				{
  35. 					return ShowMaskHelp( );
  36. 				}
  37. 				mask = args[0];
  38. 			}
  39. 			if ( args.Length > 1 )
  40. 			{
  41. 				if ( args[1] == "/?" )
  42. 				{
  43. 					return ShowHelp( );
  44. 				}
  45. 				if ( args[1].ToUpper( ) == "/M" )
  46. 				{
  47. 					return ShowMaskHelp( );
  48. 				}
  49. 				text = args[1];
  50. 			}
  51. 			if ( args.Length > 2 )
  52. 			{
  53. 				return ShowHelp( );
  54. 			}
  55.  
  56. 			#endregion Command Line Arguments
  57.  
  58.  
  59. 			if ( String.IsNullOrWhiteSpace( text ) )
  60. 			{
  61. 				if ( ConsoleEx.InputRedirected )
  62. 				{
  63. 					text = Console.In.ReadLine( );
  64. 				}
  65. 				else
  66. 				{
  67. 					text = Console.ReadLine( );
  68. 				}
  69. 			}
  70. 			try
  71. 			{
  72. 				MaskedTextBox textbox = new MaskedTextBox( mask );
  73. 				textbox.Text = text;
  74. 				Console.Write( textbox.Text );
  75. 				if ( textbox.MaskCompleted )
  76. 				{
  77. 					return 0;
  78. 				}
  79. 				else
  80. 				{
  81. 					return 1;
  82. 				}
  83. 			}
  84. 			catch ( Exception e )
  85. 			{
  86. 				return ShowHelp( e.Message );
  87. 			}
  88. 		}
  89.  
  90.  
  91. 		static int ShowHelp( params string[] errmsg )
  92. 		{
  93. 			#region Error Message
  94.  
  95. 			if ( errmsg.Length > 0 )
  96. 			{
  97. 				List<string> errargs = new List<string>( errmsg );
  98. 				errargs.RemoveAt( 0 );
  99. 				Console.Error.WriteLine( );
  100. 				Console.ForegroundColor = ConsoleColor.Red;
  101. 				Console.Error.Write( "ERROR:\t" );
  102. 				Console.ForegroundColor = ConsoleColor.White;
  103. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  104. 				Console.ResetColor( );
  105. 			}
  106.  
  107. 			#endregion Error Message
  108.  
  109. 			/*
  110. 			Input.exe,  Version 1.00
  111. 			Apply a mask to input text
  112.  
  113. 			Usage:   MaskText.exe  mask  [ text ]
  114.  
  115. 			   or:   command | MaskText.exe  mask
  116.  
  117. 			Where:   mask        is the mask to be applied to the input text
  118. 			                     (type MaskText.exe /M for help on the mask "language")
  119. 			         text        is the input text to be matched against the mask
  120. 			         command     is a console command whose output is piped as input text
  121.  
  122. 			Notes:   If text is specified on the command line, the program will ignore any
  123. 			         piped input. If no text is specified on the command line, nor piped,
  124. 			         the program will wait for typed input (no prompt).
  125. 			         Whether interactive or piped, only one line of input text is accepted,
  126. 			         i.e. everything including and following the first linefeed is ignored.
  127. 			         Return code is 0 if the input text matches the mask, otherwise 1.
  128.  
  129. 			Credits: Code to detect redirection by Hans Passant on StackOverflow.com:
  130. 			         http://stackoverflow.com/a/3453272
  131.  
  132. 			Written by Rob van der Woude
  133. 			http://www.robvanderwoude.com
  134. 			*/
  135.  
  136. 			#region Help Text
  137.  
  138. 			Console.Error.WriteLine( );
  139.  
  140. 			Console.Error.WriteLine( "Input.exe,  Version {0}", progver );
  141.  
  142. 			Console.Error.WriteLine( "Apply a mask to input text" );
  143.  
  144. 			Console.Error.WriteLine( );
  145.  
  146. 			Console.Error.Write( "Usage:   " );
  147. 			Console.ForegroundColor = ConsoleColor.White;
  148. 			Console.Error.WriteLine( "MaskText.exe  mask  [ text ]" );
  149. 			Console.ResetColor( );
  150.  
  151. 			Console.Error.WriteLine( );
  152.  
  153. 			Console.Error.Write( "   or:   " );
  154. 			Console.ForegroundColor = ConsoleColor.White;
  155. 			Console.Error.WriteLine( "command | MaskText.exe  mask" );
  156. 			Console.ResetColor( );
  157.  
  158. 			Console.Error.WriteLine( );
  159.  
  160. 			Console.Error.Write( "Where:   " );
  161. 			Console.ForegroundColor = ConsoleColor.White;
  162. 			Console.Error.Write( "mask        " );
  163. 			Console.ResetColor( );
  164. 			Console.Error.Write( "is the " );
  165. 			Console.ForegroundColor = ConsoleColor.White;
  166. 			Console.Error.Write( "mask " );
  167. 			Console.ResetColor( );
  168. 			Console.Error.Write( "to be applied to the input " );
  169. 			Console.ForegroundColor = ConsoleColor.White;
  170. 			Console.Error.WriteLine( "text" );
  171. 			Console.ResetColor( );
  172. 			Console.Error.Write( "                     (type " );
  173. 			Console.ForegroundColor = ConsoleColor.White;
  174. 			Console.Error.Write( "MaskText.exe /M " );
  175. 			Console.ResetColor( );
  176. 			Console.Error.WriteLine( " for help on the mask \"language\")" );
  177.  
  178. 			Console.ForegroundColor = ConsoleColor.White;
  179. 			Console.Error.Write( "         text        " );
  180. 			Console.ResetColor( );
  181. 			Console.Error.Write( "is the input " );
  182. 			Console.ForegroundColor = ConsoleColor.White;
  183. 			Console.Error.Write( "text " );
  184. 			Console.ResetColor( );
  185. 			Console.Error.Write( "to be matched against the " );
  186. 			Console.ForegroundColor = ConsoleColor.White;
  187. 			Console.Error.WriteLine( "mask" );
  188. 			Console.ResetColor( );
  189.  
  190. 			Console.ForegroundColor = ConsoleColor.White;
  191. 			Console.Error.Write( "         command     " );
  192. 			Console.ResetColor( );
  193. 			Console.Error.Write( "is a console " );
  194. 			Console.ForegroundColor = ConsoleColor.White;
  195. 			Console.Error.Write( "command " );
  196. 			Console.ResetColor( );
  197. 			Console.Error.Write( "whose output is piped as input " );
  198. 			Console.ForegroundColor = ConsoleColor.White;
  199. 			Console.Error.WriteLine( "text" );
  200. 			Console.ResetColor( );
  201.  
  202. 			Console.Error.WriteLine( );
  203.  
  204. 			Console.Error.Write( "Notes:   If " );
  205. 			Console.ForegroundColor = ConsoleColor.White;
  206. 			Console.Error.Write( "text " );
  207. 			Console.ResetColor( );
  208. 			Console.Error.WriteLine( "is specified on the command line, the program will ignore any" );
  209.  
  210. 			Console.Error.Write( "         piped input. If no " );
  211. 			Console.ForegroundColor = ConsoleColor.White;
  212. 			Console.Error.Write( "text " );
  213. 			Console.ResetColor( );
  214. 			Console.Error.WriteLine( "is specified on the command line, nor piped," );
  215.  
  216. 			Console.Error.WriteLine( "         the program will wait for typed input (no prompt)." );
  217.  
  218. 			Console.Error.Write( "         Whether interactive or piped, only one line of input " );
  219. 			Console.ForegroundColor = ConsoleColor.White;
  220. 			Console.Error.Write( "text " );
  221. 			Console.ResetColor( );
  222. 			Console.Error.WriteLine( "is accepted," );
  223.  
  224. 			Console.Error.WriteLine( "         i.e. everything including and following the first linefeed is ignored." );
  225.  
  226. 			Console.Error.WriteLine( "         Return code is 0 if the input text matches the mask, otherwise 1." );
  227.  
  228. 			Console.Error.WriteLine( );
  229.  
  230. 			Console.Error.WriteLine( "Credits: Code to detect redirection by Hans Passant on StackOverflow.com:" );
  231.  
  232. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  233. 			Console.Error.WriteLine( "         http://stackoverflow.com/a/3453272" );
  234. 			Console.ResetColor( );
  235.  
  236. 			Console.Error.WriteLine( );
  237.  
  238. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  239.  
  240. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  241.  
  242. 			#endregion Help Text
  243.  
  244. 			return 1;
  245. 		}
  246.  
  247.  
  248. 		public static int ShowMaskHelp( )
  249. 		{
  250. 			Console.Error.Write( "Help for this program's " );
  251. 			Console.ForegroundColor = ConsoleColor.White;
  252. 			Console.Error.Write( "mask " );
  253. 			Console.ResetColor( );
  254. 			Console.Error.WriteLine( "argument{0}(based on the Masked Edit control in Visual Basic 6.0):", ( Console.WindowWidth < 94 ? "\n" : " " ) );
  255.  
  256. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  257. 			string url1 = "http://msdn.microsoft.com/en-us/library/";
  258. 			string url2 = "system.windows.forms.maskedtextbox.mask.aspx#remarksToggle";
  259. 			if ( url1.Length + url2.Length > Console.WindowWidth )
  260. 			{
  261. 				Console.Error.WriteLine( url1 );
  262. 				Console.Error.WriteLine( url2 );
  263. 			}
  264. 			else
  265. 			{
  266. 				Console.Error.WriteLine( url1 + url2 );
  267. 			}
  268. 			Console.ResetColor( );
  269.  
  270. 			Console.Error.WriteLine( );
  271.  
  272. 			oddrow = true;
  273. 			WriteTableRow( "Masking element", "Description", true, true );
  274. 			oddrow = true;
  275. 			WriteTableRow( "===============", "===========", true, true );
  276. 			WriteTableRow( "0", "Digit, required. This element will accept any single digit between 0 and 9." );
  277. 			WriteTableRow( "9", "Digit or space, optional." );
  278. 			WriteTableRow( "#", "Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property. Plus (+) and minus (-) signs are allowed." );
  279. 			WriteTableRow( "L", "Letter, required. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z] in regular expressions." );
  280. 			WriteTableRow( "?", "Letter, optional. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-zA-Z]? in regular expressions." );
  281. 			WriteTableRow( "&", "Character, required. Any non-control character. If ASCII only is set (/A), this element behaves like the \"A\" element." );
  282. 			WriteTableRow( "C", "Character, optional. Any non-control character. If ASCII only is set (/A), this element behaves like the \"a\" element." );
  283. 			WriteTableRow( "A", "Alphanumeric, required. If ASCII only is set (/A), the only characters it will accept are the ASCII letters a-z and A-Z and numbers. This mask element behaves like the \"&\" element." );
  284. 			WriteTableRow( "a", "Alphanumeric, optional. If ASCII only is set (/A), the only characters it will accept are the ASCII letters a-z and A-Z and numbers. This mask element behaves like the \"C\" element." );
  285. 			WriteTableRow( ".", "Decimal placeholder." );
  286. 			WriteTableRow( ",", "Thousands placeholder." );
  287. 			WriteTableRow( ":", "Time separator." );
  288. 			WriteTableRow( "/", "Date separator." );
  289. 			WriteTableRow( "$", "Currency symbol." );
  290. 			WriteTableRow( "<", "Shift down. Converts all characters that follow to lowercase." );
  291. 			WriteTableRow( ">", "Shift up. Converts all characters that follow to uppercase." );
  292. 			WriteTableRow( "|", "Disable a previous shift up or shift down." );
  293. 			WriteTableRow( @"\", "Escape. Escapes a mask character, turning it into a literal. \"\\\\\" is the escape sequence for a backslash." );
  294. 			WriteTableRow( "All other characters", "Literals. All non-mask elements will appear as themselves within MaskedTextBox. Literals always occupy a static position in the mask at run time, and cannot be moved or deleted by the user." );
  295.  
  296. 			return 1;
  297. 		}
  298.  
  299.  
  300. 		public static void WriteTableRow( string col1text, string col2text, bool col1bold = true, bool col2bold = false )
  301. 		{
  302. 			// Wrap text to fit in 2 columns
  303. 			oddrow = !oddrow;
  304. 			int windowwidth = Console.WindowWidth;
  305. 			int col1width = 22;
  306. 			int col2width = windowwidth - col1width - 5; // Column separator = 4, subtract 1 extra to prevent automatic line wrap
  307. 			List<string> col2lines = new List<string>( );
  308. 			// Column 2
  309. 			if ( col2text.Length > col2width )
  310. 			{
  311. 				Regex regex = new Regex( @".{1," + col2width + @"}(?=\s|$)" );
  312. 				if ( regex.IsMatch( col2text ) )
  313. 				{
  314. 					MatchCollection matches = regex.Matches( col2text );
  315. 					foreach ( Match match in matches )
  316. 					{
  317. 						col2lines.Add( match.ToString( ).Trim( ) );
  318. 					}
  319. 				}
  320. 				else
  321. 				{
  322. 					while ( col2text.Length > 0 )
  323. 					{
  324. 						col2lines.Add( col2text.Trim( ).Substring( 0, Math.Min( col2width, col2text.Length ) ) );
  325. 						col2text = col2text.Substring( Math.Min( col2width, col2text.Length ) ).Trim( );
  326. 					}
  327. 				}
  328. 			}
  329. 			else
  330. 			{
  331. 				col2lines.Add( col2text.Trim( ) );
  332. 			}
  333. 			for ( int i = 0; i < col2lines.Count; i++ )
  334. 			{
  335. 				if ( oddrow )
  336. 				{
  337. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  338. 				}
  339. 				if ( col1bold || oddrow )
  340. 				{
  341. 					Console.ForegroundColor = ConsoleColor.White;
  342. 				}
  343. 				Console.Write( "{0,-" + col1width + "}    ", ( i < 1 ? col1text : String.Empty ) );
  344. 				Console.ResetColor( );
  345. 				if ( oddrow )
  346. 				{
  347. 					Console.BackgroundColor = ConsoleColor.DarkGray;
  348. 				}
  349. 				if ( col2bold || oddrow )
  350. 				{
  351. 					Console.ForegroundColor = ConsoleColor.White;
  352. 				}
  353. 				Console.Write( "{0,-" + col2width + "}", ( i < col2lines.Count ? col2lines[i] : String.Empty ) );
  354. 				Console.ResetColor( );
  355. 				Console.WriteLine( );
  356. 			}
  357. 		}
  358.  
  359. 		// Code to detect redirection by Hans Passant on StackOverflow.com
  360. 		// http://stackoverflow.com/a/3453272
  361. 		public static class ConsoleEx
  362. 		{
  363. 			public static bool OutputRedirected
  364. 			{
  365. 				get
  366. 				{
  367. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
  368. 				}
  369. 			}
  370.  
  371. 			public static bool InputRedirected
  372. 			{
  373. 				get
  374. 				{
  375. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
  376. 				}
  377. 			}
  378.  
  379. 			public static bool ErrorRedirected
  380. 			{
  381. 				get
  382. 				{
  383. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
  384. 				}
  385. 			}
  386.  
  387. 			// P/Invoke:
  388. 			private enum FileType { Unknown, Disk, Char, Pipe };
  389. 			private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
  390.  
  391. 			[DllImport( "kernel32.dll" )]
  392. 			private static extern FileType GetFileType( IntPtr hdl );
  393.  
  394. 			[DllImport( "kernel32.dll" )]
  395. 			private static extern IntPtr GetStdHandle( StdHandle std );
  396. 		}
  397. 	}
  398. }
  399.  

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