Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace Com.Esy.Eduen.Analizador {
- public class Analizador
- {
- static void Main(string[] args)
- {
- if (0 == args.Length
- || "--help" == args[0]
- || "-f" != args[0]
- ) {
- ImprimirAyuda();
- return;
- }
- for(int i = 1; i < args.Length; i++) {
- AnalizarArchivo(args[i]);
- }
- }
- public static void AnalizarArchivo(string path)
- {
- try {
- FileStream fs = File.OpenRead(path);
- byte[] buffer = new byte[BUFFER_SIZE];
- int n = 0;
- int r = 0;
- while((r = fs.Read(buffer, 0, buffer.Length)) > 0) {
- if((fs.Length - (++n) * BUFFER_SIZE) <= 0
- && buffer[r - 1] != '\x0A'
- ) {
- Console.WriteLine("[Error] el archivo no termina en un salto de línea de nuevo");
- } else {
- AnalizarArchivoHelper(buffer);
- }
- }
- fs.Dispose();
- } catch(Exception e) {
- if(e is FileNotFoundException){
- Console.WriteLine("[Error] El archivo `{0}` no existe", path);
- } else {
- Console.WriteLine("[Error] error inesperado:\n{0}", e.StackTrace);
- }
- }
- }
- private static void AnalizarArchivoHelper(byte[] buffer)
- {
- string[] filas = Encoding.ASCII.GetString(buffer).Split('\n');
- Console.WriteLine("[INFO] Líneas: " + (filas.Length - 1));
- for(int i = 0; i < (filas.Length - 1); i++){
- string[] columnas = filas[i].Split('\t');
- Console.WriteLine(
- "ID: {0}, CLIENTE: {1}, AREA: {2}",
- columnas[0].ToLower(),
- columnas[1].ToLower(),
- columnas[2].ToLower()
- );
- }
- }
- private static void ImprimirAyuda()
- {
- Console.WriteLine(
- "Modo de uso:\n\n" +
- "\tAnalizador -f <archivo> [,<archivo>]\t Lee los valores de los archivos listados.\n" +
- "\tAnalizador --help\t\t\t Muestra ésta ayuda."
- );
- }
- private const int BUFFER_SIZE = 4 * 1024; //4KiB
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement