9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
commit af253b63ed781d1c0a299e64626830e5c26137f1
parent f916511708bb2bc8095f81bd27b1091157f54077
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Thu, 22 Jul 2021 18:06:30 -0300

Update: divided user into a new module, user.h & user.c

Diffstat:
MMakefile | 7+++++--
Minclude/io.h | 8+-------
Ainclude/user.h | 18++++++++++++++++++
Msource/io.c | 89++-----------------------------------------------------------------------------
Msource/main.c | 1+
Asource/user.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 100 insertions(+), 96 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,8 +6,8 @@ PROGNAME=main
 
 all: main clean
 
-main: main.o cla.o status.o io.o sort.o
-    $(CC) $(CFLAGS) main.o cla.o status.o io.o sort.o -o $(PROGNAME)
+main: main.o cla.o status.o io.o sort.o user.o
+    $(CC) $(CFLAGS) main.o cla.o status.o io.o sort.o user.o -o $(PROGNAME)
 
 main.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c
@@ -24,6 +24,9 @@ io.o: $(HFOLDER)/status.h
 sort.o: $(HFOLDER)/status.h
     $(CC) $(CFLAGS) -c $(SRCFOLDER)/sort.c
 
+user.o: $(HFOLDER)/status.h
+    $(CC) $(CFLAGS) -c $(SRCFOLDER)/user.c
+
 clean:
     rm *.o
 
diff --git a/include/io.h b/include/io.h
@@ -8,6 +8,7 @@
 #include "cla.h"
 #include "status.h"
 #include "sort.h"
+#include "user.h"
 
 #define _XOPEN_SOURCE
 #define __USE_XOPEN
@@ -46,8 +47,6 @@ typedef enum {
 
 status_t process_file(cla_t cla, user_t **users, size_t *i);
 
-status_t set_data(user_t *user, char **data);
-
 status_t string_split(char *s, char **data, char *delim);
 
 status_t load_values(FILE *, cla_t *data);
@@ -57,9 +56,4 @@ status_t export_data(cla_t cla, const user_t *users, size_t size);
 status_t export_data_as_csv(FILE *fo, const user_t *users, size_t size);
 status_t export_data_as_xml(FILE *fo, const user_t *users, size_t size);
 
-status_t destroy_users(user_t *users, size_t size);
-
-status_t user_create(user_t *usr);
-status_t user_set_data(user_t usr, int id, long credit, long debt);
-
 #endif
diff --git a/include/user.h b/include/user.h
@@ -0,0 +1,18 @@
+#ifndef USER__H
+#define USER__H
+
+#include "status.h"
+#include "io.h"
+
+status_t destroy_users(user_t *users, size_t size);
+
+status_t user_create(user_t *usr);
+status_t user_set_data(user_t *user, char **data);
+
+user_t user_find(const user_t *users, int id, size_t size);
+user_t user_dup(user_t src);
+
+
+void user_clean(user_t usr);
+
+#endif
diff --git a/source/io.c b/source/io.c
@@ -3,17 +3,12 @@
 #define INIT_SIZE        1000
 #define GROWTH_FACTOR     2
 
-user_t find_user(const user_t *users, int id, size_t size);
 status_t destroy_data(char **data);
-
 status_t get_date(time_t *e, char **data);
 
-void user_clean(user_t usr);
 void clean_buffer(char *buf);
 void clean_data(char **data);
 
-user_t user_dup(user_t src);
-
 /* Lee los datos del archivo de entrada linea por linea mientras los procesa y asigna a un arreglo de usuarios */
 status_t process_file(cla_t cla, user_t **users, size_t *size)
 {
@@ -81,7 +76,7 @@ status_t process_file(cla_t cla, user_t **users, size_t *size)
         }
 
         /* Asigna a 'user' id, creditos y debitos cargados en data */
-        if((st = set_data(&user, data)) != OK) {
+        if((st = user_set_data(&user, data)) != OK) {
             destroy_data(data);
             free(buffer);
             fclose(fpi);
@@ -107,7 +102,7 @@ status_t process_file(cla_t cla, user_t **users, size_t *size)
         }
 
         /* Busca el numero de id en los usuarios ya ingresados, si no lo encuentra agrega un usuario nuevo al arreglo de usuarios */
-        if((user_found = find_user(*users, user->id, *size)) != NULL) {
+        if((user_found = user_find(*users, user->id, *size)) != NULL) {
             user_found->credit += user->credit;
             user_found->debt += user->debt;
         } else {
@@ -128,24 +123,6 @@ status_t process_file(cla_t cla, user_t **users, size_t *size)
     return OK;
 }
 
-status_t set_data(user_t *user, char **data) 
-{
-    if(data == NULL || user == NULL) return ERROR_NULL_POINTER;
-
-    char *endptr;
-    long amount;
-
-    (*user)->id = strtol(data[POS_USER_ID], &endptr, 10);
-    if(*endptr != '\0') return ERROR_CORRUPT_DATA;
-
-    amount = strtol(data[POS_AMOUNT], &endptr, 10);
-    if(*endptr != '\0') return ERROR_CORRUPT_DATA;
-
-    if(amount > 0) (*user)->credit = amount;
-    else if(amount < 0) (*user)->debt = -amount; /* '-=' Para eliminar el menos    */
-
-    return OK;
-}
 
 status_t string_split(char *s, char **data, char *delim)
 {
@@ -161,18 +138,6 @@ status_t string_split(char *s, char **data, char *delim)
     return OK;
 }
 
-
-status_t destroy_users(user_t *users, size_t size)
-{
-    if(users == NULL) return ERROR_NULL_POINTER;
-
-    for(size_t i = 0; i < size; i++)
-        free(users[i]);
-
-    free(users);
-    return OK;
-}
-
 status_t destroy_data(char **data)
 {
     if(data == NULL) return ERROR_NULL_POINTER;
@@ -278,53 +243,3 @@ status_t get_date(time_t *e, char **data)
     return OK;
 }
 
-void user_clean(user_t usr)
-{
-    usr->id = 0;
-    usr->credit = 0;
-    usr->debt = 0;
-}
-
-status_t user_set_data(user_t usr, int id, long credit, long debt)
-{
-    usr->id = id;
-    usr->credit = credit;
-    usr->debt = debt;
-
-    return OK;
-}
-
-user_t user_dup(user_t src)
-{
-    user_t dst = NULL;
-
-    user_create(&dst);
-
-    dst->id = src->id;
-    dst->credit = src->credit;
-    dst->debt = src->debt;
-
-    return dst;
-}
-
-status_t user_create(user_t *usr)
-{
-    if(((*usr) = (user_t)malloc(sizeof(ADT_user_t))) == NULL)
-        return ERROR_MEMORY;
-
-    (*usr)->id = 0;
-    (*usr)->credit = 0;
-    (*usr)->debt = 0;
-
-    return OK;
-}
-
-user_t find_user(const user_t *users, int id, size_t size)
-{
-    for(size_t i = 0; i < size; i++) {
-        if(users[i]->id == id) {
-            return users[i];
-        }
-    }
-    return NULL;
-}
diff --git a/source/main.c b/source/main.c
@@ -2,6 +2,7 @@
 #include "../include/status.h"
 #include "../include/io.h" /* output_gen() */
 #include "../include/sort.h"
+#include "../include/user.h"
 
 /* "ca" - creditos ascendentes | "cd" - creditos descendentes
  * "da" - debitos ascendentes  | "dd" - debitos descendentes */
diff --git a/source/user.c b/source/user.c
@@ -0,0 +1,73 @@
+#include "../include/user.h"
+
+status_t user_create(user_t *usr)
+{
+    if(((*usr) = (user_t)malloc(sizeof(ADT_user_t))) == NULL)
+        return ERROR_MEMORY;
+
+    (*usr)->id = 0;
+    (*usr)->credit = 0;
+    (*usr)->debt = 0;
+
+    return OK;
+}
+
+void user_clean(user_t usr)
+{
+    usr->id = 0;
+    usr->credit = 0;
+    usr->debt = 0;
+}
+
+status_t user_set_data(user_t *user, char **data) 
+{
+    if(data == NULL || user == NULL) return ERROR_NULL_POINTER;
+
+    char *endptr;
+    long amount;
+
+    (*user)->id = strtol(data[POS_USER_ID], &endptr, 10);
+    if(*endptr != '\0') return ERROR_CORRUPT_DATA;
+
+    amount = strtol(data[POS_AMOUNT], &endptr, 10);
+    if(*endptr != '\0') return ERROR_CORRUPT_DATA;
+
+    if(amount > 0) (*user)->credit = amount;
+    else if(amount < 0) (*user)->debt = -amount; /* '-=' Para eliminar el menos    */
+
+    return OK;
+}
+
+user_t user_dup(user_t src)
+{
+    user_t dst = NULL;
+
+    user_create(&dst);
+
+    dst->id = src->id;
+    dst->credit = src->credit;
+    dst->debt = src->debt;
+
+    return dst;
+}
+
+user_t user_find(const user_t *users, int id, size_t size)
+{
+    for(size_t i = 0; i < size; i++) {
+        if(users[i]->id == id) {
+            return users[i];
+        }
+    }
+    return NULL;
+}
+
+status_t destroy_users(user_t *users, size_t size)
+{
+    if(users == NULL) return ERROR_NULL_POINTER;
+
+    for(size_t i = 0; i < size; i++)
+        free(users[i]);
+
+    free(users);
+    return OK;
+}