-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clients.c
175 lines (150 loc) · 3.83 KB
/
Clients.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*****************************************************************//**
* \file Clients.c
* \brief
*
* \author Diogo Pinto & Ricardo Cruz
* \date March 2023
*********************************************************************/
#pragma warning(disable:4996)
#pragma region INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "Clients.h"
#pragma endregion
#pragma region ADD_CLIENT
/**
* \brief Add client on clients dll
*
* \param clients_head
* \param new_client
* \return
*/
Clients* add_client(Clients* clients_head, Client new_client)
{
Clients* new_node;
bool success = create_node(&new_node, new_client);
if (!success)
{
//printf("Failed to create new node.\n");
return clients_head;
}
success = add_node(&clients_head, new_node);
if (!success)
{
//printf("Failed to add node to list.\n");
}
return clients_head;
}
bool create_node(Clients** new_node, Client new_client)
{
*new_node = (Clients*)malloc(sizeof(Clients));
if (*new_node == NULL)
{
//printf("Failed to allocate memory for new node.\n");
return false;
}
(*new_node)->c = new_client;
(*new_node)->next = NULL;
(*new_node)->prev = NULL;
return true;
}
bool add_node(Clients** clients_head, Clients* new_node)
{
if (*clients_head == NULL)
{
*clients_head = new_node;
return true;
}
else
{
Clients* current_node = *clients_head;
while (current_node->next != NULL)
{
current_node = current_node->next;
}
current_node->next = new_node;
new_node->prev = current_node;
return true;
}
return false;
}
#pragma endregion
#pragma region LIST_CLIENTS
/**
* \brief List clients dll
*
* \param clients_head
*/
void print_clients(Clients* clients_head)
{
Clients* current_node = clients_head;
while (current_node != NULL)
{
printf("Name: %s\n", current_node->c.name);
printf("Balance: %.2f\n", current_node->c.balance);
printf("NIF: %d\n", current_node->c.nif);
printf("Address: %s\n", current_node->c.address);
printf("Phone: %s\n", current_node->c.phone);
printf("Email: %s\n\n", current_node->c.email);
current_node = current_node->next;
}
}
#pragma endregion
#pragma region FREE_CLIENTS
/**
* \brief Free clients dll
*
* \param clients_head
*/
void free_clients_list(Clients* clients_head)
{
Clients* current_node = clients_head;
while (current_node != NULL)
{
Clients* next_node = current_node->next;
free(current_node);
current_node = next_node;
}
}
#pragma endregion
#pragma region READ_FILE_CLIENTS
/**
* \brief Reads clients from text file
*
* \return
*/
Clients* read_clients_from_file()
{
Clients* clients_head = NULL;
FILE* file = fopen("clients.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return clients_head;
}
char line[MAX_LINE_SIZE];
int i = 0;
while (fgets(line, MAX_LINE_SIZE, file) != NULL)
{
char* token = strtok(line, ",");
Client new_client;
strncpy(new_client.name, token, sizeof(new_client.name));
token = strtok(NULL, ",");
new_client.balance = atof(token);
token = strtok(NULL, ",");
new_client.nif = atoi(token);
token = strtok(NULL, ",");
strncpy(new_client.address, token, sizeof(new_client.address));
token = strtok(NULL, ",");
strncpy(new_client.phone, token, sizeof(new_client.phone));
token = strtok(NULL, ",");
strncpy(new_client.email, token, sizeof(new_client.email));
clients_head = add_client(clients_head, new_client);
i++;
}
fclose(file);
return clients_head;
}
#pragma endregion