-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdarray.c
92 lines (77 loc) · 2.22 KB
/
darray.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
/*
Copyright (C) 2016 Carles Garcia Cabot (github.com/carles-garcia)
This file is part of aptback, a tool to search, install, remove and upgrade
packages logged by apt. Released under the GNU GPLv3 (see COPYING.txt)
*/
#include "darray.h"
void init_darray(struct darray *d) {
d->size = 0;
d->capacity = 10;
d->array = malloc(10 * sizeof(struct action*));
if (d->array == NULL) eperror("Failed to malloc darray");
}
void darray_add(struct darray *d, struct action *obj) {
if (d->size == d->capacity) {
d->capacity *= 2;
d->array = realloc(d->array, d->capacity * sizeof(obj));
if (d->array == NULL) eperror("Failed to realloc darray");
}
d->array[d->size++] = obj;
}
struct action* darray_get(struct darray *d, int i) {
return d->array[i];
}
void free_darray(struct darray *d) {
free(d->array);
}
void init_darray_pack(struct darray_pack *d) {
d->size = 0;
d->capacity = 10;
d->array = malloc(10 * sizeof(struct package*));
if (d->array == NULL) eperror("Failed to malloc darray");
}
void darray_pack_add(struct darray_pack *d, struct package *obj) {
if (d->size == d->capacity) {
d->capacity *= 2;
d->array = realloc(d->array, d->capacity * sizeof(obj));
if (d->array == NULL) eperror("Failed to realloc darray");
}
d->array[d->size++] = obj;
}
struct package* darray_pack_get(struct darray_pack *d, int i) {
return d->array[i];
}
void free_darray_pack(struct darray_pack *d) {
free(d->array);
}
void free_action(struct action *actions) {
free(actions->command);
for (int j = 0; j < actions->packages.size; ++j)
free_pack(darray_pack_get(&actions->packages, j));
free_darray_pack(&actions->packages);
free(actions);
}
void free_pack(struct package *pack) {
free(pack->name);
free(pack->arch);
free(pack->version);
free(pack->newversion);
free(pack);
}
void init_action(struct action *current) {
memset(¤t->date, 0, sizeof(struct date));
init_darray_pack(&(current->packages));
current->command = NULL;
current->type = UNDEFINED;
}
void init_pack(struct package *pack) {
pack->name = NULL;
pack->arch = NULL;
pack->version = NULL;
pack->newversion = NULL;
pack->automatic = 0;
}
void eperror(char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}