Cc reads the last few lines of a file or character stream, similar to the tail command of Linux

From , 5 Years ago, written in C#, viewed 231 times.
URL https://pastebin.vip/view/d3a03b46
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace RobvanderWoude
  7. {
  8.         class Tail
  9.         {
  10.                 static int Main( string[] args )
  11.                 {
  12.                         try
  13.                         {
  14.                                 int numlines = 1;
  15.                                 string[] lines;
  16.                                
  17.                                 #region Command Line Parsing
  18.  
  19.                                 string filename = string.Empty;
  20.                                 bool redirected;
  21.                                 bool set_l = false;
  22.                                 bool set_input = false;
  23.  
  24.                                 foreach ( string arg in args )
  25.                                 {
  26.                                         if ( arg[0] == '/' )
  27.                                         {
  28.                                                 if ( arg.Length > 3 )
  29.                                                 {
  30.                                                         if ( arg.ToUpper( )[1] == 'L' )
  31.                                                         {
  32.                                                                 if ( arg[2] != ':' )
  33.                                                                 {
  34.                                                                         return WriteError( "Invalid argument: " + arg );
  35.                                                                 }
  36.                                                                 try
  37.                                                                 {
  38.                                                                         if ( set_l )
  39.                                                                         {
  40.                                                                                 return WriteError( "Duplicate /L argument" );
  41.                                                                         }
  42.                                                                         else
  43.                                                                         {
  44.                                                                                 numlines = Convert.ToInt32( arg.Substring( 3 ) );
  45.                                                                                 if ( numlines < 1 )
  46.                                                                                 {
  47.                                                                                         return WriteError( "Number of lines must be 1 or greater" );
  48.                                                                                 }
  49.                                                                                 set_l = true;
  50.                                                                         }
  51.                                                                 }
  52.                                                                 catch ( FormatException e )
  53.                                                                 {
  54.                                                                         return WriteError( "Invalid number of lines: " + arg );
  55.                                                                 }
  56.                                                         }
  57.                                                 }
  58.                                                 else
  59.                                                 {
  60.                                                         if ( arg == "/?" )
  61.                                                         {
  62.                                                                 return WriteError( );
  63.                                                         }
  64.                                                         else
  65.                                                         {
  66.                                                                 return WriteError( "Invalid argument: " + arg );
  67.                                                         }
  68.                                                 }
  69.                                         }
  70.                                         else
  71.                                         {
  72.                                                 if ( set_input )
  73.                                                 {
  74.                                                         return WriteError( "Duplicate file argument" );
  75.                                                 }
  76.                                                 else
  77.                                                 {
  78.                                                         if ( File.Exists( arg ) )
  79.                                                         {
  80.                                                                 filename = arg;
  81.                                                         }
  82.                                                         else
  83.                                                         {
  84.                                                                 return WriteError( "Invalid filename: " + arg );
  85.                                                         }
  86.                                                         set_input = true;
  87.                                                 }
  88.                                         }
  89.                                 }
  90.  
  91.                                 if ( ConsoleEx.InputRedirected )
  92.                                 {
  93.                                         if ( set_input )
  94.                                         {
  95.                                                 return WriteError( "Use either file name or redirection, not both" );
  96.                                         }
  97.                                         else
  98.                                         {
  99.                                                 set_input = true;
  100.                                                 redirected = true;
  101.                                         }
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                         if ( args.Length == 0 )
  106.                                         {
  107.                                                 return WriteError( );
  108.                                         }
  109.                                         redirected = false;
  110.                                 }
  111.  
  112.                                 #endregion Command Line Parsing
  113.  
  114.                                 if ( redirected )
  115.                                 {
  116.                                         // Read standard input and store the lines in a list
  117.                                         List<string> alllines = new List<string>( );
  118.                                         int peek = 0;
  119.                                         string line;
  120.                                         do
  121.                                         {
  122.                                                 line = Console.In.ReadLine( );
  123.                                                 alllines.Add( line );
  124.                                                 peek = Console.In.Peek( );
  125.                                         } while ( peek != -1 );
  126.                                         // Convert the list to an array
  127.                                         lines = alllines.ToArray( );
  128.                                 }
  129.                                 else
  130.                                 {
  131.                                         // Read the file and store the lines in an array
  132.                                         lines = File.ReadAllLines( filename );
  133.                                 }
  134.                                 // Display the last lines from the array
  135.                                 int startline = lines.Length - numlines;
  136.                                 if ( startline < 0 )
  137.                                 {
  138.                                         startline = 0;
  139.                                 }
  140.                                 for ( int i = startline; i < lines.Length; i++ )
  141.                                 {
  142.                                         Console.WriteLine( lines[i] );
  143.                                 }
  144.                                 return 0;
  145.                         }
  146.                         catch ( Exception e )
  147.                         {
  148.                                 return WriteError( e.Message );
  149.                         }
  150.                 }
  151.  
  152.  
  153.                 #region Redirection Detection
  154.  
  155.                 // Code to detect redirection by Hans Passant on StackOverflow.com
  156.                 // http://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  157.                 public static class ConsoleEx
  158.                 {
  159.                         public static bool OutputRedirected
  160.                         {
  161.                                 get
  162.                                 {
  163.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
  164.                                 }
  165.                         }
  166.  
  167.                         public static bool InputRedirected
  168.                         {
  169.                                 get
  170.                                 {
  171.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
  172.                                 }
  173.                         }
  174.  
  175.                         public static bool ErrorRedirected
  176.                         {
  177.                                 get
  178.                                 {
  179.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
  180.                                 }
  181.                         }
  182.  
  183.                         // P/Invoke:
  184.                         private enum FileType { Unknown, Disk, Char, Pipe };
  185.                         private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
  186.  
  187.                         [DllImport( "kernel32.dll" )]
  188.                         private static extern FileType GetFileType( IntPtr hdl );
  189.  
  190.                         [DllImport( "kernel32.dll" )]
  191.                         private static extern IntPtr GetStdHandle( StdHandle std );
  192.                 }
  193.  
  194.                 #endregion Redirection Detection
  195.  
  196.                 #region Error Handling
  197.  
  198.                 public static int WriteError( Exception e = null )
  199.                 {
  200.                         return WriteError( e == null ? null : e.Message );
  201.                 }
  202.  
  203.                 public static int WriteError( string errorMessage )
  204.                 {
  205.                         if ( string.IsNullOrEmpty( errorMessage ) == false )
  206.                         {
  207.                                 Console.Error.WriteLine( );
  208.                                 Console.ForegroundColor = ConsoleColor.Red;
  209.                                 Console.Error.Write( "ERROR: " );
  210.                                 Console.ForegroundColor = ConsoleColor.White;
  211.                                 Console.Error.WriteLine( errorMessage );
  212.                                 Console.ResetColor( );
  213.                         }
  214.  
  215.                         /*
  216.                         Tail,  Version 1.00
  217.                         Return the specified number of lines from the end of the specified file
  218.  
  219.                         Usage:   TAIL  filename  [ option ]
  220.                            or:   TAIL  [ option ]  <  filename
  221.                            or:   command  |  TAIL  [ option ]
  222.  
  223.                         Where:   filename   is the optional file to be read
  224.                                          command    is the optional command whose output is to be read
  225.                                  /L:n       read the last n Lines (default: 1 line)
  226.  
  227.                         Examples:
  228.                         TAIL  filename                 read the last line (default)
  229.                         TAIL  /L:2  <  filename        read the last 2 lines
  230.                         command  | TAIL  /L:5          read the last 5 lines
  231.  
  232.                         Check for redirection by Hans Passant on StackOverflow.com
  233.                         /questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  234.  
  235.                         Written by Rob van der Woude
  236.                         http://www.robvanderwoude.com
  237.                         */
  238.  
  239.                         Console.Error.WriteLine( );
  240.                         Console.Error.WriteLine( "Tail,  Version 1.00" );
  241.                         Console.Error.WriteLine( "Return the specified number of lines from the end of the specified file" );
  242.                         Console.Error.WriteLine( );
  243.                         Console.Error.Write( "Usage:   " );
  244.                         Console.ForegroundColor = ConsoleColor.White;
  245.                         Console.Error.WriteLine( "TAIL  filename  [ option ]" );
  246.                         Console.ResetColor( );
  247.                         Console.Error.Write( "   or:   " );
  248.                         Console.ForegroundColor = ConsoleColor.White;
  249.                         Console.Error.WriteLine( "TAIL  [ option ]  <  filename" );
  250.                         Console.ResetColor( );
  251.                         Console.Error.Write( "   or:   " );
  252.                         Console.ForegroundColor = ConsoleColor.White;
  253.                         Console.Error.WriteLine( "command  |  TAIL  [ option ]" );
  254.                         Console.ResetColor( );
  255.                         Console.Error.WriteLine( );
  256.                         Console.Error.Write( "Where:   " );
  257.                         Console.ForegroundColor = ConsoleColor.White;
  258.                         Console.Error.Write( "filename" );
  259.                         Console.ResetColor( );
  260.                         Console.Error.WriteLine( "   is the optional file to be read" );
  261.                         Console.ForegroundColor = ConsoleColor.White;
  262.                         Console.Error.Write( "         command" );
  263.                         Console.ResetColor( );
  264.                         Console.Error.WriteLine( "    is the optional command whose output is to be read" );
  265.                         Console.ForegroundColor = ConsoleColor.White;
  266.                         Console.Error.Write( "         /L:n" );
  267.                         Console.ResetColor( );
  268.                         Console.Error.Write( "       read the last " );
  269.                         Console.ForegroundColor = ConsoleColor.White;
  270.                         Console.Error.Write( "n L" );
  271.                         Console.ResetColor( );
  272.                         Console.Error.WriteLine( "ines (default: 1 line)" );
  273.                         Console.Error.WriteLine( );
  274.                         Console.Error.WriteLine( "Examples:" );
  275.                         Console.Error.WriteLine( "TAIL  filename                 read the last line (default)" );
  276.                         Console.Error.WriteLine( "TAIL  /L:2  <  filename        read the last 2 lines" );
  277.                         Console.Error.WriteLine( "command  | TAIL  /L:5          read the last 5 lines" );
  278.                         Console.Error.WriteLine( );
  279.                         Console.Error.Write( "Check for redirection by Hans Passant on " );
  280.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  281.                         Console.Error.WriteLine( "StackOverflow.com" );
  282.                         Console.Error.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
  283.                         Console.ResetColor( );
  284.                         Console.Error.WriteLine( );
  285.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  286.                         Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  287.                         return 1;
  288.                 }
  289.  
  290.                 #endregion Error Handling
  291.         }
  292. }
  293. //csharp/7352

Reply to "Cc reads the last few lines of a file or character stream, similar to the tail command of Linux"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website