Add searchUUIDByName function to retrieve UUID by name and surname; improve formatting in users.c

This commit is contained in:
2025-04-25 16:32:18 +02:00
parent e23ed280d8
commit b9a4b77a40
2 changed files with 64 additions and 21 deletions
+13 -1
View File
@@ -42,7 +42,19 @@ typedef struct node
*/ */
uint16_t generateUUID(node_t *_head); 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. * @brief Searches for a person in a linked list by their UUID.
+48 -17
View File
@@ -1,7 +1,6 @@
#include "users.h" #include "users.h"
#include "main.h" #include "main.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
@@ -11,10 +10,8 @@
#include <termios.h> #include <termios.h>
#include <string.h> #include <string.h>
uint16_t generateUUID uint16_t generateUUID(
( node_t *_head)
node_t *_head
)
{ {
uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1; uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1;
node_t *current = _head; node_t *current = _head;
@@ -33,11 +30,9 @@ uint16_t generateUUID
return uuid; return uuid;
} }
node_t *searchPersonByUUID node_t *searchPersonByUUID(
(
node_t *_head, node_t *_head,
uint16_t _uuid uint16_t _uuid)
)
{ {
node_t *current = _head; node_t *current = _head;
while (current != NULL) while (current != NULL)
@@ -51,13 +46,51 @@ node_t *searchPersonByUUID
return NULL; 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, node_t **_head,
char *_name, char *_name,
char *_surname, char *_surname,
uint32_t _department uint32_t _department)
)
{ {
node_t *new_node = (node_t *)malloc(sizeof(node_t)); node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL) if (new_node == NULL)
@@ -107,10 +140,8 @@ int addPersonToList
return 1; return 1;
} }
int addTimeEvent int addTimeEvent(
( node_t *_person)
node_t *_person
)
{ {
time_t current_time = time(0); time_t current_time = time(0);