-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.c
More file actions
executable file
·83 lines (72 loc) · 1.8 KB
/
entry.c
File metadata and controls
executable file
·83 lines (72 loc) · 1.8 KB
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
/*
* File: entry.c
*
* Implementação de struct entry_t
*
* Author: sd001 > Bruno Neves, n.º 31614
* > Vasco Orey, n.º 32550
*/
#include "utils.h"
#include "entry.h"
#include "data.h"
/*
* Cria uma entry com a string e o bloco de dados passados.
*/
struct entry_t *entry_create(char *key, struct data_t *data) {
struct entry_t *entry = NULL;
if((entry = (struct entry_t*) malloc(sizeof(struct entry_t)))) {
entry->key = key;
entry->value = data;
}
else {
ERROR("malloc entry");
}
return entry;
}
/*
* Aloca uma nova memória e copia a entry para ela.
*/
struct entry_t *entry_dup(struct entry_t *entry) {
struct entry_t *newEntry = NULL;
if (entry) {
// Aloca memória para uma nova entrada
if ((newEntry = (struct entry_t*) malloc(sizeof (struct entry_t)))) {
// Copia a chave
if ((newEntry->key = strdup(entry->key))) {
// Aloca memória para a nova data e copia-a
if (!(newEntry->value = data_dup(entry->value))) {
ERROR("data_dup");
free(newEntry->key);
free(newEntry);
newEntry = NULL;
}
}
else {
ERROR("strdup key");
free(newEntry);
newEntry = NULL;
}
}
else {
ERROR("malloc newEntry");
}
}
else {
ERROR("NULL entry");
}
return newEntry;
}
/*
* Liberta toda memória alocada pela entry.
*/
void entry_destroy(struct entry_t *entry) {
if (entry) {
if (entry->value) {
data_destroy(entry->value);
}
if (entry->key) {
free(entry->key);
}
free(entry);
}
}