-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.c
414 lines (335 loc) · 8.57 KB
/
index.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright (C) 2014 Mark Hills <mark@xwax.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#define _GNU_SOURCE /* strcasestr(), strdupa() */
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "index.h"
#define BLOCK 1024
#define MAX_WORDS 32
#define SEPARATOR ' '
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
/*
* Initialise a record index
*/
void index_init(struct index *ls)
{
ls->record = NULL;
ls->size = 0;
ls->entries = 0;
}
/*
* Deallocate resources associated with this index
*
* The index does not allocate records itself, so it is not
* responsible for deallocating them.
*/
void index_clear(struct index *ls)
{
if (ls->record != NULL)
free(ls->record);
}
/*
* Blank the index so it contains no entries
*
* We don't de-allocate memory, but this gives us an advantage where
* index re-use is of similar size.
*/
void index_blank(struct index *ls)
{
ls->entries = 0;
}
/*
* Enlarge the storage space of the index to at least the target
* size
*
* Return: 0 on success or -1 on memory allocation failure
* Post: size of index is greater than or equal to target
*/
static int enlarge(struct index *ls, size_t target)
{
size_t p;
struct record **ln;
if (target <= ls->size)
return 0;
p = target + BLOCK - 1; /* pre-allocate additional entries */
assert(p != NULL);
fprintf(stderr, "enlarge: ls->record: %p\n", ls->record);
ln = realloc(ls->record, sizeof(struct record*) * p);
if (ln == NULL) {
perror("realloc");
return -1;
}
ls->record = ln;
ls->size = p;
return 0;
}
/*
* Return: false if the caller did not call index_reserve(), otherwise
* true
*/
static bool has_space(const struct index *i)
{
return i->entries < i->size;
}
/*
* Add a record to the index
*
* Pre: at least one entry is reserved
* Post: lr is the record at the end of the index
*/
void index_add(struct index *ls, struct record *lr)
{
assert(lr != NULL);
assert(has_space(ls));
ls->record[ls->entries++] = lr;
}
/*
* Standard comparison function between two records
*/
static int record_cmp_artist(const struct record *a, const struct record *b)
{
int r;
r = strcasecmp(a->artist, b->artist);
if (r < 0)
return -1;
else if (r > 0)
return 1;
r = strcasecmp(a->title, b->title);
if (r < 0)
return -1;
else if (r > 0)
return 1;
return strcmp(a->pathname, b->pathname);
}
/*
* Compare two records principally by BPM, fastest to slowest
* followed by unknown
*/
static int record_cmp_bpm(const struct record *a, const struct record *b)
{
if (a->bpm < b->bpm)
return 1;
if (a->bpm > b->bpm)
return -1;
return record_cmp_artist(a, b);
}
/*
* Check if a record matches the given string. This function is the
* definitive code which defines what constitutes a 'match'.
*
* Return: true if this is a match, otherwise false
*/
static bool record_match_word(struct record *re, const char *match)
{
if (strcasestr(re->artist, match) != NULL)
return true;
if (strcasestr(re->title, match) != NULL)
return true;
return false;
}
/*
* Check for a match against the given search criteria
*
* Return: true if the given record matches, otherwise false
*/
bool record_match(struct record *re, const struct match *h)
{
char *const *matches;
matches = h->words;
while (*matches != NULL) {
if (!record_match_word(re, *matches))
return false;
matches++;
}
return true;
}
/*
* Copy the source index
*
* Return: 0 on success or -1 on memory allocation failure
* Post: on failure, dest is valid but incomplete
*/
int index_copy(const struct index *src, struct index *dest)
{
int n;
index_blank(dest);
if (index_reserve(dest, src->entries) == -1)
return -1;
for (n = 0; n < src->entries; n++)
index_add(dest, src->record[n]);
return 0;
}
/*
* Compile a search object from a given string
*
* Pre: search string is within length
*/
void match_compile(struct match *h, const char *d)
{
char *buf;
size_t n;
assert(strlen(d) < sizeof h->buf);
strcpy(h->buf, d);
buf = h->buf;
n = 0;
for (;;) {
char *s;
if (n == ARRAY_SIZE(h->words) - 1) {
fputs("Ignoring excessive words in match string.\n", stderr);
break;
}
h->words[n] = buf;
n++;
s = strchr(buf, SEPARATOR);
if (s == NULL)
break;
*s = '\0';
buf = s + 1; /* skip separator */
}
h->words[n] = NULL; /* terminate list */
}
/*
* Find entries from the source index which match
*
* Copy the subset of the source index which matches the given
* string into the destination.
*
* Return: 0 on success, or -1 on memory allocation failure
* Post: on failure, dest is valid but incomplete
*/
int index_match(struct index *src, struct index *dest,
const struct match *match)
{
int n;
struct record *re;
index_blank(dest);
for (n = 0; n < src->entries; n++) {
re = src->record[n];
if (record_match(re, match)) {
if (index_reserve(dest, 1) == -1)
return -1;
index_add(dest, re);
}
}
return 0;
}
/*
* Binary search of sorted index
*
* We implement our own binary search rather than using the bsearch()
* from stdlib.h, because we need to know the position to insert to if
* the item is not found.
*
* Pre: base is sorted
* Return: position of match >= item
* Post: on exact match, *found is true
*/
static size_t bin_search(struct record **base, size_t n,
struct record *item, int sort,
bool *found)
{
int r;
size_t mid;
struct record *x;
/* Return the first entry ordered after this one */
if (n == 0) {
*found = false;
return 0;
}
mid = n / 2;
x = base[mid];
switch (sort) {
case SORT_ARTIST:
r = record_cmp_artist(item, x);
break;
case SORT_BPM:
r = record_cmp_bpm(item, x);
break;
case SORT_PLAYLIST:
default:
abort();
}
if (r < 0)
return bin_search(base, mid, item, sort, found);
if (r > 0) {
return mid + 1
+ bin_search(base + mid + 1, n - mid - 1, item, sort, found);
}
*found = true;
return mid;
}
/*
* Insert or re-use an entry in a sorted index
*
* Pre: index is sorted
* Pre: at least one entry is reserved
* Return: pointer to item, or existing entry (ie. not NULL)
* Post: index is sorted and contains item or a matching item
*/
struct record* index_insert(struct index *ls, struct record *item,
int sort)
{
bool found;
size_t z;
z = bin_search(ls->record, ls->entries, item, sort, &found);
if (found)
return ls->record[z];
assert(has_space(ls));
memmove(ls->record + z + 1, ls->record + z,
sizeof(struct record*) * (ls->entries - z));
ls->record[z] = item;
ls->entries++;
return item;
}
/*
* Reserve space in the index for the addition of n new items
*
* This function exists separately to the insert and addition
* functions because it carries the error case.
*
* Return: -1 if not enough memory, otherwise zero
* Post: if zero is returned, index has at least n free slots
*/
int index_reserve(struct index *i, unsigned int n)
{
return enlarge(i, i->entries + n);
}
/*
* Find an identical entry, or the nearest match
*/
size_t index_find(struct index *ls, struct record *item, int sort)
{
bool found;
size_t z;
z = bin_search(ls->record, ls->entries, item, sort, &found);
return z;
}
/*
* Debug the content of a index to standard error
*/
void index_debug(struct index *ls)
{
int n;
for (n = 0; n < ls->entries; n++)
fprintf(stderr, "%d: %s\n", n, ls->record[n]->pathname);
}