From b9a4b77a40964fffbf18be45eeb13ab1bacafb56 Mon Sep 17 00:00:00 2001 From: "DLABAL, Eduard" Date: Fri, 25 Apr 2025 16:32:18 +0200 Subject: [PATCH] Add searchUUIDByName function to retrieve UUID by name and surname; improve formatting in users.c --- include/users.h | 16 ++++++++++-- src/users.c | 69 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/include/users.h b/include/users.h index 9305191..204a0f1 100644 --- a/include/users.h +++ b/include/users.h @@ -42,7 +42,19 @@ typedef struct node */ uint16_t generateUUID(node_t *_head); -uint16_t searchUUIDByName(node_t *_head); +/** + * @brief Searches for a UUID associated with a user by their name and surname. + * + * This function traverses a linked list of nodes to find a user whose name + * and surname match the provided parameters. If a match is found, it returns + * a pointer to the UUID associated with that user. + * + * @param _head Pointer to the head of the linked list. + * @param _name Pointer to a string containing the user's first name. + * @param _surname Pointer to a string containing the user's surname. + * @return Pointer to the UUID of the user if found, or 0 if no match is found. + */ +uint16_t *searchUUIDByName(node_t *_head, char *_name, char *_surname); /** * @brief Searches for a person in a linked list by their UUID. @@ -68,7 +80,7 @@ node_t *searchPersonByUUID(node_t *_head, uint16_t _uuid); * @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 diff --git a/src/users.c b/src/users.c index 3a5db1f..585148b 100644 --- a/src/users.c +++ b/src/users.c @@ -1,7 +1,6 @@ #include "users.h" #include "main.h" - #include #include #include @@ -11,10 +10,8 @@ #include #include -uint16_t generateUUID -( - node_t *_head -) +uint16_t generateUUID( + node_t *_head) { uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1; node_t *current = _head; @@ -33,11 +30,9 @@ uint16_t generateUUID return uuid; } -node_t *searchPersonByUUID -( +node_t *searchPersonByUUID( node_t *_head, - uint16_t _uuid -) + uint16_t _uuid) { node_t *current = _head; while (current != NULL) @@ -51,13 +46,51 @@ node_t *searchPersonByUUID return NULL; }; -int addPersonToList -( +uint16_t *searchUUIDByName( + node_t *_head, + char *_name, + char *_surname) +{ + int sum = 0; + uint16_t *uuids = malloc(sizeof(uint16_t)); + node_t *current = _head; + + if (_head == NULL) + { + return 0; + } + + if (uuids == NULL) + { + puts("Fatal error: free memory is not sufficient for this operation"); + exit(EXIT_FAILURE); + } + + while (current != NULL) + { + if (strcmp(_name, current->user.name) == 0 && strcmp(_surname, current->user.surname) == 0) + { + sum++; + + realloc(uuids, sum * sizeof(uint16_t)); + if (uuids == NULL) + { + puts("Fatal error: free memory is not sufficient for this operation"); + exit(EXIT_FAILURE); + } + + uuids[sum - 1] = current->user.uuid; + } + } + if (sum == 0) return NULL; + return uuids; +} + +int addPersonToList( node_t **_head, char *_name, char *_surname, - uint32_t _department -) + uint32_t _department) { node_t *new_node = (node_t *)malloc(sizeof(node_t)); if (new_node == NULL) @@ -107,10 +140,8 @@ int addPersonToList return 1; } -int addTimeEvent -( - node_t *_person -) +int addTimeEvent( + node_t *_person) { time_t current_time = time(0); @@ -124,8 +155,8 @@ int addTimeEvent _person->user.total += (current_time - _person->user.last_time_event); } - _person->user.available = !_person->user.available; - _person->user.last_time_event = current_time; + _person->user.available = !_person->user.available; + _person->user.last_time_event = current_time; return 1; } \ No newline at end of file