9511_project01

project 1 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 3aa37419650314b80982f5624a4837feddbcfd66
parent 35ba81de8e50d0c549ccd188c4cc096f0f9e1ad5
Author: Martin J. Klöckner <64109770+klewer-martin@users.noreply.github.com>
Date:   Wed,  3 Feb 2021 13:52:22 -0300

Merge pull request #1 from klewer-martin/alpha

Merged alpha branch to master
Diffstat:
Marguments.c | 13+++++--------
Marguments.h | 4+---
Mdata.c | 18++++++++++++++++++
Minput.csv | 379+------------------------------------------------------------------------------
Mmacros.h | 16++++++++++++++--
Mmain.c | 40++++++++++++++++------------------------
Mmain.h | 5+++--
Mreadlines.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mreadlines.h | 2+-
9 files changed, 124 insertions(+), 435 deletions(-)
diff --git a/arguments.c b/arguments.c
@@ -6,20 +6,17 @@
 #define INPUT_FILE_NAME_MSG     "\t -> Archivo de entrada: "
 #define OUTPUT_FILE_NAME_MSG     "\t -> Archivo de salida: "
 
-//    Checks if the arguments are right;
-status_t validate_arguments(int argc, char * argv[])
+//    Valida que los argumentos esten correctos y guarda los nombres de los 
+//    archivos de entrada y salida en src y dest respectivamente;
+status_t validate_arguments(int argc, char * argv[], char * src, char * dest)
 {
     if(argc == NO_CMD_ARGUMENTS)
         return ERROR_INVOCATING_PROGRAM;
+    else if(argc == ONE_CMD_ARGUMENT)
+        return ERROR_INVOCATING_PROGRAM;
     else if(argv == NULL)
         return ERROR_NULL_POINTER;
 
-    return OK;
-}
-
-//    Set the files name acording to the arguments;
-status_t set_files_name(int argc, char * argv[], char * src, char * dest)
-{
     int i;
     status_t inputFile, outputFile;
     inputFile = outputFile = FILE_NOT_FOUND;
diff --git a/arguments.h b/arguments.h
@@ -6,8 +6,6 @@
 
 void print_error(status_t error);
 
-status_t validate_arguments(int argc, char * argv[]);
-
-status_t set_files_name(int argc, char * argv[], char * src, char * dest);
+status_t validate_arguments(int argc, char * argv[], char * src, char * dest);
 
 #endif
diff --git a/data.c b/data.c
@@ -10,6 +10,24 @@ void print_error(status_t error)
         case ERROR_NULL_POINTER:
             fprintf(stderr, MSG_ERROR_NULL_POINTER"\n");
             break;
+        case FILE_NOT_FOUND: 
+            fprintf(stderr, MSG_FILE_NOT_FOUND"\n");
+            break;
+        case ERROR_LOADING_COUNTRY_CODES:
+            fprintf(stderr, MSG_ERROR_LOADING_COUNTRY_CODES"\n");
+            break;
+        case ERROR_PRINTING: 
+            fprintf(stderr, MSG_ERROR_PRINTING"\n");
+            break;
+        case ERROR_READING_FILE:
+            fprintf(stderr, MSG_ERROR_READING_FILE"\n");
+            break;
+        case ERROR_ALLOCATING_TIME: 
+            fprintf(stderr, MSG_ERROR_ALLOCATING_TIME"\n");
+            break;
+        case ERROR_DATA_ON_FILE_MISSING: 
+            fprintf(stderr, MSG_ERROR_DATA_ON_FILE_MISSING"\n");
+            break;
         default:
             fprintf(stdin, MSG_OK"\n");
     }
diff --git a/input.csv b/input.csv
@@ -1,19 +1,5 @@
 PAIS, FECHA, INFECTADOS
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
-32,1577880000,2342
+32,1577880000
 32,1578657600,4923
 32,1579089600,9324
 170,1577880000,8234
@@ -21,365 +7,4 @@ PAIS, FECHA, INFECTADOS
 170,1579089600,9423
 276,1577880000,8432
 276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1577880000,2342
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
-32,1578657600,4923
-32,1579089600,9324
-170,1577880000,8234
-170,1578657600,9234
-170,1579089600,9423
-276,1577880000,8432
-276,1579089600,9129
-276,1579521600,4214 
-834,1577880000,8432
-764,1579089600,9129
-704,1579521600,4214 
-504,1577880000,2342
-104,1578657600,4923
-480,1579089600,9324
-484,1577880000,8234
-496,1578657600,9234
-554,1579089600,9423
-558,1577880000,8432
-574,1579089600,9129
-512,1579521600,4214 
-586,1577880000,8432
-591,1579089600,9129
-384,1579521600,4214 
+276,1579521600,4214
diff --git a/macros.h b/macros.h
@@ -2,6 +2,7 @@
 #define MACROS_H
 
 #define NO_CMD_ARGUMENTS    1
+#define ONE_CMD_ARGUMENT    2
 #define MAX_CMD_ARGUMENTS    5
 
 #define INPUT_ARGUMENT            "-in"
@@ -13,11 +14,22 @@
                                 "An unexpected error has occured during the execution\n"\
                                 "of the program"
 
-#define MSG_ERROR_INVOCATING_PROGRAM    "ERROR_INVOCATING_PROGRAM\n"\
+#define MSG_ERROR_INVOCATING_PROGRAM    "\nERROR_INVOCATING_PROGRAM\n"\
                                         "Usage:\t$ ./main -in <input file> -out <outputfile>\n"\
                                         "\t$ ./main -out <output file -in <input file>\n"\
                                         "Read documentation to know more"
 
-#define MSG_OK "Everything executed correctly"
+#define MSG_FILE_NOT_FOUND "No se ha encontrado dicho archivo\n"
+
+#define MSG_ERROR_LOADING_COUNTRY_CODES
+#define MSG_ERROR_PRINTING
+#define MSG_ERROR_READING_FILE
+#define MSG_ERROR_ALLOCATING_TIME
+#define MSG_ERROR_DATA_ON_FILE_MISSING    "\nERROR_DATA_ON_FILE_MISSING\n"\
+                                        "En alguna linea de el archivo de entrada falta un dato,\n"\
+                                        "compruebe que dicho archivo no esta corrupto y ejecute\n"\
+                                        "el programa nuevamente"
+
+#define MSG_OK "Everything executed correctly\n"
 
 #endif
diff --git a/main.c b/main.c
@@ -30,42 +30,34 @@ int main(int argc, char * argv[])
 //    caso de que haya alguno;
     status_t st;
 
+//    Las siguientes variables son para guardar los nombres de los archivos de 
+//    entrada y salida luego de validar que los argumentos recibidos sean correctos;
     char src[32], dest[32];
-//    char dest[32];
-
-
-    FILE *fpi;
-//        , *fpo;
 
+//    El siguiente arreglo de dos dimensiones es donde se van a guardar los codigos
+//    de los paises;
     char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
 
-//    char **country_codes = (char**)malloc(COUNTRIES_NUMBER * ARRAYS_LENGTH * sizeof(char));
-
-    if((st = validate_arguments(argc, argv)) != OK) {
+//    Valida de que los argumentos sean correctos y guarda los nombres de los
+//    archivos de entrada y salida en src y dest respectivamente, de haber algun
+//    error en el proceso devuelve un codigo de error de tipo status_t (definido 
+//    en main.h);
+    if((st = validate_arguments(argc, argv, src, dest)) != OK) {
         print_error(st);
         return st;
     }
     
 
-
-    if((st = set_files_name(argc, argv, src, dest)) != OK) {
-        print_error(st);
-        return st;
-    }
-
-
+//    Carga los codigos de error de los paises de acuerdo al standard iso3166 en el
+//    arreglo mencionado previamente 'country_codes', en caso de haber algun error 
+//    en el proceso devuelve dicho codigo e impreme por stderr un mensaje de error; 
     if(load_country_codes(country_codes) != OK)
         return ERROR_LOADING_COUNTRY_CODES;    
 
+    if((st = readlines(src, dest)) != OK) {
+        print_error(st);
+        return st;
+    }    
 
-    putchar('\n');
-
-    readlines();
-
-    fpi = fopen(src, "r");
-//    fpo = fopen(dest, "w");
-
-    fclose(fpi);
-//    fclose(fpo);
     return OK;
 }
diff --git a/main.h b/main.h
@@ -10,13 +10,14 @@
 
 typedef enum {
     OK,
+    FILE_NOT_FOUND,
     ERROR_INVOCATING_PROGRAM,
     ERROR_NULL_POINTER,
-    FILE_NOT_FOUND,
     ERROR_LOADING_COUNTRY_CODES,
     ERROR_PRINTING,
     ERROR_READING_FILE,
-    ERROR_ALLOCATING_TIME
+    ERROR_ALLOCATING_TIME,
+    ERROR_DATA_ON_FILE_MISSING    
 } status_t;
 
 #endif
diff --git a/readlines.c b/readlines.c
@@ -1,15 +1,26 @@
+//    Lee el archivo de entrada y va separando los datos mientras va leyendo linea
+//    por linea, y los va imprimiendo en pantalla
+
+
+
 #include "readlines.h"
 
 #define COUNTRY_PROMPT "Pais"
 
 const char date_print_format[] = "%d %b %Y";
 
-status_t readlines(void)
+status_t readlines(char *src, char *dest)
 {
     size_t line, i, j;
+
+//    Puntero para el archivo de entrada;    
     FILE *fp;
+
     char buff1[] = "                          ";
     char buff2[] = "                          ";
+
+//    Esta variable es para saber de que tipo de dato estamos hablando, si es un
+//    codigo de pais, una fecha o el numero de infectados;
     data_t data;
 
     char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
@@ -19,27 +30,70 @@ status_t readlines(void)
     unsigned long date;
     unsigned long infected;
 
-    if((fp = fopen(INPUT_FILE_NAME, "r")) == NULL)
+
+// Abre el archivo de entrada en modo lectura si por algun motivo no se puede
+// abrir devuelve un codigo de error;
+    if((fp = fopen(src, "r")) == NULL)
             return ERROR_READING_FILE;
 
+
+//    Lee de el archivo de entrada linea por linea y va guardando las 
+//    lineas en buff1 hasta que se terminen;
     for(line = 0; fgets(buff1, sizeof(buff1), fp) != NULL; line++)
     {
-//    This 'if' is to skip the first line which doesn't contain information;
+
+//    Este 'if' es para evitar la primer linea que no contiene ningun tipo de dato;
         if(line != 0) {
+
+//            Recorre el buff1 separando los datos de acuerdo a si es el codigo de
+//            un pais, una fecha o el numero de infectados;
             for(i = 0, j = 0, data = PAIS; buff1[i] != '\0'; i++)
             {
-                if((buff1[i] == ',') || (buff1[i] == '\n'))
+
+//                Si encuentra una coma cambia el tipo de dato;
+                if((buff1[i] == ','))
                 {
+
+//                    Se incrementa i para evitar que sea guardado en algun lado;
                     i++;
+
+//                    De acuerdo al tipo de dato que se guardo hasta llegar a la
+//                    coma va a guardarlo de distinta manera, ej: si el dato que
+//                    se guardo en buff2 era el codigo de un PAIS, entonces lo 
+//                    guarda en la variable country, si el tipo de dato que se 
+//                    guardo en buff2 era una fecha entonces lo guarda en date, etc;
                     switch(data) 
                     {
                         case PAIS: country = atoi(buff2); break;
                         case DATE: date = atol(buff2); j++; break;
                         case INFECTED: infected = atol(buff2); break;
                     }
+
+//                    Como encontro una coma entonces el tipo de dato cambia;
                     data++;
+
+//                    j vale cero porque es el indice de el buffer en el que se van
+//                    guardando los datos;
                     j = 0;
+
+//                    Se limpia el buffer ya que se va a volver a utilizar;
                     clean_buffer(buff2);
+
+
+//                Si en lugar de una coma se encuentra un caracter de nueva line 
+//                entonces significa que esta parado en el ultimo dato
+                } else if (buff1[i] == '\n') {
+
+//                    Si estamos parados en el ultimo dato y no es INFECTED,
+//                    entonces la variable datos no se incremento tres veces, por
+//                    lo cual se supone que falta un dato, devolviendo un codigo de
+//                    error;
+                    if(data != INFECTED) 
+                        return ERROR_DATA_ON_FILE_MISSING;
+
+//                    Si esta todo bien entonces el dato vuelve a ser PAIS que es 
+//                    el primer dato de el archivo de entrada
+                    data = PAIS;
                 }
 
                 switch(data) 
@@ -50,23 +104,12 @@ status_t readlines(void)
 
                 }
             }
-            print_country(country, country_codes);
-            print_date(date);
-            print_infected(infected);
+//            print_country(country, country_codes);
+//            print_date(date);
+//            print_infected(infected);
         }
     }
 
-//    char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
-
-//    if(load_country_codes(country_codes) != OK)
-//        return ERROR_LOADING_COUNTRY_CODES;
-//    empty_country_codes(country_codes);
-
-//    size_t i;
-//    for(i = 0; i < 500; i++)
-//        printf("%s", *(country_codes + i));
-
-
     fclose(fp);
     return OK;
 }
@@ -75,6 +118,9 @@ status_t readlines(void)
 
 status_t print_country(size_t country_code, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH])
 {
+    if(country_codes == NULL)
+        return ERROR_NULL_POINTER;
+
     printf(COUNTRY_PROMPT": %s\n", country_codes[country_code]);
     return OK;
 }
diff --git a/readlines.h b/readlines.h
@@ -19,7 +19,7 @@ typedef enum {
 } data_t;
 
 
-status_t readlines(void);
+status_t readlines(char *src, char *dest);
 status_t print_date(size_t data);
 status_t print_infected(size_t data);
 status_t clean_buffer(char *buffer);