-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
325 lines (253 loc) · 10.7 KB
/
serial.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @file serial.c
* @brief Definições estáticas e código das funções de serialização e de-serialização
*
* */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "serial.h"
#include "dados.h"
#include "mod_avl_n_dimensional.h"
#include "mod_lista_ligada.h"
#include "mod_tabela_hash.h"
int serialize(TabelaHashPTR tabela, MainTreePt camioes, MainTreePt clientes){
FILE *file;
int i, nAdj;
short int strl;
MainListPTR *lista = tabela->arraycell;
LinkedListPTR listaAux = NULL;
LinkedListPTR adjacencia = NULL;
file = fopen("dados.sav", "w");
if( file == NULL ) return 0;
//tamanho do array de hash
// assim não é preciso fazer re-hash, usa-se este como tamanho inicial
fwrite(&tabela->totalcells, sizeof(int), 1, file);
//numero de elementos da tabela de hash
fwrite(&tabela->nelems, sizeof(int), 1, file);
for(i=0; i<tabela->totalcells; i++){
listaAux = lista[i]->elems;
while( listaAux ){
strl = strlen(((LocalidadePTR)(listaAux->extdata))->nome);
fwrite( &strl, sizeof(short int), 1, file );
//printf("%d", strl);
fwrite( ((LocalidadePTR)(listaAux->extdata))->nome, sizeof(char), strl, file );
//printf("%s\n", ((LocalidadePTR)(listaAux->extdata))->nome );
nAdj = ((LocalidadePTR)(listaAux->extdata))->ligacoesida->nelems;
fwrite( &nAdj, sizeof(int), 1, file );
adjacencia = ((LocalidadePTR)(listaAux->extdata))->ligacoesida->elems;
while( adjacencia ){
strl = strlen( ((LigacoesidaPTR)adjacencia->extdata)->nome );
fwrite( &strl, sizeof(short int), 1, file );
fwrite( ((LigacoesidaPTR)adjacencia->extdata)->nome, sizeof(char), strl, file );
fwrite( &((LigacoesidaPTR)adjacencia->extdata)->custo, sizeof(double), 1, file );
fwrite( &((LigacoesidaPTR)adjacencia->extdata)->distancia, sizeof(double), 1, file );
adjacencia = adjacencia->prox;
}
listaAux = listaAux->prox;
}
}
fwrite( &camioes->nodos, sizeof(unsigned int), 1, file );
serial_camiao( camioes, file );
fwrite( &clientes->nodos, sizeof(unsigned int), 1, file );
serial_cliente( clientes, file );
fclose(file);
return 1;
}
void serial_clienteRec( TreePt thisTree, FILE *file ){
ClientePt cliente = NULL;
LinkedListPTR servico = NULL;
short int strl;
int exception = 0;
if( thisTree != NULL ){
serial_clienteRec( thisTree->l[0], file);
cliente = (ClientePt)thisTree->node;
fwrite( &cliente->nif, sizeof(unsigned int), 1, file);
strl = strlen( cliente->nome );
fwrite( &strl, sizeof(short int), 1, file );
fwrite( cliente->nome, sizeof(char), strl, file );
strl = strlen( cliente->morada );
fwrite( &strl, sizeof(short int), 1, file );
fwrite( cliente->morada, sizeof(char), strl, file );
if( cliente->servicos != NULL ){
fwrite( &(cliente->servicos->nelems), sizeof(int), 1, file );
servico = cliente->servicos->elems;
while( servico ){
fwrite( &((ServicoPt)servico->extdata)->custo, sizeof(double), 1, file );
fwrite( &((ServicoPt)servico->extdata)->peso, sizeof(double), 1, file );
strl = strlen( ((ServicoPt)servico->extdata)->datahora );
fwrite( ((ServicoPt)servico->extdata)->datahora, sizeof(char), strl, file );
strl = strlen( ((ServicoPt)servico->extdata)->camiao );
fwrite( ((ServicoPt)servico->extdata)->camiao, sizeof(char), strl, file );
strl = strlen( ((ServicoPt)servico->extdata)->origem );
fwrite( ((ServicoPt)servico->extdata)->origem, sizeof(char), strl, file );
strl = strlen( ((ServicoPt)servico->extdata)->carga );
fwrite( ((ServicoPt)servico->extdata)->carga, sizeof(char), strl, file );
strl = strlen( ((ServicoPt)servico->extdata)->destino );
fwrite( ((ServicoPt)servico->extdata)->destino, sizeof(char), strl, file );
servico = servico->prox;
}
}else
fwrite( &exception, sizeof(int), 1, file );
serial_clienteRec( thisTree->r[0], file);
}
}
void serial_cliente(MainTreePt thisMainTree, FILE *file){
if( thisMainTree != NULL )
serial_clienteRec( thisMainTree->tree[0], file );
}
void serial_camiaoRec( TreePt thisTree, FILE *file ){
CamiaoPt camiao = NULL;
short int strl;
if( thisTree != NULL ){
serial_camiaoRec( thisTree->l[0], file);
camiao = (CamiaoPt)thisTree->node;
fwrite( &camiao->id, sizeof(unsigned int), 1, file);
fwrite( &camiao->custo, sizeof(double), 1, file );
fwrite( &camiao->peso, sizeof(double), 1, file );
strl = strlen( camiao->matricula );
fwrite( &strl, sizeof(short int), 1, file );
fwrite( camiao->matricula, sizeof(char), strl, file );
strl = strlen( camiao->local );
fwrite( &strl, sizeof(short int), 1, file );
fwrite( camiao->local, sizeof(char), strl, file );
serial_camiaoRec( thisTree->r[0], file);
}
}
void serial_camiao(MainTreePt thisMainTree, FILE *file){
if( thisMainTree != NULL )
serial_camiaoRec( thisMainTree->tree[0], file );
}
char* novaString(unsigned int n){
return malloc((n+1)*sizeof(char));
}
int deserialize(
MainTreePt *camioes, int (*comparaCamioes[DIM])(void*,void*),
MainTreePt *clientes, int (*comparaClientes[DIM])(void*,void*),
TabelaHashPTR *localidades, int (*comparaLocalidades)(void*,void*), int(*hash_function)(void*,int) ){
short int strl;
char *str;
char *locOrigem;
int nelem, nelems, tmp;
int nAdj, nAdjs;
double dArr[2];
int teste;
FILE *file;
fpos_t file_pos;
apagaTabelaHash(*localidades);
tree_dispose(camioes);
tree_dispose(clientes);
file = fopen("dados.sav", "r");
if( file == NULL ) return 0;
fread( &tmp, sizeof(int), 1, file );
*localidades = criaTabelaHash(hash_function, tmp, comparaLocalidades);
fread( &nelems, sizeof(int), 1, file );
fgetpos( file, &file_pos ); //guardar a posição actual
// inserir as localidades
for( nelem=0; nelem<nelems; nelem++ ){
fread( &strl, sizeof(short int), 1, file);
str = novaString(strl);
*(str+strl) = '\0';
fread( str, sizeof(char), strl, file );
teste = inserelocalidade( *localidades, str ); // localidade inserida
if( teste != 1 )
printf(".");
fread( &nAdjs, sizeof(int), 1, file );
for( nAdj = 0; nAdj < nAdjs; nAdj++ ){
fread( &strl, sizeof(short int), 1, file );
fseek( file, strl*sizeof(char) + 2*sizeof(double) , SEEK_CUR );
}
}
//agora que já tem as localidades, volta atrás e insere as adjacencias
fsetpos( file, &file_pos );
for( nelem=0; nelem<nelems; nelem++ ){
fread( &strl, sizeof(short int), 1, file);
locOrigem = novaString(strl);
*(locOrigem+strl) = '\0';
fread( locOrigem, sizeof(char), strl, file );
fread( &nAdjs, sizeof(int), 1, file );
for( nAdj = 0; nAdj < nAdjs; nAdj++ ){
fread( &strl, sizeof(short int), 1, file );
free(str);
str = novaString(strl);
*(str+strl) = '\0';
fread( str, sizeof(char), strl, file );
fread( &dArr, sizeof(double), 2, file );
inserirligacao(*localidades, locOrigem, str, dArr[0], dArr[1]);
}
}
free(locOrigem);
//camiões
unsigned int id;
char *matricula=NULL;
char *local=NULL;
unsigned int ncamiao, ncamioes;
*camioes = tree_new( comparaCamioes );
fread( &ncamioes, sizeof(unsigned int), 1, file );
for( ncamiao=0; ncamiao<ncamioes; ncamiao++ ){
fread( &id, sizeof(unsigned int), 1, file);
fread( &dArr, sizeof(double), 2, file );
fread( &strl, sizeof(short int), 1, file );
matricula = novaString(strl);
*(matricula+strl) = '\0';
fread( matricula, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
local = novaString(strl);
*(local+strl) = '\0';
fread( local, sizeof(char), strl, file );
tree_insert( *camioes, camiao_novo(id, matricula, dArr[0], dArr[1], local));
}
//clientes
ClientePt clt=NULL;
unsigned int nif;
char *nome;
char *morada;
unsigned int ncliente, nclientes;
int nservico, nservicos;
char *datahora;
char *cmatricula;
char *origem;
char *carga;
char *destino;
*clientes = tree_new( comparaClientes );
fread( &nclientes, sizeof(unsigned int), 1, file);
for( ncliente=0; ncliente<nclientes; ncliente++ ){
fread( &nif, sizeof(unsigned int), 1, file);
fread( &strl, sizeof(short int), 1, file );
nome = novaString(strl);
*(nome+strl) = '\0';
fread( nome, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
morada = novaString(strl);
*(morada+strl) = '\0';
fread( morada, sizeof(char), strl, file );
clt = cliente_novo( nif, nome, morada, criaListaLigada( cliente_comparaServico ));
tree_insert( *clientes, clt );
fread( &nservicos, sizeof(int), 1, file );
for( nservico=0; nservico<nservicos; nservico++){
fread( &dArr, sizeof(double), 2, file );
fread( &strl, sizeof(short int), 1, file );
datahora = novaString(strl);
*(datahora+strl) = '\0';
fread( datahora, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
cmatricula = novaString(strl);
*(cmatricula+strl) = '\0';
fread( cmatricula, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
origem = novaString(strl);
*(origem+strl) = '\0';
fread( origem, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
carga = novaString(strl);
*(carga+strl) = '\0';
fread( carga, sizeof(char), strl, file );
fread( &strl, sizeof(short int), 1, file );
destino = novaString(strl);
*(destino+strl) = '\0';
fread( destino, sizeof(char), strl, file );
cliente_insereServico(*clientes, nif, matricula, dArr[0], dArr[1], origem, carga, destino );
}
}
return 1;
}