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
|
// bin.h - flat file, fixed block size, time series database
#ifndef BIN_H
#define BIN_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "type.h"
typedef struct {
uint_t length;
uint_t block_size;
} bin_meta_t;
FILE *bin_init(char const *, uint_t);
FILE *bin_open(char const *path);
void bin_close(FILE *);
void bin_read(FILE *, int_t, void *, uint_t);
void bin_write(FILE *, int_t, void *, uint_t);
void bin_insert(FILE *, int_t, void *, uint_t);
void bin_append(FILE *, void *, uint_t);
uint_t bin_length(FILE *);
uint_t bin_block_size(FILE *);
int_t bin_search(FILE *, bin_key_t);
uint_t bin_fuzzy_index(int_t);
#endif // BIN_H
#ifdef BIN_IMPLEMENTATION // IMPLEMENTATION
#include <assert.h>
#include <stdlib.h>
static void bin_write_length(FILE *, uint_t);
static void bin_write_block_size(FILE *, uint_t);
#define index_bytes(i, bs) ((i * bs) + sizeof(bin_meta_t))
#define member_sizeof(type, member) (sizeof(((type *)0)->member))
// bin_init()
//
// Creates the desired binary file
//
// path - The path and file name for the desired file
//
// return - The newly created file
FILE *bin_init(char const *path, uint_t block_size) {
FILE *f = fopen(path, "wb+");
if (!f)
return NULL;
setvbuf(f, NULL, _IONBF, 0);
bin_write_length(f, 0);
bin_write_block_size(f, block_size);
return f;
}
// bin_open()
//
// Opens the desired binary file
//
// path - The path and file name for the desired file
//
// return - The newly opened file
FILE *bin_open(char const *path) {
FILE *f = fopen(path, "rb+");
if (!f)
return NULL;
setvbuf(f, NULL, _IONBF, 0);
return f;
}
// bin_close()
//
// Closes the desired binary file
//
// f - The file descriptor to be closed
void bin_close(FILE *f) { fclose(f); }
// bin_read()
//
// Read the desired data into the buffer
//
// f - The file pointer
// i - The initial index of the read
// data - The block_t buffer to be read into
// n - The number of elements to read into the buffer
void bin_read(FILE *f, int_t i, void *data, uint_t n) {
uint_t bs = bin_block_size(f);
assert(0 <= i && (i + n) <= bin_length(f));
fseek(f, index_bytes(i, bs), SEEK_SET);
fread(data, bs, n, f);
}
// bin_write()
//
// Write the buffer into the file
//
// f - The file pointer
// i - The initial index of the write
// data - The block_t buffer to write from
// n - The number of elements to write from the buffer
void bin_write(FILE *f, int_t i, void *data, uint_t n) {
uint_t l = bin_length(f);
uint_t bs = bin_block_size(f);
assert(0 <= i && i <= l);
fseek(f, index_bytes(i, bs), SEEK_SET);
fwrite(data, bs, n, f);
if ((i + n) >= l) {
l += (i + n - l);
}
bin_write_length(f, l);
}
// bin_append()
//
// Appends the given data to the end of the binary file
//
// f - The file to be appended to
// data - The blocks to append to the file
// n - The number of block to be appended
void bin_append(FILE *f, void *data, uint_t n) {
uint_t l = bin_length(f);
uint_t bs = bin_block_size(f);
fseek(f, index_bytes(l, bs), SEEK_SET);
fwrite(data, bs, n, f);
bin_write_length(f, l + n);
}
// bin_insert()
//
// Inserts the given data into the binary file
//
// f - The file to be inserted into
// i - The initial index of the insertion
// data - The blocks to insert into the file
// n - The number of blocks to be inserted
void bin_insert(FILE *f, int_t i, void *data, uint_t n) {
uint_t l = bin_length(f);
uint_t bs = bin_block_size(f);
assert(0 <= i && i <= l);
uint_t tmp_n = (l - i) * bs;
uint8_t tmp[tmp_n];
fseek(f, index_bytes(i, bs), SEEK_SET);
fread(tmp, bs, l - i, f);
fseek(f, index_bytes(i, bs), SEEK_SET);
fwrite(data, bs, n, f);
fwrite(tmp, 1, tmp_n, f);
bin_write_length(f, l + n);
}
// bin_length()
//
// Reads the block count number from the given file
//
// f - The file to be read from
//
// return - The length / number of entries in the file
uint_t bin_length(FILE *f) {
uint_t n = 0;
fseek(f, offsetof(bin_meta_t, length), SEEK_SET);
fread(&n, member_sizeof(bin_meta_t, length), 1, f);
return n;
}
// bin_block_size()
//
// Reads the block size from the given file
//
// f - The file to be read from
//
// return - The block size for the entries in the file
uint_t bin_block_size(FILE *f) {
uint_t n = 0;
fseek(f, offsetof(bin_meta_t, block_size), SEEK_SET);
fread(&n, member_sizeof(bin_meta_t, block_size), 1, f);
return n;
}
// bin_search()
//
// Search for a given key in the file
//
// f - The file to be searched within
// k - The key to search for
//
// return - The block_t index of the key. In the case that the key is not found,
// The returned value is -( index + 1 ) where index is that of the
// first element greater than k or bin_length().
int_t bin_search(FILE *f, bin_key_t k) {
uint_t origin = ftell(f);
int_t l = 0;
int_t r = bin_length(f) - 1;
int_t m;
uint_t bs = bin_block_size(f);
bin_key_t key;
while (l <= r) {
m = (l + r) / 2;
fseek(f, index_bytes(m, bs), SEEK_SET);
fread(&key, sizeof(k), 1, f);
if (key < k)
l = m + 1;
else if (key > k)
r = m - 1;
else
break;
}
fseek(f, origin, SEEK_SET);
if (key != k)
return -(l + 1);
return m;
}
// bin_fuzzy_index()
//
// If one does a binary search on the file and is returned a negative error,
// it may be useful to retrueve the index which it would be at.
//
// i - The number to map to the correct index
//
// return - The index remapped the binary file. If the index is already
// positive, then no operation occurrs.
uint_t bin_fuzzy_index(int_t i) { return (i < 0) ? -(i + 1) : i; }
// bin_write_length()
//
// Writes a length to the given file
//
// f - The file to be written to
// count - The length to be written to the file
static void bin_write_length(FILE *f, uint_t length) {
fseek(f, offsetof(bin_meta_t, length), SEEK_SET);
fwrite(&length, member_sizeof(bin_meta_t, length), 1, f);
}
// bin_write_block_size()
//
// Writes a block size to the given file
//
// f - The file to be written to
// count - The block size to be written to the file
static void bin_write_block_size(FILE *f, uint_t block_size) {
fseek(f, offsetof(bin_meta_t, block_size), SEEK_SET);
fwrite(&block_size, member_sizeof(bin_meta_t, block_size), 1, f);
}
#endif
|