9511_project03

Command line program that parses a bank .csv file and generates and output file in format .csv or .xml containing the debits and credits of every user.
Index Commits Files Refs README LICENSE
commit 419878b94389b7edad5478e9e639bc86d24eabde
parent b0bd63f5cbe875194b8929d20802394ca794da5a
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Tue, 27 Jul 2021 20:04:10 -0300

Update: added ADT_Vector module

Diffstat:
MMakefile | 3+++
Mexamples/input_gen.py | 2+-
Minclude/cla.h | 2--
Ainclude/vector.h | 40++++++++++++++++++++++++++++++++++++++++
Asource/vector.c | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 205 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
@@ -51,6 +51,9 @@ run50k:
 run500k:
     ./main -fmt csv -out output.csv -in examples/test_file_500k.csv -ti 1320498000 -tf 1420529000
 
+run50m:
+    ./main -fmt csv -out output.csv -in examples/test_file_50m.csv -ti 1320498000 -tf 1420529000
+
 run50xml:
     ./main -fmt xml -out output.xml -in examples/test_file_50.csv -ti 1320498000 -tf 1320498049
 
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 = 500000
+LINES = 50000000
 
 # OUTPUT:
 #     ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
diff --git a/include/cla.h b/include/cla.h
@@ -33,8 +33,6 @@ status_t cla_create(cla_t *cla);
 status_t cla_setup(int argc, char **argv, cla_t *cla);
 status_t cla_destroy(cla_t *cla);
 
-void clean(cla_t cla);
-
 extern const char *available_flags[FLAGS_MAX];
 extern const char *available_formats[FORMATS_MAX];
 
diff --git a/include/vector.h b/include/vector.h
@@ -0,0 +1,40 @@
+#ifndef VECTOR__H
+#define VECTOR__H
+
+#define VECTOR_INIT_SIZE        10
+#define VECTOR_GROWTH_FACTOR    2
+
+#define STR_XML_ROOT    "root"
+#define STR_XML_HEADER    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+
+#include <stdlib.h>
+
+#include "types.h"
+
+typedef status_t (*printer_t)(const void *, FILE *);
+/* typedef int (*comparator_t)(void *, void *); */
+
+typedef int (*comparator_t)(const void *, const void *);
+
+typedef struct {
+    void **a;
+    size_t size, alloc;
+
+    printer_t printer;
+
+} ADT_Vector_t;
+
+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_load(ADT_Vector_t *, FILE *);
+status_t ADT_Vector_print(ADT_Vector_t *, FILE *);
+status_t ADT_Vector_sort(ADT_Vector_t *, comparator_t);
+
+status_t ADT_Vector_set_printer(ADT_Vector_t *, printer_t);
+
+status_t ADT_Vector_export_as_xml(ADT_Vector_t *, FILE *, printer_t);
+status_t ADT_Vector_export_as_csv(ADT_Vector_t *, FILE *, printer_t);
+
+#endif
diff --git a/source/vector.c b/source/vector.c
@@ -0,0 +1,161 @@
+#include "vector.h"
+
+void swap(void *a, void *b)
+{
+    int *A = (int *)a;
+    int *B = (int *)b;
+
+    int tmp = *A;
+    *A = *B;
+    *B = tmp;
+}
+
+status_t ADT_Vector_create(ADT_Vector_t **v)
+{
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    if((*v = (ADT_Vector_t *)malloc(sizeof(ADT_Vector_t))) == NULL)
+        return ERROR_MEMORY;
+
+    if(((*v)->a = (void **)malloc(sizeof(void *) * VECTOR_INIT_SIZE)) == NULL)
+        return ERROR_MEMORY;
+
+    (*v)->size = 0;
+    (*v)->alloc = VECTOR_INIT_SIZE;
+    (*v)->printer = NULL;
+
+    return OK;
+}
+
+status_t ADT_Vector_add(ADT_Vector_t **v, void *e)
+{
+    void *tmp;
+
+    if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+
+    /* Extends the array in case low memory left */
+    if(((*v)->size + 1) == (*v)->alloc) {
+        if((tmp = (void **)realloc((*v)->a, sizeof(void *) * (*v)->alloc * VECTOR_GROWTH_FACTOR)) == NULL)
+            return ERROR_MEMORY;
+        else {
+            (*v)->a = tmp;
+            (*v)->alloc *= VECTOR_GROWTH_FACTOR;
+        }
+    }
+
+    (*v)->a[(*v)->size++] = e;
+
+    return OK;
+}
+
+status_t ADT_Vector_destroy(ADT_Vector_t **v)
+{
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    for(size_t i = 0; i < (*v)->size; i++) {
+        free((*v)->a[i]);
+        (*v)->a[i] = NULL;
+    }
+
+    free((*v)->a);
+    free(*v);
+    *v = NULL;
+
+    return OK;
+}
+
+#define BUFFER_SIZE    100
+status_t ADT_Vector_load(ADT_Vector_t *v, FILE *fi)
+{
+    char *b, *endptr;
+    int  *tmp;
+
+    if(v == NULL || fi == NULL) return ERROR_NULL_POINTER;
+
+    if((b = calloc(sizeof(char), BUFFER_SIZE)) == NULL)
+        return ERROR_MEMORY;
+
+    while(fgets(b, BUFFER_SIZE, fi))
+    {
+        tmp = (int *)malloc(sizeof(int));
+
+        *tmp = (int)strtol(b, &endptr, 10);
+        if(*endptr != '\n') return ERROR_CORRUPT_FILE;
+
+        ADT_Vector_add(&v, tmp);
+
+        ADT_Vector_print(v, stdout);
+    }
+
+    return OK;
+}
+
+status_t ADT_Vector_print(ADT_Vector_t *v, FILE *fp)
+{
+    size_t i;
+
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    for(i = 0; i < v->size; i++)
+        v->printer(v->a[i], fp);
+
+    putchar('\n');
+    return OK;
+}
+
+status_t ADT_Vector_sort(ADT_Vector_t *v, comparator_t pf)
+{
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    /* Bubble sort */
+    /* for(size_t i = 0; i < v->size; i++) */
+    /*     for(size_t j = 0; j < (v->size - i - 1); j++) */
+    /*         if(pf(v->a[j], v->a[j + 1]) > 0) */
+    /*             swap(v->a[j], v->a[j + 1]); */
+
+    /* Quick sort */
+    qsort(v->a, v->size, sizeof(void *), pf);
+
+    return OK;
+}
+
+status_t ADT_Vector_set_printer(ADT_Vector_t *v, printer_t pf)
+{
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    v->printer = pf;
+
+    return OK;
+}
+
+status_t ADT_Vector_export_as_csv(ADT_Vector_t *v, FILE *fp, printer_t pf)
+{
+    status_t st;
+    size_t i;
+
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    for(i = 0; i < v->size; i++)
+        if((st = (*pf)(v->a[i], fp)) != OK)
+            return st;
+
+    return OK;
+}
+
+status_t ADT_Vector_export_as_xml(ADT_Vector_t *v, FILE *fp, printer_t pf)
+{
+    status_t st;
+    size_t i;
+
+    if(v == NULL) return ERROR_NULL_POINTER;
+
+    fprintf(fp, "%s\n", STR_XML_HEADER);
+    fprintf(fp, "<%s>\n", STR_XML_ROOT);
+
+    for(i = 0; i < v->size; i++)
+        if((st = (*pf)(v->a[i], fp)) != OK) 
+            return st;
+
+    fprintf(fp, "</%s>\n", STR_XML_ROOT);
+    return OK;
+}