9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit ed88c43ac3b3170c4628cc28557a7b25adfdd461
parent 6a255581fa73bc0f5a12ea53a0b26e3b9c0a8c73
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Fri,  6 Aug 2021 21:26:53 -0300

Updated cleaned source code - removed old non functional functions

Diffstat:
Mexamples/input_gen.py | 4++--
Minclude/cla.h | 2+-
Minclude/load.h | 2+-
Minclude/status.h | 2+-
Minclude/user.h | 14+++++---------
Minclude/utils.h | 9++++-----
Msource/cla.c | 4+++-
Msource/load.c | 10++++++----
Msource/main.c | 1-
Msource/status.c | 48+++++++++++++++++++++++++++++++++++-------------
Msource/user.c | 58++++++++++++++++------------------------------------------
Msource/utils.c | 63+++++++++++++++++++++++++++++++--------------------------------
Msource/vector.c | 28+---------------------------
13 files changed, 106 insertions(+), 139 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 = 15
+LINES = 1500000
 
 # OUTPUT:
 #     ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
@@ -92,7 +92,7 @@ def generate_card(type):
 def generate_file(max_lines):
     id_transaction_base = 123400
     id_user_base = 1
-    id_user_max = 10
+    id_user_max = 1000
     amount_base = 1
     amount_max = 100
     for i in range(max_lines):
diff --git a/include/cla.h b/include/cla.h
@@ -24,7 +24,7 @@ typedef struct {
     char *fmt;
     FILE *fi, *fo;
     unsigned long ti, tf;
-} ADT_cla_t, *cla_t;
+} cla_T, *cla_t;
 
 status_t validate_arguments(int, char **, cla_t);
 status_t check_flags_position(int, char **);
diff --git a/include/load.h b/include/load.h
@@ -9,6 +9,6 @@
 
 #define STR_MSG_END_PROCSS    "End processing input file\nLines processed: "
 
-status_t load_users_to_vector(ADT_Vector_t **, ADT_cla_t *);
+status_t load_users_to_vector(ADT_Vector_t **, cla_t);
 
 #endif
diff --git a/include/status.h b/include/status.h
@@ -11,7 +11,7 @@
 #define MSG_PROCESED_LINES            "Lineas procesadas"
 #define STR_INVALID_CARD_NUMBER        "Número no válido"
 
-#define STATUS_T_MAX 12
+#define STATUS_T_MAX 13
 
 typedef enum {
     OK,
diff --git a/include/user.h b/include/user.h
@@ -4,22 +4,18 @@
 #include "status.h"
 #include "utils.h"
 
-#define OUT_FILE_DELIM    ","
+#define CSV_OUT_FILE_DELIM    ","
 
 typedef unsigned long ulong;
 
 typedef struct {
     /* id: user id / c: user credits / d: user debits */
     ulong id, c, d;
-} ADT_user_t;
+} user_t;
 
-status_t user_create(ADT_user_t **);
-status_t user_dup(ADT_user_t *, ADT_user_t *);
-status_t user_destroy(ADT_user_t **);
-
-status_t user_set_data(ADT_user_t *, ulong, ulong, ulong);
-
-status_t user_add_amount(ADT_user_t *, long);
+status_t user_create(user_t **);
+status_t user_set_data(user_t *, ulong, ulong, ulong);
+status_t user_destroy(user_t **);
 
 int user_equals(const void *, const void *);
 
diff --git a/include/utils.h b/include/utils.h
@@ -32,16 +32,15 @@
 #define XML_STR_CREDIT    "credits"
 #define XML_STR_DEBIT    "debits"
 
-status_t get_date(time_t *, char *);
+status_t string_split(char *, char **, char *);
+void clean_array(char **);
+void clean_buffer(char *);
 
 status_t create_2darray(char ***, size_t, size_t);
 status_t destroy_2darray(char **, size_t);
 
-status_t string_split(char *, char **, char *);
+status_t get_date(time_t *, char *);
 
 bool is_valid_card(char *);
 
-void clean_array(char **);
-void clean_buffer(char *);
-
 #endif
diff --git a/source/cla.c b/source/cla.c
@@ -41,6 +41,8 @@ status_t check_flags_repeated(int argc, char **argv)
     size_t i, j, k, fflags_index;
     int founded_flags[FLAGS_MAX];
 
+    if(argv == NULL) return ERROR_NULL_POINTER;
+
     /* Inicializa a -1 para evitar confusiones con 0 */
     for(i = 0; i < FLAGS_MAX; i++) founded_flags[i] = -1;
 
@@ -115,7 +117,7 @@ status_t cla_create(cla_t *cla)
 {
     if(cla == NULL) return ERROR_NULL_POINTER;
 
-    if((*cla = (cla_t)malloc(sizeof(ADT_cla_t))) == NULL)
+    if((*cla = (cla_t)malloc(sizeof(cla_T))) == NULL)
         return ERROR_MEMORY;
 
     if(((*cla)->fmt = calloc(sizeof(char), 100)) == NULL) {
diff --git a/source/load.c b/source/load.c
@@ -1,14 +1,14 @@
 #include "../include/load.h"
 
-status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
+status_t load_users_to_vector(ADT_Vector_t **v, cla_t cla)
 {
     status_t st;
     char buffer[IN_FILE_MAX_LEN];
     char *endptr1, *endptr2, **data;
-    ADT_user_t *u1, *u2, *u3;
+    user_t *u1, *u2, *u3;
     time_t epoch;
     long long amount;
-    size_t user_pos, parsed_lines;
+    size_t user_pos, parsed_lines, users_created;
     ulong id, c, d;
 
     if(v == NULL || cla == NULL) return ERROR_NULL_POINTER;
@@ -17,6 +17,7 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
         return st;
 
     parsed_lines = 0;
+    users_created = 1;
     while(fgets(buffer, IN_FILE_MAX_LEN, cla->fi)) {
         if((st = user_create(&u1)) != OK) {
             destroy_2darray(data, IN_FILE_FIELDS);
@@ -108,13 +109,14 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
                 destroy_2darray(data, IN_FILE_FIELDS);
                 return st;
             }
+            users_created++;
         }
         clean_buffer(buffer); clean_array(data);
         parsed_lines++;
     } /* End while */
 
     destroy_2darray(data, IN_FILE_FIELDS);
-    printf("%s%8ld\n", STR_MSG_END_PROCSS, parsed_lines);
+    printf("%s%8ld\nUsers created:%11ld\n", STR_MSG_END_PROCSS, parsed_lines, users_created);
 
     return OK;
 }
diff --git a/source/main.c b/source/main.c
@@ -85,7 +85,6 @@ int main (int argc, char *argv[])
             return st;
         }
     }
-
     cla_destroy(cla);
     ADT_Vector_destroy(&v);
     return OK;
diff --git a/source/status.c b/source/status.c
@@ -1,21 +1,43 @@
 #include "../include/status.h"
 
+#ifdef __DEBUG_MODE__
+
+const char *status_strings[] = {
+    "(OK)",
+    "(ELEM_NOT_FOUND)",
+    "(ERROR_MEMORY)",
+    "(ERROR_WRONG_FLAGS)",
+    "(ERROR_WRONG_TIME)",
+    "(ERROR_INVALID_POS)"
+    "(ERROR_MISSING_ARGS)",
+    "(ERROR_OPENING_FILE)",
+    "(ERROR_CORRUPT_DATA)",
+    "(ERROR_FLAG_REPEATED)",
+    "(ERROR_FLAG_NOT_FOUND)",
+    "(ERROR_FORMAT_NOT_FOUND)",
+    "(ERROR_NULL_POINTER)"
+};
+
+#else
+
 const char *status_strings[] = {
-    "Everything executed succesfully. Have a nice day.",
-    "El elemento no pertenece al vector (ELEM_NOT_FOUND)",
-    "Error hubo un problema con la memoria, puede que sea escasa (ERROR_MEMORY)",
-    "There is a flags misspeled",
-    "There is a problem with entered time",
-    "Error en los argumentos ingresados (ERROR_INVALID_POS)"
-    "Error faltan argumentos (ERROR_MISSING_ARGS)",
-    "Error el archivo de se pudo abrir, puede que no existe o que el nombre sea incorrecto",
-    "Error al leer un dato, puede que el archivo de entrada este corrupto",
-    "Error hay una flag repetida",
-    "Error flag no encontrada",
-    "Error el formato de salida no se reconoce",
-    "Hubo un problema durante la ejecucion (ERROR_NULL_POINTER)"
+    "Todo se ejecuto correctamente. Tenga un buen dia.",
+    "El elemento no pertenece al vector.",
+    "ERROR: Hubo un problema con la memoria, puede que sea escasa.",
+    "ERROR: No se reconoce un argumento.",
+    "ERROR: El tiempo ingresado no es correcto.",
+    "ERROR: En los argumentos ingresados.",
+    "ERROR: Faltan argumentos.",
+    "ERROR: El archivo de se pudo abrir, puede que no exista o el nombre sea incorrecto.",
+    "ERROR: Puede que el archivo de entrada este vacio, corrupto o no se encuentre.",
+    "ERROR: Hay una flag repetida.",
+    "ERROR: Flag no encontrada.",
+    "ERROR: El formato de salida no se reconoce.",
+    "ERROR: Hubo un problema durante la ejecucion."
 };
 
+#endif
+
 void show_status(status_t st)
 {
     fprintf(stderr, "\n%s\n", status_strings[st]);
diff --git a/source/user.c b/source/user.c
@@ -1,8 +1,8 @@
 #include "../include/user.h"
 
-status_t user_create(ADT_user_t **user)
+status_t user_create(user_t **user)
 {
-    if((*user = (ADT_user_t *)malloc(sizeof(ADT_user_t))) == NULL)
+    if((*user = (user_t *)malloc(sizeof(user_t))) == NULL)
         return ERROR_MEMORY;
 
     (*user)->id = 0;
@@ -12,23 +12,7 @@ status_t user_create(ADT_user_t **user)
     return OK;
 }
 
-status_t user_dup(ADT_user_t *src, ADT_user_t *dst)
-{
-    status_t st;
-
-    if(src == NULL || dst == NULL) return ERROR_NULL_POINTER;
-
-    if((st = user_create(&dst)) != OK)
-        return st;
-
-    dst->id = src->id;
-    dst->c = src->c;
-    dst->d = src->d;
-
-    return OK;
-}
-
-status_t user_destroy(ADT_user_t **user)
+status_t user_destroy(user_t **user)
 {
     if(user == NULL) return ERROR_NULL_POINTER;
 
@@ -38,7 +22,7 @@ status_t user_destroy(ADT_user_t **user)
     return OK;
 }
 
-status_t user_set_data(ADT_user_t *user, ulong id, ulong c, ulong d) 
+status_t user_set_data(user_t *user, ulong id, ulong c, ulong d) 
 {
     if(user == NULL) return ERROR_NULL_POINTER;
 
@@ -49,24 +33,14 @@ status_t user_set_data(ADT_user_t *user, ulong id, ulong c, ulong d)
     return OK;
 }
 
-status_t user_add_amount(ADT_user_t *user, long amount)
-{
-    if(user == NULL) return ERROR_NULL_POINTER;
-
-    if(amount > 0) user->c += amount;
-    else if(amount < 0) user->d -= amount; /* '-=' Para eliminar el menos    */
-
-    return OK;
-}
-
 int user_equals(const void *a, const void *b)
 {
-    ADT_user_t *A, *B;
+    user_t *A, *B;
 
     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
 
-    A = (ADT_user_t *)a;
-    B = (ADT_user_t *)b;
+    A = (user_t *)a;
+    B = (user_t *)b;
 
     if(A->id == B->id) return 1;
 
@@ -77,9 +51,9 @@ status_t user_print_as_csv(const void *u, FILE *fp)
 {
     if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
 
-    ADT_user_t *user = (ADT_user_t *)u;
+    user_t *user = (user_t *)u;
 
-    fprintf(fp, "%ld%s%ld%s%ld\n", user->id, OUT_FILE_DELIM, user->c, OUT_FILE_DELIM, user->d);
+    fprintf(fp, "%ld%s%ld%s%ld\n", user->id, CSV_OUT_FILE_DELIM, user->c, CSV_OUT_FILE_DELIM, user->d);
 
     return OK;
 }
@@ -88,7 +62,7 @@ status_t user_print_as_xml(const void *u, FILE *fp)
 {
     if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
 
-    ADT_user_t *user = (ADT_user_t *)u;
+    user_t *user = (user_t *)u;
 
     fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_ID, user->id, XML_STR_ID);
     fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_CREDIT, user->c, XML_STR_CREDIT);
@@ -99,12 +73,12 @@ status_t user_print_as_xml(const void *u, FILE *fp)
 
 int user_comparator_credits_minmax(const void *a, const void *b)
 {
-    ADT_user_t *A, *B;
+    user_t *A, *B;
 
     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
 
-    A = *(ADT_user_t **)a;
-    B = *(ADT_user_t **)b;
+    A = *(user_t **)a;
+    B = *(user_t **)b;
 
     if(A->c > B->c) return 1;
     else if(A->c == B->c) return 0;
@@ -113,12 +87,12 @@ int user_comparator_credits_minmax(const void *a, const void *b)
 
 int user_comparator_credits_maxmin(const void *a, const void *b)
 {
-    ADT_user_t *A, *B;
+    user_t *A, *B;
 
     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
 
-    A = *(ADT_user_t **)a;
-    B = *(ADT_user_t **)b;
+    A = *(user_t **)a;
+    B = *(user_t **)b;
 
     if(A->c < B->c) return 1;
     else if(A->c == B->c) return 0;
diff --git a/source/utils.c b/source/utils.c
@@ -17,7 +17,6 @@ status_t string_split(char *s, char **data, char *delim)
     return OK;
 }
 
-
 void clean_buffer(char *buf)
 {
     size_t i;
@@ -36,6 +35,37 @@ void clean_array(char **data)
     }
 }
 
+status_t create_2darray(char ***arr, size_t r, size_t c) {
+    size_t i;
+
+    if(((*arr) = malloc(sizeof(char *) * r)) == NULL)
+        return ERROR_MEMORY;
+
+    for(i = 0; i < r; i++) {
+        if(((*arr)[i] = calloc(sizeof(char), c)) == NULL) {
+            destroy_2darray((*arr), c);
+            return ERROR_MEMORY;
+        }
+    }
+
+    return OK;
+}
+
+status_t destroy_2darray(char **arr, size_t r)
+{
+    size_t i;
+
+    if(arr == NULL) return ERROR_NULL_POINTER;
+
+    for(i = 0; i < r; i++) {
+        free(arr[i]);
+        arr[i] = NULL;
+    }
+
+    free(arr);
+    return OK;
+}
+
 status_t get_date(time_t *e, char *str)
 {
     struct tm tm;
@@ -89,34 +119,3 @@ bool is_valid_card(char *card_no)
 
     return (sum % 10) ? false : true;
 }
-
-status_t create_2darray(char ***arr, size_t r, size_t c) {
-    size_t i;
-
-    if(((*arr) = malloc(sizeof(char *) * r)) == NULL)
-        return ERROR_MEMORY;
-
-    for(i = 0; i < r; i++) {
-        if(((*arr)[i] = calloc(sizeof(char), c)) == NULL) {
-            destroy_2darray((*arr), c);
-            return ERROR_MEMORY;
-        }
-    }
-
-    return OK;
-}
-
-status_t destroy_2darray(char **arr, size_t r)
-{
-    size_t i;
-
-    if(arr == NULL) return ERROR_NULL_POINTER;
-
-    for(i = 0; i < r; i++) {
-        free(arr[i]);
-        arr[i] = NULL;
-    }
-
-    free(arr);
-    return OK;
-}
diff --git a/source/vector.c b/source/vector.c
@@ -55,40 +55,14 @@ status_t ADT_Vector_destroy(ADT_Vector_t **v)
     return OK;
 }
 
+/* Esta funcion no puede crear elementos nuevos, solo setear los que ya estan */
 status_t ADT_Vector_set_elem(ADT_Vector_t **v, void *e, size_t pos)
 {
-    /* void ** aux; */
-    /* size_t i, alloc_size; */
-
     if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
     else if(pos > (*v)->size) return ERROR_INVALID_POS;
 
-    /* alloc_size = (*v)->alloc; */
-
-    /* /1* Cuenta cuanto tiene que alargar el arreglo p/ que entre el nuevo elemento *1/ */
-    /* while(pos >= alloc_size) { */
-        /* alloc_size *= VECTOR_GROWTH_FACTOR; */
-    /* } */
-
-    /* Extiende el arreglo en caso de que no haya memoria suficiente */
-    /* if(pos > (*v)->alloc) { */
-    /*     if((aux = (void **)realloc((*v)->a, sizeof(void *) * alloc_size)) == NULL) */
-    /*         return ERROR_MEMORY; */
-    /*     else { */
-    /*         (*v)->a = aux; */
-    /*         (*v)->alloc = alloc_size; */
-    /*     } */
-    /* } */
-
-    /* for(i = (*v)->size; i < pos; i++) */
-    /*     (*v)->a[pos] = NULL; */
-
     (*v)->a[pos] = e;
 
-    /* if(pos >= (*v)->size) { */
-    /*     (*v)->size = (pos + 1); */
-    /* } */
-
     return OK;
 }