Headtail.cs

From AdonaiMedrado.Pro.Br
Jump to: navigation, search

Versão 1

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace headtail
{
    class Program
    {
        static void Main(string[] args)
        {
            bool? head = null;
            if (args[0] == "-tail" || args[0] == "-t")
                head = false;
            else if (args[0] == "-head" || args[0] == "-h")
                head = true;
 
            if (head==null || args.Length < 2)
            {
                Console.WriteLine("Usage: headtail <-tail|-t|-head|-h> <file1>[...<fileN>]"); 
                return;
            }
 
            for (int i = 1; i < args.Length; i++)
            {
                string s = args[i];
                try
                {
                    TextReader r = File.OpenText(s);
                    if (args.Length > 3)
                        Console.WriteLine("> " + s);
                    string[] linhas = r.ReadToEnd().Split(new char[] { '\n' });
 
                    if ((bool)head)
                        for (int x = 0; x < (int)(linhas.Length * .2); x++)
                            Console.WriteLine(linhas[x]);
                    else
                        for (int x = (int)(linhas.Length * .7); x < linhas.Length; x++)
                            Console.WriteLine(linhas[x]);
                    r.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("> " + s + ": " + ex.Message);
                }
            }
        }
    }
}

Versão 2

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace headtail
{
    class Program
    {
        static void Main(string[] args)
        {
            bool? head = null;
            if (args[0] == "-tail" || args[0] == "-t")
                head = false;
            else if (args[0] == "-head" || args[0] == "-h")
                head = true;
 
            if (head==null || args.Length < 2)
            {
                Console.WriteLine("Usage: headtail <-tail|-t|-head|-h> <file1>[...<fileN>]"); 
                return;
            }
 
            for (int i = 1; i < args.Length; i++)
            {
                string s = args[i];
                FileStream r = null;
                try
                {
                	r = File.OpenRead(s);
                	int porcentagem = (int)(r.Length*.2);
 
                    if (args.Length > 2)
                        Console.WriteLine("> " + s);
 
                    byte[] buf=new byte[porcentagem];
                    if (!(bool)head)
                    	r.Seek(-porcentagem,SeekOrigin.End);
                    r.Read(buf,0,buf.Length);
 
                    string[] linhas = ASCIIEncoding.Default.GetString(buf).Split(new char[] { '\n' });
 
                    for (int x = (bool)head ? 0 : 1; x < linhas.Length; x++)
                        Console.WriteLine(linhas[x]);                    
                    r.Close();
                    r.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("> " + s + ": " + ex.Message);
                }
            }
        }
    }
}