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 3b2106a35731486e4f5fb3cea45f4bc1962e02ee
parent 027f4734c9db93be71c4f3a71e4c4422f0c4f6ac
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Wed, 14 Jul 2021 13:20:57 -0300

Update: ...

Diffstat:
Minclude/cla.h | 5+++++
Minclude/io.h | 11+++++++++--
Minclude/sort.h | 3++-
Minclude/types.h | 5-----
Msource/io.c | 28+++++++++++++++++-----------
Msource/main.c | 5+----
Msource/sort.c | 13++++++++-----
7 files changed, 42 insertions(+), 28 deletions(-)
diff --git a/include/cla.h b/include/cla.h
@@ -20,6 +20,11 @@ typedef enum {
     FLAG_TF
 } flags_t;
 
+typedef struct {
+    char *fmt, *fi, *fo;
+    unsigned long ti, tf;
+} ADT_cla_t, *cla_t;
+
 status_t validate_arguments(int argc, char **argv);
 status_t check_flags_position(int argc, char **argv);
 status_t check_flags_repeated(int argc, char **argv);
diff --git a/include/io.h b/include/io.h
@@ -5,7 +5,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "cla.h"
 #include "types.h"
+#include "sort.h"
 
 #define INPUT_FILE_FIELDS 6
 #define BUFFER_SIZE        1000
@@ -19,12 +21,17 @@ typedef enum {
     POS_DESC
 } csv_pos_t;
 
-status_t tmp_gen(cla_t cla, FILE **bfp);
 status_t set_data(user_t *user, char **data);
-status_t tmp_sort(FILE *tmp);
+
+status_t tmp_file_write(FILE *bfp, const user_t user);
+status_t tmp_file_read(FILE *bfp, user_t user);
 
 status_t split(char *s, char **data);
 
+status_t load_values(FILE *);
 status_t export_data(cla_t cla, FILE *bfp);
 
+void print_user(const user_t user);
+
+status_t tmp_file_gen(cla_t cla, FILE **bfp);
 #endif
diff --git a/include/sort.h b/include/sort.h
@@ -1,6 +1,7 @@
 #ifndef SORT__H
 #define SORT__H
 
+#include "cla.h"
 #include "types.h"
 
 #include <stdio.h>
@@ -9,6 +10,6 @@
 int minmax(const void *, const void *);
 int maxmix(const void *, const void *);
 
-status_t sort_tmp_file(FILE *, char *);
+status_t tmp_file_sort(FILE *tmp, size_t len, char order);
 
 #endif
diff --git a/include/types.h b/include/types.h
@@ -16,11 +16,6 @@ typedef enum {
 } status_t;
 
 typedef struct {
-    char *fmt, *fi, *fo;
-    unsigned long ti, tf;
-} ADT_cla_t, *cla_t;
-
-typedef struct {
     int id, credit, debt;
 } ADT_user_t, *user_t;
 
diff --git a/source/io.c b/source/io.c
@@ -1,16 +1,12 @@
 #include "../include/io.h"
 
-void print_user(const user_t user);
-status_t write_tmp_file(FILE *bfp, const user_t user);
-status_t read_tmp_file(FILE *bfp, user_t user);
-status_t load_values(FILE *);
-
 /* Lee los datos de el archivo de entrada linea por linea 
  * y los imprime en un archivo binario temporal */
-status_t tmp_gen(cla_t cla, FILE **bfp)
+status_t tmp_file_gen(cla_t cla, FILE **bfp)
 {
     FILE *fpi, *fpo;
     char *buffer, **data;
+    size_t i;
     user_t user;
     status_t st;
 
@@ -22,19 +18,29 @@ status_t tmp_gen(cla_t cla, FILE **bfp)
 
     data = (char **)malloc(sizeof(char *) * INPUT_FILE_FIELDS);
 
-    for(size_t i = 0; i < INPUT_FILE_FIELDS; i++)
+    for(i = 0; i < INPUT_FILE_FIELDS; i++)
         data[i] = calloc(sizeof(char), BUFFER_SIZE);
 
     buffer = calloc(sizeof(char), BUFFER_SIZE);
 
     /* Lee los datos de el archivo de entrada linea por linea 
      * y los imprime en un archivo binario temporal */
-    while(fgets(buffer, BUFFER_SIZE, fpi) != NULL) {
+    for(i = 0; (fgets(buffer, BUFFER_SIZE, fpi) != NULL); i++) {
         if((st = split(buffer, data))) return st;
         if((st = set_data(&user, data))) return st;
-        write_tmp_file((*bfp), user);
+        tmp_file_write(*bfp, user);
     }
 
+    /* tmp_file_sort(*bfp, i, 'd'); */
+
+    /* tmp_file_read(*bfp, user); */
+
+    /* print_user(user); */
+
+    /* rewind(*bfp); */
+    /* for(i = 0; fread(user, 1, sizeof(ADT_user_t), *bfp); i++) */
+    /*     print_user(user); */
+
     fclose(fpi);
     fclose(fpo);
     return OK;
@@ -70,7 +76,7 @@ void print_user(const user_t user)
     printf("ID: %5d CREDITS: %5d DEBITS: %5d\n", user->id, user->credit, user->debt);
 }
 
-status_t write_tmp_file(FILE *bfp, const user_t user)
+status_t tmp_file_write(FILE *bfp, const user_t user)
 {
     if(bfp == NULL) return ERROR_NULL_POINTER;
 
@@ -79,7 +85,7 @@ status_t write_tmp_file(FILE *bfp, const user_t user)
     return OK;
 }
 
-status_t read_tmp_file(FILE *bfp, user_t user)
+status_t tmp_file_read(FILE *bfp, user_t user)
 {
     if(bfp == NULL || user == NULL) return ERROR_NULL_POINTER;
 
diff --git a/source/main.c b/source/main.c
@@ -25,14 +25,11 @@ int main (int argc, char *argv[])
     /* En este punto ya tengo todos los datos que necesito, el nombre de los archivos de entrada, el tiempo inicial y final, y el formato de el archivo de salida */
 
     /* Genera un archivo binario temporal con los datos parseados  */
-    if((st = tmp_gen(cla, &tmp_file)) != OK) {
+    if((st = tmp_file_gen(cla, &tmp_file)) != OK) {
         show_status(st);
         return st;
     }
 
-    /* Ordena el archivo binario */
-    sort_tmp_file(tmp_file, SORTING_ORDER);
-
     /* Exporta el archivo temporal a un archivo de texto con formato de acuerdo a la flag recibida como argumento */
     /* if((st = export_data(cla, tmp_file)) != OK) { */
     /*     show_status(st); */
diff --git a/source/sort.c b/source/sort.c
@@ -8,7 +8,7 @@ int minmax(const void *a, const void *b)
     return (*A > *B) ? *A : (*A - *B);
 }
 
-int maxmix(const void *a, const void *b)
+int maxmin(const void *a, const void *b)
 {
     int *A = (int *)a;
     int *B = (int *)b;
@@ -16,12 +16,15 @@ int maxmix(const void *a, const void *b)
     return (*A > *B) ? *A : (*A - *B);
 }
 
-status_t sort_tmp_file(FILE *bfp, char *sort_order)
+status_t tmp_file_sort(FILE *tmp, size_t len, char order)
 {
-    if(bfp == NULL || sort_order == NULL)
-        return ERROR_NULL_POINTER;
+    if(tmp == NULL) return ERROR_NULL_POINTER;
 
-    /* qsort(bfp, sizeof); */
+    switch (order)
+    {
+        case 'a': qsort(tmp, len, sizeof(ADT_cla_t), minmax); break;
+        case 'd': qsort(tmp, len, sizeof(ADT_cla_t), maxmin); break;
+    }
 
     return OK;
 }