-
Notifications
You must be signed in to change notification settings - Fork 1
/
blastlist.c
142 lines (112 loc) · 2.75 KB
/
blastlist.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "blastlist.h"
#include "strutils.h"
#include "explode.h"
static void blastdata_copy(blast *b, blastdata *d) {
b->id = d->id;
b->user = strclone(d->user);
b->content = strclone(d->content);
b->posted_at = strclone(d->posted_at);
b->attachment = strclone(d->attachment);
b->extended = strclone(d->extended);
}
static blast *blast_new() {
blast *b = calloc(1, sizeof(blast));
if (b == NULL) {
fatal_error("conch_blast_new: could not alloc");
}
return b;
}
static void blast_free(blast *b) {
free(b->user);
free(b->content);
free(b->posted_at);
free(b->attachment);
free(b->extended);
free(b);
}
blastlist *conch_blastlist_new() {
blastlist *bl = calloc(1, sizeof(blastlist));
if (bl == NULL) {
fatal_error("conch_blastlist_new: could not alloc");
}
return bl;
}
static void blasts_from_resultset(resultset *rs, blast **head, blast **tail) {
// If there are no items out of which to make a blastlist, return
if (rs == NULL || rs->count == 0) {
*head = NULL;
*tail = NULL;
return;
}
blast *cur = blast_new();
blast *chainhead = cur;
blastdata_copy(cur, rs->blasts);
for (uint64_t i = 1; i < rs->count; ++i) {
cur->next = blast_new();
cur->next->prev = cur;
blastdata_copy(cur->next, rs->blasts + i);
cur = cur->next;
}
*head = chainhead;
*tail = cur;
}
void conch_blastlist_prepend_resultset(blastlist *bl, resultset *rs) {
blast *chainhead;
blast *chaintail;
if (rs == NULL) {
return;
}
blasts_from_resultset(rs, &chainhead, &chaintail);
// Nothing in resultset. We're done.
if (chainhead == NULL || chaintail == NULL) {
return;
}
if (bl->head == NULL) {
bl->head = chainhead;
bl->tail = chaintail;
bl->current = bl->head;
return;
}
bl->head->prev = chaintail;
bl->head->prev->next = bl->head;
bl->head = chainhead;
}
void conch_blastlist_append_resultset(blastlist *bl, resultset *rs) {
blast *chainhead;
blast *chaintail;
blasts_from_resultset(rs, &chainhead, &chaintail);
// Nothing in resultset. We're done.
if (chainhead == NULL || chaintail == NULL) {
return;
}
if (bl->tail == NULL) {
bl->head = chainhead;
bl->tail = chaintail;
bl->current = bl->head;
return;
}
bl->tail->next = chainhead;
bl->tail->next->prev = bl->tail;
bl->tail = chaintail;
}
void conch_blastlist_free(blastlist *bl) {
if (bl == NULL) {
return;
}
blast *cur;
blast *next = bl->head;
// We should have received the head of a list, or I'm going to explode.
if (bl->head != NULL) {
assert(next->prev == NULL);
}
while (next != NULL) {
cur = next;
next = cur->next;
blast_free(cur);
}
free(bl);
}