Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ascii.cs

(view source code of ascii.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 ASCII
  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.Unicode.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.Unicode.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. 			ASCII,  Version 1.00
  116. 			Convert a text file or Standard Input to ASCII and send it to Standard Output
  117.  
  118. 			Usage:   some_command  |  ASCII  >  file
  119. 			   or:   some_command  |  ASCII  |  other_command
  120. 			   or:   ASCII  "textfile"  >  file
  121. 			   or:   ASCII  "textfile"  |  other_command
  122.  
  123. 			Example: Compare the results of the following commands:
  124. 			         SUBINACL  /?  |  MORE
  125. 			         SUBINACL  /?  |  ASCII  |  MORE
  126.  
  127. 			Check for redirection by Hans Passant on StackOverflow.com
  128. 			/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  129.  
  130. 			Written by Rob van der Woude
  131. 			http://www.robvanderwoude.com
  132. 			 */
  133.  
  134. 			Console.Error.WriteLine( );
  135. 			Console.Error.WriteLine( "ASCII,  Version 1.00" );
  136. 			Console.Error.WriteLine( "Convert a text file or Standard Input to ASCII and send it to Standard Output" );
  137. 			Console.Error.WriteLine( );
  138. 			Console.Error.Write( "Usage:   " );
  139. 			Console.ForegroundColor = ConsoleColor.White;
  140. 			Console.Error.WriteLine( "some_command  |  ASCII  >  file" );
  141. 			Console.ResetColor( );
  142. 			Console.Error.Write( "or:      " );
  143. 			Console.ForegroundColor = ConsoleColor.White;
  144. 			Console.Error.WriteLine( "some_command  |  ASCII  |  other_command" );
  145. 			Console.ResetColor( );
  146. 			Console.Error.Write( "or:      " );
  147. 			Console.ForegroundColor = ConsoleColor.White;
  148. 			Console.Error.WriteLine( "ASCII  \"textfile\"  >  file" );
  149. 			Console.ResetColor( );
  150. 			Console.Error.Write( "or:      " );
  151. 			Console.ForegroundColor = ConsoleColor.White;
  152. 			Console.Error.WriteLine( "ASCII  \"textfile\"  |  other_command" );
  153. 			Console.ResetColor( );
  154. 			Console.Error.WriteLine( );
  155. 			Console.Error.WriteLine( "Example: Compare the results of the following commands:" );
  156. 			Console.ForegroundColor = ConsoleColor.White;
  157. 			Console.Error.WriteLine( "         SUBINACL  /?  |  MORE" );
  158. 			Console.Error.WriteLine( "         SUBINACL  /?  |  ASCII  |  MORE" );
  159. 			Console.ResetColor( );
  160. 			Console.Error.WriteLine( );
  161. 			Console.Error.Write( "Check for redirection by Hans Passant on " );
  162. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  163. 			Console.Error.WriteLine( "StackOverflow.com" );
  164. 			Console.Error.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
  165. 			Console.ResetColor( );
  166. 			Console.Error.WriteLine( );
  167. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  168. 			Console.Error.Write( "http://www.robvanderwoude.com" );
  169. 			Console.OpenStandardOutput( );
  170. 			return 1;
  171. 		}
  172.  
  173. 		#endregion Error Handling
  174. 	}
  175. }
  176.  

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