9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit d725a953fc9d8a97b659405772a494fd069993af
parent 84e99f22532e18008b20acdf9ccaa5022e773114
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Sat, 10 Jul 2021 13:40:12 -0300

Improved input file parser and spliter

Diffstat:
Mexamples/input_gen.py | 2+-
Minclude/io.h | 16+++++++++++++++-
Msource/io.c | 46++++++++++++++++++++++++++++++++++++++++------
3 files changed, 56 insertions(+), 8 deletions(-)
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 = 2000
+LINES = 2000000
 
 # OUTPUT:
 #     ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
diff --git a/include/io.h b/include/io.h
@@ -3,11 +3,25 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "types.h"
 
+#define INPUT_FILE_FIELDS 6
+#define BUFFER_SIZE        1000
+
+typedef enum {
+    POS_ID_TRANSACTION,
+    POS_USER_ID,
+    POS_TRANSACTION_DATE,
+    POS_AMOUNT,
+    POS_CARD_NUMBER,
+    POS_DESC
+} csv_pos_t;
 
 status_t output_gen(cla_t cla);
-status_t get_data(char *buf, user_t *user);
+status_t set_data(user_t *user, char **data);
+
+status_t split(char *s, char **data);
 
 #endif
diff --git a/source/io.c b/source/io.c
@@ -1,28 +1,62 @@
 #include "../include/io.h"
 
+void print_user(user_t user);
+
 status_t output_gen(cla_t cla)
 {
     FILE *fpi, *fpo;
-    char *buffer;
+    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;
 
-    buffer = calloc(sizeof(char), 1000);
+    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);
 
-    if(fgets(buffer, 1000, fpi) == NULL) return ERROR_NULL_POINTER;
+    buffer = calloc(sizeof(char), BUFFER_SIZE);
 
-    get_data(buffer, &user);
+    while(fgets(buffer, BUFFER_SIZE, fpi) != NULL) {
+        if((st = split(buffer, data))) return st;
+        if((st = set_data(&user, data))) return st;
+        print_user(user);
+    }
 
     return OK;
 }
 
-status_t get_data(char *buf, user_t *user)
+status_t set_data(user_t *user, char **data) 
 {
+    int amount;
+    status_t st;
+
     (*user) = (user_t)malloc(sizeof(user_t));
 
-    printf("%s", buf);
+    /* size_t fields = 0; */
+    /* for(p = buf; (data[fields++] = strtok(p, ",")); p = NULL); */
+
+    (*user)->id = strtol(data[POS_USER_ID], NULL, 10);
+
+    amount = strtol(data[POS_AMOUNT], NULL, 10);
+
+    if(amount > 0) (*user)->credit += amount;
+    else if(amount < 0) (*user)->debt += amount;
 
     return OK;
 }
+
+status_t split(char *s, char **data)
+{
+    char *p;
+    size_t fields = 0;
+    for(p = s; (data[fields++] = strtok(p, ",")); p = NULL);
+
+    return OK;
+}
+
+void print_user(user_t user)
+{
+    printf("ID: %5d | CREDITS: %5d | DEBITS: %5d\n", user->id, user->credit, user->debt);
+}