-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank-utils.c
256 lines (215 loc) · 6.96 KB
/
bank-utils.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "bank-utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <uuid/uuid.h>
unsigned int counter = 0;
unsigned int id = 0;
char sha1namespace[UUID_STR_LEN];
struct cliente *lista_clientes;
struct cliente cliente_vazio = { "\0", "", 0, 0 };
void initialize_list(){
free(lista_clientes);
uuid_t namespace_uuid;
uuid_generate(namespace_uuid);
uuid_unparse(namespace_uuid, sha1namespace);
}
void print_user_data(struct cliente c){
char uuid_text[UUID_STR_LEN];
uuid_unparse(c.id, uuid_text);
printf("ID: %s | Nome: %s | Idade: %u | Saldo: R$%.2f\n", uuid_text, c.nome, c.idade, c.saldo);
}
void list_all_users(){
for (int i = 0; i < counter; i++){
if(!uuid_is_null(lista_clientes[i].id)){
print_user_data(lista_clientes[i]);
}
}
}
void create_uuid(uuid_t uuid, char plain[6]){
uuid_t uuid_namespace;
uuid_parse(sha1namespace, uuid_namespace);
uuid_generate_sha1(uuid, uuid_namespace, plain, 6);
}
void report(){
FILE *report;
char filename[50];
// Criando o nome do arquivo (de acordo com tempo)
time_t curtime = time(NULL);
struct tm *tm_struct = localtime(&curtime);
sprintf(filename, "%d-%.2d-%.2d %.2d-%.2d-%.2d report.csv", tm_struct->tm_year + 1900, tm_struct->tm_mon+1, tm_struct->tm_mday,
tm_struct->tm_hour, tm_struct->tm_min, tm_struct->tm_sec);
report = fopen(filename, "w");
// Cabeçalho do arquivo
fputs("ID,Nome,Idade,Saldo\n", report);
for (int i = 0; i < counter; i++){
struct cliente c = lista_clientes[i];
char uuid_text[UUID_STR_LEN];
uuid_unparse(c.id, uuid_text);
fprintf(report, "%s,%s,%u,%.2f\n", uuid_text, c.nome, c.idade, c.saldo);
}
fprintf(report, "$end,%s,%u,%u", sha1namespace, id, counter);
free(lista_clientes);
fclose(report);
}
int load_report(char* filename){
FILE* r_report;
r_report = fopen(filename, "r");
puts("Carregando relatorio...");
if(r_report == NULL){
fprintf(stderr, "Arquivo inexistente!\n");
return 0;
}
char *output;
size_t buffer_size = 0;
getline(&output, &buffer_size, r_report);
if(strcmp(output, "ID,Nome,Idade,Saldo\n")){
fputs("Arquivo nao compativel\n", stderr);
fclose(r_report);
return 0;
}
while(!feof(r_report)){
char** usr_in;
usr_in = (char **)malloc(NOME_LEN+40);
if(!(line_input(r_report, 4, NOME_LEN+40, ",", usr_in))){
fputs("Erro: linha em formato incompatível\n", stderr);
fclose(r_report);
return 0;
}
if(strcmp(usr_in[0], "$end") == 0){
strcpy(sha1namespace, usr_in[1]);
char *endptr;
unsigned int temp = strtoul(usr_in[2], &endptr, 10);
if(*endptr != '\0'){
fputs("Erro: id inicial invalido\n", stderr);
fclose(r_report);
return 0;
}
id = temp;
temp = strtoul(usr_in[3], &endptr, 10);
if(*endptr != '\0'){
fputs("Erro: counter inicial invalido\n", stderr);
fclose(r_report);
return 0;
}
counter = temp;
puts("Relatorio carregado com sucesso.\n");
fclose(r_report);
return 1;
}
struct cliente new_cliente;
uuid_t new_uuid;
uuid_parse(usr_in[0], new_uuid);
if(client_data(usr_in[1], usr_in[2], usr_in[3], &new_cliente)){
uuid_copy(new_cliente.id, new_uuid);
lista_clientes = (struct cliente*)realloc(lista_clientes, (++counter)*sizeof(struct cliente));
lista_clientes[counter-1] = new_cliente;
}
else{
fputs("Erro na leitura da linha\n", stderr);
fclose(r_report);
return 0;
}
}
fputs("Final do arquivo invalido\n", stderr);
fclose(r_report);
return 0;
}
int create_user(struct cliente novo_cliente){
lista_clientes = (struct cliente*)realloc(lista_clientes, (++counter)*sizeof(struct cliente));
lista_clientes[counter-1] = novo_cliente;
id++;
char idtxt[6];
sprintf(idtxt, "%.6u", id);
uuid_t newuuid;
create_uuid(newuuid, idtxt);
uuid_copy(lista_clientes[counter-1].id, newuuid);
printf("Cliente inserido com id %s com sucesso\n", idtxt);
return 1;
}
int create_users(struct cliente novos_clientes[], int size){
if(size < 1){ return 0; }
printf("Clientes inseridos com id ");
lista_clientes = (struct cliente*)realloc(lista_clientes, (counter + size)*sizeof(struct cliente));
for(int i = 0; i < size; i++){
counter++;
lista_clientes[counter-1] = novos_clientes[i];
id++;
char idtxt[7];
sprintf(idtxt, "%.6u", id);
uuid_t newuuid;
create_uuid(newuuid, idtxt);
uuid_copy(lista_clientes[counter-1].id, newuuid);
printf("%s", idtxt);
if(i != size-1){
printf(", ");
}
}
printf(" com sucesso\n");
return 1;
}
struct cliente find_user(char trg_id[6]){
uuid_t uuid;
create_uuid(uuid, trg_id);
for(int i = 0; i < counter; i++){
if(!uuid_compare(uuid, lista_clientes[i].id)){
return lista_clientes[i];
}
}
errno = 0;
fprintf(stderr, "Cliente com id %s inesistente!\n", trg_id);
return cliente_vazio;
}
struct cliente *find_user_ptr(char trg_id[6]){
uuid_t uuid;
create_uuid(uuid, trg_id);
for(int i = 0; i < counter; i++){
if(!uuid_compare(uuid, lista_clientes[i].id)){
return &lista_clientes[i];
}
}
errno = 0;
fprintf(stderr, "Cliente com id %s inesistente!\n", trg_id);
return NULL;
}
int transfer(char id_orig[6], char id_dest[6], float quant){
// Achar o usuário id_orig
struct cliente *org = find_user_ptr(id_orig);
if(org == NULL){ return 0; }
// Achar o usuário id_dest
struct cliente *dest = find_user_ptr(id_dest);
if(dest == NULL){ return 0; }
// Verificação de erros
if(org == dest){
errno = 0;
fputs("Transferencia entre mesmo usuario!\n", stderr);
return 0;
}
else if(quant <= 0){
errno = 0;
fputs("Saldo invalido!\n", stderr);
return 0;
}
else if(org->saldo < quant){
errno = 0;
fputs("Transferencia com saldo insuficiente!\n", stderr);
return 0;
}
// Transferência
org->saldo -= quant;
dest->saldo += quant;
return 1;
}
int delete_user(char trg_id[6]){
struct cliente *cp = find_user_ptr(trg_id);
if(cp == NULL){ return 0; }
// Deslocar os itens não deletados
for(int index = cp - lista_clientes; index < counter-1; index++){
lista_clientes[index] = lista_clientes[index+1];
}
lista_clientes = realloc(lista_clientes, (--counter)*sizeof(struct cliente));
printf("Cliente com id %s deletado.\n", trg_id);
return 1;
}