Difference between revisions of "Headtail.cs"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(New page: <code lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.IO; namespace headtail { class Program { static void Main(string[] arg...)
(No difference)

Revision as of 00:28, 13 May 2009

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);
                }
            }
        }
    }
}