9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit f132c749e54dad46fa91546958c9efd9e889e4bf
parent 5f508c961494cb93003b230621e5a7706f450416
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Thu, 29 Jul 2021 22:27:16 -0300

Updated source code and input file generator

Diffstat:
Mexamples/input_gen.py | 2+-
Minclude/user.h | 10+++++-----
Minclude/vector.h | 4++--
Msource/main.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
Msource/user.c | 10++++++++++
Msource/vector.c | 9++++-----
6 files changed, 72 insertions(+), 43 deletions(-)
diff --git a/examples/input_gen.py b/examples/input_gen.py
@@ -2,7 +2,7 @@ from string import digits
 from time import strftime, gmtime
 from random import randint, choice
 
-LINES = 100000
+LINES = 10000
 
 # OUTPUT:
 #     ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
diff --git a/include/user.h b/include/user.h
@@ -14,15 +14,15 @@ typedef struct {
 } ADT_user_t;
 
 status_t user_create(ADT_user_t **);
-status_t user_set_data(ADT_user_t *, char **data);
-status_t user_add_amount(ADT_user_t *, long amount);
+status_t user_destroy(ADT_user_t **);
+status_t user_set_data(ADT_user_t *, char **);
+status_t user_add_amount(ADT_user_t *, long);
 
 int user_equals(const void *, const void *);
 
-status_t user_print_as_csv(const void *u, FILE *fp);
-status_t user_print_as_xml(const void *u, FILE *fp);
+status_t user_print_as_csv(const void *, FILE *);
+status_t user_print_as_xml(const void *, FILE *);
 
-int user_comparator(const void *, FILE *);
 int user_comparator_credits_minmax(const void *, const void *);
 int user_comparator_credits_maxmin(const void *, const void *);
 
diff --git a/include/vector.h b/include/vector.h
@@ -34,7 +34,7 @@ void *ADT_Vector_get_elem(const ADT_Vector_t *v, void *e);
 status_t ADT_Vector_set_printer(ADT_Vector_t *, printer_t);
 status_t ADT_Vector_set_comparator(ADT_Vector_t *, comparator_t);
 
-status_t ADT_Vector_export_as_xml(const ADT_Vector_t *, FILE *, printer_t);
-status_t ADT_Vector_export_as_csv(const ADT_Vector_t *, FILE *, printer_t);
+status_t ADT_Vector_export_as_xml(const ADT_Vector_t *, FILE *);
+status_t ADT_Vector_export_as_csv(const ADT_Vector_t *, FILE *);
 
 #endif
diff --git a/source/main.c b/source/main.c
@@ -63,26 +63,6 @@ int main (int argc, char *argv[])
         return st;
     }
 
-    /* Setea el impresor a ADT_Vector */
-    if(!strcmp(cla->fmt, STR_FMT_CSV)) {
-        if((st = ADT_Vector_set_printer(v, user_print_as_csv)) != OK) {
-            show_status(st);
-            cla_destroy(cla);
-            ADT_Vector_destroy(&v);
-            array_destroy(data, IN_FILE_FIELDS);
-            return st;
-        }
-    }
-    else if(!strcmp(cla->fmt, STR_FMT_XML)) {
-        if((st = ADT_Vector_set_printer(v, user_print_as_xml)) != OK) {
-            show_status(st);
-            cla_destroy(cla);
-            ADT_Vector_destroy(&v);
-            array_destroy(data, IN_FILE_FIELDS);
-            return st;
-        }
-    }
-
     /* Crea un usuario temporal */
     if((st = user_create(&user_tmp)) != OK) {
         show_status(st);
@@ -101,6 +81,7 @@ int main (int argc, char *argv[])
             cla_destroy(cla);
             ADT_Vector_destroy(&v);
             array_destroy(data, IN_FILE_FIELDS);
+            free(user_tmp);
             return st;
         }
 
@@ -110,11 +91,19 @@ int main (int argc, char *argv[])
             cla_destroy(cla);
             ADT_Vector_destroy(&v);
             array_destroy(data, IN_FILE_FIELDS);
+            free(user_tmp);
             return st;
         }
 
         amount = strtol(data[POS_AMOUNT], &endptr, 10);
-        if(*endptr != '\0') return ERROR_CORRUPT_DATA;
+        if(*endptr != '\0') {
+            show_status(st);
+            cla_destroy(cla);
+            ADT_Vector_destroy(&v);
+            array_destroy(data, IN_FILE_FIELDS);
+            free(user_tmp);
+            return ERROR_CORRUPT_DATA;
+        }
 
         /* Transforma el tiempo de la linea leida a formato UNIX */
         get_date(&epoch, data);
@@ -137,6 +126,7 @@ int main (int argc, char *argv[])
                 cla_destroy(cla);
                 ADT_Vector_destroy(&v);
                 array_destroy(data, IN_FILE_FIELDS);
+                free(user_tmp);
                 return st;
             }
         }
@@ -148,6 +138,7 @@ int main (int argc, char *argv[])
                 cla_destroy(cla);
                 ADT_Vector_destroy(&v);
                 array_destroy(data, IN_FILE_FIELDS);
+                free(user_tmp);
                 return st;
             }
 
@@ -156,6 +147,7 @@ int main (int argc, char *argv[])
                 cla_destroy(cla);
                 ADT_Vector_destroy(&v);
                 array_destroy(data, IN_FILE_FIELDS);
+                free(user_tmp);
                 return st;
             }
 
@@ -166,12 +158,13 @@ int main (int argc, char *argv[])
                 cla_destroy(cla);
                 ADT_Vector_destroy(&v);
                 array_destroy(data, IN_FILE_FIELDS);
+                free(user_tmp);
                 return st;
             }
         }
         clean_buffer(buffer);
         clean_array(data);
-    }
+    } /* End while */
 
     /* Ordena el vector con los usuarios */
     if((st = ADT_Vector_sort(v, user_comparator_credits_maxmin)) != OK) {
@@ -183,14 +176,41 @@ int main (int argc, char *argv[])
         return st;
     }
 
-    /* Imprime el vector con los usuarios de acuerdo al argumento recibido */
-    if((st = ADT_Vector_print(v, cla->fo)) != OK) {
-        show_status(st);
-        free(user_tmp);
-        cla_destroy(cla);
-        ADT_Vector_destroy(&v);
-        array_destroy(data, IN_FILE_FIELDS);
-        return st;
+    /* Setea el impresor a ADT_Vector */
+    if(!strcmp(cla->fmt, STR_FMT_CSV)) {
+        if((st = ADT_Vector_set_printer(v, user_print_as_csv)) != OK) {
+            show_status(st);
+            cla_destroy(cla);
+            ADT_Vector_destroy(&v);
+            array_destroy(data, IN_FILE_FIELDS);
+            return st;
+        }
+        /* E imprime el vector con los usuarios */
+        if((st = ADT_Vector_export_as_csv(v, cla->fo)) != OK) {
+            show_status(st);
+            free(user_tmp);
+            cla_destroy(cla);
+            ADT_Vector_destroy(&v);
+            array_destroy(data, IN_FILE_FIELDS);
+            return st;
+        }
+    }
+    else if(!strcmp(cla->fmt, STR_FMT_XML)) {
+        if((st = ADT_Vector_set_printer(v, user_print_as_xml)) != OK) {
+            show_status(st);
+            cla_destroy(cla);
+            ADT_Vector_destroy(&v);
+            array_destroy(data, IN_FILE_FIELDS);
+            return st;
+        }
+        if((st = ADT_Vector_export_as_xml(v, cla->fo)) != OK) {
+            show_status(st);
+            free(user_tmp);
+            cla_destroy(cla);
+            ADT_Vector_destroy(&v);
+            array_destroy(data, IN_FILE_FIELDS);
+            return st;
+        }
     }
 
     free(user_tmp);
diff --git a/source/user.c b/source/user.c
@@ -12,6 +12,16 @@ status_t user_create(ADT_user_t **user)
     return OK;
 }
 
+status_t user_destroy(ADT_user_t **user)
+{
+    if(user == NULL) return ERROR_NULL_POINTER;
+
+    free(*user);
+    *user = NULL;
+
+    return OK;
+}
+
 status_t user_set_data(ADT_user_t *user, char **data) 
 {
     char *endptr;
diff --git a/source/vector.c b/source/vector.c
@@ -119,7 +119,6 @@ status_t ADT_Vector_print(const ADT_Vector_t *v, FILE *fp)
         if((st = v->printer(v->a[i], fp)) != OK)
             return st;
 
-    putchar('\n');
     return OK;
 }
 
@@ -151,7 +150,7 @@ status_t ADT_Vector_set_comparator(ADT_Vector_t *v, comparator_t pf)
     return OK;
 }
 
-status_t ADT_Vector_export_as_csv(const ADT_Vector_t *v, FILE *fp, printer_t pf)
+status_t ADT_Vector_export_as_csv(const ADT_Vector_t *v, FILE *fp)
 {
     status_t st;
     size_t i;
@@ -159,13 +158,13 @@ status_t ADT_Vector_export_as_csv(const ADT_Vector_t *v, FILE *fp, printer_t pf)
     if(v == NULL) return ERROR_NULL_POINTER;
 
     for(i = 0; i < v->size; i++)
-        if((st = (*pf)(v->a[i], fp)) != OK)
+        if((st = v->printer(v->a[i], fp)) != OK)
             return st;
 
     return OK;
 }
 
-status_t ADT_Vector_export_as_xml(const ADT_Vector_t *v, FILE *fp, printer_t pf)
+status_t ADT_Vector_export_as_xml(const ADT_Vector_t *v, FILE *fp)
 {
     status_t st;
     size_t i;
@@ -177,7 +176,7 @@ status_t ADT_Vector_export_as_xml(const ADT_Vector_t *v, FILE *fp, printer_t pf)
 
     for(i = 0; i < v->size; i++){
         fprintf(fp, "\t<%s>\n", XML_STR_ROW);
-        if((st = (*pf)(v->a[i], fp)) != OK)
+        if((st = v->printer(v->a[i], fp)) != OK)
             return st;
 
         fprintf(fp, "\t</%s>\n", XML_STR_ROW);