Advertisement
eduensarceno

analizdor de archivos separados por tabs

Nov 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace Com.Esy.Eduen.Analizador {
  6.  
  7. public class Analizador
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         if (0 == args.Length
  12.             || "--help" == args[0]
  13.             || "-f" != args[0]
  14.         ) {
  15.             ImprimirAyuda();
  16.             return;
  17.         }
  18.         for(int i = 1; i < args.Length; i++) {
  19.             AnalizarArchivo(args[i]);
  20.         }
  21.     }
  22.  
  23.     public static void AnalizarArchivo(string path)
  24.     {
  25.         try {
  26.             FileStream fs = File.OpenRead(path);
  27.             byte[] buffer = new byte[BUFFER_SIZE];
  28.            
  29.             int n = 0;
  30.             int r = 0;
  31.            
  32.             while((r = fs.Read(buffer, 0, buffer.Length)) > 0) {
  33.                 if((fs.Length - (++n) * BUFFER_SIZE) <= 0
  34.                     && buffer[r - 1] != '\x0A'
  35.                 ) {
  36.                     Console.WriteLine("[Error] el archivo no termina en un salto de línea de nuevo");
  37.                 } else {
  38.                     AnalizarArchivoHelper(buffer);
  39.                 }
  40.             }
  41.  
  42.             fs.Dispose();
  43.         } catch(Exception e) {
  44.             if(e is FileNotFoundException){
  45.                 Console.WriteLine("[Error] El archivo `{0}` no existe", path);
  46.             } else {
  47.                 Console.WriteLine("[Error] error inesperado:\n{0}", e.StackTrace);
  48.             }
  49.         }
  50.     }
  51.  
  52.     private static void AnalizarArchivoHelper(byte[] buffer)
  53.     {
  54.         string[] filas = Encoding.ASCII.GetString(buffer).Split('\n');
  55.         Console.WriteLine("[INFO] Líneas: " + (filas.Length - 1));
  56.         for(int i = 0; i < (filas.Length - 1); i++){
  57.             string[] columnas = filas[i].Split('\t');
  58.             Console.WriteLine(
  59.                     "ID: {0}, CLIENTE: {1}, AREA: {2}",
  60.                     columnas[0].ToLower(),
  61.                     columnas[1].ToLower(),
  62.                     columnas[2].ToLower()
  63.             );
  64.         }
  65.     }
  66.  
  67.     private static void ImprimirAyuda()
  68.     {
  69.         Console.WriteLine(
  70.           "Modo de uso:\n\n" +
  71.           "\tAnalizador -f <archivo> [,<archivo>]\t Lee los valores de los archivos listados.\n" +
  72.           "\tAnalizador --help\t\t\t Muestra ésta ayuda."
  73.         );
  74.     }
  75.  
  76.     private const int BUFFER_SIZE = 4 * 1024; //4KiB
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement