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 027f4734c9db93be71c4f3a71e4c4422f0c4f6ac
parent bfe9c77690b29e0e96f42ac5e51d96bbeb452e8f
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Mon, 12 Jul 2021 22:12:33 -0300

Update: ...

Diffstat:
A.gitignore | 7+++++++
MMakefile | 7+++++--
Minclude/errors.h | 4++++
Minclude/io.h | 2+-
Minclude/sort.h | 9+++++++++
Minclude/types.h | 2++
Msource/errors.c | 22++++++++++++++++++++++
Msource/io.c | 45++++++++++++++++++++++++++++++++-------------
Msource/main.c | 18++++++++++++------
Asource/sort.c | 27+++++++++++++++++++++++++++
10 files changed, 121 insertions(+), 22 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,7 @@
+*.docx
+*.docx#
+*.o
+*.csv
+*.xml
+*.bin
+main
diff --git a/Makefile b/Makefile
@@ -6,8 +6,8 @@ PROGNAME=main
 
 all: main clean
 
-main: main.o cla.o errors.o io.o
-    $(CC) $(CFLAGS) main.o cla.o errors.o io.o -o $(PROGNAME)
+main: main.o cla.o errors.o io.o sort.o
+    $(CC) $(CFLAGS) main.o cla.o errors.o io.o sort.o -o $(PROGNAME)
 
 main.o: $(HFOLDER)/cla.h $(HFOLDER)/errors.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c
@@ -21,5 +21,8 @@ errors.o: $(HFOLDER)/types.h
 io.o: $(HFOLDER)/types.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/io.c
 
+sort.o: $(HFOLDER)/types.h
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/sort.c
+
 clean:
     rm *.o
diff --git a/include/errors.h b/include/errors.h
@@ -2,11 +2,15 @@
 #define ERRORS__H
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
 
 #include "types.h"
 
 void show_status(status_t st);
 
+void free_arrays(size_t num,...);
+
 extern const char *status_strings[STATUS_T_MAX];
 
 #endif
diff --git a/include/io.h b/include/io.h
@@ -19,7 +19,7 @@ typedef enum {
     POS_DESC
 } csv_pos_t;
 
-status_t tmp_gen(cla_t cla);
+status_t tmp_gen(cla_t cla, FILE **bfp);
 status_t set_data(user_t *user, char **data);
 status_t tmp_sort(FILE *tmp);
 
diff --git a/include/sort.h b/include/sort.h
@@ -1,5 +1,14 @@
 #ifndef SORT__H
 #define SORT__H
 
+#include "types.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int minmax(const void *, const void *);
+int maxmix(const void *, const void *);
+
+status_t sort_tmp_file(FILE *, char *);
 
 #endif
diff --git a/include/types.h b/include/types.h
@@ -3,6 +3,8 @@
 
 #define STATUS_T_MAX 6
 
+#define TMP_FILE_NAME "tmp.bin"
+
 typedef enum {
     OK,
     ERROR_MISSING_ARGS,
diff --git a/source/errors.c b/source/errors.c
@@ -13,3 +13,25 @@ void show_status(status_t st)
 {
     fprintf(stderr, "%s\n", status_strings[st]);
 }
+
+void free_arrays(size_t num,...)
+{
+    va_list valist;
+    size_t i;
+
+    va_start(valist, num);
+
+    for(i = 0; i < num; i++)
+        free(va_arg(valist, char *));
+}
+
+void close_streams(size_t num,...)
+{
+    va_list valist;
+    size_t i;
+
+    va_start(valist, num);
+
+    for(i = 0; i < num; i++)
+        free(va_arg(valist, FILE *));
+}
diff --git a/source/io.c b/source/io.c
@@ -1,36 +1,42 @@
 #include "../include/io.h"
 
-void print_user(user_t user);
-void print_tmp_file(FILE *bfp, user_t user);
-
-status_t tmp_gen(cla_t cla)
+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)
 {
-    FILE *fpi, *fpo, *bfp;
+    FILE *fpi, *fpo;
     char *buffer, **data;
     user_t user;
     status_t st;
 
     if((fpi = fopen(cla->fi, "rt")) == NULL) return ERROR_OPENING_FILE;
+
     if((fpo = fopen(cla->fo, "wt")) == NULL) return ERROR_OPENING_FILE;
-    if((bfp = fopen("tmp.bin", "wb")) == NULL) return ERROR_OPENING_FILE;
+
+    if(((*bfp) = fopen(TMP_FILE_NAME, "wb")) == NULL) return ERROR_OPENING_FILE;
 
     data = (char **)malloc(sizeof(char *) * INPUT_FILE_FIELDS);
+
     for(size_t 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 y los imprime en un archivo binario temporal */
+    /* 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) {
         if((st = split(buffer, data))) return st;
         if((st = set_data(&user, data))) return st;
-        print_tmp_file(bfp, user);
-        print_user(user);
+        write_tmp_file((*bfp), user);
     }
 
     fclose(fpi);
     fclose(fpo);
-    fclose(bfp);
     return OK;
 }
 
@@ -59,12 +65,25 @@ status_t split(char *s, char **data)
     return OK;
 }
 
-void print_user(user_t user)
+void print_user(const user_t user)
 {
-    printf("ID: %5d | CREDITS: %5d | DEBITS: %5d\n", user->id, user->credit, user->debt);
+    printf("ID: %5d CREDITS: %5d DEBITS: %5d\n", user->id, user->credit, user->debt);
 }
 
-void print_tmp_file(FILE *bfp, user_t user)
+status_t write_tmp_file(FILE *bfp, const user_t user)
 {
+    if(bfp == NULL) return ERROR_NULL_POINTER;
+
     fwrite(user, 1, sizeof(ADT_user_t), bfp);
+
+    return OK;
+}
+
+status_t read_tmp_file(FILE *bfp, user_t user)
+{
+    if(bfp == NULL || user == NULL) return ERROR_NULL_POINTER;
+
+    fread(user, 1, sizeof(ADT_user_t), bfp);
+
+    return OK;
 }
diff --git a/source/main.c b/source/main.c
@@ -1,9 +1,12 @@
 #include "../include/cla.h"
 #include "../include/errors.h"
 #include "../include/io.h" /* output_gen() */
+#include "../include/sort.h"
 
 #include <stdlib.h>
 
+#define SORTING_ORDER    "a"
+
 int main (int argc, char *argv[])
 {
     status_t st;
@@ -21,17 +24,20 @@ 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 ordenado y listo para ser impresos */
-    if((st = tmp_gen(cla, tmp_file)) != OK) {
+    /* Genera un archivo binario temporal con los datos parseados  */
+    if((st = tmp_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);
-        return st;
-    }
+    /* if((st = export_data(cla, tmp_file)) != OK) { */
+    /*     show_status(st); */
+    /*     return st; */
+    /* } */
 
     clean(cla);
     return OK;
diff --git a/source/sort.c b/source/sort.c
@@ -0,0 +1,27 @@
+#include "../include/sort.h"
+
+int minmax(const void *a, const void *b)
+{
+    int *A = (int *)a;
+    int *B = (int *)b;
+
+    return (*A > *B) ? *A : (*A - *B);
+}
+
+int maxmix(const void *a, const void *b)
+{
+    int *A = (int *)a;
+    int *B = (int *)b;
+
+    return (*A > *B) ? *A : (*A - *B);
+}
+
+status_t sort_tmp_file(FILE *bfp, char *sort_order)
+{
+    if(bfp == NULL || sort_order == NULL)
+        return ERROR_NULL_POINTER;
+
+    /* qsort(bfp, sizeof); */
+
+    return OK;
+}