-
Notifications
You must be signed in to change notification settings - Fork 0
/
blarr.c
83 lines (70 loc) · 1.75 KB
/
blarr.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
/* blarr.c
*
* At ADT to deal with blocks of files. Each block is represented with a
* 'Block' structure containing the key of the block (on which the file
* will be sorted), its starting location in the file, and its ending
* location. The blocks are contained into a dynamic array.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include "blarr.h"
// Add large file support
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE 1
void InitializeArray(Array *a, size_t initial_size)
{
a->array = (Block*) malloc(initial_size * sizeof(Block));
if (a->array == NULL)
{
FreeArray(a);
err(1, "malloc: %s", "InitializeArray");
exit(EXIT_FAILURE);
}
a->used = 0;
a->size = initial_size;
}
void AddItem(Array *a, char *key, unsigned long long start)
{
Block pnew;
// Increase the size of the array if we have reached capacity
if (a->used == a->size) {
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(Block));
if (a->array == NULL)
{
FreeArray(a);
err(1, "realloc: %s", "AddItem");
exit(EXIT_FAILURE);
}
}
// Add the new item
strcpy(pnew.key, key);
pnew.start = start;
a->array[a->used] = pnew;
// Set the end of the previous block
if (a->used > 0)
a->array[a->used - 1].end = start;
a->used++;
}
void SetLastEnd(Array *a, unsigned long long end)
{
if (a->used > 0)
a->array[a->used - 1].end = end;
}
void FreeArray(Array *a)
{
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
bool ArrayIsEmpty(Array *a)
{
return (a->used == 0);
}
char *LastKey(Array *a)
{
return a->array[a->used - 1].key;
}