9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit 037c246f134873305f86d3ccc3a38ec29308ff66
parent 15adeb13e4c04308f360b41b909a1ab5b3f6c706
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Fri,  6 Aug 2021 17:35:22 -0300

Added new module 'load'

Diffstat:
MMakefile | 13++++++++-----
Ainclude/load.h | 12++++++++++++
Minclude/status.h | 2+-
Minclude/utils.h | 3+--
Minclude/vector.h | 6++++--
Asource/load.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msource/main.c | 1+
Msource/status.c | 9+++++----
Msource/utils.c | 132+++----------------------------------------------------------------------------
Msource/vector.c | 18+++++++++++++++++-
10 files changed, 163 insertions(+), 143 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,10 +6,10 @@ PROGNAME=main
 
 all: main clean
 
-main: cla.o status.o utils.o main.o user.o vector.o
-    $(CC) $(CFLAGS) main.o cla.o status.o utils.o user.o vector.o -o $(PROGNAME)
+main: cla.o status.o utils.o main.o user.o vector.o load.o
+    $(CC) $(CFLAGS) main.o cla.o status.o utils.o user.o load.o vector.o -o $(PROGNAME)
 
-main.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h $(HFOLDER)/user.h
+main.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/load.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c
 
 cla.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h
@@ -27,6 +27,9 @@ utils.o: $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/vector.h
 user.o: $(HFOLDER)/status.h $(HFOLDER)/user.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/user.c
 
+load.o: $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/vector.h $(HFOLDER)/utils.h $(HFOLDER)/load.h $(HFOLDER)/cla.h
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/load.c
+
 clean:
     rm *.o
 
@@ -45,8 +48,8 @@ run50:
 run500:
     ./main -fmt csv -out output.csv -in examples/test_file_500.csv -ti 1320498000 -tf 1320529000
 
-run5k:
-    ./main -fmt csv -out output.csv -in examples/test_file_5k.csv -ti 1320498000 -tf 1320529000
+run10k:
+    ./main -fmt csv -out output.csv -in examples/test_file_10k.csv -ti 1320498000 -tf 1320529000
 
 run50k:
     ./main -fmt csv -out output.csv -in examples/test_file_50k.csv -ti 1320498000 -tf 1420529000
diff --git a/include/load.h b/include/load.h
@@ -0,0 +1,12 @@
+#ifndef LOAD__H
+#define LOAD__H
+
+#include "../include/cla.h"
+#include "../include/user.h"
+#include "../include/utils.h"
+#include "../include/vector.h"
+#include "../include/status.h"
+
+status_t load_users_to_vector(ADT_Vector_t **, ADT_cla_t *);
+
+#endif
diff --git a/include/status.h b/include/status.h
@@ -15,6 +15,7 @@
 
 typedef enum {
     OK,
+    ELEM_NOT_FOUND,
     ERROR_MEMORY,
     ERROR_WRONG_FLAGS,
     ERROR_WRONG_TIME,
@@ -25,7 +26,6 @@ typedef enum {
     ERROR_FLAG_REPEATED,
     ERROR_FLAG_NOT_FOUND,
     ERROR_FORMAT_NOT_FOUND,
-    ERROR_USER_NOT_FOUND,
     ERROR_NULL_POINTER
 } status_t;
 
diff --git a/include/utils.h b/include/utils.h
@@ -34,11 +34,10 @@
 
 status_t get_date(time_t *, char *);
 
-status_t create_2darray(char **, size_t, size_t);
+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 load_users_to_vector(ADT_Vector_t **, ADT_cla_t *);
 
 bool is_valid_card(char *);
 
diff --git a/include/vector.h b/include/vector.h
@@ -28,11 +28,13 @@ status_t ADT_Vector_create(ADT_Vector_t **);
 status_t ADT_Vector_add(ADT_Vector_t **, void *);
 status_t ADT_Vector_destroy(ADT_Vector_t **);
 
-status_t ADT_Vector_set(ADT_Vector_t **, void *, size_t);
+status_t ADT_Vector_set_elem(ADT_Vector_t **, void *, size_t);
 status_t ADT_Vector_print(const ADT_Vector_t *, FILE *);
 status_t ADT_Vector_sort(ADT_Vector_t *, comparator_t);
 
-void *ADT_Vector_get_elem(const ADT_Vector_t *v, void *e);
+void *ADT_Vector_get_elem(const ADT_Vector_t *, void *);
+
+status_t ADT_Vector_get_elem_pos(const ADT_Vector_t *, void *, size_t *);
 
 status_t ADT_Vector_set_printer(ADT_Vector_t *, printer_t);
 status_t ADT_Vector_set_comparator(ADT_Vector_t *, comparator_t);
diff --git a/source/load.c b/source/load.c
@@ -0,0 +1,110 @@
+#include "../include/load.h"
+
+status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
+{
+    status_t st;
+    char buffer[IN_FILE_MAX_LEN];
+    char *endptr, **data;
+    ADT_user_t *user, *user_tmp;
+    time_t epoch;
+    long long amount;
+    ulong id, c, d;
+
+    if(v == NULL || cla == NULL) return ERROR_NULL_POINTER;
+
+    if((st = create_2darray(&data, IN_FILE_FIELDS, IN_FILE_FIELDS_MAX_LEN)) != OK)
+        return st;
+
+    /* Crea un usuario temporal */
+    if((st = user_create(&user_tmp)) != OK) {
+        destroy_2darray(data, IN_FILE_FIELDS);
+        return st;
+    }
+
+    while(fgets(buffer, IN_FILE_MAX_LEN, cla->fi)) {
+        if((st = string_split(buffer, data, IN_FILE_DELIM)) != OK) {
+            free(user_tmp);
+            destroy_2darray(data, IN_FILE_FIELDS);
+            return st;
+        }
+
+        id = strtol(data[POS_USER_ID], &endptr, 10);
+        if(*endptr != '\0') {
+            free(user_tmp);
+            destroy_2darray(data, IN_FILE_FIELDS);
+            return ERROR_CORRUPT_DATA;
+        }
+
+        amount = strtol(data[POS_AMOUNT], &endptr, 10);
+        if(*endptr != '\0') {
+            free(user_tmp);
+            destroy_2darray(data, IN_FILE_FIELDS);
+            return ERROR_CORRUPT_DATA;
+        }
+
+        if(amount > 0) { c = amount; d = 0; }
+        else if(amount < 0) { c = 0; d = -amount; }
+
+        if((st = user_set_data(user_tmp, id, c, d)) != OK) {
+            free(user_tmp);
+            destroy_2darray(data, IN_FILE_FIELDS);
+            return ERROR_CORRUPT_DATA;
+        }
+
+        if((st = get_date(&epoch, data[POS_TXN_DATE])) != OK){
+            free(user_tmp);
+            destroy_2darray(data, IN_FILE_FIELDS);
+            return ERROR_CORRUPT_DATA;
+        }
+
+        /* Comprueba el que el tiempo este dentro de los valores pasados como argumentos */
+        if(epoch < cla->ti) continue;
+        else if(epoch > cla->tf) break;
+
+        /* Comprueba que la tarjeta usada en la transaccion sea la correcta */
+        if(!is_valid_card(data[POS_CARD_NUMBER])) {
+            fprintf(stderr, "%s: %s\n",STR_INVALID_CARD_NUMBER, data[POS_CARD_NUMBER]);
+            continue;
+        }
+
+        /* Busca el id del usuario en el vector */
+        if((user = ADT_Vector_get_elem(*v, user_tmp)) != NULL) {
+            /* Si lo encuentra le suma el monto correspondiente */
+            if((st = user_add_amount(user, amount)) != OK) {
+                destroy_2darray(data, IN_FILE_FIELDS);
+                free(user_tmp);
+                return st;
+            }
+        }
+
+        /* Si no lo encuentra crea un usuario nuevo */
+        else { 
+            if((st = user_create(&user)) != OK) {
+                destroy_2darray(data, IN_FILE_FIELDS);
+                free(user_tmp);
+                return st;
+            }
+
+            if((st = user_set_data(user, id, c, d))) {
+                destroy_2darray(data, IN_FILE_FIELDS);
+                free(user_tmp);
+                return st;
+            }
+
+            /* Y lo agrega al vector */
+            if((st = ADT_Vector_add(v, user)) != OK){
+                destroy_2darray(data, IN_FILE_FIELDS);
+                free(user_tmp);
+                return st;
+            }
+        }
+        clean_buffer(buffer);
+        clean_array(data);
+    } /* End while */
+
+        
+    destroy_2darray(data, IN_FILE_FIELDS);
+    free(user_tmp);
+
+    return OK;
+}
diff --git a/source/main.c b/source/main.c
@@ -1,5 +1,6 @@
 #include "../include/cla.h"
 #include "../include/user.h"
+#include "../include/load.h"
 #include "../include/utils.h"
 #include "../include/status.h"
 #include "../include/vector.h"
diff --git a/source/status.c b/source/status.c
@@ -2,17 +2,18 @@
 
 const char *status_strings[] = {
     "Everything executed succesfully. Have a nice day.",
-    "There is a problem with the memory",
+    "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",
-    "There are arguments missing",
+    "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",
-    "Error usuario no encontrado, puede que el archivo de entrada este corrupto",
-    "ERROR_NULL_POINTER"
+    "Hubo un problema durante la ejecucion (ERROR_NULL_POINTER)"
 };
 
 void show_status(status_t st)
diff --git a/source/utils.c b/source/utils.c
@@ -36,21 +36,6 @@ void clean_array(char **data)
     }
 }
 
-status_t array_destroy(char **data, size_t fields)
-{
-    size_t i;
-
-    if(data == NULL) return ERROR_NULL_POINTER;
-
-    for(i = 0; i < fields; i++) {
-        free(data[i]);
-        data[i] = NULL;
-    }
-
-    free(data);
-    return OK;
-}
-
 status_t get_date(time_t *e, char *str)
 {
     struct tm tm;
@@ -105,15 +90,15 @@ bool is_valid_card(char *card_no)
     return (sum % 10) ? false : true;
 }
 
-status_t create_2darray(char **arr, size_t r, size_t c) {
+status_t create_2darray(char ***arr, size_t r, size_t c) {
     size_t i;
 
-    if((arr = malloc(sizeof(char *) * r)) == NULL)
+    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);
+        if(((*arr)[i] = calloc(sizeof(char), c)) == NULL) {
+            destroy_2darray((*arr), c);
             return ERROR_MEMORY;
         }
     }
@@ -135,112 +120,3 @@ status_t destroy_2darray(char **arr, size_t r)
     free(arr);
     return OK;
 }
-
-status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
-{
-    status_t st;
-    char buffer[IN_FILE_MAX_LEN];
-    char *endptr, **data;
-    ADT_user_t *user, *user_tmp;
-    time_t epoch;
-    long long amount;
-    ulong id, c, d;
-
-    if(v == NULL || cla == NULL) return ERROR_NULL_POINTER;
-
-    if((st = create_2darray(data, IN_FILE_FIELDS, IN_FILE_FIELDS_MAX_LEN)) != OK)
-        return st;
-
-    /* Crea un usuario temporal */
-    if((st = user_create(&user_tmp)) != OK) {
-        array_destroy(data, IN_FILE_FIELDS);
-        return st;
-    }
-
-    while(fgets(buffer, IN_FILE_MAX_LEN, cla->fi)) {
-        if((st = string_split(buffer, data, IN_FILE_DELIM)) != OK) {
-            free(user_tmp);
-            destroy_2darray(data, IN_FILE_FIELDS);
-            return st;
-        }
-
-        id = strtol(data[POS_USER_ID], &endptr, 10);
-        if(*endptr != '\0') {
-            free(user_tmp);
-            destroy_2darray(data, IN_FILE_FIELDS);
-            return ERROR_CORRUPT_DATA;
-        }
-
-        amount = strtol(data[POS_AMOUNT], &endptr, 10);
-        if(*endptr != '\0') {
-            free(user_tmp);
-            destroy_2darray(data, IN_FILE_FIELDS);
-            return ERROR_CORRUPT_DATA;
-        }
-
-        if(amount > 0) { c = amount; d = 0; }
-        else if(amount < 0) { c = 0; d = -amount; }
-
-        if((st = user_set_data(user_tmp, id, c, d)) != OK) {
-            free(user_tmp);
-            destroy_2darray(data, IN_FILE_FIELDS);
-            return ERROR_CORRUPT_DATA;
-        }
-
-        if((st = get_date(&epoch, data[POS_TXN_DATE])) != OK){
-            free(user_tmp);
-            destroy_2darray(data, IN_FILE_FIELDS);
-            return ERROR_CORRUPT_DATA;
-        }
-
-        /* Comprueba el que el tiempo este dentro de los valores pasados como argumentos */
-        if(epoch < cla->ti) continue;
-        else if(epoch > cla->tf) break;
-
-        /* Comprueba que la tarjeta usada en la transaccion sea la correcta */
-        if(!is_valid_card(data[POS_CARD_NUMBER])) {
-            fprintf(stderr, "%s: %s\n",STR_INVALID_CARD_NUMBER, data[POS_CARD_NUMBER]);
-            continue;
-        }
-
-        /* Busca el id del usuario en el vector */
-        if((user = ADT_Vector_get_elem(*v, user_tmp)) != NULL) {
-            /* Si lo encuentra le suma el monto correspondiente */
-            if((st = user_add_amount(user, amount)) != OK) {
-                destroy_2darray(data, IN_FILE_FIELDS);
-                free(user_tmp);
-                return st;
-            }
-        }
-
-        /* Si no lo encuentra crea un usuario nuevo */
-        else { 
-            if((st = user_create(&user)) != OK) {
-                destroy_2darray(data, IN_FILE_FIELDS);
-                free(user_tmp);
-                return st;
-            }
-
-            if((st = user_set_data(user, id, c, d))) {
-                destroy_2darray(data, IN_FILE_FIELDS);
-                free(user_tmp);
-                return st;
-            }
-
-            /* Y lo agrega al vector */
-            if((st = ADT_Vector_add(v, user)) != OK){
-                destroy_2darray(data, IN_FILE_FIELDS);
-                free(user_tmp);
-                return st;
-            }
-        }
-        clean_buffer(buffer);
-        clean_array(data);
-    } /* End while */
-
-        
-    destroy_2darray(data, IN_FILE_FIELDS);
-    free(user_tmp);
-
-    return OK;
-}
diff --git a/source/vector.c b/source/vector.c
@@ -55,7 +55,7 @@ status_t ADT_Vector_destroy(ADT_Vector_t **v)
     return OK;
 }
 
-status_t ADT_Vector_set(ADT_Vector_t **v, void *e, size_t pos)
+status_t ADT_Vector_set_elem(ADT_Vector_t **v, void *e, size_t pos)
 {
     void ** aux;
     size_t i, alloc_size;
@@ -108,6 +108,22 @@ void *ADT_Vector_get_elem(const ADT_Vector_t *v, void *e)
     return NULL;
 }
 
+status_t ADT_Vector_get_elem_pos(const ADT_Vector_t *v, void *e, size_t *pos)
+{
+    size_t i;
+
+    if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+
+    for(i = 0; i < v->size; i++) { 
+        if(v->comparator(e, v->a[i])) {
+            *pos = i;
+            return OK;
+        }
+    }
+
+    return ELEM_NOT_FOUND;
+}
+
 status_t ADT_Vector_print(const ADT_Vector_t *v, FILE *fp)
 {
     status_t st;