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 84e99f22532e18008b20acdf9ccaa5022e773114
parent c26e8ea495c22632e08ef2075f6f9f2e811637b5
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Sat, 10 Jul 2021 12:35:29 -0300

Updated source code, started io module development

Diffstat:
MMakefile | 7+++++--
Mexamples/input_gen.py | 2+-
Minclude/cla.h | 12+++++++++++-
Minclude/io.h | 9+++++++++
Minclude/types.h | 10++++++++++
Msource/cla.c | 34++++++++++++++++++++++++++++++++++
Msource/io.c | 28++++++++++++++++++++++++++++
Msource/main.c | 32+++++++++++++-------------------
Dsource/parse.c | 0
Dsource/sort.c | 0
10 files changed, 111 insertions(+), 23 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,8 +6,8 @@ PROGNAME=main
 
 all: main clean
 
-main: main.o cla.o errors.o
-    $(CC) $(CFLAGS) main.o cla.o errors.o -o $(PROGNAME)
+main: main.o cla.o errors.o io.o
+    $(CC) $(CFLAGS) main.o cla.o errors.o io.o -o $(PROGNAME)
 
 main.o: $(HFOLDER)/cla.h $(HFOLDER)/errors.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c
@@ -18,5 +18,8 @@ cla.o: $(HFOLDER)/cla.h $(HFOLDER)/types.h
 errors.o: $(HFOLDER)/types.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/errors.c
 
+io.o: $(HFOLDER)/types.h
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/io.c
+
 clean:
     rm *.o
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 = 10000000
+LINES = 2000
 
 # OUTPUT:
 #     ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
diff --git a/include/cla.h b/include/cla.h
@@ -1,6 +1,7 @@
 #ifndef CLA__H
 #define CLA__H
 
+#include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 
@@ -11,11 +12,20 @@
 #define FLAGS_MAX 5
 #define FORMATS_MAX 2
 
+typedef enum {
+    FLAG_FMT,
+    FLAG_OUT,
+    FLAG_IN,
+    FLAG_TI,
+    FLAG_TF
+} flags_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);
 
-status_t get_flags_values(int argc, char **argv);
+status_t setup(int argc, char **argv, 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/io.h b/include/io.h
@@ -1,4 +1,13 @@
 #ifndef IO__H
 #define IO__H
 
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "types.h"
+
+
+status_t output_gen(cla_t cla);
+status_t get_data(char *buf, user_t *user);
+
 #endif
diff --git a/include/types.h b/include/types.h
@@ -9,7 +9,17 @@ typedef enum {
     ERROR_WRONG_FLAGS,
     ERROR_FLAG_NOT_FOUND,
     ERROR_FLAG_REPEATED,
+    ERROR_OPENING_FILE,
     ERROR_NULL_POINTER
 } 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;
+
 #endif
diff --git a/source/cla.c b/source/cla.c
@@ -54,3 +54,37 @@ status_t check_flags_repeated(int argc, char **argv)
     }
     return OK;
 }
+
+status_t setup(int argc, char **argv, cla_t *cla)
+{
+    /* Falta validar memoria */
+    *cla = (cla_t)malloc(sizeof(ADT_cla_t));
+
+    (*cla)->fmt = calloc(sizeof(char), 100);
+    (*cla)->fo = calloc(sizeof(char), 100);
+    (*cla)->fi = calloc(sizeof(char), 100);
+
+    for(size_t i = 1; i < argc; i += 2) {
+        for(flags_t f = FLAG_FMT; f < FLAGS_MAX; f++) {
+            if(!strcmp(available_flags[f], argv[i])) {
+                switch (f) {
+                    case FLAG_FMT: strcpy((*cla)->fmt, argv[i + 1]); break;
+                    case FLAG_OUT: strcpy((*cla)->fo, argv[i + 1]); break;
+                    case FLAG_IN: strcpy((*cla)->fi, argv[i + 1]); break;
+                    case FLAG_TI: (*cla)->ti = strtoul(argv[i + 1], NULL, 10); break;
+                    case FLAG_TF: (*cla)->tf = strtoul(argv[i + 1], NULL, 10); break;
+                    default: return ERROR_FLAG_NOT_FOUND;
+                }
+            }
+        }
+    }
+    return OK;
+}
+
+void clean(cla_t cla)
+{
+    free(cla->fmt);
+    free(cla->fi);
+    free(cla->fo);
+    free(cla);
+}
diff --git a/source/io.c b/source/io.c
@@ -0,0 +1,28 @@
+#include "../include/io.h"
+
+status_t output_gen(cla_t cla)
+{
+    FILE *fpi, *fpo;
+    char *buffer;
+    user_t user;
+
+    if((fpi = fopen(cla->fi, "rt")) == NULL) return ERROR_OPENING_FILE;
+    if((fpo = fopen(cla->fo, "wt")) == NULL) return ERROR_OPENING_FILE;
+
+    buffer = calloc(sizeof(char), 1000);
+
+    if(fgets(buffer, 1000, fpi) == NULL) return ERROR_NULL_POINTER;
+
+    get_data(buffer, &user);
+
+    return OK;
+}
+
+status_t get_data(char *buf, user_t *user)
+{
+    (*user) = (user_t)malloc(sizeof(user_t));
+
+    printf("%s", buf);
+
+    return OK;
+}
diff --git a/source/main.c b/source/main.c
@@ -1,36 +1,30 @@
 #include "../include/cla.h"
 #include "../include/errors.h"
+#include "../include/io.h" /* output_gen() */
 
-typedef struct {
-    int id, credit, debt;
-} user_t, *user;
-
-typedef struct {
-    char *fmt, *fi, fo;
-    unsigned long ti, tf;
-} cla_t, *cla;
-
-/* user_create */
-/* user_destroy */
-/* user_assign_amount */
-/* user_get_amount */
+#include <stdlib.h>
 
 int main (int argc, char *argv[])
 {
     status_t st;
+    cla_t cla;
 
     if((st = validate_arguments(argc, argv)) != OK) {
         show_status(st);
         return st;
     }
 
-    /* parse_arguments(argc, argv); */
+    if((st = setup(argc, argv, &cla))) {
+        show_status(st);
+        return st;
+    }
+    /* 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 */
 
-    /* get_argument(argc, argv); */
-    /* if((st = parse_file())) { */
-    /*     show_error(st); */
-    /*     return st; */
-    /* } */
+    if((st = output_gen(cla))) {
+        show_status(st);
+        return st;
+    }
 
+    clean(cla);
     return OK;
 }
diff --git a/source/parse.c b/source/parse.c
diff --git a/source/sort.c b/source/sort.c