commit 15adeb13e4c04308f360b41b909a1ab5b3f6c706
parent 95ef9920529da0a2511562604ff2b1813601259b
Author: klewer-martin <martin.cachari@gmail.com>
Date: Thu, 5 Aug 2021 22:21:30 -0300
renamed function array_destroy -> destroy_2darray, also created function create_2darray
Diffstat:
3 files changed, 47 insertions(+), 21 deletions(-)
diff --git a/include/user.h b/include/user.h
@@ -15,8 +15,10 @@ typedef struct {
status_t user_create(ADT_user_t **);
status_t user_destroy(ADT_user_t **);
+
status_t user_set_data_from_str(ADT_user_t *, char **);
status_t user_set_data(ADT_user_t *, ulong, ulong, ulong);
+
status_t user_add_amount(ADT_user_t *, long);
int user_equals(const void *, const void *);
diff --git a/include/utils.h b/include/utils.h
@@ -34,7 +34,8 @@
status_t get_date(time_t *, char *);
-status_t array_destroy(char **, size_t);
+status_t create_2darray(char **, size_t, size_t);
+status_t destroy_2darray(char **, size_t);
status_t string_split(char *, char **, char *);
status_t load_users_to_vector(ADT_Vector_t **, ADT_cla_t *);
diff --git a/source/utils.c b/source/utils.c
@@ -105,6 +105,37 @@ bool is_valid_card(char *card_no)
return (sum % 10) ? false : true;
}
+status_t create_2darray(char **arr, size_t r, size_t c) {
+ size_t i;
+
+ if((arr = malloc(sizeof(char *) * r)) == NULL)
+ return ERROR_MEMORY;
+
+ for(i = 0; i < r; i++) {
+ if((arr[i] = calloc(sizeof(char), c)) == NULL) {
+ destroy_2darray(arr, c);
+ return ERROR_MEMORY;
+ }
+ }
+
+ return OK;
+}
+
+status_t destroy_2darray(char **arr, size_t r)
+{
+ size_t i;
+
+ if(arr == NULL) return ERROR_NULL_POINTER;
+
+ for(i = 0; i < r; i++) {
+ free(arr[i]);
+ arr[i] = NULL;
+ }
+
+ free(arr);
+ return OK;
+}
+
status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
{
status_t st;
@@ -114,19 +145,11 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
time_t epoch;
long long amount;
ulong id, c, d;
- size_t i;
if(v == NULL || cla == NULL) return ERROR_NULL_POINTER;
- /* Asigna memoria a data */
- if((data = malloc(sizeof(char *) * IN_FILE_FIELDS)) == NULL)
- return ERROR_MEMORY;
- for(i = 0; i < IN_FILE_FIELDS; i++) {
- if((data[i] = calloc(sizeof(char), IN_FILE_FIELDS_MAX_LEN)) == NULL) {
- array_destroy(data, IN_FILE_FIELDS);
- return ERROR_MEMORY;
- }
- }
+ if((st = create_2darray(data, IN_FILE_FIELDS, IN_FILE_FIELDS_MAX_LEN)) != OK)
+ return st;
/* Crea un usuario temporal */
if((st = user_create(&user_tmp)) != OK) {
@@ -137,21 +160,21 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
while(fgets(buffer, IN_FILE_MAX_LEN, cla->fi)) {
if((st = string_split(buffer, data, IN_FILE_DELIM)) != OK) {
free(user_tmp);
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
return st;
}
id = strtol(data[POS_USER_ID], &endptr, 10);
if(*endptr != '\0') {
free(user_tmp);
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
return ERROR_CORRUPT_DATA;
}
amount = strtol(data[POS_AMOUNT], &endptr, 10);
if(*endptr != '\0') {
free(user_tmp);
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
return ERROR_CORRUPT_DATA;
}
@@ -160,13 +183,13 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
if((st = user_set_data(user_tmp, id, c, d)) != OK) {
free(user_tmp);
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
return ERROR_CORRUPT_DATA;
}
if((st = get_date(&epoch, data[POS_TXN_DATE])) != OK){
free(user_tmp);
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
return ERROR_CORRUPT_DATA;
}
@@ -184,7 +207,7 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
if((user = ADT_Vector_get_elem(*v, user_tmp)) != NULL) {
/* Si lo encuentra le suma el monto correspondiente */
if((st = user_add_amount(user, amount)) != OK) {
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
free(user_tmp);
return st;
}
@@ -193,20 +216,20 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
/* Si no lo encuentra crea un usuario nuevo */
else {
if((st = user_create(&user)) != OK) {
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
free(user_tmp);
return st;
}
if((st = user_set_data(user, id, c, d))) {
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
free(user_tmp);
return st;
}
/* Y lo agrega al vector */
if((st = ADT_Vector_add(v, user)) != OK){
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
free(user_tmp);
return st;
}
@@ -216,7 +239,7 @@ status_t load_users_to_vector(ADT_Vector_t **v, ADT_cla_t *cla)
} /* End while */
- array_destroy(data, IN_FILE_FIELDS);
+ destroy_2darray(data, IN_FILE_FIELDS);
free(user_tmp);
return OK;