Powered by GeSHi

Source code for unicode2ascii.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class Unicode2ASCII
  9. 	{
  10. 		static int Main( string[] args )
  11. 		{
  12. 			bool isredirected = ConsoleEx.InputRedirected;
  13. 			if ( isredirected ^ args.Length == 0 )
  14. 			{
  15. 				return WriteError( );
  16. 			}
  17. 			if ( args.Length > 1 )
  18. 			{
  19. 				return WriteError( "Invalid command line arguments" );
  20. 			}
  21.  
  22. 			if ( isredirected )
  23. 			{
  24. 				Console.Write( Encoding.ASCII.GetString( Encoding.ASCII.GetBytes( Console.In.ReadToEnd( ) ) ) );
  25. 			}
  26. 			else
  27. 			{
  28. 				string file = args[0];
  29. 				if ( file[0] == '/' )
  30. 				{
  31. 					return WriteError( ( file == "/?" ? "" : "Invalid command line argument" ) );
  32. 				}
  33. 				else
  34. 				{
  35. 					if ( File.Exists( file ) )
  36. 					{
  37. 						StreamReader sr = new StreamReader( file );
  38. 						Console.Write( Encoding.ASCII.GetString( Encoding.ASCII.GetBytes( sr.ReadToEnd( ) ) ) );
  39. 						sr.Close( );
  40. 					}
  41. 					else
  42. 					{
  43. 						return WriteError( "File not found" );
  44. 					}
  45. 				}
  46. 			}
  47. 			return 0;
  48. 		}
  49.  
  50. 		#region Redirection Detection
  51.  
  52. 		// Code to detect redirection by Hans Passant on StackOverflow.com
  53. 		// http://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  54. 		public static class ConsoleEx
  55. 		{
  56. 			public static bool OutputRedirected
  57. 			{
  58. 				get
  59. 				{
  60. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
  61. 				}
  62. 			}
  63.  
  64. 			public static bool InputRedirected
  65. 			{
  66. 				get
  67. 				{
  68. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
  69. 				}
  70. 			}
  71.  
  72. 			public static bool ErrorRedirected
  73. 			{
  74. 				get
  75. 				{
  76. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
  77. 				}
  78. 			}
  79.  
  80. 			// P/Invoke:
  81. 			private enum FileType { Unknown, Disk, Char, Pipe };
  82. 			private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
  83.  
  84. 			[DllImport( "kernel32.dll" )]
  85. 			private static extern FileType GetFileType( IntPtr hdl );
  86.  
  87. 			[DllImport( "kernel32.dll" )]
  88. 			private static extern IntPtr GetStdHandle( StdHandle std );
  89. 		}
  90.  
  91. 		#endregion Redirection Detection
  92.  
  93.  
  94. 		#region Error Handling
  95.  
  96. 		public static int WriteError( Exception e = null )
  97. 		{
  98. 			return WriteError( e == null ? null : e.Message );
  99. 		}
  100.  
  101. 		public static int WriteError( string errorMessage )
  102. 		{
  103. 			Console.OpenStandardError( );
  104. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  105. 			{
  106. 				Console.Error.WriteLine( );
  107. 				Console.ForegroundColor = ConsoleColor.Red;
  108. 				Console.Error.Write( "ERROR: " );
  109. 				Console.ForegroundColor = ConsoleColor.White;
  110. 				Console.Error.WriteLine( errorMessage );
  111. 				Console.ResetColor( );
  112. 			}
  113.  
  114. 			/*
  115. 			Unicode2ASCII,  Version 1.00
  116. 			Convert Unicode text to ASCII and send it to Standard Output
  117.  
  118. 			Usage:   some_command  |  Unicode2ASCII  >  file
  119. 			   or:   some_command  |  Unicode2ASCII  |  other_command
  120. 			   or:   Unicode2ASCII  "textfile"  >  file
  121. 			   or:   Unicode2ASCII  "textfile"  |  other_command
  122.  
  123. 			Check for redirection by Hans Passant on StackOverflow.com
  124. 			/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  125.  
  126. 			Written by Rob van der Woude
  127. 			http://www.robvanderwoude.com
  128. 			 */
  129.  
  130. 			Console.Error.WriteLine( );
  131. 			Console.Error.WriteLine( "Unicode2ASCII,  Version 1.00" );
  132. 			Console.Error.WriteLine( "Convert Unicode text to ASCII and send it to Standard Output" );
  133. 			Console.Error.WriteLine( );
  134. 			Console.Error.Write( "Usage:   " );
  135. 			Console.ForegroundColor = ConsoleColor.White;
  136. 			Console.Error.WriteLine( "some_command  |  Unicode2ASCII  >  file" );
  137. 			Console.ResetColor( );
  138. 			Console.Error.Write( "or:      " );
  139. 			Console.ForegroundColor = ConsoleColor.White;
  140. 			Console.Error.WriteLine( "some_command  |  Unicode2ASCII  |  other_command" );
  141. 			Console.ResetColor( );
  142. 			Console.Error.Write( "or:      " );
  143. 			Console.ForegroundColor = ConsoleColor.White;
  144. 			Console.Error.WriteLine( "Unicode2ASCII  \"textfile\"  >  file" );
  145. 			Console.ResetColor( );
  146. 			Console.Error.Write( "or:      " );
  147. 			Console.ForegroundColor = ConsoleColor.White;
  148. 			Console.Error.WriteLine( "Unicode2ASCII  \"textfile\"  |  other_command" );
  149. 			Console.ResetColor( );
  150. 			Console.Error.WriteLine( );
  151. 			Console.Error.WriteLine( "Example: Compare the results of the following commands:" );
  152. 			Console.ForegroundColor = ConsoleColor.White;
  153. 			Console.Error.WriteLine( "         SUBINACL  /?  |  MORE" );
  154. 			Console.Error.WriteLine( "         SUBINACL  /?  |  Unicode2ASCII  |  MORE" );
  155. 			Console.ResetColor( );
  156. 			Console.Error.WriteLine( );
  157. 			Console.Error.Write( "Check for redirection by Hans Passant on " );
  158. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  159. 			Console.Error.WriteLine( "StackOverflow.com" );
  160. 			Console.Error.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
  161. 			Console.ResetColor( );
  162. 			Console.Error.WriteLine( );
  163. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  164. 			Console.Error.Write( "http://www.robvanderwoude.com" );
  165. 			Console.OpenStandardOutput( );
  166. 			return 1;
  167. 		}
  168.  
  169. 		#endregion Error Handling
  170. 	}
  171. }
  172.