From 58e02ad54c9a5fd5a8a09cf6151c06e9b7ecc798 Mon Sep 17 00:00:00 2001 From: Eduard Dlabal Date: Tue, 22 Apr 2025 20:51:06 +0200 Subject: [PATCH] Refactor user management functions and improve memory handling in addPersonToList --- .vscode/settings.json | 6 +++-- include/users.h | 39 ++++++++++++++++++++++----- src/users.c | 63 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 41e1322..3483249 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,8 @@ "ncurses.h": "c", "stdlib.h": "c", "errno.h": "c", - "rfid_handler.h": "c" - } + "rfid_handler.h": "c", + "fcntl.h": "c" + }, + "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/include/users.h b/include/users.h index bfe8431..54f0072 100644 --- a/include/users.h +++ b/include/users.h @@ -17,7 +17,7 @@ typedef struct person time_t last_time_event; uint32_t total; - pthread_mutex_t lock; + pthread_mutex_t lock; } person_t; typedef struct node @@ -26,14 +26,41 @@ typedef struct node struct node *next; } node_t; -uint16_t generateUUID(node_t * _head); +uint16_t generateUUID(node_t *_head); -uint16_t searchUUIDByName(node_t * _head); +uint16_t searchUUIDByName(node_t *_head); -node_t * searchPersonByUUID(node_t * _head); +node_t *searchPersonByUUID(node_t *_head); -void addPersonToList(node_t * _head, char * _name, char * _surname, uint32_t _department); +/** + * @brief Adds a new person to the linked list. + * + * This function creates a new person and appends it to the end of the linked list. + * It dynamically allocates memory for the new person and initializes their details. + * If memory allocation fails or the input parameters are invalid, the function returns an error. + * + * @param _head Pointer to the head of the linked list (node_t **). + * @param _name Pointer to a string containing the person's first name. + * @param _surname Pointer to a string containing the person's last name. + * @param _department The department number to which the person belongs. + * + * @return int Returns 1 if the person was successfully added, otherwise 0 on failure. + * + * @note Memory for the name and surname is dynamically allocated. It is the caller's + * responsibility to free the memory for the entire list to avoid memory leaks. + * + * @example + * ```c + * node_t *head = NULL; + * if (addPersonToList(&head, "John", "Doe", 101)) { + * printf("Person successfully added.\n"); + * } else { + * printf("Error adding person.\n"); + * } + * ``` + */ +int addPersonToList(node_t **_head, char *_name, char *_surname, uint32_t _department); -void addTimeEvent(node_t * _person); +int addTimeEvent(node_t *_person); #endif \ No newline at end of file diff --git a/src/users.c b/src/users.c index 06b11a5..3a384e6 100644 --- a/src/users.c +++ b/src/users.c @@ -1 +1,62 @@ -#include "users.h" \ No newline at end of file +#include "users.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int addPersonToList( + node_t **_head, + char *_name, + char *_surname, + uint32_t _department) +{ + node_t *new_node = (node_t *)malloc(sizeof(node_t)); + if (new_node == NULL) + return 0; + + if (!strlen(_name) || !strlen(_surname)) + { + free(new_node); + new_node = NULL; + return 0; + } + + new_node->user.name = (char *)malloc(strlen(_name) + 1); + new_node->user.surname = (char *)malloc(strlen(_surname) + 1); + + if (new_node->user.name == NULL || new_node->user.surname == NULL) + { + free(new_node->user.name); + free(new_node->user.surname); + free(new_node); + return 0; + } + + strcpy(new_node->user.name, _name); + strcpy(new_node->user.surname, _surname); + new_node->user.department = _department; + new_node->user.last_time_event = -255; + new_node->user.total = 0; + + new_node->next = NULL; + + if (*_head == NULL) + { + *_head = new_node; + return; + } + + node_t *current = *_head; + while (current->next != NULL) + { + current = current->next; + } + current->next = new_node; + + return 0; +} \ No newline at end of file